Files
GoakeFlax/Source/Game/Console/ConfigParser.cs
2023-03-02 22:07:43 +02:00

51 lines
1.3 KiB
C#

using System.Collections.Generic;
using System.IO;
namespace Game
{
public class ConfigParser
{
private ConfigParser()
{
}
public static Config ParseFile(string path)
{
Config config = new Config();
if (!File.Exists(path))
{
Console.Print($"Config file not found in path: {path}");
return config;
}
/*using*/ FileStream file = File.OpenRead(path);
/*using*/ StreamReader sr = new StreamReader(file);
List<string> commands = new List<string>();
string line;
while ((line = sr.ReadLine()?.Trim()) != null)
{
if (line.StartsWith(@"//"))
continue;
int spacePos = line.IndexOf(' ');
if (spacePos == -1)
{
commands.Add(line);
continue;
}
string key = line.Substring(0, spacePos);
string value = line.Substring(spacePos+1);
value = value.Trim('"');
config[key] = value;
}
config.Commands = commands.ToArray();
return config;
}
}
}