From 56ed62e739a04b7a7cdddef3b617863f70ba3e74 Mon Sep 17 00:00:00 2001 From: Wleeaf Date: Sat, 21 Nov 2026 12:01:24 +0300 Subject: [PATCH] Load records with attributes on their tag; run Cheat Engine tables' Lua Cheat Engine marks some records on the tag itself (, ). The loader looked only for "", so it skipped such a record and moved its children to the top level, and the nesting count missed them too. The Dave the Diver table from issue #15 loaded 550 of its 1040 records, without its "Initialize Mono" script and all 18 hooks under it. Start tags are now matched with or without attributes, and the attributes are kept and written back on save (through the GUI's records as well). The same table's Lua needed more of Cheat Engine: - Cheat Engine registers every Lua function under both cases of its first letter, and tables call OpenProcess and ShellExecute. Engine functions now get the other name too (not Lua's own, and never over a name in use); the pass runs again after the GUI's functions. - getProcessIDFromProcessName and openProcess find a Windows program under Wine by the .exe name in argv[0], ignoring case. Only the program itself has it there, not Proton's launcher or steam.exe. - mono_initialize() and LaunchMonoDataCollector() exist. Mono and IL2CPP support is built in, so nothing is injected; the latter returns the runtime module's base, or 0, as Cheat Engine does. - beep() exists (the desktop bell in the GUI). --- CHANGELOG.md | 16 ++++++++++ analysis/mono_dissector.cpp | 34 ++++++++++++++------ analysis/mono_dissector.hpp | 3 ++ core/ct_file.cpp | 41 ++++++++++++++++++++---- core/ct_file.hpp | 1 + docs/SCRIPTING.md | 19 +++++++++++ gui/addresslist_model.cpp | 4 +-- gui/mainwindow_table.cpp | 2 ++ scripting/lua_bindings.cpp | 3 ++ scripting/lua_bindings_internal.hpp | 3 ++ scripting/lua_bindings_process.cpp | 22 ++++++++++++- scripting/lua_bindings_runtimes.cpp | 21 +++++++++++++ scripting/lua_engine.cpp | 48 ++++++++++++++++++++++++++++ scripting/lua_engine.hpp | 7 +++++ scripting/lua_gui.cpp | 3 ++ test/cecore_test.hpp | 1 + test/cecore_test_lua_scans.cpp | 34 ++++++++++++++++++++ test/cecore_test_remote.cpp | 49 +++++++++++++++++++++++++++++ test/main.cpp | 1 + 19 files changed, 294 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edcf8bc..d327af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,22 @@ reimplementation of Cheat Engine). ### Fixes +- Tables whose records carry attributes on their tag (``, ``) load completely again + ([#15](https://github.com/wleeaf/cheat-engine-linux/issues/15)). Such a + record was skipped and its children moved to the top level: the Dave the + Diver table from that issue lost its "Initialize Mono" script with all 18 + hooks under it and loaded 550 of its 1040 records. The attributes are now + kept and written back on save. +- Lua in tables written for Cheat Engine runs further: + - every function answers to both cases of its first letter, as in Cheat + Engine (`OpenProcess`, `ShellExecute`); + - `getProcessIDFromProcessName` and `openProcess` find a Windows game under + Wine or Proton by its `.exe` name; + - `mono_initialize()` and `LaunchMonoDataCollector()` are accepted, so an + "Initialize Mono" script enables (Mono and IL2CPP support is built in, so + they inject nothing); + - `beep()` exists. - Memory that Wine commits for a hook (see v1.2.0 and [#9](https://github.com/wleeaf/cheat-engine-linux/issues/9)) now also works for a game inside a Proton (Steam Runtime) or Flatpak container. The small diff --git a/analysis/mono_dissector.cpp b/analysis/mono_dissector.cpp index 5279087..62552e6 100644 --- a/analysis/mono_dissector.cpp +++ b/analysis/mono_dissector.cpp @@ -100,10 +100,15 @@ MonoDissection parseMonoDump(const std::string& text) { return out; } -ManagedKind detectManagedKind(ProcessHandle& proc) { +namespace { +struct RuntimeModules { + std::optional mono, il2cpp; +}; + +RuntimeModules runtimeModules(ProcessHandle& proc) { // Mono ships libmono*/mono-2.0; IL2CPP builds put the AOT code in // GameAssembly.so (Unity/Linux) and carry global-metadata.dat + il2cpp_* code. - bool mono = false, il2cpp = false; + RuntimeModules found; for (const auto& m : proc.modules()) { std::string n = m.name; for (auto& ch : n) ch = static_cast(std::tolower((unsigned char)ch)); @@ -111,17 +116,28 @@ ManagedKind detectManagedKind(ProcessHandle& proc) { // libmonobdwgc-2.0 (Linux), mono-2.0-bdwgc.dll / mono.dll (Unity under // Proton), or a mono / mono-sgen executable. Not any name containing // "mono": fonts such as DejaVuSansMono.ttf are mapped by most games. - if (n.starts_with("libmono-2.0") || n.starts_with("libmonosgen") || n.starts_with("libmonobdwgc") || - n.starts_with("mono-2.0") || n == "mono.dll" || n == "mono" || n.starts_with("mono-sgen")) - mono = true; - if (n.find("gameassembly") != std::string::npos || n.find("il2cpp") != std::string::npos) - il2cpp = true; + if (!found.mono && (n.starts_with("libmono-2.0") || n.starts_with("libmonosgen") || n.starts_with("libmonobdwgc") || + n.starts_with("mono-2.0") || n == "mono.dll" || n == "mono" || n.starts_with("mono-sgen"))) + found.mono = m; + if (!found.il2cpp && (n.find("gameassembly") != std::string::npos || n.find("il2cpp") != std::string::npos)) + found.il2cpp = m; } - if (mono) return ManagedKind::Mono; - if (il2cpp) return ManagedKind::Il2Cpp; + return found; +} +} // namespace + +ManagedKind detectManagedKind(ProcessHandle& proc) { + const auto found = runtimeModules(proc); + if (found.mono) return ManagedKind::Mono; + if (found.il2cpp) return ManagedKind::Il2Cpp; return ManagedKind::None; } +std::optional managedRuntimeModule(ProcessHandle& proc) { + auto found = runtimeModules(proc); + return found.mono ? found.mono : found.il2cpp; +} + std::string findMonoAgentPath() { return findCompanionLibrary("libcecore_mono_agent.so"); } std::string findCompanionLibrary(const char* kName) { diff --git a/analysis/mono_dissector.hpp b/analysis/mono_dissector.hpp index d943e4b..20ecc5a 100644 --- a/analysis/mono_dissector.hpp +++ b/analysis/mono_dissector.hpp @@ -71,6 +71,9 @@ std::string findCompanionLibrary(const char* name); /// rather than injecting an agent that finds no mono_* symbols. enum class ManagedKind { None, Mono, Il2Cpp }; ManagedKind detectManagedKind(ProcessHandle& proc); +/// The module detectManagedKind decided by: the Mono runtime library, else the +/// IL2CPP binary (GameAssembly). None for a target without either. +std::optional managedRuntimeModule(ProcessHandle& proc); /// Inject the agent .so into `proc`, wait up to timeoutMs for its dump to /// complete, and parse it. `agentSoPath` is the path to libcecore_mono_agent.so. diff --git a/core/ct_file.cpp b/core/ct_file.cpp index 2f2f04d..9a09472 100644 --- a/core/ct_file.cpp +++ b/core/ct_file.cpp @@ -930,7 +930,10 @@ bool writeTableXml(const CheatTable& table,std::ostream& f) { std::function writeEntry = [&](int i, int depth) { if (done[i]) return; done[i] = 1; - f << " \n"; + f << " \n"; writeEntryFields(entries[i]); if (!children[i].empty() && depth + 1 < kMaxEntryDepth) { f << " \n"; @@ -1127,19 +1130,36 @@ static std::string unknownRecordElements(const std::string& own) { // for nested pairs. npos if unbalanced. Needed because Cheat Engine nests a // group's children inside its own , so a naive find of the first // close tag matches a child's close instead of the parent's. +// Position of the next start tag of `tag` at or after `from`: "" or one +// with attributes ("", as Cheat Engine writes a +// record without a check box). Not a longer name ("") and not a +// self-closing "", which has no close tag to match. +static size_t findStartTag(const std::string& xml, const std::string& tag, size_t from) { + const std::string open = "<" + tag; + for (size_t p = xml.find(open, from); p != std::string::npos; p = xml.find(open, p + 1)) { + const size_t after = p + open.size(); + if (after >= xml.size()) break; + if (xml[after] == '>') return p; + if (xml[after] != ' ' && xml[after] != '\t' && xml[after] != '\r' && xml[after] != '\n') continue; + const size_t end = xml.find('>', after); + if (end == std::string::npos) break; + if (xml[end - 1] != '/') return p; + } + return std::string::npos; +} + static size_t findMatchingClose(const std::string& xml, size_t openPos, const std::string& tag) { - std::string open = "<" + tag + ">"; std::string close = ""; // Keep the next opening and closing tag and advance only the one taken: // searching both again on every step rescanned the text up to the next // close each time, which made a deeply nested table quadratic to load. - size_t nextOpen = xml.find(open, openPos + open.size()); - size_t nextClose = xml.find(close, openPos + open.size()); + size_t nextOpen = findStartTag(xml, tag, openPos + 1); + size_t nextClose = xml.find(close, openPos + 1); int depth = 1; while (nextClose != std::string::npos) { if (nextOpen != std::string::npos && nextOpen < nextClose) { ++depth; - nextOpen = xml.find(open, nextOpen + open.size()); + nextOpen = findStartTag(xml, tag, nextOpen + 1); } else { if (--depth == 0) return nextClose; nextClose = xml.find(close, nextClose + close.size()); @@ -1172,7 +1192,7 @@ static void parseCheatEntriesBlock(const std::string& xml, size_t begin, size_t if (depth >= kMaxEntryDepth) { validFormat = false; return; } size_t pos = begin; while (true) { - auto entryStart = xml.find("", pos); + auto entryStart = findStartTag(xml, "CheatEntry", pos); if (entryStart == std::string::npos || entryStart >= end) break; auto entryClose = findMatchingClose(xml, entryStart, "CheatEntry"); if (entryClose == std::string::npos || entryClose + CE_CLOSE.size() > end) break; @@ -1205,6 +1225,13 @@ static void parseCheatEntriesBlock(const std::string& xml, size_t begin, size_t CheatEntry e; e.parentId = parentId; + // CE marks some records on the tag itself (NoCheckbox="1" for a label, + // NoScriptTag="1"); kept so a save writes them back. + if (const size_t tagEnd = xml.find('>', entryStart); tagEnd != std::string::npos && tagEnd < entryClose) { + const size_t first = entryStart + std::string(" (DisplayValueAsItem="1" ...), kept as written + std::string entryAttributes; // Attributes of the record's own (NoCheckbox="1", NoScriptTag="1"), kept as written std::string hotkeyKeys; // Hotkey binding std::string increaseHotkeyKeys; std::string decreaseHotkeyKeys; diff --git a/docs/SCRIPTING.md b/docs/SCRIPTING.md index 2235747..9324bb2 100644 --- a/docs/SCRIPTING.md +++ b/docs/SCRIPTING.md @@ -15,6 +15,25 @@ local layout = getIl2CppClassLayout("Player") if not layout then print("no IL2CPP here"); return end ``` +### Tables written for Cheat Engine + +As in Cheat Engine, every function also answers to its name with the other +case of the first letter: `OpenProcess` is `openProcess`, `ShellExecute` is +`shellExecute`. Lua's own functions (`print`, `pairs`) keep their one name. + +`getProcessIDFromProcessName("Game.exe")` and `openProcess("Game.exe")` find a +Windows program under Wine or Proton by its `.exe` name, ignoring case, as +Windows does. Wine runs it as `wine64-preloader`, with the program's Windows +path in its first argument. + +Cheat Engine's `mono_initialize()` and `LaunchMonoDataCollector()` are accepted, +so a table's "Initialize Mono" script runs. Mono and IL2CPP support is built in, +and nothing is injected: `mono_initialize()` does nothing, and +`LaunchMonoDataCollector()` returns the base address of the target's Mono or +IL2CPP runtime module, or 0 when the target has neither. The collector's +`mono_*` query functions are not provided. Use `monoDissect` and the +`getIl2Cpp*` functions below instead. + --- ## Typed memory access diff --git a/gui/addresslist_model.cpp b/gui/addresslist_model.cpp index 5967a76..e7e220a 100644 --- a/gui/addresslist_model.cpp +++ b/gui/addresslist_model.cpp @@ -539,8 +539,8 @@ bool AddressListModel::fromJson(const QJsonArray& arr, bool preserveExisting) { e.decreaseHotkeyKeys = obj["decreaseHotkey"].toString(); e.hotkeyStep = obj["hotkeyStep"].toString("1"); e.luaScript = obj["lua"].toString(); - for (const char* key : {"customType", "async", "dropdownAttributes", "unparsedAddress", "unparsedOffsets", - "extraHotkeys", "extraXml"}) + for (const char* key : {"customType", "async", "dropdownAttributes", "entryAttributes", "unparsedAddress", + "unparsedOffsets", "extraHotkeys", "extraXml"}) if (obj.contains(key)) e.tableFields[key] = obj[key]; e.indent = std::max(0, obj.contains("indent") ? obj["indent"].toInt() diff --git a/gui/mainwindow_table.cpp b/gui/mainwindow_table.cpp index bb3eb35..ed6b4ba 100644 --- a/gui/mainwindow_table.cpp +++ b/gui/mainwindow_table.cpp @@ -172,6 +172,7 @@ ce::CheatTable MainWindow::buildCheatTable() const { e.customTypeName = obj["customType"].toString().toStdString(); e.asyncScript = obj["async"].toBool(); e.dropdownAttributes = obj["dropdownAttributes"].toString().toStdString(); + e.entryAttributes = obj["entryAttributes"].toString().toStdString(); e.unparsedAddress = obj["unparsedAddress"].toString().toStdString(); for (const auto& text : obj["unparsedOffsets"].toArray()) e.unparsedOffsets.push_back(text.toString().toStdString()); @@ -349,6 +350,7 @@ QJsonArray cheatEntriesToJson(const ce::CheatTable& table) { if (!e.customTypeName.empty()) obj["customType"] = QString::fromStdString(e.customTypeName); if (e.asyncScript) obj["async"] = true; if (!e.dropdownAttributes.empty()) obj["dropdownAttributes"] = QString::fromStdString(e.dropdownAttributes); + if (!e.entryAttributes.empty()) obj["entryAttributes"] = QString::fromStdString(e.entryAttributes); if (!e.unparsedOffsets.empty()) { QJsonArray texts; for (const auto& text : e.unparsedOffsets) texts.append(QString::fromStdString(text)); diff --git a/scripting/lua_bindings.cpp b/scripting/lua_bindings.cpp index 2749228..a97f438 100644 --- a/scripting/lua_bindings.cpp +++ b/scripting/lua_bindings.cpp @@ -638,6 +638,9 @@ static void registerTableToolBindings(lua_State* L) { lua_register(L, "playSound", l_playSound); lua_register(L, "getManagedRuntimes", l_getManagedRuntimes); lua_register(L, "monoDissect", l_monoDissect); + lua_register(L, "mono_initialize", l_monoInitialize); + lua_register(L, "LaunchMonoDataCollector", l_launchMonoDataCollector); + lua_register(L, "beep", l_beep); lua_register(L, "findMonoFunction", l_findMonoFunction); lua_register(L, "findMonoInstances", l_findMonoInstances); lua_register(L, "executeCode", l_executeCode); diff --git a/scripting/lua_bindings_internal.hpp b/scripting/lua_bindings_internal.hpp index 35e816b..8c3dc6b 100644 --- a/scripting/lua_bindings_internal.hpp +++ b/scripting/lua_bindings_internal.hpp @@ -231,6 +231,9 @@ int l_generateTrainer(lua_State* L); // lua_bindings_runtimes.cpp int l_getManagedRuntimes(lua_State* L); int l_monoDissect(lua_State* L); +int l_monoInitialize(lua_State* L); +int l_launchMonoDataCollector(lua_State* L); +int l_beep(lua_State* L); int l_createSimpleHook(lua_State* L); int l_removeSimpleHook(lua_State* L); int l_findMonoFunction(lua_State* L); diff --git a/scripting/lua_bindings_process.cpp b/scripting/lua_bindings_process.cpp index 4a9df3e..3981e7a 100644 --- a/scripting/lua_bindings_process.cpp +++ b/scripting/lua_bindings_process.cpp @@ -109,13 +109,33 @@ static std::string readProcessName(const std::filesystem::path& procDir) { return name; } +// A Windows program under Wine or Proton runs as wine64-preloader with a +// truncated comm ("DaveTheDiver.ex"); Wine puts the program's Windows path in +// argv[0] ("Z:\\games\\DaveTheDiver.exe"). Only the program itself has it +// there: Proton's launcher, steam.exe and the runtime's wrappers name the +// .exe later in their arguments. Windows compares the name without case. +static bool windowsProgramMatches(const std::filesystem::path& procDir, std::string_view target) { + auto lower = [](std::string_view text) { + std::string out(text); + for (char& c : out) c = static_cast(std::tolower(static_cast(c))); + return out; + }; + const std::string wanted = lower(target); + if (wanted.size() <= 4 || !wanted.ends_with(".exe")) return false; + std::ifstream cmdline(procDir / "cmdline", std::ios::binary); + std::string argv0; + if (!cmdline || !std::getline(cmdline, argv0, '\0')) return false; + const size_t slash = argv0.find_last_of("/\\"); + return lower(slash == std::string::npos ? argv0 : argv0.substr(slash + 1)) == wanted; +} + static bool processNameMatches(const std::filesystem::path& procDir, std::string_view target) { if (readProcessName(procDir) == target) return true; std::error_code ec; auto exe = std::filesystem::read_symlink(procDir / "exe", ec); - return !ec && exe.filename().string() == target; + return (!ec && exe.filename().string() == target) || windowsProgramMatches(procDir, target); } pid_t findProcessIdByName(std::string_view target) { diff --git a/scripting/lua_bindings_runtimes.cpp b/scripting/lua_bindings_runtimes.cpp index 327cedc..7a6de48 100644 --- a/scripting/lua_bindings_runtimes.cpp +++ b/scripting/lua_bindings_runtimes.cpp @@ -89,6 +89,27 @@ extern "C" { #include "scripting/lua_bindings_internal.hpp" namespace ce::lua_detail { +// Cheat Engine's mono_initialize() sets up its injected Mono data collector +// (menus, process-open hooks). Mono and IL2CPP support is built in here +// (monoDissect, getIl2CppClasses, ...), so there is nothing to set up; tables +// call it before their own scripts. +int l_monoInitialize(lua_State*) { return 0; } + +// LaunchMonoDataCollector() -> the base address of the target's Mono or IL2CPP +// runtime module, or 0 without one (Cheat Engine returns the runtime's base +// once its collector runs, and 0 on failure). Nothing is injected: the +// collector's mono_* query functions are not provided, and monoDissect and +// the getIl2Cpp* functions read that information instead. +int l_launchMonoDataCollector(lua_State* L) { + auto* p = getProc(L); + auto runtime = p ? ce::managedRuntimeModule(*p) : std::nullopt; + lua_pushinteger(L, runtime ? static_cast(runtime->base) : 0); + return 1; +} + +// beep(): the GUI sounds the desktop's bell; without one there is nothing to do. +int l_beep(lua_State*) { return 0; } + int l_getManagedRuntimes(lua_State* L) { auto* p = getProc(L); lua_newtable(L); diff --git a/scripting/lua_engine.cpp b/scripting/lua_engine.cpp index fd417cc..f191ae3 100644 --- a/scripting/lua_engine.cpp +++ b/scripting/lua_engine.cpp @@ -8,6 +8,9 @@ #include "platform/linux/ceserver_client.hpp" // complete type for ownedCeserverClient_ extern "C" { +#include +#include +#include #include #include #include @@ -357,6 +360,51 @@ void LuaEngine::registerBindings() { // Register extended bindings (readByte, readString, disassemble, autoAssemble, etc.) registerExtendedBindings(L_); + addCheatEngineCaseAliases(L_); +} + +void addCheatEngineCaseAliases(lua_State* L) { + // Lua's own functions keep their one name, as in Cheat Engine. + static const std::set> baseLibrary = { + "assert", "collectgarbage", "dofile", "error", "getmetatable", "ipairs", "load", "loadfile", "next", + "pairs", "pcall", "print", "rawequal", "rawget", "rawlen", "rawset", "require", "select", + "setmetatable", "tonumber", "tostring", "type", "xpcall"}; + static constexpr const char* kAliases = "ce_case_aliases"; // alias name -> the function it was given + if (lua_getfield(L, LUA_REGISTRYINDEX, kAliases) != LUA_TTABLE) { + lua_pop(L, 1); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, kAliases); + } + const int aliases = lua_gettop(L); + lua_pushglobaltable(L); + const int globals = lua_gettop(L); + std::vector names; + for (lua_pushnil(L); lua_next(L, globals); lua_pop(L, 1)) + if (lua_type(L, -2) == LUA_TSTRING && lua_iscfunction(L, -1)) names.emplace_back(lua_tostring(L, -2)); + for (const auto& name : names) { + const unsigned char first = static_cast(name.front()); + if (!std::isalpha(first) || baseLibrary.contains(name)) continue; + // An alias is not a source of further aliases. + if (lua_getfield(L, aliases, name.c_str()) != LUA_TNIL) { lua_pop(L, 1); continue; } + lua_pop(L, 1); + std::string other = name; + other.front() = static_cast(first ^ 0x20); + lua_pushstring(L, other.c_str()); + lua_rawget(L, globals); // what the other name holds now + lua_getfield(L, aliases, other.c_str()); // what this gave it before, if anything + const bool free = lua_isnil(L, -2) || (!lua_isnil(L, -1) && lua_rawequal(L, -1, -2)); + lua_pop(L, 2); + if (!free) continue; + lua_pushstring(L, name.c_str()); + lua_rawget(L, globals); + lua_pushvalue(L, -1); + lua_setfield(L, aliases, other.c_str()); + lua_pushstring(L, other.c_str()); + lua_insert(L, -2); + lua_rawset(L, globals); + } + lua_pop(L, 2); } std::string LuaEngine::execute(const std::string& code) { diff --git a/scripting/lua_engine.hpp b/scripting/lua_engine.hpp index a1f0aaa..85fd7ef 100644 --- a/scripting/lua_engine.hpp +++ b/scripting/lua_engine.hpp @@ -168,4 +168,11 @@ class LuaEngine { /// Register extended CE API bindings (defined in lua_bindings.cpp) void registerExtendedBindings(lua_State* L); +/// Cheat Engine registers every Lua function under both cases of its first +/// letter (openProcess and OpenProcess, shellExecute and ShellExecute), and +/// tables call either. Gives each engine function its other name, unless that +/// name holds something else. Run again after more functions are registered +/// (the GUI's): an alias this made follows its function when it is replaced. +void addCheatEngineCaseAliases(lua_State* L); + } // namespace ce diff --git a/scripting/lua_gui.cpp b/scripting/lua_gui.cpp index a31d28b..71d34b4 100644 --- a/scripting/lua_gui.cpp +++ b/scripting/lua_gui.cpp @@ -6,6 +6,7 @@ #include "scripting/lua_gui.hpp" #include "scripting/lua_safe.hpp" +#include "scripting/lua_engine.hpp" #include "gui/canvaswidget.hpp" extern "C" { @@ -955,6 +956,8 @@ void registerLuaGuiBindings(lua_State* L) { lua_register(L, "addMainMenuItem", l_addMainMenuItem); lua_register(L, "getProperty", l_getProperty); lua_register(L, "setProperty", l_setProperty); + lua_register(L, "beep", [](lua_State*) -> int { QApplication::beep(); return 0; }); + ce::addCheatEngineCaseAliases(L); } void setLuaMainForm(QMainWindow* w) { diff --git a/test/cecore_test.hpp b/test/cecore_test.hpp index f556342..67ca0a4 100644 --- a/test/cecore_test.hpp +++ b/test/cecore_test.hpp @@ -313,6 +313,7 @@ void test_distributed_pointer_scan(); void test_pointer_scan_shard_through_static(); void test_pointer_scan_persistence(); void test_ct_table_luascript_after_entries(); +void test_ct_entry_tag_attributes(); void test_ct_json_pointer_roundtrip(); void test_il2cpp_pe_header_bounds(); void test_dwarf_symbols(); diff --git a/test/cecore_test_lua_scans.cpp b/test/cecore_test_lua_scans.cpp index 16288f4..53bb9e9 100644 --- a/test/cecore_test_lua_scans.cpp +++ b/test/cecore_test_lua_scans.cpp @@ -600,6 +600,40 @@ void test_lua_process_bindings(pid_t pid) { auto err = lua.execute(script); printf(" openProcess/getProcessList: %s\n", err.empty() ? "OK" : "FAILED"); + + // Tables written for Cheat Engine (issue #15) call functions with either + // case of the first letter, find a Windows game by its .exe name (Wine + // runs it as wine64-preloader, with the Windows path in argv[0]), and + // start Cheat Engine's Mono collector before their own scripts. + // A name of this run's own: another program may have the same .exe name. + const std::string exeName = "CeLookup" + std::to_string(getpid()) + ".exe"; + const std::string windowsPath = "Z:\\games\\Some Game\\" + exeName; + const pid_t game = fork(); + if (game == 0) { + execl("/bin/sleep", windowsPath.c_str(), "30", static_cast(nullptr)); + _exit(127); + } + const std::string gameText = std::to_string(game); + for (int i = 0; i < 200; ++i) { + std::ifstream cmdline("/proc/" + gameText + "/cmdline"); + std::string argv0; + if (std::getline(cmdline, argv0, '\0') && argv0.ends_with(".exe")) break; + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + const auto compat = lua.execute( + "assert(OpenProcess == openProcess and ShellExecute == shellExecute and Beep == beep)\n" + "assert(GetProcessIDFromProcessName == getProcessIDFromProcessName)\n" + "assert(Print == nil and Pairs == nil and Dofile == nil and Loadfile == nil)\n" + "assert(getProcessIDFromProcessName(string.lower('" + exeName + "')) == " + gameText + ")\n" + "assert(getProcessIDFromProcessName('Some Game') == nil)\n" + "assert(OpenProcess('" + exeName + "') == " + gameText + ")\n" + "assert(mono_initialize() == nil and Mono_initialize == mono_initialize)\n" + "assert(LaunchMonoDataCollector() == 0)\n" // sleep has no Mono or IL2CPP runtime + "beep()\n"); + kill(game, SIGKILL); + waitpid(game, nullptr, 0); + if (!compat.empty()) printf(" Lua error: %s\n", compat.c_str()); + printf(" Cheat Engine name aliases, .exe lookup and Mono start-up: %s\n", compat.empty() ? "OK" : "FAILED"); } // enumerateFunctions / buildCallGraph Lua bindings over the live target's main diff --git a/test/cecore_test_remote.cpp b/test/cecore_test_remote.cpp index dff821f..ab68b6b 100644 --- a/test/cecore_test_remote.cpp +++ b/test/cecore_test_remote.cpp @@ -866,6 +866,55 @@ void test_ct_table_luascript_after_entries() { printf(" table LuaScript not shadowed by a sibling record: %s (got '%s')\n", nestedOk ? "OK" : "FAILED", t2.luaScript.c_str()); } +void test_ct_entry_tag_attributes() { + printf("\n── Test: records whose tag has attributes (issue #15) ──\n"); + // Cheat Engine marks some records on the tag itself: for a label, . A loader that + // looked only for "" dropped such a record and promoted its + // children to the top level, so a table whose first record is its + // "Initialize" script lost that script and every group's nesting. + const char* xml = + "\n\n\n" + "3\"Initialize\"" + "Auto Assembler Script[ENABLE]\n[DISABLE]\n" + "10\"Group\"1" + "11\"Value\"" + "4 Bytes
400000
" + "12\"Note\"1" + "
\n" + "4\"\"1\n" + "5\"Link\"" + "Auto Assembler Script[ENABLE]\n[DISABLE]\n
\n" + "
\n\n"; + auto parentOf = [](const ce::CheatTable& table, int id) { + for (const auto& e : table.entries) if (e.id == id) return e.parentId; + return -2; + }; + auto attributesOf = [](const ce::CheatTable& table, int id) { + for (const auto& e : table.entries) if (e.id == id) return e.entryAttributes; + return std::string("(missing)"); + }; + auto check = [&](const ce::CheatTable& t) { + return t.entries.size() == 6 && parentOf(t, 3) == -1 && parentOf(t, 10) == 3 && parentOf(t, 11) == 10 && + parentOf(t, 12) == 10 && parentOf(t, 4) == -1 && parentOf(t, 5) == -1 && + attributesOf(t, 3) == "NoScriptTag=\"1\"" && attributesOf(t, 12) == "NoCheckbox=\"1\"" && + attributesOf(t, 5) == "NoScriptTag=\"1\"" && attributesOf(t, 11).empty(); + }; + ce::CheatTable t; + const bool loaded = t.loadFromString(xml) && check(t); + printf(" records with tag attributes load with their children nested: %s (%zu records)\n", + loaded ? "OK" : "FAILED", t.entries.size()); + auto path = (std::filesystem::temp_directory_path() / "ce_entry_attributes.ct").string(); + ce::CheatTable again; + std::string saved; + const bool written = t.save(path); + if (std::ifstream in(path); in) saved.assign(std::istreambuf_iterator(in), {}); + const bool roundTrip = written && again.load(path) && check(again) && + saved.find("") != std::string::npos && + saved.find("") != std::string::npos; + std::filesystem::remove(path); + printf(" a save writes the tag attributes back: %s\n", roundTrip ? "OK" : "FAILED"); +} void test_ct_json_pointer_roundtrip() { printf("\n── Test: JSON round-trip preserves pointer chain / length ──\n"); // saveJson/loadJson (also the .CETRAINER path) used to drop these, so a diff --git a/test/main.cpp b/test/main.cpp index 7830566..bc424db 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -226,6 +226,7 @@ int main(int argc, char* argv[]) { test_pointer_scan_shard_through_static(); test_pointer_scan_persistence(); test_ct_table_luascript_after_entries(); + test_ct_entry_tag_attributes(); test_ct_json_pointer_roundtrip(); test_il2cpp_pe_header_bounds(); test_dwarf_symbols();