-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
327 lines (250 loc) · 9.32 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
// TODO: Get texture paths from .map files instead using regex?
// The regex in question: /^(?:\s+)?(?:\(\s(?:\-?\d+(?:\.\d+(?:e\-\d+)?)?\s){3}\)\s?){3}\s([A-z\d\.\_\-\/]+)/gm
// TODO: Copy .map file to %TEMP% so that stuff like "auxiliary\NULL" can be changed to "NULL" instead without affecting original file?
namespace Map2DifWrapper
{
class Config
{
public string TexturesPath { get; set; }
public bool CopyTextures { get; set; }
public bool SilentMode { get; set; }
}
class Program
{
public static string version = "1.0.1";
public static Config config;
static int Main(string[] args)
{
if (!File.Exists("map2dif_wrapper.yaml"))
{
Debug.WriteLine("Config file not found.");
if (!CreateConfigFile())
{
ShowMessage("Failed to create config file in the current directory.");
return -1;
}
Debug.WriteLine("Config file created.");
}
if (!ReadConfigFile())
{
ShowMessage("The config file is invalid.");
return -1;
}
if (!ValidateConfig())
{
return -1;
}
string[] fileNames = { "map2dif_plus.exe", "map2dif_plus_MBG.exe", "map2dif.exe", "map2dif_DEBUG.exe" };
string selectedFileName = null;
foreach (string fileName in fileNames)
{
if (File.Exists(fileName))
{
Debug.WriteLine("Found " + fileName);
selectedFileName = fileName;
break;
}
}
if (String.IsNullOrEmpty(selectedFileName))
{
ShowMessage("Could not find a map2dif executable in the current directory.");
return -1;
}
Process map2dif = new Process();
map2dif.StartInfo.FileName = selectedFileName;
map2dif.StartInfo.UseShellExecute = false;
map2dif.StartInfo.RedirectStandardOutput = true;
string textureArg = null;
if (args.Length > 0)
{
bool isTextureArgNext = false;
foreach (string arg in args)
{
if (isTextureArgNext)
{
textureArg = arg.Trim().Replace('/', '\\');
break;
}
if (arg == "-t")
{
isTextureArgNext = true;
}
}
string map2difArgs = String.Join(" ", args).Trim();
Regex isPath = new Regex(@"^[A-z]\:\\", RegexOptions.Compiled);
if (isPath.Match(args[0]).Success)
{
map2difArgs = "\"" + map2difArgs.Trim('"') + "\"";
}
if (!config.SilentMode)
{
Console.WriteLine(selectedFileName + " " + map2difArgs);
}
Debug.WriteLine(selectedFileName + " " + map2difArgs);
map2dif.StartInfo.Arguments = map2difArgs;
}
else
{
if (!config.SilentMode)
{
Console.WriteLine(selectedFileName);
}
Debug.WriteLine(selectedFileName);
}
map2dif.Start();
string standardOutput = map2dif.StandardOutput.ReadToEnd();
string[] lines = standardOutput.Split('\n');
Regex isTexture = new Regex(@"^\s+(?:Unable\ to\ load\ |Loaded\ )texture\ (.+)$", RegexOptions.Compiled); // TODO: This should be read from the .map file directly.
bool rerun = false;
foreach (string line in lines)
{
MatchCollection matches = isTexture.Matches(line);
if (matches.Count > 0)
{
rerun = true;
foreach (Match match in matches)
{
string texture = match.Groups[1].Value.Replace('/', '\\');
string texturePath = config.TexturesPath + "\\" + texture;
CopyTexture(texturePath, textureArg); // FIXME: Why doesn't textureArg work? Torque or Map2Dif isn't honoring the -t flag and still reads the textures from the root regardless of what it is set to.
}
}
else
{
Console.WriteLine(line);
Debug.Write(line);
}
}
map2dif.WaitForExit();
int exitCode = map2dif.ExitCode;
if (rerun && exitCode == 0)
{
map2dif.Start();
map2dif.WaitForExit();
exitCode = map2dif.ExitCode;
}
if (exitCode != 0)
{
if (args.Length > 0)
{
string reason = "Generic error";
if (exitCode == -2147483645)
{
reason = "Invalid argument supplied";
}
ShowMessage("" + selectedFileName + " exited with error code " + map2dif.ExitCode.ToString() + " (" + reason + ")");
}
return map2dif.ExitCode;
}
Debug.WriteLine("" + selectedFileName + " exited with code 0 (Success)");
return 0;
}
static void CopyTexture(string source, string texturesPath = null)
{
if (!config.CopyTextures)
{
return;
}
string[] extensions = { "jpg", "jpeg", "png", "bmp", "gif" };
int fileCount = 0;
foreach (string extension in extensions)
{
string texturePath = source.Trim() + "." + extension;
if (File.Exists(texturePath))
{
string textureFileName = Path.GetFileName(texturePath);
fileCount++;
Debug.WriteLine(texturePath);
if (!String.IsNullOrEmpty(texturesPath))
{
textureFileName = texturesPath + "\\" + textureFileName;
Directory.CreateDirectory(Path.GetDirectoryName(textureFileName));
}
File.Copy(texturePath, textureFileName, true);
}
}
if (fileCount > 1)
{
// DisplayMessage("Warning, texture with more than one file of the same name detected.");
Debug.WriteLine("Duplicate texture found.");
}
}
static bool CreateConfigFile()
{
var config = new Config {
TexturesPath = null,
CopyTextures = true,
SilentMode = false
};
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
var yaml = serializer.Serialize(config);
using (StreamWriter file = new StreamWriter("map2dif_wrapper.yaml"))
{
file.Write(yaml);
}
if (File.Exists("map2dif_wrapper.yaml"))
{
return true;
}
return false;
}
static bool ReadConfigFile()
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
string yaml = null;
using (StreamReader file = new StreamReader("map2dif_wrapper.yaml"))
{
yaml = file.ReadToEnd();
}
try
{
var config = deserializer.Deserialize<Config>(yaml);
Program.config = config;
}
catch // TODO: lazy.
{
return false;
}
return true;
}
static bool ValidateConfig()
{
if (String.IsNullOrEmpty(config.TexturesPath))
{
ShowMessage("texturesPath has not been set.");
return false;
}
if (!Directory.Exists(config.TexturesPath))
{
ShowMessage("texturesPath \"" + config.TexturesPath + "\" could not be found.");
return false;
}
return true;
}
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr h, string m, string c, int type);
static void ShowMessage(string text)
{
if (!config.SilentMode)
{
MessageBox((IntPtr)0, text, "Map2Dif Wrapper", 0);
}
else
{
Console.WriteLine(text);
}
Debug.WriteLine(text);
}
}
}