Skip to content

Commit ea0dbaa

Browse files
committed
1.39.1 hotfix release
The caching mechansims for enum descriptions now uses the string name of the enum member instead of the underlying integer value. This is necessary to propely support enums where different members share the same value. It does make the cache a bit less performant as it now involve a ToString() call on the enum value and a string hashing, but this is still much better than the stock implementation.
1 parent cad76d3 commit ea0dbaa

File tree

5 files changed

+12
-37
lines changed

5 files changed

+12
-37
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### Changelog
22

3+
##### 1.39.1
4+
**Bug fixes**
5+
- **FastAndFixedEnumExtensions** : fixed the caching mechanism erroring out on enums containing multiple members using the same underlying value. Was causing various issues in RO/RP1 due to such an enum being defind here.
6+
37
##### 1.39.0
48
**New/improved patches**
59
- New performance patch : [**FasterEditorPartList**](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/326) Improve the responsiveness of the part list when switching between categories, sorting and searching by tag. Adress [issue #242](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/242) reported by @Rodg88.

GameData/KSPCommunityFixes/KSPCommunityFixes.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"NAME": "KSPCommunityFixes",
33
"URL": "https://raw.githubusercontent.com/KSPModdingLibs/KSPCommunityFixes/master/GameData/KSPCommunityFixes/KSPCommunityFixes.version",
44
"DOWNLOAD": "https://github.com/KSPModdingLibs/KSPCommunityFixes/releases",
5-
"VERSION": {"MAJOR": 1, "MINOR": 39, "PATCH": 0, "BUILD": 0},
5+
"VERSION": {"MAJOR": 1, "MINOR": 39, "PATCH": 1, "BUILD": 0},
66
"KSP_VERSION": {"MAJOR": 1, "MINOR": 12, "PATCH": 5},
77
"KSP_VERSION_MIN": {"MAJOR": 1, "MINOR": 8, "PATCH": 0},
88
"KSP_VERSION_MAX": {"MAJOR": 1, "MINOR": 12, "PATCH": 5}

KSPCommunityFixes/BugFixes/FastAndFixedEnumExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace KSPCommunityFixes
88
{
99
internal class FastAndFixedEnumExtensions : BasePatch
1010
{
11-
internal static Dictionary<Type, Dictionary<long, EnumMemberDescription>> enumMemberDescriptionCache = new Dictionary<Type, Dictionary<long, EnumMemberDescription>>();
11+
internal static Dictionary<Type, Dictionary<string, EnumMemberDescription>> enumMemberDescriptionCache = new Dictionary<Type, Dictionary<string, EnumMemberDescription>>();
1212

1313
internal class EnumMemberDescription
1414
{
@@ -31,9 +31,9 @@ protected override void ApplyPatches()
3131
internal static bool TryGetEnumMemberDescription(Enum enumValue, out EnumMemberDescription enumMemberDescription)
3232
{
3333
Type enumType = enumValue.GetType();
34-
if (!enumMemberDescriptionCache.TryGetValue(enumType, out Dictionary<long, EnumMemberDescription> enumDescriptions))
34+
if (!enumMemberDescriptionCache.TryGetValue(enumType, out Dictionary<string, EnumMemberDescription> enumDescriptions))
3535
{
36-
enumDescriptions = new Dictionary<long, EnumMemberDescription>();
36+
enumDescriptions = new Dictionary<string, EnumMemberDescription>();
3737
string[] names = enumType.GetEnumNames();
3838

3939
foreach (string enumMemberName in names)
@@ -44,13 +44,13 @@ internal static bool TryGetEnumMemberDescription(Enum enumValue, out EnumMemberD
4444

4545
DescriptionAttribute descriptionAttribute = enumMembers[0].GetCustomAttribute<DescriptionAttribute>();
4646
Enum enumMember = (Enum)Enum.Parse(enumType, enumMemberName);
47-
enumDescriptions.Add(enumMember.GetSignedBoxedEnumValue(), new EnumMemberDescription(enumMemberName, descriptionAttribute));
47+
enumDescriptions.Add(enumMemberName, new EnumMemberDescription(enumMemberName, descriptionAttribute));
4848
}
4949

5050
enumMemberDescriptionCache.Add(enumType, enumDescriptions);
5151
}
5252

53-
if (enumDescriptions.TryGetValue(enumValue.GetSignedBoxedEnumValue(), out enumMemberDescription))
53+
if (enumDescriptions.TryGetValue(enumValue.ToString(), out enumMemberDescription))
5454
return true;
5555

5656
return false;

KSPCommunityFixes/Library/Extensions.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,6 @@ public static bool IsPAWOpen(this Part part)
2929
{
3030
return part.PartActionWindow.IsNotNullOrDestroyed() && part.PartActionWindow.isActiveAndEnabled;
3131
}
32-
33-
/// <summary>
34-
/// Get the value of a boxed enum, as a 64 bit signed integer.
35-
/// If the enum underlying type is an unsigned 64 bit integer and the value is greater than long.MaxValue,
36-
/// the result will overflow and be negative.
37-
/// </summary>
38-
public static long GetSignedBoxedEnumValue(this Enum enumInstance)
39-
{
40-
Type underlyingType = enumInstance.GetType().GetEnumUnderlyingType();
41-
42-
if (underlyingType == typeof(int))
43-
return (int)(object)enumInstance;
44-
else if (underlyingType == typeof(sbyte))
45-
return (sbyte)(object)enumInstance;
46-
else if (underlyingType == typeof(short))
47-
return (short)(object)enumInstance;
48-
else if (underlyingType == typeof(long))
49-
return (long)(object)enumInstance;
50-
else if (underlyingType == typeof(uint))
51-
return (uint)(object)enumInstance;
52-
else if (underlyingType == typeof(byte))
53-
return (byte)(object)enumInstance;
54-
else if (underlyingType == typeof(ushort))
55-
return (ushort)(object)enumInstance;
56-
else if (underlyingType == typeof(ulong))
57-
return (long)(ulong)(object)enumInstance;
58-
59-
throw new Exception($"Enum {enumInstance.GetType()} is of unknown size");
60-
}
6132
}
6233

6334
static class ParticleBuffer

KSPCommunityFixes/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
// Revision
3131
//
3232
[assembly: AssemblyVersion("1.37.3.0")]
33-
[assembly: AssemblyFileVersion("1.39.0.0")]
33+
[assembly: AssemblyFileVersion("1.39.1.0")]
3434

35-
[assembly: KSPAssembly("KSPCommunityFixes", 1, 39, 0)]
35+
[assembly: KSPAssembly("KSPCommunityFixes", 1, 39, 1)]
3636
[assembly: KSPAssemblyDependency("MultipleModulePartAPI", 1, 0, 0)]
3737
[assembly: KSPAssemblyDependency("ModuleManager", 1, 0)]
3838
[assembly: KSPAssemblyDependency("HarmonyKSP", 1, 0)]

0 commit comments

Comments
 (0)