122 lines
3.8 KiB
C#
122 lines
3.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public static class SettingsApplier
|
|
{
|
|
public static (bool OK, string log) ApplySettings(Dictionary<string, string> keyValuePairs)
|
|
{
|
|
List<Setting> settings = SettingsManager.Container.Settings;
|
|
|
|
//тут надо еще взять все id из настроек и если какое то значение не пришло значит оно false
|
|
|
|
|
|
foreach (var kv in keyValuePairs)
|
|
{
|
|
var searchResult = GetSettingById(kv.Key, settings);
|
|
if (searchResult.found)
|
|
{//есть такая настройка в менеджере
|
|
|
|
InjectNewValueToSetting(kv.Value, searchResult.setting);
|
|
}
|
|
else
|
|
{//нет такой настройки в менеджере
|
|
|
|
}
|
|
}
|
|
|
|
foreach(string id in FalseBoolSettingIds(settings, keyValuePairs.Keys.ToList()))
|
|
{
|
|
InjectNewValueToSetting("off", GetSettingById(id, settings).setting);
|
|
}
|
|
|
|
|
|
return (OK: true, log: "");
|
|
}
|
|
|
|
private static (bool found, Setting setting) GetSettingById(string settingId, List<Setting> settings)
|
|
{
|
|
Setting setting = settings.Find(s => s.Id == settingId);
|
|
|
|
if (setting != null)
|
|
return (found: true, setting);
|
|
else
|
|
return (found: false, setting);
|
|
}
|
|
|
|
private static void InjectNewValueToSetting(string newValue, Setting setting)
|
|
{
|
|
if (setting.Type == SettingType.String)
|
|
{
|
|
if (setting.AllowedValues.Contains(newValue))
|
|
{
|
|
setting.StringValue = newValue;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{setting.Id}: Попытка установить недопустимое значение \"{newValue}\"");
|
|
}
|
|
}
|
|
else if (setting.Type == SettingType.Float)
|
|
{
|
|
float value=0;
|
|
try
|
|
{
|
|
value = float.Parse(newValue, CultureInfo.InvariantCulture);
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.Log($"{setting.Id}: Ошибка парсинга значения \"{newValue}\". {e}");//тут парсится неправильно
|
|
return;
|
|
}
|
|
|
|
//если спарсилось, проверяем пределы
|
|
if(value <= setting.FloatMax && value >= setting.FloatMin)
|
|
{
|
|
setting.FloatValue = value;
|
|
}
|
|
else
|
|
{//не подходит под интервал
|
|
Debug.Log($"{setting.Id}: Новое значение за пределами ограничителей\"{newValue}\".");
|
|
}
|
|
}
|
|
else if (setting.Type == SettingType.Bool)
|
|
{
|
|
bool value=false;
|
|
if(newValue == "on") { value = true; }
|
|
|
|
setting.BoolValue = value;
|
|
}
|
|
else if (setting.Type == SettingType.Int)
|
|
{
|
|
setting.IntValue = int.Parse(newValue);
|
|
}
|
|
}
|
|
|
|
private static List<string> FalseBoolSettingIds(List<Setting> settings, List<string> entry)
|
|
{
|
|
List<string> ids = new();
|
|
|
|
foreach (Setting setting in settings)
|
|
{
|
|
if (entry.Contains(setting.Id)) continue;
|
|
|
|
ids.Add(setting.Id);
|
|
}
|
|
|
|
List<string> BoolSettings = new List<string>();
|
|
foreach (string id in ids)
|
|
{
|
|
var searchResult = GetSettingById(id, settings);
|
|
if (searchResult.found && searchResult.setting.Type == SettingType.Bool)
|
|
{
|
|
BoolSettings.Add(id);
|
|
}
|
|
}
|
|
return BoolSettings;
|
|
}
|
|
|
|
}
|