1

I know you cant override or inherit from a static class and why. That is clear.

I am looking for some advice on how to replace that static class with my own static class. Any hackish or wildest attempts please.

I am basically writing a MOD for a game and the way the game writer wrote one class in particular, he set it as static and put the implementation in there. So when we write our own DLL with this thing, the only way to execute a calculation on the pixel grid is when his code calls this particular calculation in his static class. Both classes are static but I only need to change one.

That is great for him but I want my thing to do another calculation and make it more awesome. I used ILspy and can see all the code in that static class of the base game, so I can copy and paste it and I only need to modify two or three lines.

But now I want to nuke the games core static class and make mine the only implementation.

I want to force replace that static class at runtime, before the static class is ever called and after loading my mod, how? There must be a way to swap static classes?

I read about creating a proxy DLL that redirects all methods to the old DLL and my method to my DLL but that would require gamers to replace a core game DLL and that is even dirtier than just telling people what my mod does. I am changing thas implementation for this mod, if you dont like don use my mod. That is more reasonable.

Piotr Kula
  • 9,597
  • 8
  • 59
  • 85

3 Answers3

2

I will assume you don't have access to the source and thus can't modify it directly.

You COULD (probably shouldn't) use microsoft fakes since it is mainly for testing. You could create a fakes assembly based on the original author's dll, and override just the type you want. It even supports overriding static classes. Again, I am not saying that you necessarily SHOULD do this, but you COULD.
Here is the page for isolating code under test, it includes an example for shimming a static class (DateTime) https://msdn.microsoft.com/en-us/library/hh549175.aspx

Matt Clark
  • 1,171
  • 6
  • 12
  • We dont have the latest source code, obviolsy the game writter wants to sell his newest versions, but he exposes 90% of the DLL and hides his secret sauces. So I can see the 100% code of what I want to change. So even if I copied it exact, I want to replace His static, with my Static at runtime. – Piotr Kula Sep 15 '16 at 17:57
  • Heheh. Thanks. I will look into this fakes thing :) Sounds like something I should have known about any way... 3 years ago. :) – Piotr Kula Sep 15 '16 at 17:58
  • Rquires Visual Studio Enterprise? :( noooooooo - As if I dont pay enough moeny to MSDN already.. dammit – Piotr Kula Sep 15 '16 at 18:03
  • @ppumkin If you are quick enough, you can [download](https://www.visualstudio.com/en-en/downloads/visual-studio-next-downloads-vs.aspx) vs 15 enterprise (the next vs version) as a preview. However somewhere in the future it will no longer be usable. – Nico Sep 15 '16 at 18:11
  • I already got VS 2015 Pro licensed. No wonder I never heard of Fakes, bloody discrimination against poor people who shell out a ton of money for MSDN any way. *sigh* – Piotr Kula Sep 15 '16 at 18:12
  • We also have VS 2015 enterprise licensed, but we use the preview version currently at work everyday. Works fine. – Nico Sep 15 '16 at 18:14
  • It looks like i can VS2012 Update 5 using my MSDN, which supports fakes. But then I am stuck with VS2012 for this mod only. Should be OK I suppose – Piotr Kula Sep 15 '16 at 18:35
  • Nope.. obviously Pro does not include premium. :( oh well – Piotr Kula Sep 15 '16 at 18:39
1

This seems pretty close to this question: Can I redirect .NET method calls to a new method at runtime?

One of the answers to this post suggests looking at a library called Moles which seems to be similar to Detours and may help

Moles allows to replace any .NET method with a delegate. Moles supports static or non-virtual methods

Community
  • 1
  • 1
Scott Perham
  • 2,410
  • 1
  • 10
  • 20
1

A few options ...

  • Review how the original developer said to modify the game

  • You could use something like JustDecompile to get their code.

  • Use Fakes as suggested above

  • Create your own assembly that calls into their assembly and hack the IL dynamically

Community
  • 1
  • 1
Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
  • Thanks. We can modify the game in any way we want. The problem is that his implementation, for generating light tiles on a grid, specifically calls his static class on the tick, where UntiyGameEngine recals the graphcis. If he had made an interface, or allowed delegates in literraly one line of code.. it would be easy. Create my own assembly means every player then has to "hack" their game with my assembly, instead of just following the MOD loading practice, of loading my MOD assembly ontop of his. Fakes is for Enterprise only, apparently. – Piotr Kula Sep 15 '16 at 18:10
  • You think RhinoMocks will do the same thing? – Piotr Kula Sep 15 '16 at 18:13
  • No, mocking frameworks will not work. You need something that will overload the type resolution. Or just rewrite the methods to inject the code that you want to run. – Matthew Whited Sep 15 '16 at 18:23
  • If you want learn IL you could do this with ILDASM and ILASM. – Matthew Whited Sep 15 '16 at 18:24
  • OK yea I thought so. So I found PRIG that is an alternatie to Fakes. Ahh. ILDASM. Heheh.. its worth a try but it means I have to alter the original DLL though? – Piotr Kula Sep 15 '16 at 18:25
  • Yes, but if the assembly isn't strong named (or isn't required to be strong named) then it doesn't matter. – Matthew Whited Sep 15 '16 at 18:35
  • Yea I get you are saying, but when i want other people to use my mod. They have to go and replace the game mod manually, out of band to what the mod downloader does. So for me, ye sure no problemo. But to distribute it it wont work – Piotr Kula Sep 15 '16 at 18:36
  • Well, when you are hacking other people's stuff you don't really have any other options. – Matthew Whited Sep 15 '16 at 18:37
  • All of these solutions will have that same problem – Matthew Whited Sep 15 '16 at 18:38
  • Even if I used VS2015 Ent with Fakes? – Piotr Kula Sep 15 '16 at 18:39
  • yes, you would be replacing the entry point with your own assembly – Matthew Whited Sep 15 '16 at 18:48
  • Just one last question mate. I found a public class that is not static with a public method, prior to calling the code I wanted to replace. Basically this class calls the Static method. Is there a way I could add an extra thing to execute after that original method executed? :) hehe. Since its not sealed or static. I can inherit from it and override the method, but UnityEngine will not execute my inherited class. UNless I can reinject my class somehow – Piotr Kula Sep 15 '16 at 19:20
  • http://www.codeproject.com/Articles/37549/CLR-Injection-Runtime-Method-Replacer ? – Piotr Kula Sep 15 '16 at 19:46
  • You would still have to make changes that are outside of what you could program. You could redirect the assembly bindings from theirs to yours then have your assembly call back into theirs after replacing that one call. But it would still require changes on the receiving persons end. (more complex changes than just replacing an assembly) – Matthew Whited Sep 15 '16 at 19:52
  • That code project is along the lines of what I was suggesting in my fourth assembly. If your code in your modal is called before their static method you might be able to use reflection to grab their method then replace that method with injected IL. – Matthew Whited Sep 15 '16 at 19:54
  • .Net security is designed to prevent this sort of thing... but if nothing is strong named/signed it should be possible. – Matthew Whited Sep 15 '16 at 19:54
  • As a note it would probably be easier to write an old school C++ app that grabs the running memory of the other application and screws around with in. – Matthew Whited Sep 15 '16 at 19:56
  • Heheh - Old school c++ to the rescue :) Thanks for your comments. I will accept your answer based on the IL injection.. but maybe a small update for other readers would be good. – Piotr Kula Sep 15 '16 at 19:58
  • 1
    As a note I wasn't intending to say c++ is old school. I was refering to how old mods were c++ memory hacks. – Matthew Whited Sep 15 '16 at 20:04
  • Just wanted to say that you were spot on with the IL injection. I have found out that when the game starts it loads up mods before starting the game engine. This enables the mod to detour methods, even static ones to my own ones. After reading that codeproject article I was going in the correct direction, then found another mod that does this trick. So looks promising! Thanks again! – Piotr Kula Sep 15 '16 at 20:40