PDA

View Full Version : .NET - LfsReplayParser - Library to parse LFS replay headers


DarkTimes
5th April 2008, 23:42
Hello,

I needed to use my MPR header parser in a separate project, so I pulled it out of my maze of spaghetti code and put it in its own .dll. I liked this idea so I also added SPR header parsing code to it as well. I figured other people might find it useful, so I've uploaded it here.

Trivial example of parsing an MPR header using the library:

using System;
using System.IO;

// You'll need this.
using DarkTimes.Lfs.Replays;

namespace LfsReplayParserExample
{
class Program
{
static void Main()
{
string path = @"X:\Path\To\Replay\Replay.mpr";
MultiPlayerReplay replay = null;

try
{
replay = MultiPlayerReplay.Parse(path);

Console.WriteLine("Track: {0}", replay.Track.ShortTrackName);
Console.WriteLine("Laps: {0}", replay.Laps);
Console.WriteLine("Start Time: {0}", replay.StartTime);

for (int i = 0, j = 1; i < replay.Results.Length; i++, j++)
{
Console.WriteLine(
"{0}. {1} | Time: {2} | Best Lap: {3}",
j,
replay.Results[i].PlayerName,
replay.Results[i].OverallTime,
replay.Results[i].BestLapTime);
}
}
catch (ReplayException ex)
{
Console.WriteLine("Error parsing replay: {0}", ex.Message);
}
}
}
}The library is completely documented with XML for intellisense, so it should be fairly straight-forward to use. As with anything I ever make I'm extremely grateful for any comments, suggestions and bug-reports.

Note: This library and its source are released under the GNU General Public License (http://www.gnu.org/copyleft/gpl.html). Please make sure you read and understand the license before you use it.

AndroidXP
5th April 2008, 23:54
Nice, though you shall receive five trout slaps for using goto :p

mcgas001
5th April 2008, 23:57
Nice, though you shall receive five trout slaps for using goto :p

LOL Where?

Nice work DarkTimes, Certainly something i shall play with! :thumb:

AndroidXP
6th April 2008, 00:02
Well, at least I think he does. Maybe it's a compiler optimization, who knows :tilt:

mcgas001
6th April 2008, 00:05
Well, at least I think he does. Maybe it's a compiler optimization, who knows :tilt:

Been disassembling code now have we? :p

DarkTimes
6th April 2008, 00:06
Hmm, weird. Must be an optimisation by the C# compiler, as I really don't use any goto in it. Funnily enough I was thinking about using a goto for something else the other day, but I couldn't actually remember the syntax for delaring one, as I've genuinely used it that little.

AndroidXP
6th April 2008, 00:09
I've rechecked and yes, it seems to be a compiler optimization. I guess you have a bit of "duplicate code" in many of the switch cases in LfsFileReader.ReadEncodedString(), the compiler seems to put a bunch of labels after the switch to optimize code usage for cases that would use the same or similar parts of code.

E: So only one trout slap for allowing the compiler to use goto in your precious code :p

DarkTimes
6th April 2008, 00:12
Well, I need to get over my weird paranoid complex about posting source-code, so I've uploaded the Visual C# 2008 Express Edition project files. Be gentle, this is a new experience for me. :)

Edit: Also remember it's code mostly hacked from a different program and put together after a few whisky's on a Saturday night. :)

AndroidXP
6th April 2008, 00:27
If you wanted to protect your sources, .NET is in my experience unfortunately a rather bad place to be in general. By using .NET Reflector you can pretty much look at any MSIL based code (.NET dlls and exe files) and get a 99% working copy & pasteable code (in the language you specify; C#, VB.NET... whatever you want). It's so simple any doofus can use it, and it's actually a quite nice tool if you want to learn how MSIL code works. Using the Reflexil addon you can even do simple code manipulations, though they mostly have to be done in IL directly.

Afaik your best bet is trying to use some obfuscators, but they only do what their name suggests, obfuscate the code to make it harder to read, not impossible. :shrug:

You could argue that reverse engineering has always been possible, but it's actually a bit scary once it gets so easy and produces such nice readable code.

traxxion
7th April 2008, 10:10
Holy shit, just one day after I finished my own replay parser I find this thread :schwitz: :banghead:
Good job there though, some interesting solutions for problems I also bumped into! :)