make loading config from json

This commit is contained in:
Alexander Popov 2021-02-04 21:11:31 +03:00
parent e51612639d
commit 0c224c87d1
2 changed files with 13 additions and 17 deletions

View File

@ -9,6 +9,7 @@ class Program
static void Main() static void Main()
{ {
Application.Init(); Application.Init();
Colors.Base = Colors.Dialog; // WTF?!
var top = Application.Top; var top = Application.Top;
var mainWindow = new Window("Delver Save Editor") var mainWindow = new Window("Delver Save Editor")
@ -22,11 +23,11 @@ class Program
}) })
}); });
var delverPath = loadSettings(); var delverPath = loadGamePath();
var delverPathLabel = new Label("Delver path:") var delverPathLabel = new Label("Delver path:")
{ X = 1, Y = 1, Width = 13, Height = 1 }; { X = 1, Y = 1, Width = 13, Height = 1 };
var delverPathField = new TextField(delverPath) var delverPathField = new TextField(delverPath)
{ X = 14, Y = 1, Width = Dim.Fill(), Height = 1 }; { X = 14, Y = 1, Width = Dim.Fill() - 1, Height = 1 };
var savesListData = new List<string>() { "Save 0", "Save 1", "Save 3" }; var savesListData = new List<string>() { "Save 0", "Save 1", "Save 3" };
var savesList = new ListView(savesListData) { var savesList = new ListView(savesListData) {
@ -83,27 +84,23 @@ class Program
return n == 0; return n == 0;
} }
static string loadSettings() // return Delver path
static string loadGamePath()
{ {
string createConfigFile()
{
StreamWriter delverConfigPath = System.IO.File.CreateText("./config.txt");
delverConfigPath.WriteLine("C:\\");
delverConfigPath.Close();
return("C:\\");
}
// check config.json file exist // check config.json file exist
if (File.Exists("./config.txt")) if (File.Exists("./config.json"))
{ {
string jsonRaw = System.IO.File.ReadAllText("./config.txt"); string jsonString = System.IO.File.ReadAllText("./config.json");
var data = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonString);
return(jsonRaw); return(data["delver_path"]);
} }
else else
{ {
createConfigFile(); // create file with "default" path
StreamWriter configFile = System.IO.File.CreateText("./config.json");
configFile.WriteLine("{\n \"delver_path\": \"C:\\\\\"\n}");
configFile.Close();
return("C:\\"); return("C:\\");
} }

View File

@ -18,7 +18,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<!-- <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> -->
<PackageReference Include="Terminal.Gui" Version="0.90.3" /> <PackageReference Include="Terminal.Gui" Version="0.90.3" />
</ItemGroup> </ItemGroup>