LVL Lua Code Helper

Releasing the source files for your mod or map? Post em' here. (Applies to both SWBF1 & SWBF2)

Moderator: Moderators

Private Third Class
User avatar
Posts: 46
Joined: Sun Jan 18, 2015 5:23 pm
Projects :: SWBF2 XBOX Mod Environment
Games I'm Playing :: SW Battlefront 2
xbox live or psn: No gamertag set

LVL Lua Code Helper

Postby cbadal » Sat Jan 13, 2018 5:48 pm

Looking at the swbf-unmunge thread by SleepKiller, Marth8880 requested a Lua decompiler.
And of course, I want one too.

I tried looking for a Lua decompiler last month and found 2 that didn't work on lua code compiled without debugging info (like is the case with the SWBF2 lua code).
I had no idea why one didn't exist. Lua seems like such a simple language.
I started looking into a paper called "A no Frills introduction to Lua 51 VM Instruction set" today to try to see if it would bee all that difficult to write one.

I got about 2 pages in and the author mentions that luac has a 'listing mode'. I tried out this listing mode and was quite surprised at just how easy the output is to understand.
Here is an example:
Code: Select all
-- cor1c_con
-- luac -l listing

main <(none):0> (20 instructions, 80 bytes at 02894830)
0 params, 2 stacks, 0 upvalues, 0 locals, 12 constants, 2 functions
   1   [-]   GETGLOBAL   0 1   ; ScriptCB_DoFile
   2   [-]   LOADK       1 2   ; "ObjectiveConquest"
   3   [-]   CALL        0 2 2
   4   [-]   SETGLOBAL   0 0   ; Conquest
   5   [-]   GETGLOBAL   0 1   ; ScriptCB_DoFile
   6   [-]   LOADK       1 3   ; "setup_teams"
   7   [-]   CALL        0 2 1
   8   [-]   LOADK       0 5   ; 1
   9   [-]   SETGLOBAL   0 4   ; ATT
   10   [-]   LOADK       0 7   ; 2
   11   [-]   SETGLOBAL   0 6   ; DEF
   12   [-]   GETGLOBAL   0 4   ; ATT
   13   [-]   SETGLOBAL   0 8   ; CIS
   14   [-]   GETGLOBAL   0 6   ; DEF
   15   [-]   SETGLOBAL   0 9   ; REP
   16   [-]   CLOSURE     0 0   ; 02894AA0
   17   [-]   SETGLOBAL   0 10   ; ScriptPostLoad
   18   [-]   CLOSURE     0 1   ; 02895AC0
   19   [-]   SETGLOBAL   0 11   ; ScriptInit
   20   [-]   RETURN      0 1 0

You do have to stare at it for a little bit, but it's pretty understandable. Certainly useful for figuring out what functions are being called and what their arguments are.
Using that output, it seems like a lua decompiler would be 'not so hard'. I incorporated this view to a tool I've been using to figure out the .lvl files.

If you'd like to use that utility yourself to understand lua code inside .lvl files, you can try it out for yourself.
Download the file.

IT assumes that you have the mod tools located at:
Last edited by cbadal on Fri Jan 26, 2018 6:23 pm, edited 2 times in total.

Jedi Admin
User avatar
Posts: 10931
Joined: Wed Sep 07, 2005 11:42 pm
Location: Indiana
Projects :: No Mod project currently.
Games I'm Playing :: Destiny
xbox live or psn: No gamertag set

Re: LVL Lua Code Helper

Postby Teancum » Sat Jan 13, 2018 10:34 pm

Looks very cool, however when doing anything but loading the lvl and populating the search I get exception errors. Here's an example:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ComponentModel.Win32Exception: The system cannot find the file specified
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at SWBF2_Tool.ScriptSearchForm.RunCommand(String programName, String args, Boolean includeStdErr)
at SWBF2_Tool.ScriptSearchForm.LuacCodeSize(String luaSourceFile)
at SWBF2_Tool.ScriptSearchForm.ShowCodeSize()
at SWBF2_Tool.ScriptSearchForm.mLuacCodeSizeButton_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
Assembly Version:
Win32 Version: 2.0.50727.8825 (WinRelRS3.050727-8800)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
Assembly Version:
Win32 Version:
CodeBase: file:///D:/Games/SWBF2_Modtools/data_xbx/!!SRC/SWBF2_Tool.exe
Assembly Version:
Win32 Version: 2.0.50727.8825 (WinRelRS3.050727-8800)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/
Assembly Version:
Win32 Version: 2.0.50727.8825 (WinRelRS3.050727-8800)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/
Assembly Version:
Win32 Version: 2.0.50727.8825 (WinRelRS3.050727-8800)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/
Assembly Version:
Win32 Version: 2.0.50727.8825 (WinRelRS3.050727-8800)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/
Assembly Version:
Win32 Version: 2.0.50727.8827 (WinRelRS3.050727-8800)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the section.
The application must also be compiled with debugging

For example:

< jitDebugging="true" />

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Private Third Class
User avatar
Posts: 46
Joined: Sun Jan 18, 2015 5:23 pm
Projects :: SWBF2 XBOX Mod Environment
Games I'm Playing :: SW Battlefront 2
xbox live or psn: No gamertag set

Re: LVL Lua Code Helper

Postby cbadal » Sun Jan 14, 2018 3:32 pm

Good catch.
I guess I never tried listing the assets with no file selected.
Release is updated.

I also disabled some stuff that probably no else (other than me) would use.
It also now populates when you 'Drag N Drop' a file onto the filename text box.

Return to Released Assets

Who is online

Users browsing this forum: No registered users and 1 guest