--[====================================================================================================================[ Example: require('I2CETUpdater') CETUpdater.checkTableVersion(versionUrl, tableVersion, tableName) Example: require('I2CETUpdater') CETUpdater.TableVersion = '3.0.8' CETUpdater.TableName = 'mgsvtpp' CETUpdater.VersionUrl = 'https://docs.google.com/document/d/1pPgHNGV1-EmIpfxxXp0vnoeoZxlCg11ofdV1x78bJEw/export?format=txt' CETUpdater.checkTableVersion() Example: require('I2CETUpdater') CETUpdater.checkTableVersion('https://docs.google.com/document/d/1pPgHNGV1-EmIpfxxXp0vnoeoZxlCg11ofdV1x78bJEw/export?format=txt', '3.0.8', 'mgsvtpp') ]====================================================================================================================]-- local NAME = 'I2 Cheat Engine Table Updater' local CLASS_NAME = 'I2CETUpdater' local VERSION = '1.0.4' local AUTHOR = 'Matt Irwin; Matt.Irwin.US@Gmail.com' local LICENSE = [=[MIT License Copyright (c) 2018 Matt Irwin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]=] local format = string.format local strE = string.empty or STRING_EMPTY or '' local t = translate -- local Logger = { -- LEVELS = { -- OFF = 0, -- FATAL = 1, -- ERROR = 2, -- WARN = 3, -- INFO = 4, -- DEBUG = 5, -- TRACE = 6 -- }, -- } -- for k, v in pairs(Logger.LEVELS) do -- -- Logger[k:lower()] = function( ... ) return end -- -- Logger[k:lower() .. 'f'] = function( ... ) return end -- Logger[k:lower()] = function(msg, ex) return print(msg, ex) end -- Logger[k:lower() .. 'f'] = function(msg, ... ) return print(string.format(msg, ... )) end -- end local Logger = Logger if Logger == nil then if type(CETrequire) == 'function' then Logger = CETrequire('I2CETLogger') else Logger = require('I2CETLogger') end Logger.LogName = 'CETupdater' end local function split(s, delimiter) result = {} for match in (s .. delimiter):gmatch('(.-)' .. delimiter) do table.insert(result, match) end return result end local function interp(s, tbl) if s == nil then return end return (s:gsub('($%b{})', function(w) return tbl[w:sub(3, -2)] or w end)) end local defaultFileExtension = 'CT' CETUpdater = { Name = NAME, ClassName = CLASS_NAME, Version = VERSION, Author = AUTHOR, License = LICENSE, TableVersion = TABLE_VERSION, TableName = TABLE_TITLE, TableUrl = TABLE_URL, VersionUrl = VERSION_URL, FileExtension = defaultFileExtension, DefaultVersion = '1.0.1', FileNameFormat = '${FileName}.[v${Version}].${FileExtension}', } function CETUpdater.getVersionNumber(versionString) local vers = { string.match(versionString, '(%d+)%.(%d+)%.(%d+)') } local verStr = '' for i = 1, #vers do verStr = format('%s%08X', verStr, tonumber(vers[i])) end return tonumber(verStr, 16) end function CETUpdater.isTableToDownLoad(versionUrl, tableVersion) versionUrl = versionUrl or CETUpdater.VersionUrl local tableVersionStr = tableVersion or CETUpdater.TableVersion tableVersion = CETUpdater.getVersionNumber(tableVersionStr) ---- ADD: nil check local iNet = getInternet() local text = iNet.getURL(versionUrl) iNet.destroy() if text then local args = split(text:gsub('\r\n', '\n'), '\n') local version = CETUpdater.getVersionNumber(args[1]) local url = args[2] return (version > tableVersion), tableVersionStr, url else Logger.errorf('Unable to download version file; at: "%s"', versionUrl) return nil end end function CETUpdater.getNewTable(versionStr, tableUrl, tableName) versionStr = versionStr or CETUpdater.DefaultVersion tableUrl = tableUrl or CETUpdater.TableUrl tableName = tableName or CETUpdater.TableName or CETUpdater.ClassName ---- ADD: nil check local iNet = getInternet() local newTableStr = iNet.getURL(tableUrl) if newTableStr ~= nil then newTableStr = newTableStr:gsub('\r\n', '\n') local tbl = { TrainerOrigin = TrainerOrigin, Process = process, Temp = os.getenv('TEMP') .. package.config:sub(1,1), FileName = tableName or strE, Version = versionStr or strE, FileExtension = CETUpdater.FileExtension or defaultFileExtension, } local newTablePath = interp(CETUpdater.FileNameFormat, tbl) local f, err = io.open(newTablePath, 'w') if f and not err then f:write(newTableStr) f:close() Logger.infof('New table/trainer downloaded; from: "%s", to: "%s"', tableUrl, newTablePath) loadTable(newTablePath) else Logger.errorf('Unable to create temp file for new table/trainer; at: "%s"', newTablePath) end else Logger.errorf('Unable to download latest table/trainer file; at: "%s"', tableUrl) end iNet.destroy() end function CETUpdater.checkTableVersion(versionUrl, tableVersion, tableName) local downloadTable, versionStr, url = CETUpdater.isTableToDownLoad(versionUrl, tableVersion) if downloadTable then local answer = messageDialog(t('There is a newer version of this table/trainer.') .. '\n' .. format('At: "%s"', url) .. '\n' .. t('Would you like to download it now?'), mtWarning, mbYes, mbNo) if answer == mrYes then CETUpdater.getNewTable(versionStr, url, tableName) messageDialog(t('Be sure to save the new table/trainer!'), mtInformation, mbOK) end else messageDialog(format(t('You currently have the latest version') .. ', "%s"', versionStr), mtInformation, mbOK) end end return CETUpdater