-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
178 lines (139 loc) · 5.9 KB
/
Program.cs
File metadata and controls
178 lines (139 loc) · 5.9 KB
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
// Made by GitMuslim
using System;
using System.IO;
using System.Threading.Tasks;
using System.Linq;
using UndertaleModLib.Util;
using UndertaleModLib.Models;
using UndertaleModLib;
using DataWinLoad.Utils;
using UndertaleModLib.Scripting;
using Microsoft.Extensions.Configuration;
using System.Diagnostics;
namespace DataWinLoad {
internal class DataWinLoad {
static public UndertaleData? data;
static public string? workingDir;
static public string? jsonPath;
static public Dictionary<string, UndertaleEmbeddedTexture> textureIDs = [];
static public Dictionary<string, UndertaleTexturePageItem> pageItemIDs = [];
static public Types.BuildJSON? json;
static public IConfigurationRoot? config;
static void Main(string[] args) {
// Handling unhandled errors, are they unhandled anymore?
AppDomain.CurrentDomain.UnhandledException += (sender, e) => {
Exception ex = (Exception)e.ExceptionObject;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Unhandled exception: " + ex.Message);
};
// Load or create the config.ini
string exeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string exeDirectory = Path.GetDirectoryName(exeFilePath);
string iniPath = Path.Combine(exeDirectory, "config.ini");
if (!File.Exists(iniPath)) {
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Please re-run, config file was created.");
File.Create(iniPath);
return;
}
config = new ConfigurationBuilder()
.AddIniFile("config.ini", true)
.Build();
// Kill game procces
if (config["gamePath"] != null) {
var name = Path.GetFileNameWithoutExtension(config["gamePath"]);
foreach (var process in Process.GetProcessesByName(name)) {
process.Kill();
}
}
// Warn dev mode
if (config["devMode"] == "true") {
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("DevMode Active, the main loop will repeat if you press the enter or space key");
Console.WriteLine("after the execution of the saving, you can exit by pressing 'X'");
}
// Benchmark
Stopwatch stopwatch = new();
stopwatch.Start();
// Get json path
if (config["jsonPath"] == null) {
Console.ForegroundColor = ConsoleColor.White;
Console.Write("Enter the path to the json: ");
jsonPath = Console.ReadLine();
if (jsonPath == null) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Input for path failed");
return;
};
} else {
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine($"Used preset json path: \"{config["jsonPath"]}\" (set in config.ini)");
jsonPath = config["jsonPath"];
}
// Load json
json = JSON.LoadJson(jsonPath);
if (json == null) return;
// Set working directory
workingDir = json?.workingDir;
// Load data
data = Data.LoadData(json?.dataPath);
if (data == null) return;
// Add and load textures
if (json?.textures == null) goto skipImageProccesing;
foreach (var texture in json?.textures) {
Sprites.AddTexture(data, texture);
}
// Add and load sprites
if (json?.sprites == null) goto skipImageProccesing;
foreach (var sprite in json?.sprites) {
Sprites.AddSprite(data, sprite);
}
skipImageProccesing:
// Add and load objects
foreach (var obj in json?.objects) {
Objects.AddObject(data, obj);
}
while (true) {
// Add and load scripts
foreach (var script in json?.scripts) {
Scripts.AddScript(data, script);
}
// Add and load object events
foreach (var obj in json?.objects) {
Objects.AddObjectEvents(data, obj);
}
// Dev mode management
if (config["devMode"] == "true") {
var key = Console.ReadKey();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"{key.KeyChar}");
if (key.Key == ConsoleKey.X) {
Console.WriteLine("Exited loop.");
break;
}
if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar) {
Data.SaveData(json?.outputDataPath, data);
continue;
}
} else {
break;
}
};
// Save data.win to the output path with all it's changes
Data.SaveData(json?.outputDataPath, data);
// Benchmark
stopwatch.Stop();
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"\n {stopwatch.Elapsed}");
// Start game if game path
if (config["gamePath"] != null) {
var game = new ProcessStartInfo(config["gamePath"]);
Process.Start(game);
} else {
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\nPress any key to close...");
Console.ReadKey();
}
}
}
}