[Lua] Lua Script Template Engine ("compiled templates")

Post/find Cheat Engine extensions here.
Post Reply
TheyCallMeTim13
Posts: 16
Joined: Tue Apr 25, 2023 4:27 pm

[Lua] Lua Script Template Engine ("compiled templates")

Post by TheyCallMeTim13 »

Lua Script Template Engine ("compiled templates")

For Auto Assembler Templates see this post.

This is a Lua script template engine that compiles/renders templates, the template engine is based off this blog post. You can access variables and run lua code in the templates using the appropriate tags. There are two file's you'll need to start; "TemplateEngine.lua" and "LuaScriptTemplates.lua". You can just stick them both in the "autorun" folder. And you'll need to create the "Templates\lua" Folder (in the "autorun" folder) for the templates.

When CE loads, "LuaScriptTemplates" will scan the templates folder for "LoadOrder.lua" if found and it returns a table/array of file names it will load in that order; if not found is will scan the templates folder for any ".lua" files. When it finds a template file it will also look for "[template file name].settings.lua", the settings file is not required but allows for more control of the template and menu items added to the Lua script form. The settings file can return a table of the template's settings, but this is not required (more on why later).

For simple output you can use "<<" and ">>" tags, what ever is returned will be converted to string if not already and replace where the open and close tags are; these can be a called function, variable, or just a string/number. For lua code you can use "<%" and "%>" tags, these can be full lua code; thus you can set/create variables, use loops, and if statements.

Here is an archive with the needed files and one example template and setting file.



Using a template file named "My basic template.lua" like this:
CODE: SELECT ALL

-- Cheat Engine Version: << getCEVersion() >>
<% print('This will print when rendered but will not render in the template.') %>
<% local text = 'here is a local variable created in the template.' %>
'<< text >> So that's pretty cool.'
-- if you need "\<<" or "\<%" to be in the rendered template then you need to escape them.
-- A single "<" doesn't need to be escaped, nor do ">>" and "%>".
print('This is in the template and not processed when rendered.')
It will show up in the auto assembler form's template menu as "My basic template" and render like this:
CODE: SELECT ALL

-- Cheat Engine Version: 7.2

here is a local variable created in the template. So that's pretty cool.
-- if you need "<<" or "<%" to be in the rendered template then you need to escape them.
-- A single "<" doesn't need to be escaped, nor do ">>" and "%>".
print('This is in the template and not processed when rendered.')

There is another special file named "Header.lua" which is rendered then passed to the template as "Header":
CODE: SELECT ALL

<< Header >>
-- This allows for a common header without polluting the template file it self
-- and can have lua code like the template files.
-- But it will be a blank line if there's no header file.

For a settings file for "My basic template.lua" we can create a file named "My basic template.settings.lua" like this:
CODE: SELECT ALL

local settings = {
Caption = 'Custom: Basic Template',
Shortcut = 'Ctrl+Alt+1',
}
return settings
Template settings are listed here:
CODE: SELECT ALL

Template Settings:
Caption : string
The caption of the template's menu item.
Shortcut : string
The shortcut of the template's menu item.
The variables passed to the template, and settings file on final compilation, are listed here:
CODE: SELECT ALL

Template Variables:
CEVersion : number
The current CE version.
Date : string
The current date.
FinalCompilation : boolean
Only for the settings files as they get loaded once when CE loads then again at the start
of template rendering to get the template settings, and again just before rendering the
template but only on the last load will the environment be setup.
So this is used to know if the environment is setup if more environment setup is done in
the setting file.
Globals are accessible in the templates and settings files, but through a metatable thus they can not be set in a template or setting's file. But the environment variables can be manipulated in the settings file's directly during final compilation.
Post Reply