public static void Prefix(ref Thing __instance, Map map, bool respawningAfterLoad)
 {
     if (respawningAfterLoad)
     {
         Type           type     = __instance.GetType();
         ThingWithComps original = __instance as ThingWithComps;
         if (original != null)
         {
             try
             {
                 Pawn pawn = original as Pawn;
                 if (pawn != null)
                 {
                     //    if (AMAMod.Dev) Log.Message(string.Format("checking {0}", pawn.Name.ToStringFull ?? pawn.NameShortColored));
                     Pawn_EquipmentTracker equipmentTracker = pawn.equipment;
                     Pawn_ApparelTracker   apparelTracker   = pawn.apparel;
                     Pawn_InventoryTracker inventoryTracker = pawn.inventory;
                     if (equipmentTracker != null)
                     {
                         if (!equipmentTracker.AllEquipmentListForReading.NullOrEmpty())
                         {
                             //    if (AMAMod.Dev) Log.Message(string.Format("checking {0}'s equipment", pawn.NameShortColored));
                             for (int i = 0; i < equipmentTracker.AllEquipmentListForReading.Count; i++)
                             {
                                 if (ShouldUpdate(equipmentTracker.AllEquipmentListForReading[i]))
                                 {
                                     //    if (AMAMod.Dev) Log.Message(string.Format("repalce {0}'s equipment({1}) class", pawn.NameShortColored, equipmentTracker.AllEquipmentListForReading[i]));
                                     equipmentTracker.AllEquipmentListForReading[i] = ReplacedThing(equipmentTracker.AllEquipmentListForReading[i]) as ThingWithComps;
                                 }
                             }
                         }
                     }
                     if (apparelTracker != null)
                     {
                         if (!apparelTracker.WornApparel.NullOrEmpty())
                         {
                             //    if (AMAMod.Dev) Log.Message(string.Format("checking {0}'s apparel", pawn.NameShortColored));
                             for (int i = 0; i < apparelTracker.WornApparel.Count; i++)
                             {
                                 if (ShouldUpdate(apparelTracker.WornApparel[i]))
                                 {
                                     //   if (AMAMod.Dev) Log.Message(string.Format("repalce {0}'s apparel({1}) class", pawn.NameShortColored, apparelTracker.WornApparel[i]));
                                     apparelTracker.WornApparel[i] = ReplacedThing(apparelTracker.WornApparel[i]) as Apparel;
                                 }
                             }
                         }
                     }
                     if (inventoryTracker != null)
                     {
                         if (!inventoryTracker.GetDirectlyHeldThings().NullOrEmpty())
                         {
                             //    if (AMAMod.Dev) Log.Message(string.Format("checking {0}'s inventory", pawn.NameShortColored));
                             for (int i = inventoryTracker.GetDirectlyHeldThings().Count - 1; i > 0; i--)
                             {
                                 if (ShouldUpdate(inventoryTracker.GetDirectlyHeldThings()[i]))
                                 {
                                     //    if (AMAMod.Dev) Log.Message(string.Format("replace {0}'s inventory({1}) class", pawn.NameShortColored, inventoryTracker.GetDirectlyHeldThings()[i]));
                                     Thing replace = ReplacedThing(inventoryTracker.GetDirectlyHeldThings()[i] as ThingWithComps);
                                     inventoryTracker.GetDirectlyHeldThings().RemoveAt(i);
                                     inventoryTracker.GetDirectlyHeldThings().TryAdd(ReplacedThing(replace as ThingWithComps));
                                 }
                             }
                         }
                     }
                 }
                 else
                 {
                     if (ShouldUpdate(original))
                     {
                         __instance = ReplacedThing(original);
                     }
                 }
             }
             catch (Exception)
             {
                 if (AMAMod.Dev)
                 {
                     Log.Warning("Something went wrong trying to replace " + __instance.LabelCap + "'s ThingClass");
                 }
             }
             finally
             {
                 if (type != __instance.def.thingClass && type != typeof(Pawn))
                 {
                     if (AMAMod.Dev)
                     {
                         Log.Warning("Failed to replace " + __instance.LabelCap + "'s ThingClass(" + type.Name + ") to " + __instance.def.thingClass.Name);
                     }
                 }
             }
         }
     }
 }
 public static void Prefix(ref Thing __instance, Map map, bool respawningAfterLoad)
 {
     if (__instance?.def?.modContentPack != null && respawningAfterLoad)
     {
         ThingWithComps original = __instance as ThingWithComps;
         if (original != null)
         {
             try
             {
                 Pawn pawn = original as Pawn;
                 if (pawn != null)
                 {
                     Pawn_EquipmentTracker equipmentTracker = pawn.equipment;
                     Pawn_ApparelTracker   apparelTracker   = pawn.apparel;
                     Pawn_InventoryTracker inventoryTracker = pawn.inventory;
                     if (equipmentTracker != null)
                     {
                         if (!equipmentTracker.AllEquipmentListForReading.NullOrEmpty())
                         {
                             for (int i = 0; i < equipmentTracker.AllEquipmentListForReading.Count; i++)
                             {
                                 equipmentTracker.AllEquipmentListForReading[i] = ReplacedThing(equipmentTracker.AllEquipmentListForReading[i]) as ThingWithComps;
                             }
                         }
                     }
                     if (apparelTracker != null)
                     {
                         if (!apparelTracker.WornApparel.NullOrEmpty())
                         {
                             for (int i = 0; i < apparelTracker.WornApparel.Count; i++)
                             {
                                 apparelTracker.WornApparel[i] = ReplacedThing(apparelTracker.WornApparel[i]) as Apparel;
                             }
                         }
                     }
                     if (inventoryTracker != null)
                     {
                         if (!inventoryTracker.GetDirectlyHeldThings().NullOrEmpty())
                         {
                             for (int i = inventoryTracker.GetDirectlyHeldThings().Count - 1; i > 0; i--)
                             {
                                 Thing replace = ReplacedThing(inventoryTracker.GetDirectlyHeldThings()[i] as ThingWithComps);
                                 inventoryTracker.GetDirectlyHeldThings().RemoveAt(i);
                                 inventoryTracker.GetDirectlyHeldThings().TryAdd(ReplacedThing(replace as ThingWithComps));
                             }
                         }
                     }
                 }
                 else
                 {
                     __instance = ReplacedThing(original);
                 }
             }
             catch (Exception)
             {
                 Log.Warning("Something went wrong trying to replace " + __instance.LabelCap + "'s ThingClass");
             }
             finally
             {
                 if (__instance.GetType() != __instance.def.thingClass)
                 {
                     Log.Warning("Failed to replace " + __instance.LabelCap + "'s ThingClass");
                 }
             }
         }
     }
 }