-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistry.cs
76 lines (69 loc) · 2.21 KB
/
Registry.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lika
{
internal class Registry
{
// Converts .reg with variables to .reg, save .reg to temp.
// Returns path to .reg file.
public static string RegWithVarsToRegSaveToTemp(string srcRegPath)
{
var lines = File.ReadAllLines(srcRegPath);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Length == 0 || lines[i].StartsWith("["))
{
continue;
}
KeyValue kv;
try
{
kv = new KeyValue(lines[i]);
}
catch
{
continue;
}
kv.Value = Utils.Variables.Decode(kv.Value);
if (!Utils.IsValidPath(kv.Value))
{
continue;
}
lines[i] = kv.ToString().Replace(@"\", @"\\");
}
var regFilePath = Utils.CreateTempFile(".reg");
File.WriteAllLines(regFilePath, lines);
return regFilePath;
}
private class KeyValue
{
public string Key = "";
private readonly bool isValueStr = false;
public string Value = "";
public KeyValue(string line)
{
var kv = line.Split('=', 2, StringSplitOptions.RemoveEmptyEntries);
if(kv.Length != 2)
{
throw new ArgumentException("Not an key-value");
}
Key = kv[0].TrimStart('"').TrimEnd('"');
isValueStr = kv[1].StartsWith('"');
Value = kv[1].TrimStart('"').TrimEnd('"');
}
public override string ToString()
{
var convertedValue = Value;
if(isValueStr)
{
convertedValue = '"' + convertedValue + '"';
}
var convertedKey = '"' + Key + '"';
return $"{convertedKey}={convertedValue}";
}
}
}
}