Jump to content
  • Advertisement
Sign in to follow this  

Reading .PAK files

This topic is 4267 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone. I'm a little confused about how this code is working. This is from cmdlib.c in the HL 1 SDK. Now I'm trying to learn what this code is doing because I need to learn to manipulate the way the program reads and understands the .PAK files. I don't understand anything about this code and was wondering if you guys could help. I don't think this is cpp either, I'm pretty sure this is c and I don't know anything about c. Here's the Code: Taken from HL SDK 2.0:
/* ============ ListPak Prints the contents of the specified pak file to stdout ============ */ void ListPak(char* pakname) { FILE* f = SafeOpenRead(pakname); packheader_t head; packfile_t* pdir; long i=0,imax=0; long totlen=0; SafeRead(f,&head,sizeof(packheader_t)); pdir=malloc(head.dirlen); fseek(f,head.dirofs,SEEK_SET); SafeRead(f,pdir,head.dirlen); fseek(f,0,SEEK_END); totlen=ftell(f); fclose(f); imax=head.dirlen/sizeof(packfile_t); for(i;i<imax;i++) { printf ("%64s : %7i\n", pdir.name,pdir.filelen); } printf("Total Length: %li bytes\n",totlen); } long flen(FILE* f) { long savepos=ftell(f); long filelen=0; fseek(f,0,SEEK_END); filelen=ftell(f); fseek(f,savepos,SEEK_SET); return filelen; }[/Quote] I just need help understand what's going on here, what is it doing and how can I convert it to cpp. Thanks, Taylor (Odin)

Share this post

Link to post
Share on other sites
Hey, What do you mean by "how can I convert it to cpp" ?

Well the "void ListPak(char* pakname)" function reads the PAK file and store the information from it in 2 structures called:

packheader_t head;
packfile_t* pdir;

then it prints out the names of all the files stored in it and the size of them.

the long flen(FILE* f) function just return the size of the file.


Try to call this in your program.

ListPak( "MyPakFile.pak" );

Good luck.

Share this post

Link to post
Share on other sites

You need to understand the Format of a Pak file.

There is usually a static header (fixed struct) and then a directory list of all the subentities (directory entries for the files).

That is then followed by all the file images, linearly.

The program first reads in the header and then the entire directory (array of file names/data structs) -- it knows how big the dir block is from data in the header.

It then prints the individual file names (traversing the array with a for loop)

There is a .h file that defines the Structs involved.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!