- 20
- 0
updater.lua:
script_name('Updater')
script_version_number(1)
function downloadUrlToFileSync(url, path)
local dlstatus = require 'moonloader'.download_status
download_id = downloadUrlToFile(url, path, function(id, status, p1, p2)
if status ~= dlstatus.STATUSEX_ENDDOWNLOAD then return end
download_id = nil
end)
while download_id do wait(100) end
end
function EXPORTS.autoupdate(url, version, force)
-- Download version file
local version_url = url .. '/version.json'
local version_path = os.getenv('TEMP') .. '\\' .. 'version.json'
if doesFileExist(version_path) then os.remove(version_path) end
downloadUrlToFileSync(version_url, version_path)
-- Open version file
local version_file = io.open(version_path, 'r')
if not file then return false end
-- Read version data
local version_data = decodeJson(file:read('*a'))
file:close()
os.remove(version_path)
if not version_data then return false end
-- Check update available
if not force and version_data.version <= version then return false end
print('Script update found, downloading...')
-- Download update files
for index, file in pairs(version_data.files) do
local file_url = base_url .. '/files/' .. file
local file_path = getWorkingDirectory() .. '\\' .. file
downloadUrlToFileSync(file_url, file_path)
end
-- Reload script
print('Download complete, reloading script...')
return true
end
function main()
wait(-1)
end
script.lua:
script_name('Some updatable script')
script_version_number(1)
local updater = import('updater.lua')
function main()
local script = thisScript()
local url = 'https://some-direct-url-to-script-update-server.directory/'
if updater.autoupdate(url, script.version) then script:reload() end
wait(-1)
end
Попытка выполнения данного кода, вызывает ошибку (номера строк могут не соответствовать действительности)
console:
(error) Updater: D:\Games\GTA San Andreas\moonloader\updater.lua:10: attempt to yield across C-call boundary
stack traceback:
[C]: in function 'wait'
D:\Games\GTA San Andreas\moonloader\updater.lua:10: in function 'downloadUrlToFileSync'
D:\Games\GTA San Andreas\moonloader\updater.lua:26: in function <D:\Games\GTA San Andreas\moonloader\updater.lua:31>
stack traceback:
[C]: in function 'autoupdate'
D:\Games\GTA San Andreas\moonloader\script.lua:91: in function <D:\Games\GTA San Andreas\moonloader\script.lua:84>
(error) Script: Script died due to an error. (1F50E674)