189 lines
6.9 KiB
C#
189 lines
6.9 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using System.Runtime.InteropServices;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Text;
|
|||
|
|
using System;
|
|||
|
|
|
|||
|
|
namespace GeneralTools
|
|||
|
|
{
|
|||
|
|
public class IniFile
|
|||
|
|
{
|
|||
|
|
protected static Dictionary<string, Dictionary<string, Dictionary<string, string>>> dataDict = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
|
|||
|
|
public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
return Read(Section, Key, NoText, iniFilePath);
|
|||
|
|
}
|
|||
|
|
public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
return Write(Section, Key, Value, iniFilePath);
|
|||
|
|
}
|
|||
|
|
public static void ClearCache(string iniFilePath)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrWhiteSpace(iniFilePath))
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dataDict.Remove(iniFilePath);
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
dataDict.Remove(Path.GetFullPath(iniFilePath));
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
// 忽略路径规范化失败,原始键已尝试清理。
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static T GetTypeValue<T>(string section, string key, T defultValue, string iniFilePath) where T : IComparable
|
|||
|
|
{
|
|||
|
|
string s = ReadIniData(section, key, "", iniFilePath);
|
|||
|
|
object obj;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
obj = (T)Convert.ChangeType(s, typeof(T));
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
obj = defultValue;
|
|||
|
|
}
|
|||
|
|
return (T)obj;
|
|||
|
|
}
|
|||
|
|
#if UNITY_STANDALONE_WIN
|
|||
|
|
[DllImport("kernel32", CharSet = CharSet.Auto)]
|
|||
|
|
private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
|
|||
|
|
[DllImport("kernel32", CharSet = CharSet.Auto)]
|
|||
|
|
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
|
|||
|
|
public static string Read(string Section, string Key, string NoText, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
if (File.Exists(iniFilePath))
|
|||
|
|
{
|
|||
|
|
StringBuilder temp = new StringBuilder(1024);
|
|||
|
|
GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
|
|||
|
|
return temp.ToString();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return string.Empty;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public static bool Write(string Section, string Key, string Value, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
if (!File.Exists(iniFilePath))
|
|||
|
|
{
|
|||
|
|
using (File.Create(iniFilePath)) { }
|
|||
|
|
}
|
|||
|
|
long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
|
|||
|
|
return OpStation != 0;
|
|||
|
|
}
|
|||
|
|
#else
|
|||
|
|
private static string Read(string Section, string Key, string NoText, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
iniFilePath = Path.GetFullPath(iniFilePath);
|
|||
|
|
if (File.Exists(iniFilePath))
|
|||
|
|
{
|
|||
|
|
return GetValue(Section, Key, NoText, iniFilePath);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return NoText;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private static bool Write(string Section, string Key, string Value, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
iniFilePath = Path.GetFullPath(iniFilePath);
|
|||
|
|
if (!File.Exists(iniFilePath))
|
|||
|
|
{
|
|||
|
|
using (File.Create(iniFilePath)) { }
|
|||
|
|
}
|
|||
|
|
SetValue(Section, Key, Value, iniFilePath);
|
|||
|
|
WriteIni(iniFilePath);
|
|||
|
|
dataDict[iniFilePath] = ReadIniFile(iniFilePath);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
#endif
|
|||
|
|
|
|||
|
|
|
|||
|
|
private static string GetValue(string Section, string Key, string NoText, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
if (!dataDict.ContainsKey(iniFilePath))
|
|||
|
|
{
|
|||
|
|
dataDict[iniFilePath] = ReadIniFile(iniFilePath);
|
|||
|
|
}
|
|||
|
|
if (!dataDict[iniFilePath].ContainsKey(Section))
|
|||
|
|
{
|
|||
|
|
return NoText;
|
|||
|
|
}
|
|||
|
|
else if (!dataDict[iniFilePath][Section].ContainsKey(Key))
|
|||
|
|
{
|
|||
|
|
return NoText;
|
|||
|
|
}
|
|||
|
|
return dataDict[iniFilePath][Section][Key];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static Dictionary<string, Dictionary<string, string>> SetValue(string Section, string Key, string Value, string iniFilePath)
|
|||
|
|
{
|
|||
|
|
Dictionary<string, Dictionary<string, string>> groupDict = new Dictionary<string, Dictionary<string, string>>();
|
|||
|
|
if (!dataDict.ContainsKey(iniFilePath))
|
|||
|
|
{
|
|||
|
|
dataDict[iniFilePath] = ReadIniFile(iniFilePath);
|
|||
|
|
}
|
|||
|
|
groupDict = dataDict[iniFilePath];
|
|||
|
|
if (!groupDict.ContainsKey(Section))
|
|||
|
|
{
|
|||
|
|
groupDict[Section] = new Dictionary<string, string>();
|
|||
|
|
}
|
|||
|
|
groupDict[Section][Key] = Value;
|
|||
|
|
return groupDict;
|
|||
|
|
}
|
|||
|
|
private static void WriteIni(string iniFilePath)
|
|||
|
|
{
|
|||
|
|
if (!dataDict.ContainsKey(iniFilePath))
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
Dictionary<string, Dictionary<string, string>> groupDict = dataDict[iniFilePath];
|
|||
|
|
string waitWriteStr = "";
|
|||
|
|
foreach (var group in groupDict)
|
|||
|
|
{
|
|||
|
|
waitWriteStr += $"[{group.Key}]" + "\r\n";
|
|||
|
|
foreach (var kw in group.Value)
|
|||
|
|
{
|
|||
|
|
waitWriteStr += $"{kw.Key}={kw.Value}" + "\r\n";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
File.WriteAllText(iniFilePath, waitWriteStr, Encoding.UTF8);
|
|||
|
|
}
|
|||
|
|
private static Dictionary<string, Dictionary<string, string>> ReadIniFile(string iniFilePath)
|
|||
|
|
{
|
|||
|
|
Dictionary<string, Dictionary<string, string>> groupDict = new Dictionary<string, Dictionary<string, string>>();
|
|||
|
|
|
|||
|
|
string[] textArr = File.ReadAllLines(iniFilePath);
|
|||
|
|
string nowGroupName = "";
|
|||
|
|
for (int i = 0; i < textArr.Length; i++)
|
|||
|
|
{
|
|||
|
|
textArr[i] = textArr[i].Trim();
|
|||
|
|
if (textArr[i].StartsWith(";") || textArr[i].StartsWith(";") || textArr[i].StartsWith("#") || textArr[i].StartsWith("-") || textArr[i].StartsWith("!"))
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
if (textArr[i].Contains("=") && groupDict.ContainsKey(nowGroupName))
|
|||
|
|
{
|
|||
|
|
string[] kw = textArr[i].Split('=');
|
|||
|
|
groupDict[nowGroupName][kw[0]] = kw[1];
|
|||
|
|
}
|
|||
|
|
else if (textArr[i].StartsWith("[") && textArr[i].EndsWith("]"))
|
|||
|
|
{
|
|||
|
|
//表示是组
|
|||
|
|
string groupName = textArr[i].TrimStart('[').TrimEnd(']');
|
|||
|
|
nowGroupName = groupName;
|
|||
|
|
groupDict[groupName] = new Dictionary<string, string>();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return groupDict;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|