Skip to content

Commit

Permalink
GetValue and SetValue now works for fields on parent classes
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingalek committed Dec 22, 2019
1 parent 14fa2d1 commit cc5036f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
12 changes: 9 additions & 3 deletions OWML.Events/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@ public static class TypeExtensions

public static MethodInfo GetAnyMethod(this Type type, string name)
{
return type.GetMethod(name, Flags);
return type.GetMethod(name, Flags) ??
type.BaseType?.GetMethod(name, Flags) ??
type.BaseType?.BaseType?.GetMethod(name, Flags);
}

public static PropertyInfo GetAnyProperty(this Type type, string name)
{
return type.GetProperty(name, Flags);
return type.GetProperty(name, Flags) ??
type.BaseType?.GetProperty(name, Flags) ??
type.BaseType?.BaseType?.GetProperty(name, Flags);
}

public static FieldInfo GetAnyField(this Type type, string name)
{
return type.GetField(name, Flags);
return type.GetField(name, Flags) ??
type.BaseType?.GetField(name, Flags) ??
type.BaseType?.BaseType?.GetField(name, Flags);
}

public static T GetValue<T>(this object obj, string name)
Expand Down
2 changes: 1 addition & 1 deletion OWML.Launcher/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OWML.Launcher
{
public class App
{
private const string OWMLVersion = "0.1.6";
private const string OWMLVersion = "0.1.7";

private readonly string[] _filesToCopy = { "UnityEngine.CoreModule.dll", "Assembly-CSharp.dll" };

Expand Down
2 changes: 1 addition & 1 deletion OWML.SampleMods/OWML.EnableDebugMode/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"name": "EnableDebugMode",
"uniqueName": "Alek.EnableDebugMode",
"version": "0.1",
"owmlVersion": "0.1.6",
"owmlVersion": "0.1.7",
"enabled": true
}
2 changes: 1 addition & 1 deletion OWML.SampleMods/OWML.TestMod/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"name": "TestMod",
"uniqueName": "Alek.TestMod",
"version": "0.1",
"owmlVersion": "0.1.6",
"owmlVersion": "0.1.7",
"enabled": false
}
27 changes: 20 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Note: ModHelper can not be used in Awake, it's not initialized at that time.

### Events

Start in your ModBehaviour will be called when the game starts (at the title menu), which is usually too early for what you want to do. The mod helper contains events we can use to know when certain behaviours start. Here we add an event for when Flashlight has loaded, which is after the player has "woken up":
Start in your ModBehaviour will be called when the game starts (at the title menu), which is usually too early for what you want to do. The mod helper contains events we can use to know when certain things happen. Here we add an event for when Flashlight has started, which is after the player has "woken up":

~~~~
private void Start()
Expand All @@ -97,12 +97,14 @@ private void OnEvent(MonoBehaviour behaviour, Events ev)

### Tips and tricks

#### Multiple MonoBehaviours

Your mod can contain more MonoBehaviors which can be added dynamically:
~~~~
AddComponent<SomeBehaviour>();
~~~~

Listen for inputs:
#### Listen for inputs
~~~~
private void Update()
{
Expand All @@ -113,19 +115,30 @@ private void Update()
}
~~~~

Decompile the game with [dnSpy](https://github.com/0xd4d/dnSpy). Open {gamePath}\OuterWilds_Data\Managed\Assembly-CSharp.dll in dnSpy to learn how the game works and find what you want to change.
#### Decompile the game

Use [dnSpy](https://github.com/0xd4d/dnSpy) to browse the game code and learn how the game ticks. Open {gamePath}\OuterWilds_Data\Managed\Assembly-CSharp.dll in dnSpy.

#### Reflection

Change private variables with [reflection](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection). Example:
~~~~
typeof(GUIMode).GetAnyField("_renderMode").SetValue(null, _renderValue);
~~~~

Modify existing game methods with [Harmony](https://github.com/pardeike/Harmony). The mod helper contains a wrapper for Harmony, making some of the functionality easy to use. See the source code of HarmonyHelper and ModEvents. As an example, here we remove the contents of DebugInputManagers Awake method which makes sure the debug mode isn't disabled:
OWML.Events.dll contains extension methods for easy reflection. Get and set private variables like this:

~~~~
ModHelper.HarmonyHelper.EmptyMethod<DebugInputManager>("Awake");
var foo = behaviour.GetValue<string>("_foo");
behaviour.SetValue<string>("_bar", foo);
~~~~

If you develop new functionality using Harmony, please consider working with me to expand the mod helper classes, to aid other modders as well.
#### Patch game methods

Modify existing game methods with [Harmony](https://github.com/pardeike/Harmony). The mod helper contains a wrapper for Harmony, making some of the functionality easy to use. See the source code of HarmonyHelper and ModEvents. Here we remove the contents of DebugInputManagers Awake method which makes sure the debug mode isn't disabled:
~~~~
ModHelper.HarmonyHelper.EmptyMethod<DebugInputManager>("Awake");
~~~~

### Manifest

Expand All @@ -138,7 +151,7 @@ Add a manifest file called manifest.json. Example:
"name": "EnableDebugMode",
"uniqueName": "Alek.EnableDebugMode",
"version": "0.1",
"owmlVersion": "0.1.6",
"owmlVersion": "0.1.7",
"enabled": true
}
~~~~
Expand Down

0 comments on commit cc5036f

Please sign in to comment.