forked from ivanmeler/SamFirm_Reborn
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Settings.cs
72 lines (66 loc) · 2.79 KB
/
Settings.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
namespace hadesFirm
{
internal class Settings
{
public static T ReadSetting<T>(string element)
{
bool returnString = typeof(T) == typeof(string);
bool returnList = typeof(T) == typeof(string[]);
if (!returnString && !returnList)
{
throw new ArgumentException("Return value must be String or String[] !");
}
string AppLocation = System.AppDomain.CurrentDomain.BaseDirectory;
string SettingFile = AppLocation + "hadesFirm.xml";
try
{
if (!File.Exists(SettingFile))
Settings.GenerateSettings();
string value = XDocument.Load(SettingFile).Element((XName)"hadesFirm")?.Element((XName)element)?.Value;
if (returnString)
return (T)(object)(value ?? string.Empty);
return (T)(object)(value?.Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0]);
}
catch (Exception ex)
{
Logger.WriteLog("Error reading config file: " + ex.Message, false);
return returnString ? (T)(object)string.Empty : (T)(object)new string[0];
}
}
public static void SetSetting(string element, string value)
{
try
{
string AppLocation = System.AppDomain.CurrentDomain.BaseDirectory;
string SettingFile = AppLocation + "hadesFirm.xml";
if (!File.Exists(SettingFile))
Settings.GenerateSettings();
XDocument xdocument = XDocument.Load(SettingFile);
XElement xelement = xdocument.Element((XName)"hadesFirm").Element((XName)element);
if (xelement == null)
xdocument.Element((XName)"hadesFirm").Add((object)new XElement((XName)element, (object)value));
else
xelement.Value = value;
xdocument.Save(SettingFile);
}
catch (Exception ex)
{
Logger.WriteLog($"Error writing {element} to config file: " + ex.Message, false);
}
}
public static void SetSetting(string element, IEnumerable<string> values)
{
SetSetting(element, string.Join(" ", values));
}
private static void GenerateSettings()
{
string AppLocation = System.AppDomain.CurrentDomain.BaseDirectory;
string SettingFile = AppLocation + "hadesFirm.xml";
File.WriteAllText(SettingFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<hadesFirm>\r\n <SaveFileDialog></SaveFileDialog>\r\n <AutoInfo>true</AutoInfo>\r\n\t<Region></Region>\r\n\t<Model></Model>\r\n\t<Models></Models>\r\n\t<PDAVer></PDAVer>\r\n\t<CSCVer></CSCVer>\r\n\t<PHONEVer></PHONEVer>\r\n <BinaryNature></BinaryNature>\r\n <CheckCRC></CheckCRC>\r\n <AutoDecrypt></AutoDecrypt>\r\n</hadesFirm>");
}
}
}