-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
192 lines (180 loc) · 7.39 KB
/
Program.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.IO;
using System.Xml;
using Newtonsoft.Json.Linq;
using Tommy;
namespace WUAICT
{
class Program
{
static void Main(string[] args)
{
string defaultPrograms;
string otherProgramToDisplay;
string XMLsavePath;
string ventoyConfigPath;
//Check if config exist
if (!File.Exists("config.toml"))
{
Console.WriteLine("Nie znaleziono pliku konfiguracyjnego!\nGeneruje nowy...");
TomlTable toml = new TomlTable
{
["WUAICT"]=
{
["defaultPrograms"] = "firefox google-chrome notepad++ vlc onlyoffice-desktopeditors anydesk 7zip",
["otherProgramToDisplay"] = "microsoft-teams teamspeak discord obs-studio steam origin opera epic-games-launcher",
},
["ventoy"] =
{
["XMLsavePath"] = "../",
["ConfigPath"] = "/../ventoy/ventoy.json"
}
};
using (StreamWriter writer = File.CreateText("config.toml"))
{
toml.WriteTo(writer);
writer.Flush();
}
PrintAndWait("Pomyślnie wygenerowano plik konfiguracyjny, zedytuj go!");
return;
}
//Config get
using (StreamReader reader = File.OpenText("config.toml"))
{
TomlTable table = TOML.Parse(reader);
defaultPrograms = table["WUAICT"]["defaultPrograms"];
otherProgramToDisplay = table["WUAICT"]["otherProgramToDisplay"];
ventoyConfigPath = table["ventoy"]["ConfigPath"];
XMLsavePath = table["ventoy"]["XMLsavePath"];
}
//instalation config
string[] arguments = Environment.GetCommandLineArgs();
string filePath;
string WindowsKey;
string programsToInstall;
if ((arguments.Length > 1) && arguments[1].EndsWith(".toml") && File.Exists(arguments[1]))
{
using (StreamReader reader = File.OpenText(arguments[1]))
{
TomlTable table = TOML.Parse(reader);
filePath = table["filePath"];
WindowsKey = GetKey(table["options"]["keyGetMethod"]);
programsToInstall = table["options"]["programsToInstall"];
}
}
else if ((arguments.Length > 1) && arguments[1].EndsWith(".xml") && File.Exists(arguments[1]))
{
filePath = arguments[1];
WindowsKey = GetKey();
programsToInstall = GetProgramsToInstall(defaultPrograms, otherProgramToDisplay);
}
else
{
filePath = getXMLfile();
WindowsKey = GetKey();
programsToInstall = GetProgramsToInstall(defaultPrograms, otherProgramToDisplay);
}
//Set output file path
DateTime dt = DateTime.Now;
string creationdate = dt.Year + "-" + dt.Month + "-" + dt.Day + "_" + dt.Hour + "-" + dt.Minute + "-" + dt.Second;
string outputfilename = creationdate + "-" + WindowsKey.Substring(Math.Max(0, WindowsKey.Length - 5)) + ".xml";
Console.WriteLine(outputfilename);
string outputFilePath = Path.Combine(XMLsavePath, outputfilename);
//Open and load xml
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(filePath);
//Change productkey
XmlNode n = doc.GetElementsByTagName("ProductKey")[1];
n.InnerText = WindowsKey;
//Change programs to install
var index = 0;
XmlNodeList commandlist = doc.GetElementsByTagName("CommandLine");
foreach (XmlNode item in commandlist)
{
if (item.InnerText.Contains("just-install "))
{
break;
}
index++;
}
XmlNode programNode = commandlist[index];
programNode.InnerText = "just-install " + programsToInstall;
//Save xml
doc.Save(outputFilePath);
//Ventoy edit config
var outputFilePathforVentoy = Path.GetFullPath(outputFilePath).Substring(2, Path.GetFullPath(outputFilePath).Length-2);
outputFilePathforVentoy = outputFilePathforVentoy.Replace('\\', '/');
if (!File.Exists(ventoyConfigPath))
{
PrintAndWait("Nie znaleziono pliku " + ventoyConfigPath);
return;
}
var configv = File.ReadAllText(ventoyConfigPath);
JObject ventoyconf = JObject.Parse(configv);
int WindowsIsoLocation =0;
foreach (var element in ventoyconf["auto_install"])
{
if (element["image"].ToString().ToLower().Contains("windows"))
{
break;
}
WindowsIsoLocation++;
}
var xd = ventoyconf["auto_install"][WindowsIsoLocation].SelectToken("template");
xd.Last.AddAfterSelf(outputFilePathforVentoy);
File.WriteAllText(ventoyConfigPath, ventoyconf.ToString());
}
static void PrintAndWait(string komunikat="")
{
Console.WriteLine(komunikat);
Console.ReadLine();
}
static string getXMLfile(){
string file =null;
while (file == null) {
Console.WriteLine("Podaj ścieżkę do pliku xml");
file = Console.ReadLine();
if (!File.Exists(file))
{
Console.WriteLine("ścieżka nie istnieje");
file = null;
}
}
return file;
}
static string GetProgramsToInstall(string defaultPrograms, string otherProgramToDisplay)
{
Console.WriteLine("Wybierz programy do instalacji. Aby wybrać domyślne wybierz enter");
Console.WriteLine("Domyślne programy: " + defaultPrograms);
Console.WriteLine("Inne dostępne porgramy:" + otherProgramToDisplay);
var choose = Console.ReadLine();
if (choose == "")
{
choose = defaultPrograms;
}
Console.WriteLine("Wybrane programy: " + choose);
return choose;
}
static string GetKey(string keyGetMethod=null)
{
if (keyGetMethod==null || keyGetMethod=="" || keyGetMethod == "ask") {
Console.WriteLine("Podaj klucz do Windowsa lub kliknij enter aby poszukać keyfinderem");
var choose = Console.ReadLine();
if (choose == "")
{
keyGetMethod = KeyDecoder.GetWindowsProductKeyFromRegistry();
}
else
{
keyGetMethod = choose;
}
}else if (keyGetMethod == "KeyFind")
{
keyGetMethod = KeyDecoder.GetWindowsProductKeyFromRegistry();
}
Console.WriteLine("Klucz:" + keyGetMethod);
return keyGetMethod;
}
}
}