-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
184 lines (165 loc) · 8.85 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
using Newtonsoft.Json;
namespace TruckTransmissionGenerator {
// TODO
// - add check for empty string in json (f.ex. empty truck in array)
// - add default json location
class TruckTransmissionGenerator {
static string VerifyJsonFile(string? jsonFile) {
// check if jsonFile has been passed as argument, if not ask for input
if(jsonFile == null) {
Console.WriteLine("Please specify json location");
Console.Write("Input: ");
while(string.IsNullOrWhiteSpace(jsonFile = Console.ReadLine().Replace("\"",""))) {
Console.SetCursorPosition(0,Console.CursorTop - 1);
Console.Write("Input: ");
}
} else {
Console.WriteLine(jsonFile + "\n");
}
// check if jsonFile exists, if not ask for new input
while(!File.Exists(jsonFile)) {
Console.BackgroundColor = ConsoleColor.DarkRed; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("File does not exist! Please enter valid json file");
Console.ResetColor();
Console.Write("Input: ");
while(string.IsNullOrWhiteSpace(jsonFile = Console.ReadLine().Replace("\"",""))) {
Console.SetCursorPosition(0,Console.CursorTop - 1);
Console.Write("Input: ");
}
}
Console.WriteLine("\u2713 Valid JSON File");
return jsonFile;
}
static dynamic GetJsonData(string jsonFile) {
string jsonString = new StreamReader(jsonFile).ReadToEnd();
dynamic? jsonData = JsonConvert.DeserializeObject(jsonString);
if(jsonFile == null || jsonString.Length == 0) {
Console.WriteLine("JSON File is empty. Please try again");
Environment.Exit(1);
}
return jsonData;
}
static void SetData(Data data) {
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Receiving data from input...");
try {
Console.WriteLine("... JSON data received");
data.TemplateFolder = data.JsonData.templateFolder;
data.TransmissionsFiles = Directory.GetFiles(data.TemplateFolder,"*.sii");
Console.WriteLine("... Transmission Templates received");
data.OutputFolder = data.JsonData.outputFolder;
Console.WriteLine("... Output directory set");
Console.ResetColor();
}
catch(Exception e) {
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
static void DeleteExistingFiles(string directory) {
if(Directory.Exists(directory + "/def")) {
Directory.Delete(directory + "/def",true);
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("\nExisting directory deleted!\n");
Console.ResetColor();
}
}
static void PressAnyKeyTo(string keyword, ConsoleColor color) {
Console.ForegroundColor = color;
Console.WriteLine($"Press any key to {keyword}...");
Console.ResetColor();
Console.ReadKey();
}
static string ModifySiiFile(string siiFile, dynamic truck, dynamic transmission, dynamic differential_ratio) {
siiFile = siiFile.Replace("@name",transmission.name.ToString());
siiFile = siiFile.Replace("@differential_ratio_name",differential_ratio.ToString().Replace(".",""));
siiFile = siiFile.Replace("@truck",truck.ToString());
siiFile = siiFile.Replace("@displayName",$"\"{transmission.displayName.ToString()}\"");
siiFile = siiFile.Replace("@price",transmission.price.ToString());
siiFile = siiFile.Replace("@unlock",transmission.unlock.ToString());
siiFile = siiFile.Replace("@differential_ratio",differential_ratio.ToString());
return siiFile;
}
static void Main(string[] args) {
Console.Title = "TruckTransmissionGenerator";
Data data = new();
data.JsonFile = VerifyJsonFile(args[0]);
data.JsonData = GetJsonData(data.JsonFile);
// DEBUG
//Console.WriteLine($"JsonFile: {data.JsonFile}");
//Console.WriteLine(data.JsonData);
//Console.WriteLine(data.TemplateFolder);
//Console.WriteLine(data.OutputFolder);
//Console.WriteLine(data.TransmissionsFiles[0]);
try {
Output ConsoleOutput = new();
ConsoleOutput.WriteGameTitel(data.JsonData.game.ToString());
SetData(data);
ConsoleOutput.WriteDataFound(data.JsonData);
PressAnyKeyTo("continue",ConsoleColor.White);
DeleteExistingFiles(data.OutputFolder);
foreach(var truck in data.JsonData.trucks) {
foreach(var transmission in data.JsonData.transmissions) {
Console.Write($"{truck}: Added {transmission.name}\t");
foreach(var differential_ratio in data.JsonData.differential_ratios) {
// read the template transmission file
string siiFile = File.ReadAllText($@"{data.TemplateFolder}\{transmission.name}.sii");
// replacing placeholder values in template transmission file with data from the json file
siiFile = ModifySiiFile(siiFile,truck,transmission,differential_ratio);
// setting string variable to truck transmission folder
string outputFolderFull = $@"{data.OutputFolder}\def\vehicle\truck\{truck.ToString()}\transmission";
// creates said directory, likely doesn't exist because all folders will be deleted with DeleteExistingFiles();
Directory.CreateDirectory(outputFolderFull);
// setting file name
string outputFile = $@"{outputFolderFull}\{transmission.name.ToString()}.{differential_ratio.ToString().Replace(".","")}.sii";
// writes file to folder
File.WriteAllText(outputFile,siiFile);
Console.Write($" ({differential_ratio})");
}
Console.Write("\n");
}
Console.Write("\n");
Console.ForegroundColor = Output.RandomConsoleColor();
}
var totalCount = data.JsonData.trucks.Count * data.JsonData.transmissions.Count * data.JsonData.differential_ratios.Count;
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"\nFinished! (Total: {totalCount})\n");
Console.ForegroundColor = ConsoleColor.White;
PressAnyKeyTo("exit",ConsoleColor.DarkGreen);
}
catch(Exception e) {
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
finally {
Environment.Exit(0);
}
}
class Output {
private static Random _random = new Random();
private static ConsoleColor GetRandomConsoleColor() {
var consoleColors = Enum.GetValues(typeof(ConsoleColor));
string[] c = new string[] { "ConsoleColor.DarkGreen","ConsoleColor.DarkRed" };
return (ConsoleColor)consoleColors.GetValue(_random.Next(consoleColors.Length));
}
public static ConsoleColor RandomConsoleColor() {
return (ConsoleColor)new Random().Next(1,15);
}
public void WriteGameTitel(string gameTitle) {
Console.ResetColor();
Console.BackgroundColor = ConsoleColor.DarkRed; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"\n {gameTitle} \n");
Console.ResetColor();
}
public void WriteDataFound(dynamic data) {
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("\n---------------------------------");
Console.WriteLine($"| Found {data.trucks.Count} trucks\t\t|");
Console.WriteLine($"| Found {data.transmissions.Count} transmissions\t\t|");
Console.WriteLine($"| Found {data.differential_ratios.Count} differential ratios\t|");
Console.WriteLine("---------------------------------\n");
Console.ResetColor();
}
}
}
}