From cc55ebb28fe32183d675d60ace27e81f69b7a910 Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Thu, 29 Dec 2022 21:11:38 +0100 Subject: [PATCH] Add a suffix of a stable hash of the original filename to texture paths. --- Penumbra/Mods/ItemSwap/CustomizationSwap.cs | 1 + Penumbra/Mods/ItemSwap/ItemSwap.cs | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/Penumbra/Mods/ItemSwap/CustomizationSwap.cs b/Penumbra/Mods/ItemSwap/CustomizationSwap.cs index 46b85f00..e442ae8e 100644 --- a/Penumbra/Mods/ItemSwap/CustomizationSwap.cs +++ b/Penumbra/Mods/ItemSwap/CustomizationSwap.cs @@ -144,6 +144,7 @@ public static class CustomizationSwap var newPath = ReplaceAnyRace( path, race ); newPath = ReplaceAnyBody( newPath, slot, idFrom ); + newPath = AddSuffix( newPath, ".tex", $"_{Path.GetFileName(texture.Path).GetStableHashCode():x8}", true ); if( newPath != path ) { texture.Path = addedDashes ? newPath.Replace( "--", string.Empty ) : newPath; diff --git a/Penumbra/Mods/ItemSwap/ItemSwap.cs b/Penumbra/Mods/ItemSwap/ItemSwap.cs index 2e6585c3..3771cd6d 100644 --- a/Penumbra/Mods/ItemSwap/ItemSwap.cs +++ b/Penumbra/Mods/ItemSwap/ItemSwap.cs @@ -109,4 +109,26 @@ public static class ItemSwap file = null; return false; } + + public static int GetStableHashCode( this string str ) + { + unchecked + { + var hash1 = 5381; + var hash2 = hash1; + + for( var i = 0; i < str.Length && str[ i ] != '\0'; i += 2 ) + { + hash1 = ( ( hash1 << 5 ) + hash1 ) ^ str[ i ]; + if( i == str.Length - 1 || str[ i + 1 ] == '\0' ) + { + break; + } + + hash2 = ( ( hash2 << 5 ) + hash2 ) ^ str[ i + 1 ]; + } + + return hash1 + hash2 * 1566083941; + } + } } \ No newline at end of file