KumiScript/loader/KSMetaParser.cs

43 lines
1.2 KiB
C#
Raw Normal View History

2024-01-26 05:37:51 +00:00
using System.Text.RegularExpressions;
namespace KumiScript.Loader
{
public class KSMetaParser
{
readonly string _path;
readonly StreamReader _streamReader;
readonly Dictionary<string, int> _properties;
public KSMetaParser(string path)
{
_path = path;
_streamReader = new StreamReader(File.OpenRead(path));
_properties = new Dictionary<string, int>(8);
ParseFile();
}
private void ParseFile()
{
while (!_streamReader.EndOfStream)
{
string? line = _streamReader.ReadLine();
if (line is null)
continue;
line = Regex.Replace(line, "\\s+", string.Empty);
string[] kvp = line.Split(":", 2);
int v;
if (!int.TryParse(kvp[1], out v))
throw new Exception("Bad file!");
_properties.Add(kvp[0], v);
}
}
public int GetAttribute(string name)
{
int v = 0;
_properties.TryGetValue(name, out v);
return v;
}
}
}