LZSS Compression

Release mod tools/source code to the tools here.
Forum rules
Its preferred you release the source code, but if not its ok, but if its found you stole/borrowed source code from another tool/program and violated its license it will be removed.
Zmathue
VIP
Posts: 30
Joined: Tue Mar 02, 2010 8:03 pm

LZSS Compression

Postby Zmathue » Fri Dec 23, 2011 8:46 pm

I was doing some disassembly work recently and figured I could use some practice, and I remembered how a lzss (specific to thps/neversoft games) compressor is needed to recreate pre's. So I started removing the lzss function from thug2.exe but found that it depended on too many other functions for me to easily remove it so I created this instead. This is SAMPLE code for a dll that can be injected into thug2.exe while running and compress your files. Files compressed with this have been successfully decompressed with aluigi's unlszz.h(used in prereaper). I would create a pre builder but I do not really have any interest in THPS any more.

Attached is a compiled dll/source

Happy Holidays

lzsshook.cpp

Code: Select all

//   Zmathue 2011
//   Do what ever you like with this code

//   Usage, inject this dll into thug2.exe with any dll injector and it will lzss compress your in.txt file for you

/*
I was doing some disassembly work recently and figured I could use some practice, and I remembered how a lzss (specific to thps/neversoft games) compressor is needed to recreate pre's. So I started removing the lzss function from thug2.exe but found that it depended on too many other functions for me to easily remove it so I created this instead. This is SAMPLE code for a dll that can be injected into thug2.exe while running and will compress your files. Files compressed with this have been successfully decompressed with aluigi's unlszz.h(used in prereaper). I would create a pre builder but I do not really have any interest in THPS any more.
*/

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

bool HOOKED = false;

int __stdcall (*lzss)(unsigned char *in, unsigned char *out, int size, int isDebug) = NULL;


extern "C" BOOL APIENTRY DllMain( HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved ){

    switch (ul_reason_for_call)
   {
      case DLL_PROCESS_ATTACH:

         if(!HOOKED){
            FILE *file,*outfile;
            unsigned char *buffer,*outbuffer;
            char buf[200];
            int *address = (int*)0x00401700;
            lzss = (int (__stdcall *)(unsigned char *in, unsigned char *out, int size, int isDebug))address;

            MessageBox(0,"Press ok to continue","",0);

            //Open file
            file = fopen("in.txt", "rb");
            outfile = fopen("out.txt", "wb");
            if (!file){

               return false;
            }else if (!outfile){
               MessageBox(0,"Unable to open file out.txt","",0);
               return false;
            }

            fseek(file, 0, SEEK_END);
            unsigned long fileLen= ftell(file);
            fseek(file, 0, SEEK_SET);

            buffer=(unsigned char*)malloc(fileLen+1);
            if (!buffer){
               MessageBox(0,"Memory error!","",0);
                 fclose(file);
               return false;
            }

            fread(buffer, fileLen, 1, file);

            outbuffer=(unsigned char*)malloc(fileLen*3);      //hope this is large enough, i mean it is a compressor after all
            if (!outbuffer){
               MessageBox(0,"Memory error!","",0);
                 fclose(file);
               return false;
            }
            //sprintf(buf,"buffer size: %d",fileLen+1);
            //MessageBox(0,buf,"",0);
            int size = lzss(buffer, outbuffer, fileLen+1, 1);

            fwrite (outbuffer, 1, size, outfile);
            fclose(file);
            fclose(outfile);
            free(buffer);
            free(outbuffer);

            HOOKED = true;
         }

      case DLL_THREAD_ATTACH:
      case DLL_THREAD_DETACH:
      case DLL_PROCESS_DETACH:
         break;
    }
    return TRUE;
}
Attachments
lzsshook.zip
(8.05 KiB) Downloaded 411 times

%.gone.
Global Moderator
Posts: 388
Joined: Sun Mar 07, 2010 5:12 pm

Re: LZSS Compression

Postby %.gone. » Fri Dec 23, 2011 10:59 pm

You don't need to compress the files for a pre to be correct. You don't even need to properly align the files for the pre to work in the game.
As a matter of fact none of the audio files are compressed due to the fact that they get larger when compressed using the LZSS compression routine.
Also, i've noticed that throughout the pre containers some files are compressed while others aren't and this varies from file type to file type even in the same pre.

Zmathue
VIP
Posts: 30
Joined: Tue Mar 02, 2010 8:03 pm

Re: LZSS Compression

Postby Zmathue » Fri Dec 23, 2011 11:14 pm

%.gone. wrote:Also, i've noticed that throughout the pre containers some files are compressed while others aren't and this varies from file type to file type even in the same pre.

What they do is store it compressed if the compressed size is smaller then the original size, they do the same thing in the net code.

%.gone.
Global Moderator
Posts: 388
Joined: Sun Mar 07, 2010 5:12 pm

Re: LZSS Compression

Postby %.gone. » Fri Dec 23, 2011 11:30 pm

Not always. I'm talking about all types of files that can be found inside a pre container including .img, .tex, .scn, .qb, etc.
While NONE of the audio files will ever be smaller if compressed using LZSS. That's why I had to disable the ability to compress audio files when building the pres.

Lalita
Posts: 15
Joined: Tue Mar 16, 2010 5:18 am

Re: LZSS Compression

Postby Lalita » Sun Dec 25, 2011 7:39 am

@%.gone. could you convert a .tex file for me? From th3 which was in a .tex file.


Return to “Mod Tools”

Who is online

Users browsing this forum: No registered users and 1 guest