rofl0r / agsutils

contains utils for AGS: game extractor, repacker, disassembler and assembler
44 stars 14 forks source link

Maniac Mansion Deluxe v1.4 - extract/repack breaks the game #22

Closed d0k3 closed 2 years ago

d0k3 commented 2 years ago

As the title says. If I simply extract the game with agsex and pack it again with agspack, I can't run it anymore (neither via the EXE, nor via ScummVM). In ScummVM I get an error about sprite format, although I changed nothing in that regard.

I can provide more details if needed. The game is easily available via a simple Google search.

rofl0r commented 2 years ago

that game ships with an annoying windows installer i can't extract with innosetup on linux. can you upload a tarball or zip containing the game contents after installation?

d0k3 commented 2 years ago

Yeah, the installer sucks. Here the installed folder: https://www.mediafire.com/file/h2357gazkqzrllw/Maniac+Mansion+Deluxe+v1.4.7z/file

rofl0r commented 2 years ago

thanks! i unpacked it via agsex MANIAC.EXE F O, then ran make, and the resulting game.ags works fine in the standalone ags engine. agspack -e F test.exe produces an exe that can also be loaded successfully in the ags engine, however i didn't test whether it works in wine. the test.exe also works in my scummvm 2.5.1 build (detection fails, but it loads as "generic ags" sortof game)

d0k3 commented 2 years ago

That's weird. Could this be a Windows issue? It even fails for me if I just use agstract and then agspack. Is there anything I could try?

rofl0r commented 2 years ago

are you using the windows binaries from latest release ? or did you build it yourself using cygwin or something ?

d0k3 commented 2 years ago

Latest release, did not build myself. I could get you more info tomorrow (don't have my PC with me right now).

rofl0r commented 2 years ago

odd. i tested the binaries from the win32 package using wine like this:

wine ../agsutils/agstract.exe Maniac.exe F
wine ../agsutils/agspack.exe -e F TEST.exe

and the resulting TEST.exe is bit-identical to the one created using the same commands on linux:

$ md5sum LINUX.exe 
7dc941b6733b2b4f36c6aa95de0664ec  LINUX.exe
d0k3 commented 2 years ago

Alright, I got my laptop back. It still does not work, here's what I did: ./agstract.exe Maniac.exe man14 -> everything is extracted to ./man14, and everything looks fine. From here, I can also extract sprites, and it works. ./agspack.exe -e man14 Maniac_.exe -> Maniac_.exe is created. It's approx 14MB in size (vs. 10MB in the orginal executable), and I can't get it to run.

Just via starting it, I get: image

SCUMMVM allows to add the game as unknown AGS game or as unknown Maniac Mansion Deluxe variant. If I start it, I get: "ERROR: invalid color depth!"

For reference, the compiled Maniac_.exe is attached here. Maniac_.zip

rofl0r commented 2 years ago

thanks. your exe has the same md5 than i pasted above so it's most likely identical. the size difference is because agspack doesn't write multi-file packs, it packs all files into one. so Maniac.exe, Maniac.001, etc, get merged into a single file. i also figured out why the repacked exe doesn't work: agspack always writes files in libversion 20, or 30 for very new games with 64 bit offsets. but the original exe stub doesn't know that version yet, since it is itself using version 11. this is because i never actually intended to reuse the original exe, and version 20 covers all of (my) usecase, which is to load the game into the standalone ags engine (which is imo preferable over the scummvm port). the functionality to restore the exe stub was added only very recently and i didn't think about this cornercase. before that, agsutils always created a non-executable .ags file to load in the standalone engine. scummvm is wrong here, as the created ags pack is perfectly valid; and my scummvm 2.5.1 release swallows it without issues. you must be facing a bug that was introduced later, or if you use an older version, fixed in a newer version.

in order to get the original exe stub working, we'd need to add support to CLib32.c to write libversion 11 packs. alternatively, you could agstract any newer ags game, and then copy its exestub file over and replace the one in your extracted directory, before agspacking it all together.

d0k3 commented 2 years ago

Great explanation, thank you! Is the standalone AGS engine also available for Windows? Also, are these AGS EXE stubs identical in all games using the same version?

rofl0r commented 2 years ago

i think yes, the stubs are identical for the specific ags version used to create the game. this one here works: https://github.com/ags-archives/engines/blob/master/ags302sp1/acwin.exe

wget 'https://github.com/ags-archives/engines/blob/master/ags302sp1/acwin.exe?raw=true' -O F/agspack.exestub

and yes, the ags engine has windows binaries too: https://github.com/adventuregamestudio/ags/releases/download/v.3.6.0.24/AGS-3.6.0.24-Beta4.zip

though i've never used the windows build of the engine, on linux after compiling from source you get a "ags" binary inside the Engine directory, so i guess there should be an ags.exe inside that zip.

d0k3 commented 2 years ago

With the stub you provided, I managed to create a working (that means runnable and working in SCUMMVM) EXE. Actually, I'm unsure what went wrong in SCUMMVM earlier, it possibly got confused by me leaving the numbered files (Maniac.001, ...) in the directory.

Now there's another problem. If I do ./agsprite.exe x man14/acsprset.spr man14spr followed by ./agsprite.exe c acsprset_repacked.spr man14spr, I get a borked file. The acsprset_repacked.spr can't be extracted with agsprite.exe anymore. The error is:

.\acsprset_repacked.spr: oob access attempted (want: 8786711, have 2787637)
CRT: unhandled exception (main) -- terminating

EXEs created with this new acsprset.spr don't work either. There's no sprindex.dat file, so that's not the issue. I did notice, however, that the agsprite.info mentions 2055 sprites, while I count only 1211 TGAs.

For reference, here's a ZIP with both, original and repacked acsprset.spr: acsprset_original_and_repacked.zip

rofl0r commented 2 years ago

no such issue on linux, must be windows specific. i've seen in agsprite.c there are 2 occurences of fopen() without "b", and windows is kinda stupid when opening files without that mode in that it replaces all "\n" characters written with "\r\n". unfortunatelny i didnt have time yesterday to make a windows build with that fixed and looking into whether that's the only thing groing wrong there.

rofl0r commented 2 years ago

fixed with 012501f140e41f849f118854431ec76ad4fc01e5 i'm attaching here a fixed build of agsprite for your convenience: agsprite.zip

note that there's a further issue with agsprite, after unpacking, repacking, and unpacking again the initial 16 bit sprites are suddenly 8 bit, it looks as if my obsession with efficiency went too far (16 bit spites, if total count of colors is <= 255 will be saved as 8bit to save space, which is fine by itself, but it seems the bookkeeping loses track that these ought to be restored as 16bit for ags) . so for the moment (until fixed) it's best if you keep your original unpacked folder around, and always only ever repack that one, rather than packing/extracting multiple times.

rofl0r commented 2 years ago

oh, i forgot to answer on your comment:

I did notice, however, that the agsprite.info mentions 2055 sprites, while I count only 1211 TGAs.

if you look at the last 2 lines in the .info file, you see a gap of ~1000 - this is because spritenumbers can be arbitrarily assigned in the ags editor.

rofl0r commented 2 years ago

ok, the mentioned bug is now fixed too. 05b24b5c52c6505ef56c8eaf33949c395ac69059

attaching latest agsprite agsprite.zip

rofl0r commented 2 years ago

@d0k3 can you confirm whether the agsprite issues are fixed for you ?

d0k3 commented 2 years ago

I just tested it, works great. Thank you! I hope there will be a new release.

rofl0r commented 2 years ago

ok, got some other minor improvements to agsprite. https://github.com/rofl0r/agsutils/releases/tag/v0.9.7