Files
EnglewoodLAB/Assets/Scripts/UI/SettingPanel/OptionManager.cs
SOOBEEN HAN f1894889ee <refactor> Octopus Twin 템플릿 적용
- 기능 외 UI 구조만 적용
- 프로젝트에 걸맞는 UI는 재작업 필요
2026-02-23 18:20:09 +09:00

118 lines
4.3 KiB
C#

using RTGLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
namespace EnglewoodLAB.UI
{
public class OptionManager : MonoBehaviour, ISingle
{
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
private string filePath;
public Dictionary<MonoBehaviour, OptionBase> optionBases = new();
public Action onCompletedLoadOption;
public void LoadOption()
{
#if UNITY_EDITOR
filePath = "./Option.ini";
#else
filePath = "./Option.ini";
#endif
var allBehaviours = Core.singleTable.Values;
var optionables = allBehaviours.Where(mo=>mo is IOptionable).ToList();
var defaultOptionBases = optionables.Select(mo => new OptionBase(mo)).ToList();
if (!File.Exists(filePath))
{
//Create
StringBuilder sb = new StringBuilder();
using (var fs = File.Create(filePath))
{
foreach (var optionBase in defaultOptionBases)
{
var section = optionBase.section;
sb.AppendLine($"[{section}]");
foreach (var option in optionBase.options)
{
sb.AppendLine($"{option.Key}={option.Value}");
}
sb.AppendLine();
}
var bytes = Encoding.UTF8.GetBytes(sb.ToString());
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
}
}
foreach (var defaultOption in defaultOptionBases)
{
var section = defaultOption.section;
Dictionary<string, string> temp = new Dictionary<string, string>();
foreach (var option in defaultOption.options)
{
var key = option.Key;
var newValue = StringDataReadIni(filePath, section, key);
if (newValue == "")
{
WritePrivateProfileString(section, key, option.Value, filePath);
newValue = option.Value;
}
temp.Add(key, newValue);
}
defaultOption.options = temp;
defaultOption.SetValue(defaultOption.target);
optionBases.Add(defaultOption.target, defaultOption);
}
onCompletedLoadOption?.Invoke();
}
public string StringDataReadIni(string filePath, string section, string key)
{
StringBuilder buffer = new StringBuilder(1024);
GetPrivateProfileString(section, key, "", buffer, 1024, filePath);
return buffer.ToString();
}
public Dictionary<string, string> GetOption(MonoBehaviour mb)
{
var defaultOption = optionBases[mb];
return defaultOption.options;
}
public void SetOptionValue(MonoBehaviour mb, Dictionary<string, string> optionData)
{
Dictionary<string, string> temp = new Dictionary<string, string>();
var defaultOption = optionBases[mb];
var section = defaultOption.section;
temp.Clear();
foreach(var key in defaultOption.options.Keys)
{
temp.Add(key, defaultOption.options[key]);
}
foreach (var option in optionData)
{
var key = option.Key;
if (temp.ContainsKey(key))
{
WritePrivateProfileString(section, key, optionData[key], filePath);
temp[key] = optionData[key];
}
}
defaultOption.options = temp;
defaultOption.SetValue(defaultOption.target);
}
}
}