/// <summary> /// Assign all variables to their configured values /// </summary> /// <param name="Config">Dictionary of variable names and values</param> public static void LoadConfig(Dictionary <string, object> Config) { foreach (Type AssemblyType in Assembly.GetExecutingAssembly().GetTypes() ) // Loop through all types in the assembly { foreach (FieldInfo FInfo in AssemblyType.GetFields() .Where(f => Attribute.IsDefined(f, typeof(SaveAttribute))) ) // Loop through all fields with the save attribute defined { // Get field name and type in our format string Name = $"{AssemblyType.Name}_{FInfo.Name}"; Type FIType = FInfo.FieldType; object DefaultInfo = FInfo.GetValue(null); // Get the default value for the fieldinfo if (!Config.ContainsKey(Name)) // If the field does not exist in the configuration dictionary { Config.Add(Name, DefaultInfo); } try //Try to parse intermediate JSON because the object itself isnt actually deserialized or something { // If field is an array, set the field variable to a JArray if (Config[Name].GetType() == typeof(JArray)) { Config[Name] = ((JArray)Config[Name]).ToObject(FInfo.FieldType); } // If field is an object, set the field variable to JObject if (Config[Name].GetType() == typeof(JObject)) { Config[Name] = ((JObject)Config[Name]).ToObject(FInfo.FieldType); } // Assign field values FInfo.SetValue(null, FInfo.FieldType.IsEnum ? Enum.ToObject(FInfo.FieldType, Config[Name]) : Convert.ChangeType(Config[Name], FInfo.FieldType)); } catch { //#if DEBUG DebugUtilities.Log("Error loading config value: " + Name); //#endif Config[Name] = DefaultInfo; } } } DebugUtilities.Log("Setting default colors..."); foreach (var identifier in ColorOptions.DefaultColorDict) { if (!ColorOptions.ColorDict.ContainsKey(identifier.Key)) { ColorOptions.ColorDict.Add(identifier.Key, new ColorVariable(identifier.Value)); } } foreach (var newIdentifier in ColorOptions.ColorDict.ToList()) { if (!ColorOptions.DefaultColorDict.ContainsKey(newIdentifier.Key)) { ColorOptions.ColorDict.Remove(newIdentifier.Key); } } DebugUtilities.Log("Setting defualt hotkeys..."); foreach (var kvp in HotkeyOptions.DefaultHotkeyDict) { foreach (var kvp2 in kvp.Value) { if (!HotkeyOptions.UnorganizedHotkeys.ContainsKey(kvp2.Key)) { HotkeyOptions.UnorganizedHotkeys.Add(kvp2.Key, kvp2.Value); } } } foreach (var str in HotkeyOptions.UnorganizedHotkeys.ToList()) { if (HotkeyOptions.DefaultHotkeyDict.All(kvp => !kvp.Value.ContainsKey(str.Key))) { HotkeyOptions.UnorganizedHotkeys.Remove(str.Key); } } DebugUtilities.Log("Saving config..."); SaveConfig(Config); }
/// <summary> /// Convert a Panel to a DPanelInfo /// </summary> /// <typeparam name="T">Derives from Panel</typeparam> /// <param name="Input">Any object deriving from Panel</param> /// <returns></returns> public static DPanelInfo PanelToInfo <T>(T Input) where T : Panel // Must be a Panel { Type InputType = Input.GetType(); FieldInfo[] fields = GetSaveableFields(Input); PropertyInfo[] properties = GetSaveableProperties(Input); DPanelInfo ifo = new DPanelInfo(); foreach (FieldInfo FInfo in fields) { object val = FInfo.GetValue(Input); Type t = val.GetType(); string key = FInfo.Name; // If I need to add more, tell me @Gbps if (t == typeof(string)) { ifo.Insert(key, Encoding.UTF8.GetBytes((string)val)); } else if (t == typeof(int)) { ifo.Insert(key, BitConverter.GetBytes((int)val)); } else if (t == typeof(bool)) { ifo.Insert(key, BitConverter.GetBytes((bool)val)); } else if (t == typeof(char)) { ifo.Insert(key, BitConverter.GetBytes((char)val)); } else if (t == typeof(float)) { ifo.Insert(key, BitConverter.GetBytes((float)val)); } else if (t == typeof(double)) { ifo.Insert(key, BitConverter.GetBytes((double)val)); } else if (t == typeof(List <string>)) { ifo.Insert(key, SerializeStringList((List <string>)val)); } } foreach (PropertyInfo FInfo in properties) { object val = FInfo.GetValue(Input, null); Type t = val.GetType(); string key = FInfo.Name; // If I need to add more, tell me @Gbps if (t == typeof(string)) { ifo.Insert(key, Encoding.UTF8.GetBytes((string)val)); } else if (t == typeof(int)) { ifo.Insert(key, BitConverter.GetBytes((int)val)); } else if (t == typeof(bool)) { ifo.Insert(key, BitConverter.GetBytes((bool)val)); } else if (t == typeof(char)) { ifo.Insert(key, BitConverter.GetBytes((char)val)); } else if (t == typeof(float)) { ifo.Insert(key, BitConverter.GetBytes((float)val)); } else if (t == typeof(double)) { ifo.Insert(key, BitConverter.GetBytes((double)val)); } else if (t == typeof(List <string>)) { ifo.Insert(key, SerializeStringList((List <string>)val)); } } if (Input.hasParent && Input.parent) { ifo.Insert("___parent", Encoding.UTF8.GetBytes(Input.parent.varname)); } ifo.Insert("___type", Encoding.UTF8.GetBytes(Input.GetType().Name)); return(ifo); }
public virtual object Read(object instance, FInfo i) { return(i.GetValue(instance)); }