Skip to content

Commit

Permalink
[REFAC] Use RegisterResourceAsset and RegisterStreamingFileFromCache …
Browse files Browse the repository at this point in the history
…natives
  • Loading branch information
ktx-mega committed Sep 30, 2024
1 parent cadb863 commit 40cea96
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 52 deletions.
8 changes: 1 addition & 7 deletions .github/actions/bump-manifest-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ const newVersion = version.replace('v', '')
// Update kinetix_mod manifest
const modManifestFile = fs.readFileSync('kinetix_mod/fxmanifest.lua', {encoding: 'utf8'})
const newModFileContent = modManifestFile.replace(/\bversion\s+(.*)$/gm, `version '${newVersion}'`)
fs.writeFileSync('kinetix_mod/fxmanifest.lua', newModFileContent)


// Update kinetix_anim manifest
const animManifestFile = fs.readFileSync('kinetix_anim/fxmanifest.lua', {encoding: 'utf8'})
const newAnimFileContent = animManifestFile.replace(/\bversion\s+(.*)$/gm, `version '${newVersion}'`)
fs.writeFileSync('kinetix_anim/fxmanifest.lua', newAnimFileContent)
fs.writeFileSync('kinetix_mod/fxmanifest.lua', newModFileContent)
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ jobs:
- name: Bundle files
run: |
mkdir -p ./temp/kinetix_fivem
cp -r ./{kinetix_anim,kinetix_mod,README.md} ./temp/kinetix_fivem
mkdir -p ./temp/kinetix_fivem/kinetix_anim/stream
cp -r ./{kinetix_mod,README.md} ./temp/kinetix_fivem
mkdir -p ./temp/kinetix_fivem/kinetix_mod/stream
cd ./temp && zip -r ../kinetix_fivem.zip ./kinetix_fivem
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Extract the zip file into your server's resource folder.

            ![](settings.png)

- Add these ACE rules in your server config
- Add these ACE rules in your server config (not required since v0.2.0)
```
add_ace resource.kinetix_mod command.restart allow
add_ace resource.kinetix_mod command.start allow
Expand Down
4 changes: 0 additions & 4 deletions kinetix_anim/fxmanifest.lua

This file was deleted.

1 change: 0 additions & 1 deletion kinetix_anim/stream/README.md

This file was deleted.

24 changes: 23 additions & 1 deletion kinetix_mod/client/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,32 @@ RegisterNetEvent("emote_ready")
AddEventHandler("emote_ready", function(data)
TriggerServerEvent("requestAvailableEmotes")
if data.emote ~= nil then
RequestAnimDict(data.emote .. "@animation")
if not HasAnimDictLoaded(data.emote .. "@animation") then
RequestAnimDict(data.emote .. "@animation")
end
end
end)

RegisterNetEvent("new_cached_emote")
AddEventHandler("new_cached_emote", function(data)
RegisterStreamingFileFromCache(
'kinetix_mod',
data.uuid .. '@animation.ycd',
data.cacheString
)
end)

RegisterNetEvent("new_cached_emotes")
AddEventHandler("new_cached_emotes", function(cacheData)
for _, data in ipairs(cacheData) do
RegisterStreamingFileFromCache(
'kinetix_mod',
data.uuid .. '@animation.ycd',
data.cacheString
)
end
end)

RegisterNetEvent("emote_ready_notify")
AddEventHandler("emote_ready_notify", function(data)
NotifyEmoteReady(data)
Expand Down
52 changes: 16 additions & 36 deletions kinetix_mod/server/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ checkWebhookHMAC = true

local url = 'https://sdk-api.kinetix.tech'
local configuration = {}
local newYCDFilesCache = {}

local headers = {
["Content-Type"] = "application/json",
Expand All @@ -16,12 +17,20 @@ function GetUserId(source)
end

function file_exists(name)
local dir = GetResourcePath('kinetix_anim')
local dir = GetResourcePath('kinetix_mod')
local correctedDir = string.gsub(dir, "//", "/")
local f = io.open(correctedDir .. '/' .. name, "r")
return f ~= nil and io.close(f)
end

function cacheNewEmote(uuid)
local fileName = 'stream/' .. uuid .. '@animation.ycd'
local cacheString = RegisterResourceAsset('kinetix_mod', fileName)
local cacheObject = { cacheString= cacheString, fileName= fileName, uuid=uuid }
table.insert(newYCDFilesCache, cacheObject)
TriggerClientEvent('new_cached_emote', -1, cacheObject)
end

function CreateUser(userId, callback)
if userExists == true then
return callback()
Expand Down Expand Up @@ -73,29 +82,10 @@ function GetAvailableEmotes(userId, callback)
return
end
-- Ensure file is properly loaded, if the webhook call was missed
local hasToRestart = false
local missing = 0
local processed = 0
for _, emote in pairs(responseObject) do
local fileName = 'stream/' .. emote.data.uuid .. '@animation.ycd'
if not file_exists(fileName) then
missing = missing + 1
end
end
for _, emote in pairs(responseObject) do
local fileName = 'stream/' .. emote.data.uuid .. '@animation.ycd'
if not file_exists(fileName) then
DownloadYCD({ emote = emote.data.uuid }, source, false, false, function(response)
processed = processed + 1
if response == true then
hasToRestart = true
end
if missing == processed and hasToRestart == true then
ExecuteCommand('refresh')
ExecuteCommand('restart kinetix_anim')
TriggerClientEvent("emote_ready", -1, {})
end
end)
DownloadYCD({ emote = emote.data.uuid }, source, true, false)
end
end
callback(responseObject)
Expand All @@ -115,7 +105,7 @@ function RequestQRCode(userId, callback)
end, "POST", metadata, headers)
end

function DownloadYCD(body, playerId, refresh, notify, cb)
function DownloadYCD(body, playerId, refresh, notify)
local route = '/v1/emotes/' .. body.emote
PerformHttpRequest(url .. route, function(statusCode, response, responseHeaders)
local responseObject = json.decode(response)
Expand All @@ -128,30 +118,19 @@ function DownloadYCD(body, playerId, refresh, notify, cb)

local fileName = 'stream/' .. body.emote .. '@animation.ycd'
if fileUrl == '' then
if cb ~= nil then
cb(false)
end
return
end

PerformHttpRequest(fileUrl, function(fileStatusCode, fileResponse, fileHeaders)
if fileStatusCode == 200 then
SaveResourceFile('kinetix_anim', fileName, fileResponse, string.len(fileResponse))
SaveResourceFile('kinetix_mod', fileName, fileResponse, string.len(fileResponse))
if refresh == true then
ExecuteCommand('refresh')
ExecuteCommand('restart kinetix_anim')
cacheNewEmote(body.emote)
TriggerClientEvent("emote_ready", playerId, body)
end
if playerId ~= nil and notify == true then
TriggerClientEvent("emote_ready", -1, body)
TriggerClientEvent("emote_ready_notify", playerId, body)
end
if cb ~= nil then
cb(true)
end
else
if cb ~= nil then
cb(false)
end
print("Error downloading the file : " .. fileName)
end
end, "GET", "", headers)
Expand Down Expand Up @@ -333,6 +312,7 @@ end)
RegisterNetEvent("requestConfiguration")
AddEventHandler("requestConfiguration", function(processUuid)
local _source = source
TriggerClientEvent("new_cached_emotes", _source, newYCDFilesCache)
GetConfig(function(config)
TriggerClientEvent("config", _source, config)
configuration = config
Expand Down

0 comments on commit 40cea96

Please sign in to comment.