Skip to content

Commit

Permalink
fix patches & null skin application
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Jun 30, 2023
1 parent 9b5c8e5 commit 2cda7da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
31 changes: 12 additions & 19 deletions SkinManagerMod/CarPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,33 @@

namespace SkinManagerMod
{
[HarmonyPatch(typeof(CarSpawner), "SpawnCar")]
[HarmonyPatch(typeof(CarSpawner))]
class CarSpawner_SpawnCar_Patch
{
static void Postfix(TrainCar __result)
[HarmonyPostfix]
[HarmonyPatch(nameof(CarSpawner.SpawnCar))]
[HarmonyPatch(nameof(CarSpawner.SpawnLoadedCar))]
static void SpawnCar(TrainCar __result)
{
var skin = SkinManager.GetCurrentCarSkin(__result);
if (!skin.IsDefault)
if ((skin != null) && !skin.IsDefault)
{
// only need to replace textures if not staying with default skin
SkinManager.ApplySkin(__result, skin);
}
}
}

[HarmonyPatch(typeof(CarSpawner), "SpawnLoadedCar")]
class CarSpawner_SpawnExistingCar_Patch
{
static void Postfix(TrainCar __result)
{
var skin = SkinManager.GetCurrentCarSkin(__result);
if (!skin.IsDefault)
{
SkinManager.ApplySkin(__result, skin);
}
}
}

[HarmonyPatch(typeof(TrainCar), "LoadInterior")]
[HarmonyPatch(typeof(TrainCar))]
class TrainCar_LoadInterior_Patch
{
static void Postfix(TrainCar __instance)
[HarmonyPostfix]
[HarmonyPatch(nameof(TrainCar.LoadInterior))]
[HarmonyPatch(nameof(TrainCar.LoadExternalInteractables))]
static void LoadInterior(TrainCar __instance)
{
var skin = SkinManager.GetCurrentCarSkin(__instance);
if (!skin.IsDefault)
if ((skin != null) && !skin.IsDefault)
{
SkinManager.ApplySkinToInterior(__instance, skin);
}
Expand Down
16 changes: 12 additions & 4 deletions SkinManagerMod/SkinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ public static Skin GetNewSkin(TrainCarLivery carType)
}

// fall back to default skin
return defaultSkins[carType.id];
if (defaultSkins.TryGetValue(carType.id, out Skin skin))
{
return skin;
}
return null;
}

/// <summary>Get the currently assigned skin for given car, or a new one if none is assigned</summary>
Expand Down Expand Up @@ -221,11 +225,15 @@ public static void ReloadSkins(TrainCarLivery livery)
foreach (var car in carsInScene)
{
var toApply = GetCurrentCarSkin(car);
ApplySkin(car, toApply);

if (car.IsInteriorLoaded)
if (toApply != null)
{
ApplySkinToInterior(car, toApply);
ApplySkin(car, toApply);

if (car.IsInteriorLoaded)
{
ApplySkinToInterior(car, toApply);
}
}
}
}
Expand Down

0 comments on commit 2cda7da

Please sign in to comment.