Zetrith / Prepatcher

Structured assembly rewriting library/mod for RW
MIT License
74 stars 7 forks source link

Private ValueInitializer methods (in some situations?) cause exceptions while wiki gives it as an example #21

Open SokyranTheDragon opened 7 months ago

SokyranTheDragon commented 7 months ago

The wiki gives an example of a private ValueInitializer method: https://github.com/Zetrith/Prepatcher/wiki/Adding-fields#default-values

However, doing that seems to cause exceptions (or at least in specific situations). The wiki should likely be updated to give a warning/change the example.

Code that causes issues:

public static class PawnDataUtility
{
    private static PawnDrawData MakeBlankDrawData() => new();

    [PrepatcherField]
    [ValueInitializer(nameof(MakeBlankDrawData))]
    public static ref PawnDrawData GetData(this Pawn key) => ref PrepatcherInactiveFallback(key);
}

Exception:

Error while generating pawn. Rethrowing. Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MethodAccessException: Method `YayoAnimation.Data.PawnDataUtility.MakeBlankDrawData()' is inaccessible from method `Verse.Pawn..ctor()'
  at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_throw_method_access(intptr,intptr)
  at Verse.Pawn..ctor () [0x0001c] in <8b65cb0a4c2c41c5b3fbd3a92fbde076>:0 
  at (wrapper managed-to-native) System.Reflection.MonoCMethod.InternalInvoke(System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in <eae584ce26bc40229c1b1aa476bfa589>:0 
   --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00014] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x000a8] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00009] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00027] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00020] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at System.Activator.CreateInstance (System.Type type) [0x00000] in <eae584ce26bc40229c1b1aa476bfa589>:0 
  at Verse.ThingMaker.MakeThing (Verse.ThingDef def, Verse.ThingDef stuff) [0x000a9] in <8b65cb0a4c2c41c5b3fbd3a92fbde076>:0 
  at Verse.PawnGenerator.TryGenerateNewPawnInternal (Verse.PawnGenerationRequest& request, System.String& error, System.Boolean ignoreScenarioRequirements, System.Boolean ignoreValidator) [0x00010] in <8b65cb0a4c2c41c5b3fbd3a92fbde076>:0 
  at Verse.PawnGenerator.GenerateNewPawnInternal (Verse.PawnGenerationRequest& request) [0x00094] in <8b65cb0a4c2c41c5b3fbd3a92fbde076>:0 
  at Verse.PawnGenerator.GenerateOrRedressPawnInternal (Verse.PawnGenerationRequest request) [0x00136] in <8b65cb0a4c2c41c5b3fbd3a92fbde076>:0 
  at Verse.PawnGenerator.GeneratePawn (Verse.PawnGenerationRequest request) [0x00007] in <8b65cb0a4c2c41c5b3fbd3a92fbde076>:0 
UnityEngine.StackTraceUtility:ExtractStackTrace ()
Verse.Log:Error (string)
Verse.PawnGenerator:GeneratePawn (Verse.PawnGenerationRequest)
RimWorld.Faction:TryGenerateNewLeader ()
RimWorld.FactionGenerator:NewGeneratedFaction (RimWorld.FactionGeneratorParms)
RimWorld.FactionGenerator:AddFactionToManager (RimWorld.FactionDef)
RimWorld.FactionGenerator:GenerateFactionsIntoWorld (System.Collections.Generic.List`1<RimWorld.FactionDef>)
RimWorld.Planet.WorldGenStep_Factions:GenerateFresh (string)
RimWorld.Planet.WorldGenerator:GenerateWorld (single,string,RimWorld.Planet.OverallRainfall,RimWorld.Planet.OverallTemperature,RimWorld.Planet.OverallPopulation,System.Collections.Generic.List`1<RimWorld.FactionDef>,single)
Verse.Root_Play:SetupForQuickTestPlay ()
RimWorld.MainMenuDrawer/<>c:<DoMainMenuControls>b__22_10 ()
Verse.LongEventHandler:RunEventFromAnotherThread (System.Action)
Verse.LongEventHandler/<>c:<UpdateCurrentAsynchronousEvent>b__28_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 ()