darknesswind / NutCracker

fork from DamianXVI's squirrel decompiler
20 stars 9 forks source link

Accessing non valid stack position? #18

Open trias702 opened 5 years ago

trias702 commented 5 years ago

Sorry to trouble you, but every time I try to decompile this particular cnut, I keep getting the "accessing non valid stack position" error. I have tried to debug the code, and it looks like the BinaryReader is mangling some of the int32 reads, but I'm not advanced enough with C++ to be able to figure out why.

If you're still working at all on this repo, could you please take a look? Would greatly appreciate it.

global.zip

darknesswind commented 5 years ago

The string and bytecode was encrypted, you need to find how the host application decrypt them.

AdamMil commented 5 years ago

I guess you're trying to look at Battle Brothers cnut files. :-) After some significant effort I figured out the encryption algorithm and wrote a program to decrypt those files. An excerpt from the decompiled global.cnut file:

...
gt.Const.EntityType <- {
    Player = -1,
    Necromancer = 0,
    Zombie = 1,
    ZombieYeoman = 2,
    ZombieKnight = 3,
    ZombieBoss = 4,
    SkeletonLight = 5,
...

The encryption algorithm is very peculiar as it has a close interplay with the Squirrel file parser. Basically, all runs of 8+ bytes that the file parser tries to read are encrypted, and smaller runs are not. This means that the format of a Battle Brothers .cnut file can't really be described as it's inseparable from the internal implementation details of the Squirrel language engine.

I also worked out how to get the game to load a modified script and how to package a collection of changes into a single mod that can be dropped into the game directory. "Only" took about 10 hours all in all, but it'd probably take several days to assemble everything into a nice and usable package...

trias702 commented 5 years ago

You are correct, that was indeed what I was trying to decrypt :-)

I spent an entire weekend trying to figure out how the game processes sales using the SwapItem JavaScript function call to the Backend, which is inside the cnut files. Unfortunately, you cannot do it from the js side alone, everything to do with sale prices is processed in cnut so I gave up. The next step would've been OlyDbg to see if I could reverse engineer the decrypt via process calls, but I never got around to it. Was hoping to make a mod which more equalises the unfair disparity between buy and sell prices for things.

That's amazing and fantastic news about everything you have found! Very well done! I would of course be very keen to see and try out your method, but given the magnitude of your discovery can respect if you want to keep it for yourself. If you do choose to share it with the community it would be a wonderful boon and open the door to proper modding for the game.

On Sun, 20 Jan 2019, 00:29 AdamMil <notifications@github.com wrote:

I guess you're trying to look at Battle Brothers cnut files. :-) After some significant effort I figured out the encryption algorithm and wrote a program to decrypt those files. An excerpt from the decompiled global.cnut file:

... gt.Const.EntityType <- { Player = -1, Necromancer = 0, Zombie = 1, ZombieYeoman = 2, ZombieKnight = 3, ZombieBoss = 4, SkeletonLight = 5, ...

The encryption algorithm is very peculiar as it has a close interplay with the Squirrel file parser. Basically, all runs of 8+ bytes that the file parser tries to read are encrypted, and smaller runs are not. This means that the format of a Battle Brothers .cnut file can't really be described as it's inseparable from the internal implementation details of the Squirrel language engine.

I also worked out how to get the game to load a modified script and how to package a collection of changes into a single mod that can be dropped into the game directory. "Only" took about 10 hours all in all, but it'd probably take several days to assemble everything into a nice and usable package...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/darknesswind/NutCracker/issues/18#issuecomment-455843942, or mute the thread https://github.com/notifications/unsubscribe-auth/AYqzNHG6fnGVyMRb93DzGzskCpqoEuy0ks5vFBrEgaJpZM4ZF-a7 .

AdamMil commented 5 years ago

Almost everything about the game is in script. Very little is hardcoded within the game's executable. So it should be possible to mod almost anything. I decided to skip the "nice and usable package" - I'm getting too old, I guess - and just write up what I know.

I do hope the community makes something cool, and the developers don't try to stop it. They clearly went out of their way to make it hard to see the scripts, perhaps because that's nearly all the game's source code, but still.

Anyway, I think this thread can be closed since we confirmed that it really was an encrypted file and not a bug in NutCracker. :-)