Skip to content

Commit 7483043

Browse files
committed
Add ConfigLoader.cs
1 parent 5879870 commit 7483043

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace RepostConfirmationCanceler
10+
{
11+
internal class Section
12+
{
13+
public string Name;
14+
public bool Disabled = false;
15+
public bool WarningWhenCloseDialog = false;
16+
public StringBuilder ExcludeGroups = new StringBuilder();
17+
public StringBuilder Excludes = new StringBuilder();
18+
public StringBuilder Patterns = new StringBuilder();
19+
20+
public Section(string name)
21+
{
22+
Name = name;
23+
}
24+
}
25+
26+
internal class Config
27+
{
28+
public bool IgnoreQueryString = false;
29+
public bool OnlyMainFrame = false;
30+
public List<Section> SectionList = new List<Section>();
31+
32+
public Section GetEdgeSection()
33+
{
34+
return SectionList.FirstOrDefault(_ => _.Name.ToLowerInvariant() == "[edge]");
35+
}
36+
}
37+
38+
internal static class ConfigLoader
39+
{
40+
internal static Config LoadConfig()
41+
{
42+
var ruleFilePath = GetRulefilePath();
43+
if (string.IsNullOrEmpty(ruleFilePath))
44+
{
45+
Console.Error.WriteLine("Rulefile path is not set in the registry.");
46+
return new Config();
47+
}
48+
using (var fileStream = new FileStream(ruleFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
49+
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 1024, true))
50+
{
51+
string data = streamReader.ReadToEnd();
52+
return ParseConf(data);
53+
}
54+
}
55+
56+
internal static string GetRulefilePath()
57+
{
58+
const string registryPath = @"SOFTWARE\RepostConfirmationCanceler";
59+
const string valueName = "Rulefile";
60+
try
61+
{
62+
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryPath))
63+
{
64+
if (key == null)
65+
{
66+
Console.Error.WriteLine($"cannot read {registryPath}: key not found");
67+
return null;
68+
}
69+
object value = key.GetValue(valueName);
70+
if (value is string rulefile)
71+
{
72+
return rulefile;
73+
}
74+
else
75+
{
76+
Console.Error.WriteLine($"cannot read {registryPath}: 'Rulefile' not found or not string");
77+
return null;
78+
}
79+
}
80+
}
81+
catch (Exception ex)
82+
{
83+
Console.Error.WriteLine($"cannot read {registryPath}: {ex.Message}");
84+
return null;
85+
}
86+
}
87+
88+
internal static Config ParseConf(string data)
89+
{
90+
var conf = new Config();
91+
bool global = false;
92+
Section section = null;
93+
94+
var lines = data.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
95+
foreach (var rawLine in lines)
96+
{
97+
string line = rawLine.Trim();
98+
if (string.IsNullOrEmpty(line))
99+
continue;
100+
101+
switch (line[0])
102+
{
103+
case ';':
104+
case '#':
105+
// コメント行
106+
break;
107+
case '[':
108+
global = false;
109+
if (line == "[GLOBAL]")
110+
{
111+
global = true;
112+
}
113+
else
114+
{
115+
// 新しいセクション追加
116+
section = new Section(line);
117+
conf.SectionList.Add(section);
118+
}
119+
break;
120+
case '@':
121+
if (global)
122+
{
123+
if (line == "@TOP_PAGE_ONLY")
124+
{
125+
conf.IgnoreQueryString = true;
126+
}
127+
else if (line == "@ONLY_MAIN_FRAME")
128+
{
129+
conf.OnlyMainFrame = true;
130+
}
131+
}
132+
else if (section != null)
133+
{
134+
if (line == "@DISABLED")
135+
{
136+
section.Disabled = true;
137+
}
138+
else if (line == "@WARNING_WHEN_CLOSE_DIALOG")
139+
{
140+
section.WarningWhenCloseDialog = true;
141+
}
142+
else if (line.StartsWith("@EXCLUDE_GROUP:"))
143+
{
144+
section.ExcludeGroups.Append(line.Substring("@EXCLUDE_GROUP:".Length));
145+
section.ExcludeGroups.Append('\n');
146+
}
147+
}
148+
break;
149+
case '-':
150+
if (section != null && !line.StartsWith("-#"))
151+
{
152+
section.Excludes.AppendLine(line);
153+
}
154+
break;
155+
default:
156+
if (section != null)
157+
{
158+
section.Patterns.AppendLine(line);
159+
}
160+
break;
161+
}
162+
}
163+
return conf;
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)