-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
131 lines (114 loc) · 4.75 KB
/
Config.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;
using System.Linq;
using static Pythonic.ListHelpers;
using System.Windows.Media;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;
namespace KeyTrain
{
public static class ConfigManager
{
public static string profilePath => Settings["profilePath"];
public static dynamic dictionaryPaths { get => Settings["dictionaryPath"]; set => Settings["dictionaryPath"] = value; }
public static int lessonLength { get => Settings["lessonLength"]; set => Settings["lessonLength"] = value; }
static Dictionary<string, dynamic> defaultSettings = new Dictionary<string, dynamic>(){
{"lessonLength", 100 },
{"profilePath", "Profile/profile.kts" },
{"dictionaryPath", "Resources/dictionaryEN.txt" },
{"capitalsLevel", 2 },
{"generator", "random" },
{"presetText", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" },
{"emphasizedLetters", "" } //0: force lower, 1: keep existing, 2: half-half 3: first letter, 4: all caps
};
static Dictionary<string, dynamic> userSettings = new Dictionary<string, dynamic>();
static Dictionary<string, dynamic> styleSheet = new Dictionary<string, dynamic>();
public static ChainMap<string, dynamic> Settings { get; set; } =
ChainMap.FromDicts(userSettings, styleSheet, defaultSettings);
public static void ReadConfigFile(string path = "KeyTrain.cfg")
{
if (File.Exists(path) == false)
{
Trace.WriteLine("No configuration file found");
return;
}
foreach (string line in File.ReadAllLines(path)
//ignore empty or comments
.Where(l => (!string.IsNullOrWhiteSpace(l)) || l.StartsWith("//") ))
{
try{
var split = line.Split(":", 2);
string key = split[0].Trim();
string value = split[1].Trim();
if(value.StartsWith("'")) // string value
{
//makes a list all values inside ''
var result = new List<string>();
int start = 1;
do
{
int end = value.IndexOf("'", start);
string next = value.Substring(start, end - start);
result.Add(next);
start = value.IndexOf("'", end + 1) + 1;
} while (start > 0);
if (result.Count() == 1) Settings[key] = result.Single(); //a list with one element is converted into a single string
else Settings[key] = result;
}
else if (int.TryParse(value, out int result))
{
Settings[key] = result;
}
else
{
try
{
Settings[key] = new SolidColorBrush((Color)ColorConverter.ConvertFromString(value));
}
catch (FormatException)
{
Trace.WriteLine($"Unrecognized key '{key}' in ConfigManager; it will be ignored.");
}
}
}
catch
{
Trace.WriteLine($"Failed to read line in config file: {line}");
}
}
}
public static void WriteConfigFile(string path = "KeyTrain.cfg")
{
StreamWriter sw = new StreamWriter(path);
foreach (var item in Settings.dicts.First())
{
var v = item.Value;
if(v is IList)
{
List<object> en = (v as IEnumerable<object>).Cast<object>().ToList();
sw.WriteLine($"{item.Key}: {string.Join(", ", en.Select(x => DynamicToString(x)))}" );
}
else
{
sw.WriteLine($"{item.Key}: {DynamicToString(v)}" );
}
//Trace.WriteLine($"{item.Key}: {result}");
}
sw.Close();
}
static string DynamicToString(dynamic d)
{
if(d is string)
{
return $"'{d}'";
}
else
{
return d.ToString();
}
}
}
}