Skip to content

Commit 3bcdb67

Browse files
committed
Better handling of invalid data
1 parent 42ec3bd commit 3bcdb67

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

Core_SkinEffects/SkinEffectKind.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace KK_SkinEffects
88
/// <summary>
99
/// List of all effect texture kinds. The values and names are important and should not be changed.
1010
/// Names are used for resource lookup, effect textures should have the same name as the enum value + a number that represents the level (e.g. WetBody_01).
11-
/// Values are used for indexing and should not be changed or some saved data might become misaligned. Values must be sequential starting from 0.
11+
/// Values are used for indexing and should not be changed or some saved data might become misaligned. Values must be sequential starting from 0. Higher value effect texture is overlayed above lower ones.
1212
/// Description is used only for display names and can be changed.
1313
/// AffectsBody / AffectsFace attributes are used to determine where the effect should be applied, only one can be used.
1414
/// </summary>
@@ -50,5 +50,7 @@ static SkinEffectKindUtils()
5050
public static string ToDataKey(this SkinEffectKind kind) => ((int)kind).ToDataKey();
5151
public static string ToDataKey(this int id) => id.ToString("D", CultureInfo.InvariantCulture);
5252
public static string GetDisplayName(this SkinEffectKind kind) => kind.GetAttributeOfType<DescriptionAttribute>()?.Description ?? kind.ToString();
53+
54+
public static bool IsValidKind(this SkinEffectKind kind) => kind >= 0 && (int)kind < ValidSkinEffectKinds.Length;
5355
}
5456
}

Core_SkinEffects/SkinEffectsController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public class SkinEffectsController : CharaCustomFunctionController
3939
/// </summary>
4040
public int GetEffectLevel(SkinEffectKind kind)
4141
{
42-
// todo throw instead?
43-
if (kind < 0 || (int)kind >= SkinEffectKindUtils.ValidSkinEffectKinds.Length) return -1;
42+
if (!kind.IsValidKind())
43+
throw new ArgumentException($"Invalid effect kind: {kind}", nameof(kind));
44+
4445
return _effectLevels[(int)kind];
4546
}
4647

@@ -49,8 +50,8 @@ public int GetEffectLevel(SkinEffectKind kind)
4950
/// </summary>
5051
public bool SetEffectLevel(SkinEffectKind kind, int level, bool updateEffects)
5152
{
52-
// todo throw instead?
53-
if (kind < 0 || (int)kind >= SkinEffectKindUtils.ValidSkinEffectKinds.Length) return false;
53+
if (!kind.IsValidKind())
54+
return false;
5455

5556
level = Mathf.Clamp(level, 0, TextureLoader.GetTextureCount(kind));
5657

@@ -352,6 +353,7 @@ public void ApplyCharaState(IDictionary<string, object> dataDict, bool onlyCusto
352353
if (dataDict != null && dataDict.Count > 0)
353354
{
354355
var newLevels = ReadEffectLevelsFromData(dataDict);
356+
TextureLoader.ClampEffectLevelArray(newLevels);
355357
if (newLevels.Any(x => x > 0)) needsUpdate = true;
356358
_effectLevels = newLevels;
357359

Core_SkinEffects/TextureLoader.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ static TextureLoader()
4343

4444
public static int GetTextureCount(SkinEffectKind kind)
4545
{
46-
if (kind < 0)
46+
if (kind < 0 || (int)kind >= _resources.Length)
47+
{
48+
SkinEffectsPlugin.Logger.LogError($"Invalid effect kind: {kind}\n{new StackTrace()}");
4749
return 0;
50+
}
4851

4952
return _resources[(int)kind].Length;
5053
}
@@ -62,6 +65,9 @@ private static Texture2D[] GetTextures(string[] resourceNames)
6265

6366
public static Texture2D[] GetTextures(SkinEffectKind kind)
6467
{
68+
if (kind < 0 || (int)kind >= _resources.Length)
69+
throw new ArgumentException($"Invalid effect kind: {kind}", nameof(kind));
70+
6571
if (_textures[(int)kind] == null)
6672
{
6773
var newTextures = GetTextures(_resources[(int)kind]);
@@ -78,9 +84,12 @@ public static Texture2D[] GetTextures(SkinEffectKind kind)
7884
public static Texture2D GetTexture(SkinEffectKind kind, int level)
7985
{
8086
var textures = GetTextures(kind);
81-
if (level < 0 || level >= textures.Length)
82-
return null;
83-
return textures[level];
87+
if (level < 0)
88+
return textures[0];
89+
else if (level >= textures.Length)
90+
return textures.Last();
91+
else
92+
return textures[level];
8493
}
8594

8695
public static void PreloadAllTextures()
@@ -109,5 +118,14 @@ public static void PreloadMainGameTextures()
109118

110119
SkinEffectsPlugin.Logger.LogDebug($"{nameof(PreloadMainGameTextures)} finished in {sw.ElapsedMilliseconds}ms");
111120
}
121+
122+
public static void ClampEffectLevelArray(int[] effectLevels)
123+
{
124+
if (effectLevels.Length > _resources.Length)
125+
Array.Resize(ref effectLevels, _resources.Length);
126+
127+
for (int i = 0; i < effectLevels.Length; i++)
128+
effectLevels[i] = Mathf.Clamp(effectLevels[i], 0, _resources[i].Length);
129+
}
112130
}
113131
}

0 commit comments

Comments
 (0)