Skip to content

Commit

Permalink
Merge pull request #24 from derail-valley-modding/b96
Browse files Browse the repository at this point in the history
fix bug in skin reapplication after load
  • Loading branch information
katycat5e authored Aug 20, 2023
2 parents ca8a0a7 + 051b810 commit 10c04de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
5 changes: 4 additions & 1 deletion SkinManagerMod/SkinManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static void Initialize()

private static void ReapplySkinToUsers(SkinConfig skinConfig)
{
if (!CarSpawner.Instance) return;

foreach (var car in CarSpawner.Instance.AllCars.Where(tc => tc.carLivery.id == skinConfig.CarId))
{
var toApply = GetCurrentCarSkin(car, false);
Expand All @@ -37,7 +39,8 @@ public static Skin GetCurrentCarSkin(TrainCar car, bool returnNewSkin = true)
{
if (carGuidToAppliedSkinMap.TryGetValue(car.CarGUID, out var skinName))
{
if (SkinProvider.TryGetDefaultSkin(car.carLivery.id, out Skin defaultSkin) && (skinName == defaultSkin.Name))
var defaultSkin = SkinProvider.GetDefaultSkin(car.carLivery.id);
if (skinName == defaultSkin.Name)
{
return defaultSkin;
}
Expand Down
24 changes: 15 additions & 9 deletions SkinManagerMod/SkinProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ private static void UnloadSkin(string liveryId, string skinName)

#region Provider Methods

public static bool TryGetDefaultSkin(string carId, out Skin skin) => defaultSkins.TryGetValue(carId, out skin);
public static Skin GetDefaultSkin(string carId) => defaultSkins[carId];
public static Skin GetDefaultSkin(string carId)
{
if (defaultSkins.TryGetValue(carId, out Skin existing))
{
return existing;
}

var newDefault = CreateDefaultSkin(Globals.G.Types.Liveries.First(l => l.id == carId));
defaultSkins[carId] = newDefault;
return newDefault;
}

public static Skin FindSkinByName(TrainCarLivery carType, string name) => FindSkinByName(carType.id, name);

Expand Down Expand Up @@ -241,7 +250,6 @@ private static void HandleSkinModToggle(UnityModManager.ModEntry mod, bool nowAc
if (nowActive)
{
ReloadSkinMod(mod, WorldStreamingInit.IsStreamingDone); // force synchronous if in-game
SkinsLoaded?.Invoke();
}
else
{
Expand Down Expand Up @@ -294,10 +302,8 @@ private static int ReloadSkinMod(UnityModManager.ModEntry mod, bool forceSync =
BeginLoadSkin(config, forceSync);

// check if not removed, but updated
if (removedSkins.Remove(config))
{
updatedSkins.Add(config);
}
removedSkins.Remove(config);
updatedSkins.Add(config);
}

skinConfigs.AddLast(newConfig);
Expand Down Expand Up @@ -343,7 +349,7 @@ private static Skin CreateDefaultSkin(TrainCarLivery carType)
// skinDir = CCLPatch.GetCarFolder(carType);
//}

var defaultSkin = new Skin($"Default_{carType.id}", skinDir, isDefault: true);
var defaultSkin = new Skin(carType.id, $"Default_{carType.id}", skinDir, isDefault: true);

foreach (var texture in TextureUtility.EnumerateTextures(carType))
{
Expand Down Expand Up @@ -398,7 +404,7 @@ internal static void BeginLoadSkin(SkinConfig config, bool forceSync = false)
Main.LogVerbose($"Async loading {config.Name} @ {config.FolderPath}");
}

var skin = new Skin(config.Name, config.FolderPath);
var skin = new Skin(config.CarId, config.Name, config.FolderPath);
config.Skin = skin;

// find correct group, remove existing skin
Expand Down
4 changes: 3 additions & 1 deletion SkinManagerMod/Skins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace SkinManagerMod
{
public class Skin
{
public readonly string LiveryId;
public readonly string Name;
public readonly string Path;
public readonly bool IsDefault;
public readonly List<SkinTexture> SkinTextures = new List<SkinTexture>();

public Skin(string name, string directory = null, bool isDefault = false)
public Skin(string liveryId, string name, string directory = null, bool isDefault = false)
{
LiveryId = liveryId;
Name = name;
Path = directory;
IsDefault = isDefault;
Expand Down

0 comments on commit 10c04de

Please sign in to comment.