Skip to content

Comments

Add Sim Lua checksum#141

Open
4z0t wants to merge 5 commits intomasterfrom
feature/sim-lua-checksum
Open

Add Sim Lua checksum#141
4z0t wants to merge 5 commits intomasterfrom
feature/sim-lua-checksum

Conversation

@4z0t
Copy link
Member

@4z0t 4z0t commented Feb 20, 2026

Include loaded lua files into sim checksum. It uses lua's bytecode for checksum via lua_dump function.
Downsides of this change include:

  • any imports that happen under conditions involving GetFocusArmy, IsReplay and etc will desync across players.
  • problems with debugging replays/games on develop branch since it will cause desync (not desync in terms of game state, only hash).

Summary by CodeRabbit

  • New Features

    • Added MD5-based checksum capability accessible from Lua, letting scripts produce checksums of runtime data for integrity or verification workflows.
  • Chores

    • Simplified template APIs by adding default type parameters, reducing verbosity in common calls and easing typical usage without explicit template arguments.

@4z0t 4z0t marked this pull request as draft February 20, 2026 16:12
@coderabbitai
Copy link

coderabbitai bot commented Feb 20, 2026

📝 Walkthrough

Walkthrough

Adds a Lua checksum feature: a hook invoking a new lua_dochecksum, MD5-based dumping of Lua state via lua_dump and Md5_Writer, new Lua/MD5 symbol declarations, and default template parameters added to Offset()/GetField() in include/global.h.

Changes

Cohort / File(s) Summary
Hook Registration
hooks/LuaSimCheckSum.hook
Adds two address-based hook entries: call to lua_dochecksum at 0x004CEE33 and xor eax, eax at 0x00914BD7.
Template Defaults
include/global.h
Adds default template parameter T = void* to Offset() and GetField(), changing default inferred return/reference types when template args are omitted.
LuaSimCheckSum Headers
section/LuaSimCheckSum/LuaSimCheckSum.h
Adds lua_Chunkwriter typedef and external bindings for lua_pcallF, lua_dump, gpg::MD5Context::Update, and gpg::MD5Digest::ToString with hardcoded asm addresses.
LuaSimCheckSum Implementation
section/LuaSimCheckSum/LuaSimCheckSum.cxx
Implements Md5_Writer (updates MD5 context) and lua_dochecksum (fetches global userdata, retrieves MD5Context via Offset(sim, 0x50), calls lua_dump with Md5_Writer, then delegates/returns to Lua).

Sequence Diagram(s)

sequenceDiagram
    participant Hook as Hook (0x004CEE33)
    participant LuaFunc as lua_dochecksum
    participant LuaState as Lua State
    participant LuaDump as lua_dump
    participant MD5 as MD5Context

    Hook->>LuaFunc: invoke
    LuaFunc->>LuaState: get global userdata (sim)
    alt userdata present
        LuaFunc->>MD5: obtain MD5Context (Offset(sim, 0x50))
        LuaFunc->>LuaDump: lua_dump(L, Md5_Writer, MD5)
        loop per chunk
            LuaDump->>LuaFunc: chunk (data, size)
            LuaFunc->>MD5: Md5_Writer -> Update(data, size)
        end
    else userdata null
        LuaFunc->>LuaState: delegate to Lua (no args)
    end
    LuaFunc-->>Hook: return (xor eax, eax at 0x00914BD7)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I nibble bytes and spin the thread,

MD5 crumbs where Lua has tread,
A hop, a dump, a tiny trace,
Templates nudged to shift their place,
Carrot checksum, soft code grace.

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The PR description explains the feature but lacks compliance with template requirements including the checklist and README updates. Complete the checklist items, add changes description to README.md, specify test instructions, and confirm reading of guides.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding Lua checksum functionality to the simulation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/sim-lua-checksum

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@4z0t
Copy link
Member Author

4z0t commented Feb 20, 2026

Right now it requires some changes since it also includes source file in dump function which may differ accross players.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
section/LuaSimCheckSum/LuaSimCheckSum.h (2)

10-10: Missing newline at end of file.

Some compilers emit a warning for files not ending with a newline. Add a trailing newline after line 10.

Fix
 void __thiscall gpg__MD5Context__Update(/* gpg::MD5Context */ void *_this, const void *block, size_t size) asm("0x008E5870");
+
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@section/LuaSimCheckSum/LuaSimCheckSum.h` at line 10, The file ends without a
trailing newline; open the header containing the declaration for
gpg__MD5Context__Update (the line with "void __thiscall
gpg__MD5Context__Update(... ) asm(\"0x008E5870\");") and add a single newline
character at the end of the file so the file ends with a newline.

7-7: lua_pcallF is declared but unused.

This function is declared here but not referenced in LuaSimCheckSum.cxx. If it's intended for future use or debugging, consider adding a brief comment noting that; otherwise it could be removed to keep the header minimal.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@section/LuaSimCheckSum/LuaSimCheckSum.h` at line 7, The header declares an
unused function symbol lua_pcallF; remove this declaration from LuaSimCheckSum.h
if it's not used anywhere, or if you intend to keep it for future use/debugging,
add a one-line comment above the declaration clarifying its purpose (e.g., "kept
for future debugging/extern reference") and optionally add an explicit // FIXME
or TODO tag; also scan LuaSimCheckSum.cxx for planned references to lua_pcallF
and update those callers if needed.
section/LuaSimCheckSum/LuaSimCheckSum.cxx (1)

9-24: Unchecked return value from lua_dump.

lua_dump can return non-zero if the writer callback errors. Currently the return is ignored, so a partial/failed dump would silently produce an incomplete checksum. Consider at least logging a warning on failure to aid debugging desync issues.

Suggested improvement
-    lua_dump(L, Md5_Writer, md5);
+    if (lua_dump(L, Md5_Writer, md5) != 0)
+    {
+        WarningF("lua_dochecksum: lua_dump failed");
+    }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@section/LuaSimCheckSum/LuaSimCheckSum.cxx` around lines 9 - 24, In
lua_dochecksum, lua_dump’s return value is ignored; capture its int result when
calling lua_dump(L, Md5_Writer, md5) and if non-zero log a warning including the
return code and context (e.g., function name and sim/md5 identifiers) to aid
debugging, then proceed safely (still call lua_call to preserve behavior) or
bail if appropriate; reference the lua_dochecksum function, the lua_dump call,
the Md5_Writer callback and the md5 local to locate where to add the check and
LogF (or existing logger) call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@section/LuaSimCheckSum/LuaSimCheckSum.cxx`:
- Around line 9-24: In lua_dochecksum, lua_dump’s return value is ignored;
capture its int result when calling lua_dump(L, Md5_Writer, md5) and if non-zero
log a warning including the return code and context (e.g., function name and
sim/md5 identifiers) to aid debugging, then proceed safely (still call lua_call
to preserve behavior) or bail if appropriate; reference the lua_dochecksum
function, the lua_dump call, the Md5_Writer callback and the md5 local to locate
where to add the check and LogF (or existing logger) call.

In `@section/LuaSimCheckSum/LuaSimCheckSum.h`:
- Line 10: The file ends without a trailing newline; open the header containing
the declaration for gpg__MD5Context__Update (the line with "void __thiscall
gpg__MD5Context__Update(... ) asm(\"0x008E5870\");") and add a single newline
character at the end of the file so the file ends with a newline.
- Line 7: The header declares an unused function symbol lua_pcallF; remove this
declaration from LuaSimCheckSum.h if it's not used anywhere, or if you intend to
keep it for future use/debugging, add a one-line comment above the declaration
clarifying its purpose (e.g., "kept for future debugging/extern reference") and
optionally add an explicit // FIXME or TODO tag; also scan LuaSimCheckSum.cxx
for planned references to lua_pcallF and update those callers if needed.

@4z0t 4z0t marked this pull request as ready for review February 20, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant