Use EnumUtils (#376)

This commit is contained in:
Nick 2022-10-03 21:51:24 -04:00 committed by GitHub
commit 8c0b73f869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 107 additions and 127 deletions

View File

@ -1,17 +1,20 @@
using OWML.Utils;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace NewHorizons.Builder.General namespace NewHorizons.Builder.General
{ {
public static class SectorBuilder public static class SectorBuilder
{ {
private static readonly Sector.Name Name = EnumUtils.Create<Sector.Name>("NewHorizons");
public static Sector Make(GameObject planetBody, OWRigidbody owRigidBody, float sphereOfInfluence) public static Sector Make(GameObject planetBody, OWRigidbody owRigidBody, float sphereOfInfluence)
{ {
GameObject sectorGO = new GameObject("Sector"); var sectorGO = new GameObject("Sector");
sectorGO.SetActive(false); sectorGO.SetActive(false);
sectorGO.transform.parent = planetBody.transform; sectorGO.transform.parent = planetBody.transform;
sectorGO.transform.localPosition = Vector3.zero; sectorGO.transform.localPosition = Vector3.zero;
SphereShape SS = sectorGO.AddComponent<SphereShape>(); var SS = sectorGO.AddComponent<SphereShape>();
SS.SetCollisionMode(Shape.CollisionMode.Volume); SS.SetCollisionMode(Shape.CollisionMode.Volume);
SS.SetLayer(Shape.Layer.Sector); SS.SetLayer(Shape.Layer.Sector);
SS.layerMask = -1; SS.layerMask = -1;
@ -21,8 +24,8 @@ namespace NewHorizons.Builder.General
sectorGO.AddComponent<OWTriggerVolume>(); sectorGO.AddComponent<OWTriggerVolume>();
Sector S = sectorGO.AddComponent<Sector>(); var S = sectorGO.AddComponent<Sector>();
S._name = (Sector.Name)24; S._name = Name;
S._attachedOWRigidbody = owRigidBody; S._attachedOWRigidbody = owRigidBody;
S._subsectors = new List<Sector>(); S._subsectors = new List<Sector>();
@ -36,12 +39,12 @@ namespace NewHorizons.Builder.General
{ {
if (parent == null) return null; if (parent == null) return null;
GameObject sectorGO = new GameObject("Sector"); var sectorGO = new GameObject("Sector");
sectorGO.SetActive(false); sectorGO.SetActive(false);
sectorGO.transform.parent = planetBody.transform; sectorGO.transform.parent = planetBody.transform;
sectorGO.transform.localPosition = Vector3.zero; sectorGO.transform.localPosition = Vector3.zero;
Sector S = sectorGO.AddComponent<Sector>(); var S = sectorGO.AddComponent<Sector>();
S._idString = parent._idString; S._idString = parent._idString;
S._name = parent._name; S._name = parent._name;
S._attachedOWRigidbody = owRigidBody; S._attachedOWRigidbody = owRigidBody;

View File

@ -10,6 +10,8 @@ using UnityEngine;
using Enum = System.Enum; using Enum = System.Enum;
using Logger = NewHorizons.Utility.Logger; using Logger = NewHorizons.Utility.Logger;
using Random = UnityEngine.Random; using Random = UnityEngine.Random;
using OWML.Utils;
namespace NewHorizons.Builder.Props namespace NewHorizons.Builder.Props
{ {
public static class NomaiTextBuilder public static class NomaiTextBuilder
@ -281,7 +283,7 @@ namespace NewHorizons.Builder.Props
var computer = computerObject.GetComponent<NomaiComputer>(); var computer = computerObject.GetComponent<NomaiComputer>();
computer.SetSector(sector); computer.SetSector(sector);
computer._location = (NomaiText.Location)Enum.Parse(typeof(NomaiText.Location), Enum.GetName(typeof(PropModule.NomaiTextInfo.NomaiTextLocation), info.location)); computer._location = EnumUtils.Parse<NomaiText.Location>(info.location.ToString());
computer._dictNomaiTextData = MakeNomaiTextDict(xmlPath); computer._dictNomaiTextData = MakeNomaiTextDict(xmlPath);
computer._nomaiTextAsset = new TextAsset(xmlPath); computer._nomaiTextAsset = new TextAsset(xmlPath);
computer._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile); computer._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile);
@ -329,7 +331,7 @@ namespace NewHorizons.Builder.Props
var computer = computerObject.GetComponent<NomaiVesselComputer>(); var computer = computerObject.GetComponent<NomaiVesselComputer>();
computer.SetSector(sector); computer.SetSector(sector);
computer._location = (NomaiText.Location)Enum.Parse(typeof(NomaiText.Location), Enum.GetName(typeof(PropModule.NomaiTextInfo.NomaiTextLocation), info.location)); computer._location = EnumUtils.Parse<NomaiText.Location>(info.location.ToString());
computer._dictNomaiTextData = MakeNomaiTextDict(xmlPath); computer._dictNomaiTextData = MakeNomaiTextDict(xmlPath);
computer._nomaiTextAsset = new TextAsset(xmlPath); computer._nomaiTextAsset = new TextAsset(xmlPath);
computer._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile); computer._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile);
@ -418,7 +420,7 @@ namespace NewHorizons.Builder.Props
var nomaiWallText = cairnObject.transform.Find("Props_TH_ClutterSmall/Arc_Short").GetComponent<NomaiWallText>(); var nomaiWallText = cairnObject.transform.Find("Props_TH_ClutterSmall/Arc_Short").GetComponent<NomaiWallText>();
nomaiWallText.SetSector(sector); nomaiWallText.SetSector(sector);
nomaiWallText._location = (NomaiText.Location)Enum.Parse(typeof(NomaiText.Location), Enum.GetName(typeof(PropModule.NomaiTextInfo.NomaiTextLocation), info.location)); nomaiWallText._location = EnumUtils.Parse<NomaiText.Location>(info.location.ToString());
nomaiWallText._dictNomaiTextData = MakeNomaiTextDict(xmlPath); nomaiWallText._dictNomaiTextData = MakeNomaiTextDict(xmlPath);
nomaiWallText._nomaiTextAsset = new TextAsset(xmlPath); nomaiWallText._nomaiTextAsset = new TextAsset(xmlPath);
nomaiWallText._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile); nomaiWallText._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile);
@ -458,7 +460,7 @@ namespace NewHorizons.Builder.Props
var nomaiText = recorderObject.GetComponentInChildren<NomaiText>(); var nomaiText = recorderObject.GetComponentInChildren<NomaiText>();
nomaiText.SetSector(sector); nomaiText.SetSector(sector);
nomaiText._location = (NomaiText.Location)Enum.Parse(typeof(NomaiText.Location), Enum.GetName(typeof(PropModule.NomaiTextInfo.NomaiTextLocation), info.location)); nomaiText._location = EnumUtils.Parse<NomaiText.Location>(info.location.ToString());
nomaiText._dictNomaiTextData = MakeNomaiTextDict(xmlPath); nomaiText._dictNomaiTextData = MakeNomaiTextDict(xmlPath);
nomaiText._nomaiTextAsset = new TextAsset(xmlPath); nomaiText._nomaiTextAsset = new TextAsset(xmlPath);
nomaiText._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile); nomaiText._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile);
@ -520,7 +522,7 @@ namespace NewHorizons.Builder.Props
var nomaiWallText = trailmarkerObject.transform.Find("Arc_Short").GetComponent<NomaiWallText>(); var nomaiWallText = trailmarkerObject.transform.Find("Arc_Short").GetComponent<NomaiWallText>();
nomaiWallText.SetSector(sector); nomaiWallText.SetSector(sector);
nomaiWallText._location = (NomaiText.Location)Enum.Parse(typeof(NomaiText.Location), Enum.GetName(typeof(PropModule.NomaiTextInfo.NomaiTextLocation), info.location)); nomaiWallText._location = EnumUtils.Parse<NomaiText.Location>(info.location.ToString());
nomaiWallText._dictNomaiTextData = MakeNomaiTextDict(xmlPath); nomaiWallText._dictNomaiTextData = MakeNomaiTextDict(xmlPath);
nomaiWallText._nomaiTextAsset = new TextAsset(xmlPath); nomaiWallText._nomaiTextAsset = new TextAsset(xmlPath);
nomaiWallText._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile); nomaiWallText._nomaiTextAsset.name = Path.GetFileNameWithoutExtension(info.xmlFile);
@ -553,7 +555,7 @@ namespace NewHorizons.Builder.Props
var nomaiWallText = nomaiWallTextObj.AddComponent<NomaiWallText>(); var nomaiWallText = nomaiWallTextObj.AddComponent<NomaiWallText>();
nomaiWallText._location = (NomaiText.Location)Enum.Parse(typeof(NomaiText.Location), Enum.GetName(typeof(PropModule.NomaiTextInfo.NomaiTextLocation), info.location)); nomaiWallText._location = EnumUtils.Parse<NomaiText.Location>(info.location.ToString());
var text = new TextAsset(xmlPath); var text = new TextAsset(xmlPath);

View File

@ -7,6 +7,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Logger = NewHorizons.Utility.Logger; using Logger = NewHorizons.Utility.Logger;
using OWML.Utils;
namespace NewHorizons.Builder.Props namespace NewHorizons.Builder.Props
{ {
public static class SignalBuilder public static class SignalBuilder
@ -14,11 +16,8 @@ namespace NewHorizons.Builder.Props
private static AnimationCurve _customCurve = null; private static AnimationCurve _customCurve = null;
private static Dictionary<SignalName, string> _customSignalNames; private static Dictionary<SignalName, string> _customSignalNames;
private static Stack<SignalName> _availableSignalNames;
private static int _nextCustomSignalName;
private static Dictionary<SignalFrequency, string> _customFrequencyNames; private static Dictionary<SignalFrequency, string> _customFrequencyNames;
private static int _nextCustomFrequencyName;
public static int NumberOfFrequencies; public static int NumberOfFrequencies;
@ -31,51 +30,12 @@ namespace NewHorizons.Builder.Props
{ {
Logger.LogVerbose($"Initializing SignalBuilder"); Logger.LogVerbose($"Initializing SignalBuilder");
_customSignalNames = new Dictionary<SignalName, string>(); _customSignalNames = new Dictionary<SignalName, string>();
_availableSignalNames = new Stack<SignalName>(new SignalName[]
{
(SignalName)17,
(SignalName)18,
(SignalName)19,
(SignalName)26,
(SignalName)27,
(SignalName)28,
(SignalName)29,
(SignalName)33,
(SignalName)34,
(SignalName)35,
(SignalName)36,
(SignalName)37,
(SignalName)38,
(SignalName)39,
(SignalName)50,
(SignalName)51,
(SignalName)52,
(SignalName)53,
(SignalName)54,
(SignalName)55,
(SignalName)56,
(SignalName)57,
(SignalName)58,
(SignalName)59,
SignalName.WhiteHole_WH,
SignalName.WhiteHole_SS_Receiver,
SignalName.WhiteHole_CT_Receiver,
SignalName.WhiteHole_CT_Experiment,
SignalName.WhiteHole_TT_Receiver,
SignalName.WhiteHole_TT_TimeLoopCore,
SignalName.WhiteHole_TH_Receiver,
SignalName.WhiteHole_BH_NorthPoleReceiver,
SignalName.WhiteHole_BH_ForgeReceiver,
SignalName.WhiteHole_GD_Receiver,
});
_customFrequencyNames = new Dictionary<SignalFrequency, string>() { _customFrequencyNames = new Dictionary<SignalFrequency, string>() {
{ SignalFrequency.Statue, "FREQ_STATUE" }, { SignalFrequency.Statue, "FREQ_STATUE" },
{ SignalFrequency.Default, "FREQ_UNKNOWN" }, { SignalFrequency.Default, "FREQ_UNKNOWN" },
{ SignalFrequency.WarpCore, "FREQ_WARP_CORE" } { SignalFrequency.WarpCore, "FREQ_WARP_CORE" }
}; };
_nextCustomSignalName = 200; NumberOfFrequencies = EnumUtils.GetValues<SignalFrequency>().Length;
_nextCustomFrequencyName = 256;
NumberOfFrequencies = 8;
_qmSignals = new List<SignalName>() { SignalName.Quantum_QM }; _qmSignals = new List<SignalName>() { SignalName.Quantum_QM };
_cloakedSignals = new List<SignalName>(); _cloakedSignals = new List<SignalName>();
@ -95,6 +55,9 @@ namespace NewHorizons.Builder.Props
public static SignalFrequency AddFrequency(string str) public static SignalFrequency AddFrequency(string str)
{ {
var freq = CollectionUtilities.KeyByValue(_customFrequencyNames, str);
if (freq != default) return freq;
Logger.Log($"Registering new frequency name [{str}]"); Logger.Log($"Registering new frequency name [{str}]");
if (NumberOfFrequencies == 31) if (NumberOfFrequencies == 31)
@ -103,17 +66,10 @@ namespace NewHorizons.Builder.Props
return SignalFrequency.Default; return SignalFrequency.Default;
} }
var freq = CollectionUtilities.KeyByValue(_customFrequencyNames, str); freq = EnumUtilities.Create<SignalFrequency>(str);
if (freq != default)
{
return freq;
}
freq = (SignalFrequency)_nextCustomFrequencyName;
_nextCustomFrequencyName *= 2;
_customFrequencyNames.Add(freq, str); _customFrequencyNames.Add(freq, str);
NumberOfFrequencies++; NumberOfFrequencies = EnumUtils.GetValues<SignalFrequency>().Length;
// This stuff happens after the signalscope is Awake so we have to change the number of frequencies now // This stuff happens after the signalscope is Awake so we have to change the number of frequencies now
GameObject.FindObjectOfType<Signalscope>()._strongestSignals = new AudioSignal[NumberOfFrequencies + 1]; GameObject.FindObjectOfType<Signalscope>()._strongestSignals = new AudioSignal[NumberOfFrequencies + 1];
@ -129,14 +85,15 @@ namespace NewHorizons.Builder.Props
public static SignalName AddSignalName(string str) public static SignalName AddSignalName(string str)
{ {
var name = CollectionUtilities.KeyByValue(_customSignalNames, str);
if (name != default) return name;
Logger.Log($"Registering new signal name [{str}]"); Logger.Log($"Registering new signal name [{str}]");
SignalName newName;
if (_availableSignalNames.Count == 0) newName = (SignalName)_nextCustomSignalName++; name = EnumUtilities.Create<SignalName>(str);
else newName = _availableSignalNames.Pop(); _customSignalNames.Add(name, str);
_customSignalNames.Add(newName, str); return name;
return newName;
} }
public static string GetCustomSignalName(SignalName signalName) public static string GetCustomSignalName(SignalName signalName)
@ -238,27 +195,12 @@ namespace NewHorizons.Builder.Props
private static SignalFrequency StringToFrequency(string str) private static SignalFrequency StringToFrequency(string str)
{ {
foreach (SignalFrequency freq in Enum.GetValues(typeof(SignalFrequency))) return EnumUtils.TryParse<SignalFrequency>(str, out SignalFrequency frequency) ? frequency : AddFrequency(str);
{
if (str.Equals(freq.ToString())) return freq;
}
var customName = CollectionUtilities.KeyByValue(_customFrequencyNames, str);
if (customName == default) customName = AddFrequency(str);
return customName;
} }
public static SignalName StringToSignalName(string str) public static SignalName StringToSignalName(string str)
{ {
foreach (SignalName name in Enum.GetValues(typeof(SignalName))) return EnumUtils.TryParse<SignalName>(str, out SignalName name) ? name : AddSignalName(str);
{
if (str.Equals(name.ToString())) return name;
}
var customName = CollectionUtilities.KeyByValue(_customSignalNames, str);
if (customName == default) customName = AddSignalName(str);
return customName;
} }
} }
} }

View File

@ -2,6 +2,7 @@ using NewHorizons.External.Configs;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using NewHorizons.Handlers; using NewHorizons.Handlers;
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -14,14 +15,12 @@ namespace NewHorizons.Builder.ShipLog
{ {
private static Dictionary<CuriosityName, Color> _curiosityColors; private static Dictionary<CuriosityName, Color> _curiosityColors;
private static Dictionary<CuriosityName, Color> _curiosityHighlightColors; private static Dictionary<CuriosityName, Color> _curiosityHighlightColors;
private static Dictionary<string, CuriosityName> _rawNameToCuriosityName;
private static Dictionary<string, string> _entryIdToRawName; private static Dictionary<string, string> _entryIdToRawName;
public static void Init() public static void Init()
{ {
_curiosityColors = new Dictionary<CuriosityName, Color>(); _curiosityColors = new Dictionary<CuriosityName, Color>();
_curiosityHighlightColors = new Dictionary<CuriosityName, Color>(); _curiosityHighlightColors = new Dictionary<CuriosityName, Color>();
_rawNameToCuriosityName = new Dictionary<string, CuriosityName>();
_entryIdToRawName = new Dictionary<string, string>(); _entryIdToRawName = new Dictionary<string, string>();
} }
@ -29,10 +28,9 @@ namespace NewHorizons.Builder.ShipLog
{ {
foreach (ShipLogModule.CuriosityColorInfo newColor in newColors) foreach (ShipLogModule.CuriosityColorInfo newColor in newColors)
{ {
if (_rawNameToCuriosityName.ContainsKey(newColor.id) == false) if (!EnumUtils.IsDefined<CuriosityName>(newColor.id))
{ {
CuriosityName newName = (CuriosityName)8 + _rawNameToCuriosityName.Count; CuriosityName newName = EnumUtilities.Create<CuriosityName>(newColor.id);
_rawNameToCuriosityName.Add(newColor.id, newName);
_curiosityColors.Add(newName, newColor.color.ToColor()); _curiosityColors.Add(newName, newColor.color.ToColor());
_curiosityHighlightColors.Add(newName, newColor.highlightColor.ToColor()); _curiosityHighlightColors.Add(newName, newColor.highlightColor.ToColor());
} }
@ -193,9 +191,9 @@ namespace NewHorizons.Builder.ShipLog
if (_entryIdToRawName.ContainsKey(entry._id)) if (_entryIdToRawName.ContainsKey(entry._id))
{ {
var raw = _entryIdToRawName[entry._id]; var raw = _entryIdToRawName[entry._id];
if (_rawNameToCuriosityName.ContainsKey(raw)) if (EnumUtils.TryParse<CuriosityName>(raw, out CuriosityName name))
{ {
entry._curiosity = _rawNameToCuriosityName[raw]; entry._curiosity = name;
} }
else else
{ {

View File

@ -1,6 +1,7 @@
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
using OWML.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -47,7 +48,7 @@ namespace NewHorizons.Builder.Volumes
owAudioSource._audioSource = audioSource; owAudioSource._audioSource = audioSource;
owAudioSource.loop = info.loop; owAudioSource.loop = info.loop;
owAudioSource.SetMaxVolume(info.volume); owAudioSource.SetMaxVolume(info.volume);
owAudioSource.SetTrack((OWAudioMixer.TrackName)Enum.Parse(typeof(OWAudioMixer.TrackName), Enum.GetName(typeof(AudioMixerTrackName), info.track))); owAudioSource.SetTrack(EnumUtils.Parse<OWAudioMixer.TrackName>(info.track.ToString()));
AudioUtilities.SetAudioClip(owAudioSource, info.audio, mod); AudioUtilities.SetAudioClip(owAudioSource, info.audio, mod);
var audioVolume = go.AddComponent<AudioVolume>(); var audioVolume = go.AddComponent<AudioVolume>();

View File

@ -1,5 +1,6 @@
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
using OWML.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -13,7 +14,6 @@ namespace NewHorizons.Handlers
{ {
private static Dictionary<string, AudioType> _customAudioTypes; private static Dictionary<string, AudioType> _customAudioTypes;
private static List<AudioLibrary.AudioEntry> _audioEntries; private static List<AudioLibrary.AudioEntry> _audioEntries;
private static int _startingInt = 4000;
public static void Init() public static void Init()
{ {
@ -48,7 +48,7 @@ namespace NewHorizons.Handlers
} }
else else
{ {
return (AudioType)Enum.Parse(typeof(AudioType), audio); return EnumUtils.Parse<AudioType>(audio);
} }
} }
catch (Exception e) catch (Exception e)
@ -80,9 +80,9 @@ namespace NewHorizons.Handlers
// Create a custom audio type from a set of audio clips. Needs a unique ID // Create a custom audio type from a set of audio clips. Needs a unique ID
public static AudioType AddCustomAudioType(string id, AudioClip[] audioClips) public static AudioType AddCustomAudioType(string id, AudioClip[] audioClips)
{ {
var audioType = (AudioType)_startingInt + _customAudioTypes.Count(); Logger.LogVerbose($"Registering new audio type [{id}]");
Logger.LogVerbose($"Registering custom audio type {id} as {audioType}"); var audioType = EnumUtilities.Create<AudioType>(id);
_audioEntries.Add(new AudioLibrary.AudioEntry(audioType, audioClips)); _audioEntries.Add(new AudioLibrary.AudioEntry(audioType, audioClips));
_customAudioTypes.Add(id, audioType); _customAudioTypes.Add(id, audioType);

View File

@ -1,5 +1,6 @@
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
using OWML.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,7 +12,6 @@ namespace NewHorizons.Handlers
public static class RemoteHandler public static class RemoteHandler
{ {
private static Dictionary<string, NomaiRemoteCameraPlatform.ID> _customPlatformIDs; private static Dictionary<string, NomaiRemoteCameraPlatform.ID> _customPlatformIDs;
private static readonly int _startingInt = 19;
public static void Init() public static void Init()
{ {
@ -41,7 +41,7 @@ namespace NewHorizons.Handlers
try try
{ {
NomaiRemoteCameraPlatform.ID platformID; NomaiRemoteCameraPlatform.ID platformID;
if (_customPlatformIDs.TryGetValue(id, out platformID) || Enum.TryParse(id, true, out platformID)) if (_customPlatformIDs.TryGetValue(id, out platformID) || EnumUtils.TryParse<NomaiRemoteCameraPlatform.ID>(id, out platformID))
{ {
return platformID; return platformID;
} }
@ -52,16 +52,16 @@ namespace NewHorizons.Handlers
} }
catch (Exception e) catch (Exception e)
{ {
Logger.LogError($"Couldn't load platform id:\n{e}"); Logger.LogError($"Couldn't load platform id [{id}]:\n{e}");
return NomaiRemoteCameraPlatform.ID.None; return NomaiRemoteCameraPlatform.ID.None;
} }
} }
public static NomaiRemoteCameraPlatform.ID AddCustomPlatformID(string id) public static NomaiRemoteCameraPlatform.ID AddCustomPlatformID(string id)
{ {
var platformID = (NomaiRemoteCameraPlatform.ID)_startingInt + _customPlatformIDs.Count(); Logger.LogVerbose($"Registering new platform id [{id}]");
Logger.LogVerbose($"Registering custom platform id {id} as {platformID}"); var platformID = EnumUtilities.Create<NomaiRemoteCameraPlatform.ID>(id);
_customPlatformIDs.Add(id, platformID); _customPlatformIDs.Add(id, platformID);

View File

@ -17,6 +17,7 @@ using NewHorizons.Utility.DebugMenu;
using NewHorizons.Utility.DebugUtilities; using NewHorizons.Utility.DebugUtilities;
using OWML.Common; using OWML.Common;
using OWML.ModHelper; using OWML.ModHelper;
using OWML.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -241,6 +242,7 @@ namespace NewHorizons
ImageUtilities.ClearCache(); ImageUtilities.ClearCache();
AudioUtilities.ClearCache(); AudioUtilities.ClearCache();
AssetBundleUtilities.ClearCache(); AssetBundleUtilities.ClearCache();
EnumUtilities.ClearCache();
IsSystemReady = false; IsSystemReady = false;
} }
@ -611,7 +613,7 @@ namespace NewHorizons
private void LoadTranslations(string folder, IModBehaviour mod) private void LoadTranslations(string folder, IModBehaviour mod)
{ {
var foundFile = false; var foundFile = false;
foreach (TextTranslation.Language language in Enum.GetValues(typeof(TextTranslation.Language))) foreach (TextTranslation.Language language in EnumUtils.GetValues<TextTranslation.Language>())
{ {
if (language is TextTranslation.Language.UNKNOWN or TextTranslation.Language.TOTAL) continue; if (language is TextTranslation.Language.UNKNOWN or TextTranslation.Language.TOTAL) continue;

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net48</TargetFramework> <TargetFramework>net48</TargetFramework>
<LangVersion>default</LangVersion> <LangVersion>default</LangVersion>

View File

@ -34,12 +34,6 @@
"description": "Add bramble nodes to this planet and/or make this planet a bramble dimension", "description": "Add bramble nodes to this planet and/or make this planet a bramble dimension",
"$ref": "#/definitions/BrambleModule" "$ref": "#/definitions/BrambleModule"
}, },
"buildPriority": {
"type": "integer",
"description": "Set to a higher number if you wish for this body to be built sooner",
"format": "int32",
"default": -1
},
"canShowOnTitle": { "canShowOnTitle": {
"type": "boolean", "type": "boolean",
"description": "Should this planet ever be shown on the title screen?", "description": "Should this planet ever be shown on the title screen?",

View File

@ -1,4 +1,5 @@
using OWML.Common; using OWML.Common;
using OWML.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -34,12 +35,11 @@ namespace NewHorizons.Utility
} }
} }
try if (EnumUtils.TryParse<AudioType>(audio, out AudioType type))
{ {
var audioType = (AudioType)Enum.Parse(typeof(AudioType), audio); source._audioLibraryClip = type;
source._audioLibraryClip = audioType;
} }
catch else
{ {
var audioClip = SearchUtilities.FindResourceOfTypeAndName<AudioClip>(audio); var audioClip = SearchUtilities.FindResourceOfTypeAndName<AudioClip>(audio);
if (audioClip == null) Logger.Log($"Couldn't find audio clip {audio}"); if (audioClip == null) Logger.Log($"Couldn't find audio clip {audio}");

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OWML.Utils;
namespace NewHorizons.Utility
{
public static class EnumUtilities
{
private static List<Enum> Enums = new List<Enum>();
public static void ClearCache()
{
foreach (var @enum in Enums)
{
if (@enum == null) continue;
EnumUtils.Remove(@enum.GetType(), @enum);
}
Enums.Clear();
}
public static T Create<T>(string name) where T : Enum
{
T @enum = EnumUtils.Create<T>(name);
Enums.SafeAdd(@enum);
return @enum;
}
public static void Create<T>(string name, T value) where T : Enum
{
EnumUtils.Create(name, value);
Enums.SafeAdd(value);
}
public static void Remove<T>(string name) where T : Enum
{
T @enum = EnumUtils.Parse<T>(name);
Enums.Remove(@enum);
EnumUtils.Remove<T>(name);
}
public static void Remove<T>(T value) where T : Enum
{
Enums.Remove(value);
EnumUtils.Remove<T>(value);
}
}
}

View File

@ -1,6 +1,7 @@
using NewHorizons.External.Configs; using NewHorizons.External.Configs;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using Newtonsoft.Json; using Newtonsoft.Json;
using OWML.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -217,17 +218,6 @@ namespace NewHorizons.Utility
return xCorrect && yCorrect && zCorrect; return xCorrect && yCorrect && zCorrect;
} }
public static FluidVolume.Type ConvertToOW(this FluidType fluidType, FluidVolume.Type @default = FluidVolume.Type.NONE) public static FluidVolume.Type ConvertToOW(this FluidType fluidType, FluidVolume.Type @default = FluidVolume.Type.NONE) => EnumUtils.Parse(fluidType.ToString().ToUpper(), @default);
{
try
{
return (FluidVolume.Type)Enum.Parse(typeof(FluidVolume.Type), Enum.GetName(typeof(FluidType), fluidType).ToUpper());
}
catch (Exception ex)
{
Logger.LogError($"Couldn't parse fluid volume type [{fluidType}]:\n{ex}");
return @default;
}
}
} }
} }