for Push.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KssSmaPlaLib.Ini
|
||||
{
|
||||
public static class IniReferenceResolveUxer
|
||||
{
|
||||
public static Dictionary<string, string> ReadSectionWithReference(string path, string section, int maxDepth = 3)
|
||||
{
|
||||
var allLines = File.ReadAllLines(path, System.Text.Encoding.GetEncoding("shift_jis"));
|
||||
var allSections = ParseAllSections(allLines);
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
if (!allSections.ContainsKey(section)) return result;
|
||||
|
||||
foreach (var kvp in allSections[section])
|
||||
{
|
||||
result[kvp.Key] = ResolveValue(allSections, kvp.Value, maxDepth);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Dictionary<string, Dictionary<string, string>> ParseAllSections(string[] lines)
|
||||
{
|
||||
var sections = new Dictionary<string, Dictionary<string, string>>();
|
||||
string currentSection = null;
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var trimmed = line.Trim();
|
||||
if (trimmed.StartsWith(";") || string.IsNullOrEmpty(trimmed)) continue;
|
||||
|
||||
if (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
|
||||
{
|
||||
currentSection = trimmed.Substring(1, trimmed.Length - 2);
|
||||
sections[currentSection] = new Dictionary<string, string>();
|
||||
}
|
||||
else if (currentSection != null && trimmed.Contains("="))
|
||||
{
|
||||
var index = trimmed.IndexOf('=');
|
||||
var key = trimmed.Substring(0, index).Trim();
|
||||
var value = trimmed.Substring(index + 1).Trim();
|
||||
sections[currentSection][key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return sections;
|
||||
}
|
||||
|
||||
private static string ResolveValue(Dictionary<string, Dictionary<string, string>> sections, string value, int depth)
|
||||
{
|
||||
if (depth <= 0 || string.IsNullOrEmpty(value)) return value;
|
||||
|
||||
var match = System.Text.RegularExpressions.Regex.Match(value, @"^\{\[(.+?)\](.+?)\}$");
|
||||
if (match.Success)
|
||||
{
|
||||
var refSection = match.Groups[1].Value;
|
||||
var refKey = match.Groups[2].Value;
|
||||
|
||||
if (sections.ContainsKey(refSection) && sections[refSection].ContainsKey(refKey))
|
||||
{
|
||||
var nextValue = sections[refSection][refKey];
|
||||
return ResolveValue(sections, nextValue, depth - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user