ljd icon indicating copy to clipboard operation
ljd copied to clipboard

Decompiling LuaJit Scripts from Farming Simulator 2015

Open BlueTiger-TS7 opened this issue 10 years ago • 21 comments

Hello.

If tried to decompile bytecode files from FS15. But just getting any KeyError. Do you have any suggestion?

Hopefully Blue

Text from Commandpromptscreen:

Traceback (most recent call last): File "main.py", line 123, in retval = main() File "main.py", line 85, in main ast = ljd.ast.builder.build(prototype) File "D:\Users\Bluetiger\Downloads\dataS\scripts\ljd-master\ljd\ast\builder.py ", line 28, in build return _build_function_definition(prototype) File "D:\Users\Bluetiger\Downloads\dataS\scripts\ljd-master\ljd\ast\builder.py ", line 49, in _build_function_definition node.statements.contents = _build_function_blocks(state, instructions) File "D:\Users\Bluetiger\Downloads\dataS\scripts\ljd-master\ljd\ast\builder.py ", line 71, in _build_function_blocks _establish_warps(state, instructions) File "D:\Users\Bluetiger\Downloads\dataS\scripts\ljd-master\ljd\ast\builder.py ", line 175, in _establish_warps block.warp, shift = _build_warp(state, block.last_address, warp) File "D:\Users\Bluetiger\Downloads\dataS\scripts\ljd-master\ljd\ast\builder.py ", line 198, in _build_warp return _build_numeric_loop_warp(state, last_addr, last) File "D:\Users\Bluetiger\Downloads\dataS\scripts\ljd-master\ljd\ast\builder.py ", line 318, in _build_numeric_loop_warp warp.body = state._warp_in_block(destination) File "D:\Users\Bluetiger\Downloads\dataS\scripts\ljd-master\ljd\ast\builder.py ", line 22, in _warp_in_block block = self.block_starts[addr] KeyError: -31379

BlueTiger-TS7 avatar Jan 27 '15 19:01 BlueTiger-TS7

Odd, I haven't encountered any bugs in the builder itself! Are you decompiling stripped bytecode? It could be a good idea to share the .luac file itself.

jjdredd avatar Jan 28 '15 06:01 jjdredd

IIRC FS15 uses luajit 3.0 which may have different bytecode, though I haven't found any specifications for it...

NightNord avatar Jan 28 '15 06:01 NightNord

I don't think it is stripped. link to one file: http://www.team-sub7.com/download/ls15/main.luc That is a usefull information, that luajit 3.0 is used. I'll try to find some specification. I think hundrets of modders would be very glad for the readable scripts.

спаси́бо

BlueTiger-TS7 avatar Jan 28 '15 10:01 BlueTiger-TS7

okay i found something about giants/fs15. So it is nearly clear why it is so hard to find something.

Low-overhead profiling

GIANTS Software GmbH has sponsored the development of a low-overhead profiling functionality for LuaJIT 2.1 in June 2013. GIANTS Software develops a variety of simulation games for desktop, mobile and consoles. These games make extensive use of Lua for scripting and modding. Switching to LuaJIT was instrumental in reducing the CPU load and sustaining the required frame rates on all platforms.

BlueTiger-TS7 avatar Jan 28 '15 11:01 BlueTiger-TS7

So, some closed-source version of luajit was used, right? Having source or docs it wouldn't be too hard to include new bc in ljd (even I could try). What is much harder is using new opcodes in ast submodule. BTW I found no information about luajit 3.0 release.

jjdredd avatar Jan 28 '15 13:01 jjdredd

main.luc file header provided by @Bluetiger indicates that it's luajit 2.1 I successfully fed main.luc to luajit 2.1 (git 7f01300) and disassembled the file, so this bytecode is v2.1. Providing a file really helps :)

jjdredd avatar Jan 28 '15 14:01 jjdredd

looks like we have a few new opcodes:

diff --git a/src/lj_bc.h b/src/lj_bc.h
index 7436fab..64c1bcd 100644
--- a/src/lj_bc.h
+++ b/src/lj_bc.h
@@ -89,6 +89,8 @@
   _(ISFC,      dst,    ___,    var,    ___) \
   _(IST,       ___,    ___,    var,    ___) \
   _(ISF,       ___,    ___,    var,    ___) \
+  _(ISTYPE,    var,    ___,    lit,    ___) \
+  _(ISNUM,     var,    ___,    lit,    ___) \
   \
   /* Unary ops. */ \
   _(MOV,       dst,    ___,    var,    ___) \
@@ -143,10 +145,12 @@
   _(TGETV,     dst,    var,    var,    index) \
   _(TGETS,     dst,    var,    str,    index) \
   _(TGETB,     dst,    var,    lit,    index) \
+  _(TGETR,     dst,    var,    var,    index) \
   _(TSETV,     var,    var,    var,    newindex) \
   _(TSETS,     var,    var,    str,    newindex) \
   _(TSETB,     var,    var,    lit,    newindex) \
   _(TSETM,     base,   ___,    num,    newindex) \
+  _(TSETR,     var,    var,    var,    newindex) \
   \
   /* Calls and vararg handling. T = tail call. */ \
   _(CALLM,     base,   lit,    lit,    call) \

jjdredd avatar Jan 28 '15 14:01 jjdredd

So far I could figure out, is that luajit 3.0 is not ready. there is an article in the wiki from luajit project, which they write about the garbage collector from luajit 3.0 that is everthing so far.

back to fs15. is there a possibility get it to work? I 'm not familiar with Python , and certainly not with this kind of programming . And Assembler and Basic I didn't use since nearly 20 years anymore. I 'm more with databases and program extensions, programming in C#, C++, Delphi, Cobol and so on. I can upload the whole bytecode scripts if you wish.

BlueTiger-TS7 avatar Jan 28 '15 16:01 BlueTiger-TS7

Adding new instructions to the ljd parser is not complicated. The hard thing is rechecking header format and adding those instructions for use in ast builder and unwarper. In addition to that the decompiler seems to have severe bugs not related to luajit version >= 2.1. Also all the work with luajit 2.1 is impossible without new opcode descriptions. One bytecode sample should suffice.

jjdredd avatar Jan 28 '15 17:01 jjdredd

If only these opcodes are added then unwarper shouldn't be the problem. Builder is rather straightforward. But things other than that may have changed (like goto/continue added) - that might be a problem, really.

I've got a number of requests via mail recently with something simulator using luajit 3.0 alpha (these guys are crazy). It seems I've mistaken. Sorry for the confusion.

Well, I'll try to look into adding the new opcodes in a few days. Hopefully it will just work after that, but probably it will not.

NightNord avatar Jan 28 '15 21:01 NightNord

How are you planning to add new opcodes without description? One way I see is to compile a bunch of code, seek for those opcodes in bytecode dump and try to guess what do these opcodes do.

Another option is to add these opcodes to the parser alone. This way we can parse it correctly but won't be able to decompile. Anyway if you don't have time I could try to make a patch for this.

jjdredd avatar Jan 29 '15 06:01 jjdredd

By looking into the source. And the source tells me that these instructions are not implemented (in luajit). At least, they are not parsed (in lj_parse.c) - maybe there are used for some other means. So the only problem is that opcode numbers were changed.

But we can't just add them into the parser - it will break all 2.0 scripts. We need to add some special luajit-2.1-only tag and skip such instructions if a bytecode version is 2.9.

NightNord avatar Jan 29 '15 12:01 NightNord

EDIT Sorry, my bad. Everything seems to be working. I've just added a new branch for luajit v2.1. EDIT_2 I propose adding version flag to opcode tuples in code.py. Modify _init() to take version into account and call init() (must make it a global function) from prototype.read() passing the correct version number from header.

jjdredd avatar Feb 02 '15 10:02 jjdredd

Is it like it Sounds? A light on horizon?

BlueTiger-TS7 avatar Feb 02 '15 11:02 BlueTiger-TS7

@Bluetiger , well, I ran it on your main.luc and got something that looked like lua source. You may try it yourself on other files (branch "ljv2.1" on my fork, though it doesn't include tiny yet important imho fixes I've made on the branch "bugfix").

There is much work to be done on the slotworks and unwarper itself!

jjdredd avatar Feb 02 '15 12:02 jjdredd

I've tested it on all my luc's. Excellent work so far. Just 22 files won't decompile from over 500. The source code in the other files is looks like what i've expected. Most of the 22 files seems to have a problem with unwarper, except three files. Could you have a look at it?

http://www.team-sub7.com/download/ls15/CreditsScreen.luc http://www.team-sub7.com/download/ls15/InputBinding.luc http://www.team-sub7.com/download/ls15/Utils.luc

(file with unwarper errors) http://www.team-sub7.com/download/ls15/Vehicle.luc

BlueTiger-TS7 avatar Feb 02 '15 21:02 BlueTiger-TS7

Beware! Although you're getting readable lua source, it could be incorrect!

As for the new files, I think they aren't stripped so my little bugfix doesn't apply here. I guess the header format could have changed too (at least rarely used parts??) though it's unlikely because other files decompile ok. Anyway since there's no documentation for luajit v2.1, we'll need to read source. I'll try to peek into luajit v2.1. Lets hope we'll hear from @NightNord about my patch, new errors and the whole situation.

jjdredd avatar Feb 03 '15 06:02 jjdredd

I know. But for the most things it is okay, to know what is the script doing, how the mehtods are used, which methods are there and which variables have to set and so on.

It would be great to get some of this files to readable lua source, because in them is very usefull and needed code to know (files like Utils.luc, Vehicle.luc...).

But sofar, i look forward with my work in scripting for Simulator. I am very glad for your help.

BlueTiger-TS7 avatar Feb 03 '15 10:02 BlueTiger-TS7

HI,i'm a Italian guy

I have started a project of a remastered of the game "Fs15" u can found my project here: http://realitytotalsimulator.forumcommunity.net/

Now i need help for decrypting the source code and add more feature in the game,building mode ecc

can u help me? I'm trying to make I.A File for the workers in the game

Thx for helps and sorry for my bad english :D

lollogamer95 avatar Nov 15 '16 17:11 lollogamer95

Decrypting or decompiling? Anyway, the decompiler is not working atm and probably abandoned at least temporarily.

jjdredd avatar Nov 15 '16 22:11 jjdredd

Hi! Is there any news about decompiling the luajit luc files from FS15 or 17?

joskuijpers avatar Jan 31 '17 16:01 joskuijpers