-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.cs
114 lines (97 loc) · 3.78 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
using log4net;
using OftobTech.AppLocalizator.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace OftobTech.AppLocalizator
{
public class Config
{
private static readonly ILog _log = LogManager.GetLogger(typeof(Config));
public static ConfigModel? _config = null;
public static string app_path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
/// <summary>
/// Publishe the configuration file to the file system
/// </summary>
/// <exception cref="Exception"></exception>
public static bool PublishConfig()
{
if (Directory.Exists(app_path + "\\" + ConfigResource.config_directory_name))
{
if (File.Exists(ConfigResource.config_path))
{
throw new Exception("Lang config file is already exists, please delete them and run this command one more time");
}
}
else
{
Directory.CreateDirectory(app_path + "\\" + ConfigResource.config_directory_name);
}
var stream = File.Create(app_path + "\\" + ConfigResource.config_path);
stream.Write(Encoding.UTF8.GetBytes(ConfigResource.def_config));
stream.Close();
return true;
}
/// <summary>
/// Reading the config, the config is read from the file system
/// if it was created, if not, the default config is taken
/// </summary>
/// <returns>Configuration сontent</returns>
public static ConfigModel ReadConfig()
{
var config = new ConfigModel();
Dictionary<string, string>? configData = null;
Console.WriteLine("path to config: " + app_path + "\\" + ConfigResource.config_path);
if (!File.Exists(app_path + "\\" + ConfigResource.config_path))
{
configData = Parser.ParseFile(app_path + "\\" + ConfigResource.config_path);
if (configData == null)
{
throw new Exception("Read configuration error");
}
}
else
{
var fileContent = File.ReadAllText(app_path + "\\" + ConfigResource.config_path);
configData = Parser.ParseText(fileContent);
}
config.DefaultLang = TryGetValue(configData, "DefaultLang", "en");
config.LangsFilesPath = TryGetValue(configData, "LangsFilesPath", @"Langs");
return config;
}
/// <summary>
/// Update local variable from file or resource
/// </summary>
public static void UpdateConfig()
{
_config = ReadConfig();
}
/// <summary>
/// Retview config
/// </summary>
/// <returns></returns>
public static ConfigModel? getConfig()
{
return _config;
}
#pragma warning disable CS8629 // Тип значения, допускающего NULL, может быть NULL.
/// <summary>
/// An attempt to get a value by key, otherwise we return the default value
/// </summary>
/// <param name="dict"></param>
/// <param name="key"></param>
/// <param name="default_value"></param>
/// <returns></returns>
private static string? TryGetValue(Dictionary<string, string> dict, string key, string? default_value = null)
{
if (dict.TryGetValue(key, out var value))
{
return value;
}
return default_value;
}
#pragma warning restore CS8629 // Тип значения, допускающего NULL, может быть NULL.
}
}