The Dawn of War II Beta is upon us, which brings with it a series of new file formats compared to Dawn of War 1 and Company of Heroes. The most important of these is the SGA format, which is the archive format used by Relic games for game assets. Dawn of War 1 used version 2 SGA files, Company of Heroes used version 4 and Dawn of War II uses version 5 (for completeness, I think Impossible Creatures used version 1, some unreleased project or The Outfit used version 3, and Company of Heroes Online used version 4.1).
I've already released SgaReader2 for looking inside version 5 (and 4.1, 4 and 2) SGA archives. In order to actually mod Dawn of War II properly, we need to be make to make new version 5 archives rather than just read existing ones, which is where sga4to5 comes in. Carry on reading for details...
sga4to5 is a small (8 kilobyte) application which converts a version 4 SGA archive into a version 5 archive.
Download: http://www.corsix.org/misc/sga4to5.exe
Combine this tool with something capable of making version 4 SGA archives (e.g. Mod Studio with a CoH mod loaded, or CoH's archive.exe) and you can create new version 5 SGA archives and start modding Dawn of War II. Note that unlike most of my tools, sga4to5 is a command line application, so a quick guide to its command line parameters is in order:
Usage: sga4to5.exe -i[in[put]] file -o[ut[put]] file [-name newname] [-q[uiet]] [-v[erbose]]
-i, -in or -input specify the input file (version 4.0 / CoH SGA)
-o, -out or -output specify the output file (version 5.0 / DoW2 SGA)
-name changes the name in the file header of the output
-q or -quiet reduces the amount written to the console
-v or -verbose causes more than usual to be written to the console
Sample usage:
sga4to5.exe -i "E:\Valve\Steam\SteamApps\common\warhammer 40,000 dawn of war ii - beta\MahArchives\v4Attrib.sga" -o "E:\Valve\Steam\SteamApps\common\warhammer 40,000 dawn of war ii - beta\MahArchives\GameAttrib.sga" -v -name "Attributes"
Output from above command:
-- Corsix's SGA v4 to v5 Convertor --
Opened input file and output file
Input archive details:
name: Made with Corsix's Rainman
version: 4.0
data header offset: 184
data header size: 237960
data offset: 238144
data length: 13494961
content MD5: 0F6155399F5200D5F1FB5F890052542F
header MD5: 961A968EDB37216BE0D88DB5EDE95D48
Copying file data, this may take a while...
Output archive details:
name: Attributes
version: 5.0
data header offset: 13495157
data header size: 237958
data offset: 196
data length: 13494961
content MD5: C1322F3DE54A35A93D14F6A5DEF04ACD
header MD5: 4A6A47C63F46490A75800869CF715F75
Done
The actual conversion details are not too interesting; change the version number, add an extra field to the file header, relocate the data header to the end of the file, shrink the TOC records by 2 bytes each, update various offset and length fields, and recalculate the checksums. There are a few interesting implementation details like the initialisation vectors used for checksums and the precise definition of what data is checksummed, but the process is fairly simple conceptually.
More interesting (to me) is the process of squishing the program down to a mere 8 kilobytes. By default, a C++ program compiled using Visual Studio will either dynamically link to msvcr[t|p]90.dll (Microsoft's C/C++ runtime library) or include the required parts of the CRT within the executable itself. Using the former method, sga4to5.exe came out to around 18 kilobytes, but it referenced a several-hundred-kilobyte DLL, and using the latter method, it came to around 70 kilobytes. For a small program, the several-hundred-kilobyte runtime DLL is very excessive, and the 70 kilobyte amalgamation is still 10 times larger than it needs to be. To get any smaller, the C runtime has to be removed, which is a non-trivial process:
- CRT file access functions like fopen, fread, fwrite, fclose, etc. need to be replaced with win32 file access functions like CreateFile, ReadFile, WriteFile, CloseHandle, etc.
- CRT memory allocators like new[] and delete[] need to be replaced with win32 memory allocators like VirtualAlloc and VirtualFree (note that VirtualAlloc has a minimum granularity of one memory page (~4 KB), so HeapCreate and HeapAlloc may be better choices for some applications).
- CRT console I/O functions like [w]printf need to rewritten to use win32's WriteConsole[W]. Win32 doesn't have a direct analogue to [w]printf, so you need to write your own implementation which does enough for your needs (%s, %u and %lu were the only wprintf escapes I needed, and are simple to reimplement) and then writes string buffers using WriteConsole[W].
- [w]main()'s argc and argv parameters are provided by the CRT, so they need to be obtained via calls to CommandLineToArgv[W] and GetCommandLine[W] instead.
- CRT string functions like wcs(i)cmp need to be replaced with win32's CompareStringEx (with NORM_IGNORECASE flag).
- CRT memory functions like memcpy and memset need to be reimplemented. This is slightly difficult as the win32 functions like CopyMemory and ZeroMemory just alias to the CRT functions, and Microsoft's C++ compiler tries to be helpful and efficient by replacing code which looks like memcpy and memset with calls to memcpy and memset. In my implementation of memset, I had to insert
__asm nop into the loop body to confuse the compiler enough so that it didn't optimise it away to a memset call.
- The C++ compiler has to be instructed not to use buffer security checks (as they call CRT code), not to use C++ exceptions (as they require CRT code) and not to enable run-time-type-information (RTTI) (as again, it requires some CRT code).
- The C++ linker has to be instructed to ignore the CRT library, not to embed an XP manifest (as it'll reference the CRT, and add half a kilobyte to the file size), and to use [w]main as the entry point rather than the CRT's main function.
Note that the above list is the steps that I had to take to make sga4to5.exe CRT-less, other applications may have other requirements upon the CRT which are harder to remove (YMMV). After performing the above steps, sga4to5.exe came out to 14 kilobytes and referenced only two DLLs, both of which are core Windows DLLs: Kernel32.dll (for CloseHandle, CompareStringEx, CreateFileW, GetCommandLineW, GetStdHandle, ReadFile, SetFilePointer, VirtualAlloc, VirtualFree, WriteConsoleW and WriteFile) and Shell32.dll (for CommandLineToArgvW). The final step was to pass the executable through UPX, which squished it down a bit more to the final size of 8 kilobytes.
Comments
MSVCP71.DLL
I have tried to download the missing file which I suceeded at, and yet it still is unable to find the file I have read about 30 forums and they all say you download and walah fixed. I have also tried placing the file into the same folder as the .exe and that didnt work either. I downloaded MSVCR71.DLL as well and tried with both of them in and out of the folder with the .exe. How do I get Corsix to recognize the file sitting in my System32??
help me plz
I installed the corsix mod studio and it says I am missing ++ msvcr71.dll ++ I have installed the file in my Dawn of war directory and still to no avail. This happens every time I try to run the mod studio. can you please help?
And is there a chance to have
And is there a chance to have a Corsix's Mod Studio 0.56 with DoW2 support ? :)
Version for XP64?
Is there any chance of getting a version to run on 64 bit systems (XP64 preferably)? As it moans about the image file being for a different machine type.
When I try to run sga4to5
When I try to run sga4to5 using the command prompt it tells me that it cnnot start or run due to incompatibility with 64-bit versions fo Windows.
Is there a way around this?
DOW2 custom keys
Is this needed to make my own customkeys?
I tried a lot with this:http://dowcodex.com/User:Sturm/Grid_Key#What_Does_This_Do
but not effective, just your customkeys are ok.
I'm an expert in RTS and need better customkeys. If I design this customkeys, u will want to prepeare it to be so easy to put as the link i showed u?
CU crack ;)
It's a command line application
STARSBarry: You have to launch it from the command prompt, you can't just double click the file.
sga4to5
When I try to run sga4to5 using the command prompt it tells me that it cnnot start or run due to incompatibility with 64-bit versions fo Windows.
Is there a way around this?
trying to load this damn thing
Im new to this anyway I DL your 4 to 5 program and doubleclick to load, the DOS box opens for a milisecond and instantly closes again, so i cant enter the command line or anything dont matter how many times i do this same effect.
anyway using vista 64 bit edition any ideas?
Man, it's sites like this and
Man, it's sites like this and people like you that make me happy I went in computer programming in college (just started :)
rbf
Made any progress on RBFs? I have, but I'm still getting funky data offsets in some situations... do you have any plans to do RBFs even?
Reading data from .rbf
Hi Corsix,
Thanks a bunch for your hard work and coding, it's very enthusing. I'm not looking to directly mod DoW2 just yet, but rather I'd like to 'mine' the hard data on units, movement, combat, pvp etc so that I can chart it up and so forth like they have for DoW1.
Thanks in advance; sorry if there's already a way to do it...I'm looking! :(
-cefx
*.rbf file extension
Hey Corsix.
At first thanks for all the tools and info you give us about the software part of Relic's games.
So i just used your sgareader2 to have a look at gamestats of DoWII (gameattrib.sga) and faced that .rbf filetype.
I just know this extension as backup files for rollbacks and have no idea how to read edit them.
Any ideas ß
ace.
Thanks
<3 You Corsix!!!
Thanks! :D
help!
ok i am completely new to this.. how do you get it to work exactly, can anyone explain alot more throughly please?
Not Virus
It's a false positive.
Virus
When I want to download the Sga converter I get a Virus waring.
Antivir says it'S the Trojan Virus CryptXPack.Gen.
Is this really a Virus or does Antivir simply mistake your exe for one?
I'm a doddering old fool.
Ignore that last comment. put it down to senility on my part. :D
I've extracted gameart.sga
I've extracted gameart.sga using your tool but it seems to be simply making a file of the same name, of 0kb size, in the directory of my choice. Am I missing something?
Vista home premium, Q6600 2.4Ghz, 3GB ram.
Post new comment