//don't remove method! Method generically called in CompareReadSettingsWithModel() private void ApplyDefaultValueOnSetting <T>(ExtendedManagerSettings mergedSettings, FieldInfo modelField) { Setting <T> mergedSetting = ((Setting <T>)modelField.GetValue(_settings)); mergedSetting.Value = mergedSetting.Defaultvalue; mergedSettings.GetType().GetField(modelField.Name).SetValue(mergedSettings, mergedSetting); }
private void MergeSettings_vector3(ExtendedManagerSettings mergedSettings, FieldInfo modelField, FieldInfo readField) { Setting <Vector3> mergedSetting = ((Setting <Vector3>)modelField.GetValue(_settings)); Setting <Vector3> readSetting = ((Setting <Vector3>)readField.GetValue(_settingsReadFromXml)); mergedSetting.Value = readSetting.Value; mergedSettings.GetType().GetField(modelField.Name).SetValue(mergedSettings, mergedSetting); }
private void CompareReadSettingsWithModelAndFillValues() { //compare the settings read from the xml with the settings read from the model //copy everything from the model, but keep the value from the xml //this way editing the model (Displayname, Defaultvalue...) is applied, but the saved value from the settings in the xml remains the same ExtendedManagerSettings mergedSettings = new ExtendedManagerSettings(); var modelFields = _settings.GetType().GetFields(); foreach (FieldInfo modelField in modelFields) { //if the setting exists in the the xml file, overwrite the default value with the read value foreach (FieldInfo readField in _settingsReadFromXml.GetType().GetFields()) { //if the same fields have been found (one field read from the xml, the other coming from the model) if (modelField.Name == readField.Name) { if (modelField.Name == "SettingsCreatedVersion" || modelField.Name == "CurrentSettingsVersion") //settingsversion is a property but not a setting { if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { string modelVersion = ((string)modelField.GetValue(_settings)); string readSettingsVersion = ((string)readField.GetValue(_settingsReadFromXml)); if (modelVersion != readSettingsVersion) { Debug.LogWarningFormat("Settings created with other version of the settingsmanager. It's advised that you remove the old SettingsCanvasPrefab from your scene and replace it with the new one. \nVersion went from '{0}' to '{1}' \n----------------------------", readSettingsVersion, modelVersion); } } continue; //skip to other iteration } //copy everything from the model, but keep the value from the xml if (modelField.FieldType.IsGenericType) //generic type: string, bool, vector2... { #if !UNITY_IOS if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { //setting is present in xml, apply the xml value to the field var type = modelField.FieldType.GetGenericArguments()[0]; var genericMergeSettingsMethod = GetType().GetMethod("MergeSettings", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(type); genericMergeSettingsMethod.Invoke(this, new object[] { mergedSettings, modelField, readField }); } else { //setting is not present in xml, apply the default value to the field var type = modelField.FieldType.GetGenericArguments()[0]; var genericApplyDefaultSettingsMethod = GetType().GetMethod("ApplyDefaultValueOnSetting", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(type); genericApplyDefaultSettingsMethod.Invoke(this, new object[] { mergedSettings, modelField }); } #else if (modelField.FieldType.GetGenericArguments()[0] == typeof(bool)) { if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { MergeSettings_bool(mergedSettings, modelField, readField); } else { ApplyDefaultValueOnSetting_bool(mergedSettings, modelField); } } else if (modelField.FieldType.GetGenericArguments()[0] == typeof(string)) { if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { MergeSettings_string(mergedSettings, modelField, readField); } else { ApplyDefaultValueOnSetting_string(mergedSettings, modelField); } } else if (modelField.FieldType.GetGenericArguments()[0] == typeof(float)) { if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { MergeSettings_float(mergedSettings, modelField, readField); } else { ApplyDefaultValueOnSetting_float(mergedSettings, modelField); } } else if (modelField.FieldType.GetGenericArguments()[0] == typeof(Vector2)) { if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { MergeSettings_vector2(mergedSettings, modelField, readField); } else { ApplyDefaultValueOnSetting_vector2(mergedSettings, modelField); } } else if (modelField.FieldType.GetGenericArguments()[0] == typeof(Vector3)) { if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { MergeSettings_vector3(mergedSettings, modelField, readField); } else { ApplyDefaultValueOnSetting_vector3(mergedSettings, modelField); } } else { Debug.LogWarning(string.Format("Field of type '{0}' not yet implemented.", modelField.FieldType.Name)); } #endif } else //non generic type { if (IsSettingAlreadyPresentInXmlFile(modelField.Name)) { //setting is present in xml, apply the xml value to the field if (modelField.FieldType == typeof(MinMaxSetting)) { MinMaxSetting mergedSetting = ((MinMaxSetting)modelField.GetValue(_settings)); MinMaxSetting readSetting = ((MinMaxSetting)readField.GetValue(_settingsReadFromXml)); mergedSetting.Value = readSetting.Value; mergedSettings.GetType().GetField(modelField.Name).SetValue(mergedSettings, mergedSetting); } else if (modelField.FieldType == typeof(ButtonSetting)) { ButtonSetting mergedSetting = ((ButtonSetting)modelField.GetValue(_settings)); ButtonSetting readSetting = ((ButtonSetting)readField.GetValue(_settingsReadFromXml)); mergedSetting.Value = readSetting.Value; mergedSettings.GetType().GetField(modelField.Name).SetValue(mergedSettings, mergedSetting); } else { Debug.LogWarning(string.Format("Field of type '{0}' not yet implemented.", modelField.FieldType.Name)); } } else { if (modelField.FieldType == typeof(MinMaxSetting)) { //setting is not present in xml, apply the default value to the field MinMaxSetting mergedSetting = ((MinMaxSetting)modelField.GetValue(_settings)); mergedSetting.Value = mergedSetting.Defaultvalue; mergedSettings.GetType().GetField(modelField.Name).SetValue(mergedSettings, mergedSetting); } else if (modelField.FieldType == typeof(ButtonSetting)) { //setting is not present in xml, apply the default value to the field ButtonSetting mergedSetting = ((ButtonSetting)modelField.GetValue(_settings)); mergedSetting.Value = mergedSetting.Defaultvalue; mergedSettings.GetType().GetField(modelField.Name).SetValue(mergedSettings, mergedSetting); } else { Debug.LogWarning(string.Format("Field of type '{0}' not yet implemented.", modelField.FieldType.Name)); } } } } } } Settings = mergedSettings; //save the merged settings to the xml file Settings.SerializeToXmlFile(); }