KSP-ModularManagement / KSPe

Extensions and utilities for Kerbal Space Program
http://ksp.lisias.net/add-ons/KSPAPIExtensions
Other
11 stars 6 forks source link

get_dataPath is not allowed to be called from a MonoBehaviour constructor #58

Closed JonnyOThan closed 1 year ago

JonnyOThan commented 1 year ago

Noticed this when running with the debug unity libraries (instructions here: https://gist.github.com/gotmachine/d973adcb9ae413386291170fa346d043 )

[EXC 09:14:29.099] UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'Startup' on game object 'Startup'.
See "Script Serialization" page in the Unity Manual for further details.
    KSPUtil.get_ApplicationRootPath () (at C:/Users/Jon/source/repos/ksp-assembly-csharp/KSPUtil.cs:821)
    KSPe.Globals.Init () (at <b2935f7fa35446edace79abf00804e95>:0)
    KSPe.Globals`1[T].Get (System.Type type) (at <b2935f7fa35446edace79abf00804e95>:0)
    KSPe.Globals`1[T].get_Log () (at <b2935f7fa35446edace79abf00804e95>:0)
    KSPe.Util.Log.Logger.CreateForType[T] (System.String forceThisNamespace, System.Boolean useClassNameToo, System.Int32 skipStackLevels) (at <b2935f7fa35446edace79abf00804e95>:0)
    KSPe.Startup..cctor () (at <b2935f7fa35446edace79abf00804e95>:0)
    Rethrow as TypeInitializationException: The type initializer for 'KSPe.Startup' threw an exception.
Lisias commented 1 year ago

Hummm…

Oukey, so this is a problem that is detected only by the Debug libraries, right? There must be an explanation to this not happening normally, otherwise I would had fixed or reimplemented the thing already.

I will work on this ASAP, thank you for the report!

Lisias commented 1 year ago

Just to note: "stock" KSP 1.12.5 doesn't throws the Exception:

[LOG 14:03:10.190] [AddonLoader]: Instantiating addon 'Startup' from assembly 'KSPe.InstallChecker'
[LOG 14:03:10.206] [KSPe.InstallChecker] CKAN was detected on this KSP instalment.
[LOG 14:03:10.207] [AddonLoader]: Instantiating addon 'Startup' from assembly 'KSPe'
[LOG 14:03:10.282] [KSPe.Globals] INFO: Globals: Default { DebugMode:False Log:{ Level:TRACE ThreadSafe:False } }
[LOG 14:03:10.282] [KSPe.Globals] INFO: Globals: ModuleManager { DebugMode:False Log:{ Level:INFO ThreadSafe:False } }
[LOG 14:03:10.283] [KSPe.Globals] INFO: Globals: KSP-Recall { DebugMode:False Log:{ Level:INFO ThreadSafe:False } }
[LOG 14:03:10.283] [KSPe.Globals] INFO: Globals: TweakScale { DebugMode:False Log:{ Level:TRACE ThreadSafe:False } }
[LOG 14:03:10.283] [KSPe.Globals] INFO: Globals: Nereid.SAVE { DebugMode:False Log:{ Level:TRACE ThreadSafe:True } }
[LOG 14:03:10.283] [KSPe.Globals] INFO: Globals: TweakScaleCompanion.Frameworks.Waterfall { DebugMode:False Log:{ Level:TRACE ThreadSafe:True } }
[LOG 14:03:10.283] [KSPe.Globals] INFO: KSPe.cfg loaded.
[LOG 14:03:10.291] [KSPe] DETAIL: KSPe is running from a Case Sensitive File System.
[LOG 14:03:18.220] [KSPe.Binder] Hooked.
[LOG 14:03:18.221] [KSPe] TRACE: Trying to load KSPe.Unity.2019...
[ERR 14:03:18.228] ADDON BINDER: Cannot resolve assembly: KSPe.Unity.2019

[LOG 14:03:18.229] [KSPe.Binder] Looking for KSPe.Unity.2019.dll on GameData/000_KSPe/Plugins/PluginData/...
[LOG 14:03:18.229] [KSPe.Binder] Found it on /Users/lisias/Workspaces/KSP/runtime/1.12.5/GameData/000_KSPe/Plugins/PluginData/KSPe.Unity.2019.dll.
[LOG 14:03:18.247] [KSPe.Unity2019] Version 2.5.3.3 /L
[LOG 14:03:18.256] [KSPe] TRACE: Trying to load KSPe.KSP.112...
[ERR 14:03:18.257] ADDON BINDER: Cannot resolve assembly: KSPe.KSP.112

Meno male. I'm not spamming logs on the userland.

Lisias commented 1 year ago

Reproduced using the DEBUG build of the UnityEngine on KSP 1.12.5:

[LOG 14:12:13.201] [AddonLoader]: Instantiating addon 'Startup' from assembly 'KSPe.InstallChecker'
[ERR 14:12:13.243] [KSPe.InstallChecker] ERROR: UnityEngine.UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializ
er), call it in Awake or Start instead. Called from MonoBehaviour 'Startup' on game object 'Startup'.
See "Script Serialization" page in the Unity Manual for further details.
  at (wrapper managed-to-native) UnityEngine.Application.get_dataPath()
  at KSPUtil.get_ApplicationRootPath () [0x00031] in <106570632fc343a784fad39e75e877bf>:0
  at KSPe.InstallChecker.Checker.CheckMyself () [0x00000] in <65682bb048264d788e5765d12084ab0e>:0
  at KSPe.InstallChecker.Checker.CheckKSPe () [0x00000] in <65682bb048264d788e5765d12084ab0e>:0
        UnityEngine.Debug:LogErrorFormat(String, Object[])
        KSPe.Log:error(String, Object[])
        KSPe.InstallChecker.Checker:CheckKSPe()
        KSPe.InstallChecker.ModuleInitializer:Initialize()
        <Module>:.cctor()
        UnityEngine.GameObject:AddComponent(Type)
        AddonLoader:StartAddon(LoadedAssembly, Type, KSPAddon, Startup)
        AddonLoader:StartAddons(Startup)
        <LoadObjects>d__90:MoveNext()
        UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
        <CreateDatabase>d__71:MoveNext()
        UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
        GameDatabase:StartLoad()
        <LoadSystems>d__11:MoveNext()
        UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
        LoadingScreen:Start()
Lisias commented 1 year ago

Implemented on commits:

Still need to check this on Windows and Linux before closing it… Just in case.