Jump to content

AlgaeHabitatConfig can't be modded (simple fix included)


Nightinggale
  • Branch: Preview Branch Version: Windows Fixed

Problem: Any attempt at modding AlgaeHabitatConfig will result in a black hole eating the game while the game is starting up.

Solution: Alter the offending code to not use ElementLoader, like

public static List<Tag> pollutedWaterFilter = new List<Tag>	{ SimHashes.DirtyWater.CreateTag()	};

 

Explanation of the problem

It turns out that the issue is the Harmony attachment code, not the modded code. All it takes to crash is this:

    [HarmonyPatch(typeof(AlgaeHabitatConfig))]
    [HarmonyPatch("ConfigureBuildingTemplate")]
    public static class PatchA_AlgaeHabitatConfig
    {
        public static void Postfix()
        {
        }
    }

I automated a test and this piece of code works with all 247 out of 248 building config classes. Something is special about AlgaeHabitatConfig. Also it doesn't matter which method I attach to, they all causes the game to crash.

I suspect that attaching will trigger generation of the static variables. In that case this would be the offending code:

	public static List<Tag> pollutedWaterFilter = new List<Tag>
	{
		ElementLoader.FindElementByHash(SimHashes.DirtyWater).tag
	};

Looking at the crash log below, the game crashes with a NULL reference issue in ElementLoader.FindElementByHash. If Harmony forces this line to execute earlier than it would normally and it triggers before ElementLoader is ready, then loading the tag for SimHashes.DirtyWater would be prone to cause a NULL issue. SimHashes.DirtyWater.CreateTag() will however not rely on ElementLoader should work regardless of when it's called.

The crash log (crashing part only)

Spoiler

NullReferenceException: Object reference not set to an instance of an object
  at ElementLoader.FindElementByHash (SimHashes hash) [0x0000b] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\game\Loaders\ElementLoader.cs:308 
  at AlgaeHabitatConfig..cctor () [0x0001a] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\Mods\AlgaeHabitatConfig.cs:54 
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for AlgaeHabitatConfig
  at (wrapper managed-to-native) System.RuntimeMethodHandle:GetFunctionPointer (intptr)
  at System.RuntimeMethodHandle.GetFunctionPointer () [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/RuntimeMethodHandle.cs:96 
  at Harmony.ILCopying.Memory.GetMethodStart (System.Reflection.MethodBase method, System.Exception& exception) [0x00000] in <filename unknown>:0 
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0012c] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:519 
  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:528 
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x0000e] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:77 
  at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x000b5] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:372 
  at System.Activator.CreateInstance (System.Type type) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:254 
  at GeneratedBuildings.LoadGeneratedBuildings (System.Collections.Generic.List`1 types) [0x0008c] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\Mods\GeneratedBuildings.cs:32 
  at LegacyModMain.LoadBuildings (System.Collections.Generic.List`1 types) [0x00052] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\Mods\LegacyModMain.cs:141 
  at LegacyModMain.Load () [0x00057] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\Mods\LegacyModMain.cs:26 
  at Assets.CreatePrefabs () [0x00001] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\game\Assets.cs:234 
  at Assets.OnPrefabInit () [0x00350] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\game\Assets.cs:229 
  at KMonoBehaviour.InitializeComponent () [0x00082] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\Plugins\Klei\util\KMonoBehaviour.cs:132 
Rethrow as Exception: Error in GameAssets(Clone).Assets.OnPrefabInit
  at KMonoBehaviour.InitializeComponent () [0x000cc] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\Plugins\Klei\util\KMonoBehaviour.cs:138 
  at KMonoBehaviour.Awake () [0x00011] in D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\Plugins\Klei\util\KMonoBehaviour.cs:44 
UnityEngine.Object:Internal_InstantiateSingleWithParent_Injected(Object, Transform, Vector3&, Quaternion&)
UnityEngine.Object:Internal_InstantiateSingleWithParent(Object, Transform, Vector3, Quaternion)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion, Transform)
UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion, Transform)
Util:KInstantiate(GameObject, Vector3, Quaternion, GameObject, String, Boolean, Int32) (at D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\Plugins\Klei\util\Util.cs:222)
Util:KInstantiate(GameObject, GameObject, String) (at D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\Plugins\Klei\util\Util.cs:194)
LaunchInitializer:Update() (at D:\JenkinsWorkspace\Preview_Simgame_Windows\game\Assets\scripts\game\LaunchInitializer.cs:52)
 
(Filename: /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/RuntimeMethodHandle.cs Line: 96)

 

 


Steps to Reproduce
Use Harmony to attach to any AlgaeHabitatConfig method. Watch the game disappear into a black hole prior to reaching the main menu.
  • Like 1



User Feedback


A developer has marked this issue as fixed. This means that the issue has been addressed in the current development build and will likely be in the next update.

Updated the fix after finding CreateTag(). It appears to be THE solution for any setup where a Tag for SimHashes is needed, including this case.

Share this comment


Link to comment
Share on other sites

Changed Status to Fixed

Thanks for the details! It wouldn't surprise me if there's other statically initialized stuff like this lurking about, ping me if you bump into any others!

  • Like 1
  • Thanks 2

Share this comment


Link to comment
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
  • Create New...