using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Line14 { // Dictionary keys public enum Key { SuspendedFromFloor, } // Predefined values for certain keys public enum Value_Active { Disabled, Enabled } // Keys with a restricted range of values static readonly Dictionary restrictedKeyValues = new() { //{ Key.SuspendedByDefault, typeof(Value_Active) }, }; // Dictionary management static readonly int dictionaryIndex = 14; static readonly int lengthKeyValue = 2; static readonly char separatorPair = ';'; static readonly char separatorKeyValue = ':'; static public bool TryGetValue(int objectIndex, Key key, out string value) { value = null; bool keyExists = false; string stringKey = key.ToString(); string[] splitData = GetSplitData(objectIndex); for (int i = 0; i < splitData.Length; i += lengthKeyValue) { if (splitData[i] == stringKey) { value = splitData[i + 1]; keyExists = true; break; } } return keyExists; } static public void SetValue(int objectIndex, Key key, string value) { if (!ValidateKeyValue(key, value)) { Debug.LogError($"Invalid value entered for given key. Key : {key} ; Value : {value}"); return; } string[] splitData = GetSplitData(objectIndex); string stringKey = key.ToString(); bool keyExists = false; for (int i = 0; i < splitData.Length; i += lengthKeyValue) { if (splitData[i] == stringKey) { splitData[i + 1] = value; keyExists = true; break; } } string newData = ""; if (keyExists) { for (int i = 0; i < splitData.Length; i++) { newData += $"{GetSeparatorToAdd(i)}{splitData[i]}"; } } else { string stringKeyValue = $"{stringKey}{separatorKeyValue}{value}"; if (splitData.Length < lengthKeyValue) { newData = stringKeyValue; } else { string data = _G.OBJs[objectIndex][dictionaryIndex]; newData = $"{data}{separatorPair}{stringKeyValue}"; } } _G.OBJs[objectIndex][dictionaryIndex] = newData; } static public void SetValue(int objectIndex, Key key, Enum value) { SetValue(objectIndex, key, value.ToString()); } static public void RemoveKey(int objectIndex, Key key) { string[] splitData = GetSplitData(objectIndex); string stringKey = key.ToString(); bool keyExists = false; int keyIndex = -1; for (int i = 0; i < splitData.Length; i += lengthKeyValue) { if (splitData[i] == stringKey) { keyExists = true; keyIndex = i; break; } } if (!keyExists) { return; } string newData = ""; int newIndex = -1; for (int i = 0; i < splitData.Length; i++) { if (i == keyIndex) { i++; continue; } newIndex++; newData += $"{GetSeparatorToAdd(newIndex)}{splitData[newIndex]}"; } _G.OBJs[objectIndex][dictionaryIndex] = newData; } static private string[] GetSplitData(int objectIndex) { string data = _G.OBJs[objectIndex][dictionaryIndex]; return data != null ? data.Split(separatorPair, separatorKeyValue) : new string[0]; } static private string GetSeparatorToAdd(int index) { if (index == 0) { return ""; } else if (index % lengthKeyValue == 0) { return separatorPair.ToString(); } else { return separatorKeyValue.ToString(); } } static private bool ValidateKeyValue(Key key, string value) { bool hasNoRestrictions = true; bool containsValue = false; if (restrictedKeyValues.TryGetValue(key, out Type type)) { hasNoRestrictions = false; containsValue = Enum.TryParse(type, value, false, out _); } return hasNoRestrictions || containsValue; } }