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 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(); 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> ParseAllSections(string[] lines) { var sections = new Dictionary>(); 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(); } 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> 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; } } }