From 462a76f5ce256f332a91f41a20c70f6c770e2b77 Mon Sep 17 00:00:00 2001 From: makeittakeit2 <220555754+jakeit3232@users.noreply.github.com> Date: Thu, 11 Jun 2026 21:01:20 -0400 Subject: [PATCH 1/2] fixed pr 24 and some other light changes --- demo/Recompiler/Compiler.cs | 23 ++++++++------ demo/Recompiler/Program.cs | 7 ++--- src/Decompiler/LuaScriptFunction.cs | 47 +++++++++++++++++++++++++++-- src/Decompiler/LuaScriptLine.cs | 14 +++++++-- 4 files changed, 72 insertions(+), 19 deletions(-) diff --git a/demo/Recompiler/Compiler.cs b/demo/Recompiler/Compiler.cs index 58f99ff..2b7de4b 100644 --- a/demo/Recompiler/Compiler.cs +++ b/demo/Recompiler/Compiler.cs @@ -10,9 +10,9 @@ namespace Recompiler { public class Compiler { - public static string LuacPath = "lua/luac5.1"; // NOTE: make sure the LuaC 5.1 binary is installed right there - public static string CompiledInPath = "lua/in/"; // stores all the Lua files that got uploaded - public static string CompiledOutPath = "lua/out/"; // stores all the Lua files that got compiled + public static string LuacPath = Path.Combine(AppContext.BaseDirectory, "lua", "luac5.1.exe"); // had a problem with this?? + public static string InPath = Path.Combine(AppContext.BaseDirectory, "lua", "in"); + public static string OutPath = Path.Combine(AppContext.BaseDirectory, "lua", "out"); public List FullFileName; public List FileName; @@ -37,7 +37,8 @@ public void AddFile(byte[] Buffer, string fileName) this.FileName.Add(fileName); fileName = this.Owner + "__" + fileName + "__" + DateTime.UtcNow.ToString("yyyy_MM_dd_HH_mm_ss"); this.FullFileName.Add(fileName); - File.WriteAllBytes($"{Directory.GetCurrentDirectory()}/{CompiledInPath}{fileName}", Buffer); + Directory.CreateDirectory(InPath); + File.WriteAllBytes(Path.Combine(InPath, fileName), Buffer); // TODO: hash = hash(Owner:Date:FileName) } @@ -46,12 +47,15 @@ public bool Compile() if (!File.Exists(LuacPath)) return false; + Directory.CreateDirectory(OutPath); + Process p = new Process(); - //p.StartInfo.UseShellExecute = false; + p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = LuacPath; - p.StartInfo.Arguments = $"-o {CompiledOutPath}{this.Owner} "; + p.StartInfo.ArgumentList.Add("-o"); + p.StartInfo.ArgumentList.Add(Path.Combine(OutPath, this.Owner)); for (int i = 0; i < this.FullFileName.Count; i++) - p.StartInfo.Arguments += $"{CompiledInPath}{this.FullFileName[i]} "; + p.StartInfo.ArgumentList.Add(Path.Combine(InPath, this.FullFileName[i])); p.Start(); p.WaitForExit(); // timeout this? @@ -63,8 +67,9 @@ public bool Compile() public byte[] GetCompiled() { - if (File.Exists(CompiledOutPath + this.Owner)) - return File.ReadAllBytes(CompiledOutPath + this.Owner); + string compiledPath = Path.Combine(OutPath, this.Owner); + if (File.Exists(compiledPath)) + return File.ReadAllBytes(compiledPath); return null; } diff --git a/demo/Recompiler/Program.cs b/demo/Recompiler/Program.cs index 7edd30c..4655235 100644 --- a/demo/Recompiler/Program.cs +++ b/demo/Recompiler/Program.cs @@ -10,11 +10,8 @@ class Program static void Main(string[] args) { // NOTE: Please download and install Lua 5.1 and link the LuaC binary to ./lua/luac - Console.WriteLine("Please enter the path of your project files: "); - //string path = Console.ReadLine(); //string path = @"H:\games\World of Warcraft\_retail_\Interface\AddOns\dubai"; - string path = @"L:\Projects\LuaBytcodeInterpreter\lua_installer\files\testAddon"; - + string path = Compiler.InPath; // change to whatever path for the files u wanna decompile if (!Directory.Exists(path)) { Console.WriteLine("Directory doesn't exist"); @@ -46,7 +43,7 @@ void discoverDirectory(string dir, ref List paths, ref List buff comp.AddFiles(Buffers, Paths); if(!comp.Compile()) { - Console.WriteLine("Compiler error!!"); + Console.WriteLine("Compiler error!!" ); Console.ReadKey(); return; } diff --git a/src/Decompiler/LuaScriptFunction.cs b/src/Decompiler/LuaScriptFunction.cs index ee29ccc..6fad816 100644 --- a/src/Decompiler/LuaScriptFunction.cs +++ b/src/Decompiler/LuaScriptFunction.cs @@ -21,6 +21,7 @@ public class LuaScriptFunction public List Blocks; private List UsedLocals; + private Dictionary EndCount = new Dictionary(); // gonna actually count how many blocks close at each jmp target public string Name { @@ -87,6 +88,34 @@ private void InitArgs(int count) } } + private void AddEnd(int address) + { + if (!EndCount.ContainsKey(address)) + EndCount[address] = 0; + + EndCount[address]++; + } + + public void BuildEnd() + { + EndCount.Clear(); + + foreach (var block in this.Blocks) + { + var condition = block.GetConditionLine(); + + if (block.JumpsTo == -1 || block.JumpsNext == -1 || condition == null || !condition.IsCondition()) + continue; + + int target = block.JumpsTo; + var thenBlock = this.Blocks.FirstOrDefault(x => x.StartAddress == block.JumpsNext); + if (thenBlock != null && thenBlock.JumpsTo != -1 && thenBlock.JumpsNext == -1) + target = thenBlock.JumpsTo; + + AddEnd(target); + } + } + public override string ToString() { string args = "("; @@ -152,6 +181,7 @@ private LuaFunction FindParentFunction(LuaFunction function, LuaFunction search return null; } + // NOTE: Please do NOT touch this unless you 110% know what you are doing!!! private void GenerateBlocks(bool overwriteBlocks = false) @@ -264,6 +294,8 @@ private void GenerateBlocks(bool overwriteBlocks = false) } } + BuildEnd(); + for (int i = 0; i < this.Blocks.Count; i++) { // IF: JMP != -1 && ELSE != -1 (&& GetConditionLine != NULL; ELSE; FORLOOP END (dont care)) @@ -388,11 +420,13 @@ private void GenerateBlocks(bool overwriteBlocks = false) else if (this.Blocks[i].JumpsTo == -1 && this.Blocks[i].JumpsNext != -1 && this.Blocks[i].GetBranchLine() != null && this.Blocks[i].GetBranchLine().Instr.OpCode != LuaOpcode.FORPREP) // also make sure if condifition is set (no forloop) { - + // IF block endings are emitted by GenerateCleanCode using EndCount. +#if false #if DEBUG this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Postfix += "\r\nend -- ENDIF"; #else this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Postfix += "\r\nend"; +#endif #endif } @@ -633,16 +667,23 @@ private string GenerateCleanCode() string result = ""; for (int b = 0; b < this.Blocks.Count; b++) { + if (EndCount.TryGetValue(this.Blocks[b].StartAddress, out int endCount)) + { + for (int e = 0; e < endCount; e++) + result += "end\r\n"; + } + for (int i = 0; i < this.Blocks[b].Lines.Count; i++) { if(this.Blocks[b].Lines[i].Instr.OpCode == LuaOpcode.CLOSURE) if (this.Blocks[b].Lines[i].GetFunctionRef() != null) - result += this.Blocks[b].Lines[i].GetFunctionRef().ScriptFunction.BeautifieCode(); // inline func in parent + result += this.Blocks[b].Lines[i].GetFunctionRef().ScriptFunction.BeautifieCode(); + result += this.Blocks[b].Lines[i].Text; } if (b == this.Blocks.Count - 1) - result += "\r\n"; // keep it clean? + result += "\r\n"; } return result; } diff --git a/src/Decompiler/LuaScriptLine.cs b/src/Decompiler/LuaScriptLine.cs index 05404e9..fb43565 100644 --- a/src/Decompiler/LuaScriptLine.cs +++ b/src/Decompiler/LuaScriptLine.cs @@ -60,6 +60,15 @@ public LuaScriptLine(LuaInstruction instr, LuaDecoder decoder, LuaFunction func) SetMain(); } + private string GetClosureName() + { + LuaFunction func = GetFunctionRef(); + if (func == null && Instr.Bx >= 0 && Instr.Bx < Func.Functions.Count) + func = Func.Functions[Instr.Bx]; + + return func?.ScriptFunction?.Name ?? func?.Name ?? $"{Func.Name}_{Instr.Bx}"; + } + public void SetMain(LuaInstruction Instr = null) { if (Instr == null) @@ -391,10 +400,11 @@ public void SetMain(LuaInstruction Instr = null) // crates closutre for function prototype Bx this.Op1 = $"{WriteIndex(Instr.A)}"; this.Op2 = " = "; - if(this.Func.Functions[Instr.Bx].ScriptFunction != null) + this.Op3 = GetClosureName(); // better than "IDK_SHIT_WENT_MISSING_BRO" lol + /*if(this.Func.Functions[Instr.Bx].ScriptFunction != null) this.Op3 = this.Func.Functions[Instr.Bx].ScriptFunction.Name; else - this.Op3 = $"IDK_SHIT_WENT_MISSING_BRO"; // TODO fix + this.Op3 = $"IDK_SHIT_WENT_MISSING_BRO"; // TODO fix*/ break; case LuaOpcode.VARARG: this.Func.ScriptFunction.HasVarargs = true; From b96b1580c4d649ad2b474e48bdd0e88953de852a Mon Sep 17 00:00:00 2001 From: jake3232 <220555754+jakeit3232@users.noreply.github.com> Date: Fri, 12 Jun 2026 10:14:51 -0400 Subject: [PATCH 2/2] Update Compiler.cs --- demo/Recompiler/Compiler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/Recompiler/Compiler.cs b/demo/Recompiler/Compiler.cs index 2b7de4b..1520472 100644 --- a/demo/Recompiler/Compiler.cs +++ b/demo/Recompiler/Compiler.cs @@ -10,7 +10,7 @@ namespace Recompiler { public class Compiler { - public static string LuacPath = Path.Combine(AppContext.BaseDirectory, "lua", "luac5.1.exe"); // had a problem with this?? + public static string LuacPath = Path.Combine(AppContext.BaseDirectory, "lua", "luac5.1"); public static string InPath = Path.Combine(AppContext.BaseDirectory, "lua", "in"); public static string OutPath = Path.Combine(AppContext.BaseDirectory, "lua", "out");