Skip to content

Instantly share code, notes, and snippets.

@barncastle
Last active October 14, 2022 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barncastle/f931764404b4f5d1d2d93cbd04ac94d8 to your computer and use it in GitHub Desktop.
Save barncastle/f931764404b4f5d1d2d93cbd04ac94d8 to your computer and use it in GitHub Desktop.
File extraction and decompression for Sega's Dinosaur King for Naomi
using System.IO;
using System.Runtime.InteropServices;
namespace ConsoleApp2
{
static class DinoKingSSZL
{
public static void Extract(string directory, string filename)
{
Directory.CreateDirectory("Dump");
using var fsFil = File.OpenRead(Path.Combine(directory, filename) + ".fil");
using var fsBin = File.OpenRead(Path.Combine(directory, filename) + ".bin");
while (fsFil.Position != fsFil.Length)
{
var entry = fsFil.Read<FileInfo>();
fsBin.Position = entry.Offset;
byte[] buffer;
if (entry.Flags == 1)
{
var header = fsBin.Read<SSZL_Header>();
buffer = fsBin.ReadBytes(entry.Size - 16);
buffer = Decompress(header, buffer);
}
else
{
buffer = fsBin.ReadBytes(entry.Size);
}
File.WriteAllBytes("Dump\\" + entry.Filename, buffer);
}
}
public static byte[] Decompress(SSZL_Header header, byte[] input)
{
var output = new byte[header.DecompressedSize];
// i = input position, o = output position
for (int i = 0, o = 0; o < header.DecompressedSize;)
{
byte value = input[i++];
if (value != header.Control)
{
// store literal
output[o++] = value;
}
else
{
byte index = input[i++];
if (index == header.Control)
{
// store control byte
output[o++] = (byte)header.Control;
}
else
{
byte count = input[i++];
// fix index value
if (index > header.Control)
index--;
// back reference
for (int j = 0; j < count; j++, o++)
output[o] = output[o - index];
}
}
}
return output;
}
[StructLayout(LayoutKind.Sequential)]
struct FileInfo
{
public int Offset;
public int Size;
public int Flags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string Filename;
}
[StructLayout(LayoutKind.Sequential)]
struct SSZL_Header
{
public uint Magic;
public int DecompressedSize;
public int Size;
public int Control;
}
}
}
@Alex23344545
Copy link

Alex23344545 commented Sep 1, 2022

Hello, does this script work and how to use it?

@barncastle
Copy link
Author

This is more of an initial concept on how to decompress these files and a starting point for someone to properly investigate this format. With that being said it does work for most files.

If you want I can package it up into an app for you?

@Alex23344545
Copy link

Alex23344545 commented Sep 2, 2022

Yes please I would really like to decompressed the files of this game.

@Alex23344545
Copy link

Alex23344545 commented Sep 2, 2022

Just out of curiosity where did you get the compressed files?

@barncastle
Copy link
Author

If you download dinokingextractor.zip and extract it to the game's data directory and run, it'll created a folder called Dump containing the decompressed assets. Just to note, the data folder is the one that contains complimentary *.bin and *.fil files that share the same name e.g. CF_pack.bin + CF_pack.fil.

Just out of curiosity where did you get the compressed files?

All my research was done on the sample files provided here. These would've been extracted from the roms however that's not something I'm knowledgeable on.

@Alex23344545
Copy link

Man, thanks a lot it works, however there are some models and textures that are transparent.

@barncastle
Copy link
Author

As I said, this is concept code and is no way verified as being 100% correct. It could also be that certain models/textures have custom handling by the game.

Unfortunately you'll need someone with more knowledge to reverse engineer this game to get this working.

@Alex23344545
Copy link

Hello, I would like to extract the contents of this file containing the source code of the first Dinosaur King arcade game but unlike the next Dinosaur King arcade games, the first game does not have dir and fil files as stipulated in your script. Could someone create a new script for extracted data from the first Dinosaur King game without needing fil and dir files?

https://mega.nz/file/am4XRDjb#sRJOR3wcYnmcQRPX0ZRp7sVTOyOFNA_F4fgmbmDMMJA

@LoliJuicy
Copy link

Sorry to disturb you. I noticed that you made an extractor for a NAOMI card game which is interesting. I was wondering if this could also be applied to another naomi card game like Love and Berry or MushiKing (the former is I'm more interested in.). Unfortunately, I'm not a coder so I don't know anything.

Love and Berry 1st-2nd Collection is stored in an IC file while Love and Berry 3rd-5th Collection is stored in an mda file. There's nothing much I can do with an IC file but it's possible to convert mda to an image disk file. When I converted it to an image disk, there is an encrypted bin file (containing other models, graphics, etc.), ADX song files that can be played with a sound software, and model and texture files that are in FC format.

I'm fine with if it's just the bin file extracted as I'm most curious of what's inside.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment