AxibugEmuOnline/AxibugEmuOnline.Client/Assets/MyNes.Core/ISettings.cs

158 lines
3.8 KiB
C#
Raw Normal View History

2024-07-03 18:15:28 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
2024-07-22 13:09:27 +08:00
using Unity.IL2CPP.CompilerServices;
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
namespace MyNes.Core
2024-07-03 18:15:28 +08:00
{
2024-07-22 13:09:27 +08:00
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
[Il2CppSetOption(Option.DivideByZeroChecks, false)]
2024-07-03 18:22:22 +08:00
public abstract class ISettings
{
protected string filePath;
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
protected FieldInfo[] Fields;
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
public ISettings(string filePath)
{
this.filePath = filePath;
}
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
public virtual void LoadSettings()
{
Fields = GetType().GetFields();
if (!File.Exists(filePath))
{
return;
}
string[] array = File.ReadAllLines(filePath);
for (int i = 0; i < array.Length; i++)
{
string[] array2 = array[i].Split('=');
if (array2 != null && array2.Length == 2)
{
SetField(array2[0], array2[1]);
}
}
}
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
public virtual void SaveSettings()
{
Fields = GetType().GetFields();
List<string> list = new List<string>();
FieldInfo[] fields = Fields;
foreach (FieldInfo fieldInfo in fields)
{
if (fieldInfo.IsPublic)
{
list.Add(fieldInfo.Name + "=" + GetFieldValue(fieldInfo));
}
}
File.WriteAllLines(filePath, list.ToArray());
}
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
protected virtual void SetField(string fieldName, string val)
{
for (int i = 0; i < Fields.Length; i++)
{
if (!(Fields[i].Name == fieldName))
{
continue;
}
if (Fields[i].FieldType == typeof(string))
{
Fields[i].SetValue(this, val);
}
else if (Fields[i].FieldType == typeof(bool))
{
Fields[i].SetValue(this, val == "1");
}
else if (Fields[i].FieldType == typeof(int))
{
int result = 0;
if (int.TryParse(val, out result))
{
Fields[i].SetValue(this, result);
}
}
else if (Fields[i].FieldType == typeof(float))
{
float result2 = 0f;
if (float.TryParse(val, out result2))
{
Fields[i].SetValue(this, result2);
}
}
else if (Fields[i].FieldType == typeof(string[]))
{
string[] value = val.Split(new string[1] { "*" }, StringSplitOptions.RemoveEmptyEntries);
Fields[i].SetValue(this, value);
}
else
{
Tracer.WriteLine("Unknown setting type = " + Fields[i].FieldType);
}
break;
}
}
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
protected virtual string GetFieldValue(string fieldName)
{
for (int i = 0; i < Fields.Length; i++)
{
if (Fields[i].Name == fieldName)
{
return GetFieldValue(Fields[i]);
}
}
return "";
}
2024-07-03 18:15:28 +08:00
2024-07-03 18:22:22 +08:00
protected virtual string GetFieldValue(FieldInfo field)
{
object value = field.GetValue(this);
if (field.FieldType == typeof(string))
{
return value.ToString();
}
if (field.FieldType == typeof(bool))
{
if (!(bool)value)
{
return "0";
}
return "1";
}
if (field.FieldType == typeof(int))
{
return value.ToString();
}
if (field.FieldType == typeof(float))
{
return value.ToString();
}
if (field.FieldType == typeof(string[]))
{
string text = "";
string[] array = (string[])value;
if (array != null)
{
string[] array2 = array;
foreach (string text2 in array2)
{
text = text + text2 + "*";
}
}
if (text.Length > 0)
{
return text.Substring(0, text.Length - 1);
}
return "";
}
return "";
}
}
2024-07-03 15:40:13 +08:00
}