-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAppDataManager.cs
177 lines (159 loc) · 8.56 KB
/
AppDataManager.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Globalization;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Reflection;
namespace Shapoco {
internal static class AppDataManager {
public static bool UseAssemblyPath = false;
public static readonly string AssemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
public static string RoamingUserDataPath {
get {
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Application.CompanyName + Path.DirectorySeparatorChar
+ Application.ProductName + Path.DirectorySeparatorChar);
}
}
public static string LocalUserDataPath {
get {
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Application.CompanyName + Path.DirectorySeparatorChar
+ Application.ProductName + Path.DirectorySeparatorChar);
}
}
public static string ActiveDataPath
=> UseAssemblyPath ? AssemblyPath : RoamingUserDataPath;
public static void SavePropertiesToRoamingAppData(object targetObject, string fileName) {
StringBuilder sb = new StringBuilder();
foreach (var p in targetObject.GetType().GetProperties()) {
if (p.PropertyType.Equals(typeof(String))) {
// 文字列
var value = EscapeValue((string)p.GetValue(targetObject));
sb.AppendLine(p.Name + "=\"" + value + "\"");
}
else if (p.PropertyType.Equals(typeof(Color))) {
// 色
var value = (Color)p.GetValue(targetObject);
var valueStr = "00000000" + Convert.ToString(value.ToArgb(), 16);
valueStr = valueStr.Substring(valueStr.Length - 8);
sb.AppendLine(p.Name + "=#" + valueStr);
}
else if (p.PropertyType.IsPrimitive || p.PropertyType.IsEnum || p.PropertyType.IsValueType) {
// プリミティブ型または列挙型
sb.AppendLine(p.Name + "=" + FormattableString.Invariant($"{p.GetValue(targetObject)}"));
}
}
var filePath = Path.Combine(ActiveDataPath, fileName);
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
using (var writer = new StreamWriter(filePath)) {
writer.Write(sb.ToString());
}
}
public static void LoadPropertiesFromRoamingAppData(object targetObject, string fileName) {
var dictionary = new Dictionary<string, string>();
var filePath = Path.Combine(ActiveDataPath, fileName);
if (!File.Exists(filePath)) return;
// まず全部読み込む
using (var reader = new StreamReader(filePath)) {
while (!reader.EndOfStream) {
var line = reader.ReadLine();
// 空行とコメントは無視
if (string.IsNullOrEmpty(line)) continue;
if (line.StartsWith("#")) continue;
// 左辺と右辺に分解、分解できないものは無視
var args = line.Split(new char[] { '=' }, 2);
if (args.Length != 2) continue;
// 辞書に登録
var propName = args[0].Trim();
var propValue = args[1].Trim();
dictionary[propName] = propValue;
}
}
foreach (var p in targetObject.GetType().GetProperties()) {
if (!dictionary.ContainsKey(p.Name)) continue;
var valueStr = dictionary[p.Name];
try {
if (p.PropertyType.Equals(typeof(String))) {
// 文字列型
// "~" の形式でないものは無視
if (!valueStr.StartsWith("\"") || !valueStr.EndsWith("\"")) continue;
valueStr = UnescapeValue(valueStr.Substring(1, valueStr.Length - 2)); // "~"の中身を取り出す
p.SetValue(targetObject, valueStr);
}
else if (p.PropertyType.Equals(typeof(Boolean))) { p.SetValue(targetObject, Boolean.Parse(valueStr)); }
else if (p.PropertyType.Equals(typeof(Byte))) { p.SetValue(targetObject, Byte.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(SByte))) { p.SetValue(targetObject, SByte.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(Int16))) { p.SetValue(targetObject, Int16.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(UInt16))) { p.SetValue(targetObject, UInt16.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(Int32))) { p.SetValue(targetObject, Int32.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(UInt32))) { p.SetValue(targetObject, UInt32.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(Int64))) { p.SetValue(targetObject, Int64.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(UInt64))) { p.SetValue(targetObject, UInt64.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(Char))) { p.SetValue(targetObject, Char.Parse(valueStr)); }
else if (p.PropertyType.Equals(typeof(Double))) { p.SetValue(targetObject, Double.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(Single))) { p.SetValue(targetObject, Single.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.Equals(typeof(Decimal))) { p.SetValue(targetObject, Decimal.Parse(valueStr, CultureInfo.InvariantCulture)); }
else if (p.PropertyType.IsEnum) { p.SetValue(targetObject, Enum.Parse(p.PropertyType, valueStr)); }
else if (p.PropertyType.Equals(typeof(Color))) { p.SetValue(targetObject, Color.FromArgb(Convert.ToInt32(valueStr.Substring(1), 16))); }
else {
throw new NotImplementedException($"Unsupported type {p.PropertyType}.");
}
}
catch (Exception ex) {
Console.Error.WriteLine(ex);
}
}
}
public static string EscapeValue(string src) {
var sb = new StringBuilder();
int n = src.Length;
for (int i = 0; i < n; i++) {
char c = src[i];
switch (c) {
case '\r': sb.Append(@"\r"); break;
case '\n': sb.Append(@"\n"); break;
case '\0': sb.Append(@"\0"); break;
case '\t': sb.Append(@"\t"); break;
case '\\': sb.Append(@"\\"); break;
case '"': sb.Append("\\\""); break;
default: sb.Append(c); break;
}
}
return sb.ToString();
}
public static string UnescapeValue(string src) {
var sb = new StringBuilder();
int n = src.Length;
bool escaped = false;
for (int i = 0; i < n; i++) {
char c = src[i];
if (escaped) {
switch (c) {
case 'r': sb.Append('\r'); break;
case 'n': sb.Append('\n'); break;
case '0': sb.Append('\0'); break;
case 't': sb.Append('\t'); break;
case '\\': sb.Append('\\'); break;
case '"': sb.Append('"'); break;
default: sb.Append('\\').Append(c); break;
}
escaped = false;
}
else if (c == '\\') {
escaped = true;
}
else {
sb.Append(c);
}
}
return sb.ToString();
}
}
}