legodude17 / VanillaUIExpanded

5 stars 3 forks source link

Fix for "Tried to get a resource <...> from a different thread" error #7

Closed mthiel closed 2 years ago

mthiel commented 2 years ago

Improved FixDesPanels compatibility with mods that may invoke this from the wrong thread.

Spotted this issue when testing with my full mod list.

legodude17 commented 2 years ago

I don't see why this would fix it. There shouldn't be any LongEvents running, so the code will just be run instantly in the same thread.

mthiel commented 2 years ago

I believe this change is obsolete now and should be closed. Thanks for taking a look!

For posterity: The stack-trace suggested to me that something else was calling MainTabWindow_Architect.CacheDesPanels in an unexpected way during loading. Running it through LongEventHandler seemed to have the side-effect of invoking on the main thread instead, although I can't explain why that would be the case.

Original issue stack trace ``` Tried to get a resource "UI/Designators/Cancel" from a different thread. All resources must be loaded in the main thread. UnityEngine.StackTraceUtility:ExtractStackTrace () (wrapper dynamic-method) Verse.Log:Verse.Log.Error_Patch2 (string) Verse.ContentFinder`1:Get (string,bool) RimWorld.Designator_Cancel:.ctor () System.Reflection.MonoCMethod:InternalInvoke (object,object[]) System.RuntimeType:CreateInstanceMono (bool) System.RuntimeType:CreateInstanceSlow (bool,bool,bool,System.Threading.StackCrawlMark&) System.RuntimeType:CreateInstanceDefaultCtor (bool,bool,bool,System.Threading.StackCrawlMark&) System.Activator:CreateInstance (System.Type,bool) System.Activator:CreateInstance (System.Type) VUIE.DesignatorSaved:Load (VUIE.DesignatorSaved) System.Linq.Enumerable/SelectListIterator`2:MoveNext () System.Linq.Enumerable/WhereEnumerableIterator`1:ToList () System.Linq.Enumerable:ToList (System.Collections.Generic.IEnumerable`1) VUIE.ArchitectLoadSaver/<>c:b__5_0 (VUIE.ArchitectTabSaved) System.Linq.Enumerable/SelectListIterator`2>:ToList () System.Linq.Enumerable:ToList> (System.Collections.Generic.IEnumerable`1>) VUIE.ArchitectLoadSaver:RestoreState (VUIE.ArchitectSaved,RimWorld.MainTabWindow_Architect) VUIE.ArchitectModule:FixDesPanels (RimWorld.MainTabWindow_Architect) (wrapper dynamic-method) RimWorld.MainTabWindow_Architect:RimWorld.MainTabWindow_Architect.CacheDesPanels_Patch1 (RimWorld.MainTabWindow_Architect) Rimefeller.RimefellerMod:Postfix () (wrapper dynamic-method) HugsLib.HugsLibController:HugsLib.HugsLibController.LoadReloadInitialize_Patch2 (HugsLib.HugsLibController) Verse.LongEventHandler:RunEventFromAnotherThread (System.Action) Verse.LongEventHandler/<>c:b__27_0 () System.Threading.ThreadHelper:ThreadStart_Context (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) System.Threading.ThreadHelper:ThreadStart () ```

With the recent improvements you made, it's now occurring while loading save a instead, but it's being successfully caught by your check. After that, opening the architect menu triggers RestoreState from the main thread and everything seems to work as intended.

Current stack-trace from warning ``` [VUIE] Attempted to generate designators while not in the main thread! UnityEngine.StackTraceUtility:ExtractStackTrace () Verse.Log:Warning (string) VUIE.ArchitectLoadSaver:EnsureCached (VUIE.ArchitectSaved) VUIE.ArchitectLoadSaver:RestoreState (VUIE.ArchitectSaved,RimWorld.MainTabWindow_Architect) VUIE.ArchitectModule:FixDesPanels (RimWorld.MainTabWindow_Architect) (wrapper dynamic-method) RimWorld.MainTabWindow_Architect:RimWorld.MainTabWindow_Architect.CacheDesPanels_Patch1 (RimWorld.MainTabWindow_Architect) Rimefeller.RimefellerMod:Postfix () (wrapper dynamic-method) HugsLib.HugsLibController:HugsLib.HugsLibController.LoadReloadInitialize_Patch2 (HugsLib.HugsLibController) Verse.LongEventHandler:RunEventFromAnotherThread (System.Action) Verse.LongEventHandler/<>c:b__27_0 () System.Threading.ThreadHelper:ThreadStart_Context (object) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) System.Threading.ThreadHelper:ThreadStart () ```