November 2, 2013

Mono.Cecil: Change Base Class, Add Field

I wanted to swap out a base class in Terraria with another base class that exists in a different DLL, and wasn't quite sure how to do it, so I created a simple test project for experimenting with Mono.Cecil.

You can download the code for my experiments here.

The sample code has three projects: RewriteMe, InjectMe, and MonoCecilExperiments.  RewriteMe has a class called ReplaceMyBaseClass and I want to swap out its base class with a new base class in InjectMe.  The code should be documented well enough that people should be able to follow along.

Essentially, I want to work at the IL level as little as possible.  If I can inject a call to some C# code, all the better.

November 1, 2013

No RomTerraria 3 Release Today

The good news: I've got hook injection working throughout the project using Mono.Cecil, and none of my code had to change after 1.2.1.2 was released.  In other words, once I'm done getting this right, should be able to keep working with minimum or no code changes going forward.

The bad news: It's taking longer than I'd hoped to get this ready, and being sick for a day didn't help.

I'll try to keep you posted.

October 27, 2013

Coders: TerrariaServer without XNA?

I think it's possible to get TerrariaServer running without XNA without rewriting the code, which would be the first step in getting Terraria servers able to be hosted on Linux.

I was using Mono.Cecil to try to modify Terraria on the fly.  My hope was to make it so that I could inject hooks directly into the Terraria executable to allow things like replacement UI layers or modifying certain constant values, and while I was doing it, I found that I could replace all of the XNA assembly references with references to an assembly under my control.

I was then able to write stubs and mocks for all of the XNA objects needed on the critical path and get it to the point where Terraria wouldn't crash on connect.  It won't accept the connection, but it isn't crashing.


Even if this doesn't get out to Linux, there would be some extremely minor benefits getting this going anyway.   The mocked version uses 6MB less memory and doesn't require an XNA-compatible video card on the server.

Now, there's nothing here that's usable by a standard end user at the moment, but here's a source release for people who are interested in this problem space and want to take it and run with it.

http://www.romsteady.net/Projects/DasmAsm-20131027.zip (VS2010 solution)

October 26, 2013

Terraria Server on Linux?

This isn't directly RomTerraria related, but could be related to how RomTerraria is going to be working going forward.

The biggest problem with making a non-source mod like RomTerraria is that every time the game changes, even if your code wouldn't normally be affected, your app completely breaks because the binding changes.  There's no way to ensure a consistent experience for your code.

With early versions of RomTerraria, I took advantage of the XNA component framework.  That gave me a perfect place to inject my code where it would work nicely with all of the code that already exists in the game.  However, with v1.2, the call to base.Draw() removed my ability to easily inject rendering calls for things like the minimap and HUD updates I had put in before.  In addition, since Terraria uses some Win32 calls internally, you can't really use Terraria in a non-Windows environment.

What would work out well was if I could take an arbitrary Terraria executable and inject code without distributing Terraria's code.  Fortunately, a tool set does exist for doing this: ildasm and ilasm. You run ildasm against Terraria.exe or TerrariaServer.exe and you get the MSIL for the executable.  You apply your patches, then run ilasm to recreate your executable.

Example disassembly:
ildasm TerrariaServer.exe /out=TerrariaServer.il

Example reassembly:
ilasm /32bitpreferred /output:TerrariaServerRecompile.exe TerrariaServer.il

Then you can just run TerrariaServerRecompile.exe and have a round-tripped server executable.

So what I was thinking of doing was writing dummy stubs for most of the interop code (NATUPNPLib.UPnPNAT, etc.) and trying to get TerrariaServer.exe able to run on Linux as an experiment.  If I can get this to work, I can then make the next step for RomTerraria: inject entry points directly into the Terraria executable without redistributing any Terraria code.

Thoughts?