Skip to content

Instantly share code, notes, and snippets.

@NeKzor
Last active February 18, 2023 23:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeKzor/bf5e631e751780efd83bc214aadf7b28 to your computer and use it in GitHub Desktop.
Save NeKzor/bf5e631e751780efd83bc214aadf7b28 to your computer and use it in GitHub Desktop.
Read Lua 5.1 strings with Auto Splitting Language feat. Exodus from the Earth. Credits: @Skyrimfus
// Credits:
// Skyrimfus (original lua CE script, hash function)
// NeKz (conversion)
state("efte")
{
}
startup
{
vars.levelNameOld = "";
vars.levelNameCurrent = "";
}
init
{
vars.GetStringHash = (Func<string, uint>)((str) =>
{
var h = (uint)str.Length;
var l = (uint)str.Length;
var step = (int)(l >> 5) + 1;
for (var l1 = (int)l; l1 >= step; l1 -= step)
h = h ^ (((h << 5) + (h >> 2)) + (uint)(int)str[l1 - 1]);
return h;
});
vars.GetObject = (Func<IntPtr, string, IntPtr>)((address, key) =>
{
var result = IntPtr.Zero;
if (address == IntPtr.Zero) {
return result;
}
var hash = vars.GetStringHash(key);
var offset = (1 << memory.ReadValue<byte>(address + 7)) - 1;
var ptr = (IntPtr)memory.ReadValue<int>(address + 0x10) + (0x20 * (offset & (int)hash));
while (ptr != IntPtr.Zero)
{
var ty = memory.ReadValue<int>(ptr + 0x18);
if (ty == 4)
{
var keyPtr = (IntPtr)memory.ReadValue<int>(ptr + 0x10);
var keyValue = memory.ReadString(keyPtr + 0x10, key.Length);
if (keyValue == key)
{
result = (IntPtr)memory.ReadValue<int>(ptr);
//print("[ASL] " + key + " 0x" + result.ToString("X"));
break;
}
}
ptr = (IntPtr)memory.ReadValue<int>(ptr + 0x1c);
}
return result;
});
vars.GetStringValue = (Func<IntPtr, string>)((address) =>
{
return address != IntPtr.Zero ? memory.ReadString(address + 0x10, 64) : "";
});
vars.Module = modules.First(module => module.ModuleName == "efte.exe");
vars.glDp = new DeepPointer(vars.Module.ModuleName, (int)0x90A008, 0x48);
vars.UpdateLevelName = (Action)(() =>
{
var gl = (IntPtr)vars.glDp.Deref<int>(game);
var hud = vars.GetObject(gl, "HUD");
var hud_singleton = vars.GetObject(hud, "hud_singleton");
var state = vars.GetObject(hud_singleton, "state");
var s_levelName = vars.GetObject(state, "s_levelName");
var levelName = vars.GetStringValue(s_levelName);
if (levelName.Length != 0 && levelName != "background" && levelName != "__undefined__")
{
vars.levelNameOld = vars.levelNameCurrent;
vars.levelNameCurrent = levelName;
if (vars.levelNameOld != vars.levelNameCurrent)
{
print("[ASL] levelName changed: " + vars.levelNameOld + " -> " + vars.levelNameCurrent);
}
}
});
}
update
{
vars.UpdateLevelName();
}
split
{
return vars.levelNameCurrent != vars.levelNameOld;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment