180 lines
5.6 KiB
C#
180 lines
5.6 KiB
C#
using AZTECHWB.Core;
|
|
using Cysharp.Threading.Tasks;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
namespace AZTECHWB.Management
|
|
{
|
|
public class OptionManager : Manager
|
|
{
|
|
private string filePath;
|
|
|
|
// section -> (key -> value)
|
|
private readonly Dictionary<string, Dictionary<string, string>> options = new Dictionary<string, Dictionary<string, string>>();
|
|
|
|
public Action onCompletedLoadOption;
|
|
private UniversalRenderPipelineAsset urpAsset;
|
|
|
|
public override async UniTask Init()
|
|
{
|
|
filePath = "./Option.ini";
|
|
urpAsset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
|
|
|
|
AZTECHSceneMain.Instance.Initialized += SettingOption;
|
|
|
|
await UniTask.CompletedTask;
|
|
}
|
|
|
|
private void SettingOption()
|
|
{
|
|
LoadOption();
|
|
ApplyGraphicsImmediately();
|
|
ApplyControlImmediately();
|
|
}
|
|
|
|
public void LoadOption()
|
|
{
|
|
if (!File.Exists(filePath))
|
|
CreateDefaultIni();
|
|
|
|
ReadIniFile();
|
|
onCompletedLoadOption?.Invoke();
|
|
}
|
|
|
|
private void ApplyGraphicsImmediately()
|
|
{
|
|
var res = GetValue("Graphics", "Resolution", "1920x1080").Split('x');
|
|
Screen.SetResolution(int.Parse(res[0]), int.Parse(res[1]), FullScreenMode.FullScreenWindow);
|
|
QualitySettings.globalTextureMipmapLimit = int.Parse(GetValue("Graphics", "Texture", "0"));
|
|
QualitySettings.vSyncCount =int.Parse(GetValue("Graphics", "VSync", "1"));
|
|
QualitySettings.anisotropicFiltering = (AnisotropicFiltering)int.Parse(GetValue("Graphics", "AF", "1"));
|
|
|
|
int shadow = int.Parse(GetValue("Graphics", "Shadow", "2"));
|
|
if (urpAsset != null)
|
|
{
|
|
urpAsset.shadowDistance = shadow switch
|
|
{
|
|
0 => 0,
|
|
1 => 20,
|
|
2 => 50,
|
|
3 => 100,
|
|
_ => 50
|
|
};
|
|
}
|
|
|
|
int aaIndex = int.Parse(GetValue("Graphics", "AA", "2"));
|
|
int[] aaValues = { 0, 2, 4, 8 };
|
|
if (urpAsset != null)
|
|
urpAsset.msaaSampleCount = aaValues[aaIndex];
|
|
}
|
|
|
|
|
|
private void ApplyControlImmediately()
|
|
{
|
|
var cam = FindAnyObjectByType<OrbitalController>();
|
|
cam.moveSpeed = float.Parse(GetValue("Control", "MoveSpeed", "2"));
|
|
cam.rotateSpeed = float.Parse(GetValue("Control", "RotateSpeed", "30"));
|
|
cam.zoomSpeed = float.Parse(GetValue("Control", "ZoomSpeed", "50"));
|
|
}
|
|
|
|
public string GetValue(string section, string key, string defaultValue = "")
|
|
{
|
|
if (!options.TryGetValue(section, out var sec))
|
|
{
|
|
sec = new Dictionary<string, string>();
|
|
options[section] = sec;
|
|
}
|
|
|
|
if (!sec.TryGetValue(key, out var value))
|
|
{
|
|
sec[key] = defaultValue;
|
|
SaveIniFile();
|
|
return defaultValue;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public void SetValue(string section, string key, string value)
|
|
{
|
|
if (!options.TryGetValue(section, out var sec))
|
|
{
|
|
sec = new Dictionary<string, string>();
|
|
options[section] = sec;
|
|
}
|
|
sec[key] = value;
|
|
SaveIniFile();
|
|
}
|
|
|
|
private void CreateDefaultIni()
|
|
{
|
|
options.Clear();
|
|
|
|
options["Graphics"] = new Dictionary<string, string>
|
|
{
|
|
{ "Resolution", "1920x1080" },
|
|
{ "ScreenMode", "0" },
|
|
{ "Texture", "0" },
|
|
{ "Shadow", "2" },
|
|
{ "AA", "2" },
|
|
{ "VSync", "1" },
|
|
{ "AF", "1" }
|
|
};
|
|
|
|
options["Control"] = new Dictionary<string, string>
|
|
{
|
|
{ "MoveSpeed", "2" },
|
|
{ "RotateSpeed", "30" },
|
|
{ "ZoomSpeed", "50" }
|
|
};
|
|
SaveIniFile();
|
|
}
|
|
|
|
private void ReadIniFile()
|
|
{
|
|
options.Clear();
|
|
|
|
string currentSection = null;
|
|
|
|
foreach (var raw in File.ReadAllLines(filePath))
|
|
{
|
|
var line = raw.Trim();
|
|
|
|
if (string.IsNullOrEmpty(line) || line.StartsWith(";"))
|
|
continue;
|
|
|
|
if (line.StartsWith("[") && line.EndsWith("]"))
|
|
{
|
|
currentSection = line.Substring(1, line.Length - 2);
|
|
|
|
if (!options.ContainsKey(currentSection))
|
|
options[currentSection] = new Dictionary<string, string>();
|
|
}
|
|
else if (currentSection != null)
|
|
{
|
|
var split = line.Split('=', 2);
|
|
if (split.Length == 2)
|
|
options[currentSection][split[0]] = split[1];
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SaveIniFile()
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
foreach (var section in options)
|
|
{
|
|
sb.AppendLine($"[{section.Key}]");
|
|
foreach (var kv in section.Value)
|
|
sb.AppendLine($"{kv.Key}={kv.Value}");
|
|
sb.AppendLine();
|
|
}
|
|
File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8);
|
|
}
|
|
}
|
|
} |