Page 1 of 1

Magic Duels: how to handle card LOL files

PostPosted: 03 Feb 2017, 22:26
by thefiremind
Image -- How to handle card LOL files -- Image

Introduction:
As you may be already aware, starting from the Aether Revolt expansion, Magic Duels' cards are structured as compiled Lua modules (with LOL extension). The structure has been observed and the card is described as a Lua table: no big difference from the change in the card pools... However, as of now, it seems that Magic Duels only accepts compiled cards (unlike the card pools which can be kept plain). Also, it seems impossible to override official cards just by making a new copy with the same name.

How to compile and decompile - automated procedure
Through batch files and Lua scripts I managed to make the procedure really fast and easy!

What you need:
  • A 32-bit version of Lua 5.1.x. You can download it immediately from this SourceForge link, then extract it in a folder of your choice.
  • This ZIP file:
    tfmLua.zip
    (7.8 KiB) Downloaded 439 times
    which needs to be extracted in the same folder where you extracted Lua.
How to use the batch files:
  • Drag and drop a plain card file (with the new structure) onto Compile.bat, and you'll obtain the compiled version (with LOL extension) in the same folder.
  • Drag and drop a compiled LOL file onto Decompile.bat, and you'll obtain the plain version (with LUA extension) in the same folder.
  • If you need to compile/decompile entire folders, you can drop a folder onto BatchCompile.bat/BatchDecompile.bat. All the files in the folder will be processed by the compile/decompile script and the output will be placed in a subfolder called "compiled"/"decompiled" inside the Lua folder.

How to compile - manual procedure | Open
How to compile - manual procedure
I'm keeping this for reference, but I suggest you to use the automated procedure I described above.

What you need:
  • A 32-bit version of Lua 5.1.x. Why 32-bit? Because it matters for the compiled code (64-bit would produce an incompatible result). You can download it immediately from this SourceForge link, then extract it in a folder of your choice.
  • A hex editor of your choice (HxD, xvi32, etc.).
Steps:
  1. Make sure you have a card that follows the new structure. You can start from the ones I uploaded here as templates, but remember that you can't override existing cards (for now), so don't test the procedure without changing the file name (both the actual one and the one written inside) or it won't work.
  2. Open the card file with your hex editor. The Lua compiler doesn't like the UTF-8 header so you need to delete it, if present: delete any byte from the beginning of the file that you find before the word "module". Save and close.
  3. Drag and drop the card file onto luac5.1.exe (you can find it among the Lua binaries you downloaded and extracted before). It will produce a file called "luac.out".
  4. Open "luac.out" with your hex editor. Change the 6th byte from 00 to 5F (that's the header byte that differs between standard Lua and DotP). Save and close.
  5. Rename "luac.out" in "<filename>.lol", where <filename> is the correct card file name, then move it to your Cards folder.
  6. Reference your new card wherever you need to (in a card pool, in a campaign/AI deck, etc.).
  7. Pack the ZED and try it out!

Unfortunately, if you realize you made a mistake in the card, you'll have to do the whole procedure all over again. There's only a scenario where you can take a shortcut: if the correction just involves substituting some bytes (for example you wrote a variable name uppercase but it should have been lowercase), you can use the hex editor to do that, since the strings aren't scrambled inside the compiled file.

Note: it appears that the compiled files we get through Lua contain the full path of the original file. If you are really picky and you want to save some bytes, put the original files in the Lua folder and compile them from there. This way the compiled file will contain the original file name, but no path.

Re: Magic Duels: how to produce a working custom card LOL fi

PostPosted: 04 Feb 2017, 08:47
by Nick26
Wow, I don't know how you get all this but that's impressive and it works! I recode some (very) simple cards with no issue. Unfortunately, as you say, this is not user friendly at all, I have been trying to recode Emrakul, the promised end for like an hour and then I had to give up :cry:

Re: Magic Duels: how to handle card LOL files

PostPosted: 04 Feb 2017, 10:33
by thefiremind
Nick26 wrote:I have been trying to recode Emrakul, the promised end for like an hour and then I had to give up :cry:
That might be a problem with the card structure, more than with this procedure. Anyway, I think I found a way to make it more automatized: you'll still need Lua (since I can't find a compatible Lua 5.1 library that I can embed in a custom application), but hopefully the hex editing will be automatic. I'll report back once I tested it.

EDIT: I updated the first post with the "automated procedure". I'm sure you'll find it much more comfortable. :)