Important Message

You are browsing the archived Lancers Reactor forums. You cannot register or login.
The content may be outdated and links may not be functional.


To get the latest in Freelancer news, mods, modding and downloads, go to
The-Starport

Easy Freelancer character decoding with C#

Here you can find the postings on the different editing utilities found for Freelancer

Post Fri Jan 07, 2005 2:06 pm

Easy Freelancer character decoding with C#

I recently spent a couple of hours figuring out how to decode a Freelancer character file using C#. It turns out it's pretty easy, essentially 6 lines of copy-paste code wrapped up as a function that returns the entire decoded file in a string. It isn't really tutorial-type material, but there is one bit that can cause problems if you don't know about it, so I figured it was worth posting.

Code is at the bottom. WARNING! I substituted "<}>" for closing square brackets in the code; change it back or the code won't compile.

The major problem is that .NET makes very bad guesses about FL file content if you try to read them without specifying the encoding as UTF7. The result is garbage.

Other than that, none of this is particularly clever. I trim off the first 4 characters (the "FLS1" header) as I read the data in, and directly turn it into a character array. Then I just un-hash each character using the secret word "Gene" and the character hash outlined by Jor in his flcodec source (which he says he wrote based on algorithm information from Sherlog).

I'm not sure how good performance is, but I read the entire file into memory in 1 step, then write the decoded data to a presized stringbuilder; this theoretically makes it much faster than per-character file reading, but it probably is a bit slower than techniques that skips some content lines, and a bit more memory-intensive than a routine that simply modifies characters in-place in the data array.

<pre><font size=1 face=Courier>public string DecodeCharacter(string filepath)
{
// read the entire raw file as a character array;
// we immediately strip out the FLS1 using Substring(4).
char[<}> data = (new System.IO.StreamReader(filepath, System.Text.Encoding.UTF7)).
ReadToEnd().Substring(4).ToCharArray();
// create a pre-sized stringbuilder.
System.Text.StringBuilder sb =
new System.Text.StringBuilder(data.Length);
char[<}> gene = "Gene".ToCharArray();
for(int i = 0; i < data.GetLength(0); i++)
{
sb.Append((char)(data[i<}> ^ (((gene[i % 4<}> + i) % 256) / 0x80)));
}
return(sb.ToString());
} </font></pre>

Post Fri Mar 18, 2005 8:05 am

What's this for?what files? *i'm lost*

Post Sat Mar 19, 2005 3:33 am

It's C#.NET code for decoding the ".fl" save files. Can't believe I missed this before, I'm still using imported C functions to decode the files. Thanks for sharing this WW

Post Sat Mar 19, 2005 4:56 pm

thnx Accushot / I found this looking for character editing - as in character's bones & mesh dfm files, heard anything about that?

Post Fri May 20, 2005 12:18 am

I've been using this code recently and one line seems to be incorrect. The last operator of the following line should be a bitwise-OR, not a division:

<pre><font size=1 face=Courier>sb.Append((char)(data[i<}> ^ (((gene[i % 4<}> + i) % 256) / 0x80))); </font></pre>

So it should actually read:

<pre><font size=1 face=Courier>sb.Append((char)(data[i<}> ^ (((gene[i % 4<}> + i) % 256) <PIPE> 0x80))); </font></pre>

Very efficient code though, works a treat

EDIT: OK, it seems it wasn't your fault; the forums convert pipes to /'s automatically. I've written <PIPE> in place of the vertical pipe that isn't shown

Edited by - Accushot on 5/20/2005 1:23:33 AM

Post Mon May 23, 2005 4:48 pm

ouch! I just HATE 'helpful' software that changes what you meant to what you didn't...

Post Tue May 24, 2005 3:00 am

what forums system does TLR use anyway?

Return to Freelancer Editing Utilities Forum