Overriding source code of game inside a mod

Discuss modding questions and implementation details.
Post Reply
kiskoller
Posts: 36
Joined: Fri Sep 27, 2019 11:13 am

Overriding source code of game inside a mod

Post by kiskoller »

Is there a generic Unity or .Net way to replace a class implementation with another?

Say I see a .Net class that is part of the source code of the game. Can I somehow write a mod which replaces that class implementation with the one provided inside a mod?

I've seen methods which enable overriding formulas, I've also managed to replace one magic effect with another, but in both cases I've just called a public method of a DFU class to accomplish that, meaning this option to inject my own stuff was first implemented by the DFU devs.

Is this the only way we can change the game functionality currently?

Let's say I want to rewrite how spell absorb works. I want to change of the EntityEffectManager.AssignBundle() and TryAbsorption() methods work. Could I just make a new EntityEffectManager class and somehow override the old one? If I keep the same interfaces it should work, right?

User avatar
Kab the Bird Ranger
Posts: 123
Joined: Tue Feb 23, 2021 12:30 am

Re: Overriding source code of game inside a mod

Post by Kab the Bird Ranger »

As someone who's contributed a lot of modding support to DFU this year, I understand what you mean. DFU is a project for recreating TES2 first, and a TES2 with mod support second, so there's areas still lacking in mod support.

I don't think there's a reliable, stable way to just straight up replace any class in the code. You'd have to look at what hackers do to replace vtables and whatnot.

Magic effects and UI windows are notable examples of classes that DFU will let you entirely replace. For magic effects, simply registering a magic effect with the same "effect key" will replace the default implementation. The issue for spell absorption is that spell absorption logic is not driven by the spell absorption effect itself - as you've pointed out, it's in the effect manager, which is not moddable.

I hope this is something we can tackle after the 1.0 release, but otherwise, you might be limited on that front if you want to make those changes through a DFU mod.

User avatar
DunnyOfPenwick
Posts: 275
Joined: Wed Apr 14, 2021 1:58 am
Location: Southeast US

Re: Overriding source code of game inside a mod

Post by DunnyOfPenwick »

Since EntityEffectManager is a GameObject component, you could potentially destroy an existing one and replace it with your own override class.

However, this can't generally be done for entities that have already been created because other classes might have a 'hard-link' to the EntityEffectManager. The EnemyMotor component in this case has code in its Start() method to get and store the EntityEffectManager in a member variable.

It would be good if these 'hard-links' could be replaced with soft-links instead, but that would require a significant amount of coding, and as Kab noted, the code is in a feature-lock state right now on the way to version 1.

It might be possible to swap out the EntityEffectManager if you are still in the process of creating the entity.

kiskoller
Posts: 36
Joined: Fri Sep 27, 2019 11:13 am

Re: Overriding source code of game inside a mod

Post by kiskoller »

Yeah I perfectly understand that DFU is vanilla-first, then we can look into modding. I guess I'll focus on other stuff that can be modded while waiting for 1.0 release.

User avatar
Hazelnut
Posts: 3015
Joined: Sat Aug 26, 2017 2:46 pm
Contact:

Re: Overriding source code of game inside a mod

Post by Hazelnut »

You should take a look at this thread, although I noticed that it's not been updated for a while and the author has not visited the forums this year: viewtopic.php?f=14&t=4213

I posted my opinion in that thread: "This is great for stuff buried deep that is difficult to expose for modding, but I hope people will continue to work towards modding interfaces where it makes sense - even though it can require some patience."

I think this kind of code patching should be the last resort, with the existing modding interfaces and systems being preferred if possible. However there are core parts of DFU that likely won't ever have modding interfaces so programmers who really know what they're doing could change the internals using this method. Hopefully mod systems will continue to be added post v1.0 release, but this is an option for areas where that's not practical.

Definitely worth checking use cases with the community first though, in case you missed a mod system capability.
See my mod code for examples of how to change various aspects of DFU: https://github.com/ajrb/dfunity-mods

kiskoller
Posts: 36
Joined: Fri Sep 27, 2019 11:13 am

Re: Overriding source code of game inside a mod

Post by kiskoller »

I've checked the post, seems interesting but really unstable as well. I agree with you Hazelnut, exposing interfaces by hand should be the way to go forward. Thanks everyone for the insight!

l3lessed
Posts: 1399
Joined: Mon Aug 12, 2019 4:32 pm
Contact:

Re: Overriding source code of game inside a mod

Post by l3lessed »

You can do the dll approach. It isn't unstable, but it requires knowing and properly importing all DFU libraries for compiling and ensuring you maintain all base properties for base engine to run error free. I built and got a dll running in an afternoon. I choose not to do it though because I was able to find a approach using all the built in mod interfaces and creating what I needed from code, which was easier than rebuilding from a scratch dll.
My Daggerfall Mod Github: l3lessed DFU Mod Github

My Beth Mods: l3lessed Nexus Page

Daggerfall Unity mods: Combat Overhaul Mod

Enjoy the free work I'm doing? Consider lending your support.

Post Reply