local NAME = 'I2CEPlugins.LuaScriptTemplates' local CLASS_NAME = 'LuaScriptTemplates' local VERSION = '1.0.1' local DESCRIPTION = 'I2 Cheat Engine Lua Script Templates' local AUTHOR = 'TheyCallMeTim13@Gmail.com' if BEBUG then print(NAME, ...) end --[[ ]] pcall(require, 'I2CETLua.addressList') local status, _ = pcall(require, 'I2CETLua.TemplateEngine') if not status then status, _ = pcall(require, 'TemplateEngine') end if not status then status, _ = pcall(dofile, getAutorunPath()..'TemplateEngine.lua') end if not status then print('"TemplateEngine" not found, it is required for "'..NAME..'" to work.') return end local sep = package.config:sub(1,1) local WildCard = 'x' local TemplatesFolder = getAutorunPath()..'Templates'..sep..'lua' local LuaFileExtension = 'lua' if os.getenv('CE_LUA_PROJECTS_FOLDER') then TemplatesFolder = os.getenv('CE_LUA_PROJECTS_FOLDER')..sep..'Templates'..sep..'lua' end local LoadedTemplatesData = {} local function exists(name) if not name then return end return os.rename(name, name) and true or false end local function generateScript(name, script, sender) local settingsPath = TemplatesFolder..sep..name..'.settings.lua' local env = { FinalCompilation = false } env.__index = env setmetatable(env, { __index = _G }) local settings = exists(settingsPath) and loadfile(settingsPath, 't', env)() or { } local env = { CEVersion = getCEVersion(), Date = os.date('%x'), FinalCompilation = true, } env.__index = env setmetatable(env, { __index = _G }) if exists(settingsPath) then settings = loadfile(settingsPath, 't', env)() or { } end local path = TemplatesFolder..sep..'Header.'..LuaFileExtension if exists(path) then local template, err = TemplateEngine.compileFile(path, env) if template and not err then env.Header = template else print(err) end end path = TemplatesFolder..sep..name..'.'..LuaFileExtension template, err = TemplateEngine.compileFile(path, env) if template and not err then if type(script.addText) == 'function' then script.addText(template) else local s = script.getText() script.clear() script.setText(s..template) end if type(createMemoryRecords) == 'function' and settings.MemoryRecords then env.Script = template setmetatable(env, { }) createMemoryRecords(settings.MemoryRecords, env, false) end else print(err) end end local function registerLuaTemplate(caption, onClick, shortcut) local templateData = { Caption = caption, OnClick = onClick, Shortcut = shortcut, } table.insert(LoadedTemplatesData, templateData) end local function getTableLuaForm() local form = nil for i = 0, getFormCount() - 1 do if getForm(i).Caption == 'Lua script: Cheat Table' then form = getForm(i) end end return form end local function loadFormTemplatesMenu(form) miTemplate = createMenuItem(form.Menu.Items) miTemplate.Name = 'miTemplateLua' miTemplate.Caption = 'Template' form.Menu.Items.add(miTemplate) for i, data in ipairs(LoadedTemplatesData) do local mi = createMenuItem(miTemplate) mi.Name = 'mi'..data.Caption:gsub(' ', '') mi.Caption = data.Caption mi.Shortcut = data.Shortcut mi.OnClick = function(sender) data.OnClick(form.Assemblescreen.Lines, sender) end miTemplate.add(mi) end end local function loadTemplates(thread) local fileList if exists(TemplatesFolder..sep..'LoadOrder.'..LuaFileExtension) then fileList = dofile(TemplatesFolder..sep..'LoadOrder.'..LuaFileExtension) end if not fileList then fileList = getFileList(TemplatesFolder) end for i, fp in ipairs(fileList) do local fn = fp:match('([^'..sep..']+)$') local fn = fn:match('^(.+)%..-$') if fn ~= 'Header' and fn ~= 'LoadOrder' and not fn:match('.settings$') then local settingsPath = TemplatesFolder..sep..fn..'.settings.'..LuaFileExtension local env = { FinalCompilation = false } env.__index = env setmetatable(env, { __index = _G }) local settings = exists(settingsPath) and loadfile(settingsPath, 't', env)() or { } registerLuaTemplate(settings.Caption or fn, function(script, sender) generateScript(fn, script, sender) end, settings.Shortcut) end end loadFormTemplatesMenu(getTableLuaForm()) thread.terminate() end local function formAddNotification(form) local fnTimer = createTimer(MainForm) fnTimer.Interval = 200 fnTimer.OnTimer = function(timer) timer.destroy() if form.ClassName == 'TfrmAutoInject' and form.Caption:match('^LUA Script') then loadFormTemplatesMenu(form) end end end registerFormAddNotification(formAddNotification) createThread(loadTemplates)