-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateLvlForm.cs
355 lines (331 loc) · 15.9 KB
/
CreateLvlForm.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace LVLTool
{
public partial class CreateLvlForm : Form
{
public string OverrideScriptMunge = null;
public string OverrideLevelPack = null;
public CreateLvlForm()
{
InitializeComponent();
mPlatformComboBox.SelectedIndex = 0;
}
protected override void OnHandleCreated(EventArgs e)
{
//https://stackoverflow.com/questions/57124243/winforms-dark-title-bar-on-windows-10 (thank you:)
if (DwmSetWindowAttribute(this.Handle, 19, new[] { 1 }, 4) != 0)
DwmSetWindowAttribute(this.Handle, 20, new[] { 1 }, 4);
base.OnHandleCreated(e);
}
[System.Runtime.InteropServices.DllImport("DwmApi")] //System.Runtime.InteropServices
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
private bool AlreadyMunged(string filename)
{
string[] alreadyMungedExt = new string[] { ".texture", ".script", ".lvl", ".config" };
foreach (string ext in alreadyMungedExt)
{
if (filename.ToLower().EndsWith(ext))
return true;
}
return false;
}
internal void SetStyle(Form prevForm)
{
this.BackColor = prevForm.BackColor;
this.ForeColor = prevForm.ForeColor;
TextBox tb = null;
Button b = null;
MenuStrip ms = null;
foreach (Control tmp in prevForm.Controls)
{
if (tb == null)
tb = tmp as TextBox;
if (b == null)
b = tmp as Button;
if (ms == null)
ms = tmp as MenuStrip;
if (tb != null && b != null && ms != null)
break;
}
if (tb != null)
{
this.mListBox.BackColor = mOutputDirTextbox.BackColor = mOutputFilenameTextbox.BackColor =
tb.BackColor;
this.mListBox.ForeColor = mOutputDirTextbox.ForeColor = mOutputFilenameTextbox.ForeColor =
tb.ForeColor;
}
if (b != null)
{
mClearButton.BackColor = mPlatformComboBox.BackColor = mCreateButton.BackColor = b.BackColor;
mClearButton.ForeColor = mPlatformComboBox.ForeColor = mCreateButton.ForeColor = b.ForeColor;
}
if (ms != null)
{
this.menuStrip1.ForeColor = ms.ForeColor;
this.menuStrip1.BackColor = ms.BackColor;
}
}
private void mListBox_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void mListBox_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
StringBuilder errList = new StringBuilder();
int lastSlash = 0;
if (files != null )
{
for (int i = 0; i < files.Length; i++)
{
if (!AddItem(files[i]))
{
lastSlash = files[i].LastIndexOf("\\") + 1;
if (lastSlash > 0)
{
errList.Append(" ");
errList.Append(files[i].Substring(lastSlash));
errList.Append("\n");
}
}
}
if (errList.Length > 0)
{
MessageBox.Show(String.Format("Error:\n{0}Currently only .tga, .texture, .script and .lua files are supported",
errList.ToString()));
}
}
}
public bool AddItem(string file)
{
bool retVal = false;
if ( file.EndsWith(".lua", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".tga", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".script", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".texture", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".config", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".mcfg", StringComparison.InvariantCultureIgnoreCase) ||
file.EndsWith(".lvl" , StringComparison.InvariantCultureIgnoreCase)
)
{
MungableItem dude = new MungableItem(file);
mListBox.Items.Add(dude);
if (mListBox.Items.Count == 1 && mOutputFilenameTextbox.Text == "")
{
mOutputFilenameTextbox.Text = dude.MungedName + ".lvl";
mOutputDirTextbox.Text = dude.Path.Replace(dude.FileName, "");
}
retVal = true;
}
return retVal;
}
public void CreateLvl(string outputPath, string saveFileName, string platform, bool supressPopups )
{
if(! outputPath.EndsWith("\\"))
outputPath += "\\";
string workspace = outputPath + ".\\__LVLTOOL__workspace__\\";
string munge_dir = outputPath + ".\\__LVLTOOL__workspace__\\MUNGED\\";
string saveFilePath = outputPath + saveFileName;
if (Directory.Exists(workspace) )
{
Directory.Delete(workspace, true);
}
Directory.CreateDirectory(workspace);
Directory.CreateDirectory(munge_dir);
string reqContent = GetReqString();
string reqFileName = mOutputFilenameTextbox.Text.ToLower().Replace(".lvl", ".req");
File.WriteAllText(workspace + reqFileName, reqContent);
// now copy the files over to the workspace
foreach (MungableItem item in mListBox.Items)
{
if( AlreadyMunged(item.Path))
File.Copy(item.Path, munge_dir + item.FileName);
else
File.Copy(item.Path, workspace + item.FileName);
}
// now munge & pack
string configMunge = Program.ModToolsDir + "ToolsFL\\bin\\ConfigMunge.exe";
string textureMunge = Program.ModToolsDir + "ToolsFL\\bin\\pc_TextureMunge.exe";
string scriptMunge = Program.ModToolsDir + "ToolsFL\\bin\\ScriptMunge.exe";
string lvlPack = Program.ModToolsDir + "ToolsFL\\bin\\levelpack.exe";
if (OverrideLevelPack != null)
lvlPack = OverrideLevelPack;
if (OverrideScriptMunge != null)
scriptMunge = OverrideScriptMunge;
string req_file_noext = reqFileName.Replace(".req", "");
string configArgs = string.Format(
" -inputfile $*.mcfg -continue -QUIET -platform {0} -sourcedir {1} -outputdir {2} -hashstrings ",
platform, workspace, munge_dir);
string scriptArgs = string.Format(
" -inputfile *.lua -continue -QUIET -platform {0} -sourcedir {1} -outputdir {2} ",
platform, Path.GetFullPath( workspace), munge_dir);
string texArgs = string.Format(
" -inputfile $*.tga -checkdate -continue -QUIET -platform {0} -sourcedir {1} -outputdir {2} ",
platform, workspace, munge_dir);
string lvlPackArgs = string.Format(
"-inputfile {0}.req -writefiles {1}{0}.files -continue -QUIET -platform {2} -sourcedir {3} -inputdir {1} -outputdir {3} ",
req_file_noext, munge_dir, platform, Path.GetFullPath( workspace));
//string programOutput = Program.RunCommand(program_exe, args, true);
string programOutput = "";
if( File.Exists(scriptMunge))
programOutput += Program.RunCommand(scriptMunge, scriptArgs, true);
if( File.Exists(configMunge))
programOutput += Program.RunCommand(configMunge, configArgs, true);
if( File.Exists(textureMunge))
programOutput += Program.RunCommand(textureMunge, texArgs, true);
programOutput += Program.RunCommand(lvlPack, lvlPackArgs, true);
string batchFileContents = string.Format(
"md MUNGED \ndel /Y MUNGED\\*\n {0} {1}\n\n{2} {3}\n\n{4} {5}\n\n{6} {7}\n\nmove *.log MUNGED\n\n",
textureMunge, String.Format("-inputfile $*.tga -checkdate -continue -platform {0} -sourcedir . -outputdir MUNGED ", platform),
scriptMunge, String.Format("-inputfile *.lua -continue -platform {0} -sourcedir . -outputdir MUNGED ", platform),
configMunge, String.Format("-inputfile $*.mcfg -continue -platform {0} -sourcedir . -outputdir MUNGED -hashstrings ",platform),
lvlPack, String.Format("-inputfile {0}.req -writefiles MUNGED\\{0}.files -continue -platform {1} -sourcedir . -inputdir MUNGED\\ -outputdir . ",
req_file_noext, platform)
);
// copy output to user's choice directory
string lvlOutput = workspace + req_file_noext + ".lvl";
if (File.Exists(lvlOutput))
{
File.WriteAllText(workspace + "munge.bat", batchFileContents);
File.WriteAllText(workspace + "munge.log", programOutput);
File.Copy(lvlOutput, saveFilePath, true);
if (!supressPopups)
MessageForm.ShowMessage("Saved lvl file", "Saved to: " + saveFilePath, SystemIcons.Information, false, false);
else
Console.WriteLine("Saved lvl file to: " + saveFilePath);
if (Directory.Exists(workspace)) // allow the user to rename in order to keep the workspace
{
Directory.Delete(workspace, true);
}
}
else
{
if( !supressPopups)
MessageForm.ShowMessage("Error creating lvl file", "Something went wrong:\n" + programOutput);
else
Console.WriteLine("Error creating lvl file :\n" + programOutput);
}
}
private string GetReqString()
{
string retVal = null;
if (mListBox.Items.Count > 0)
{
List<MungableItem> configs = new List<MungableItem>();
List<MungableItem> scripts = new List<MungableItem>();
List<MungableItem> textures = new List<MungableItem>();
List<MungableItem> lvls = new List<MungableItem>();
StringBuilder builder = new StringBuilder(200);
foreach( MungableItem item in mListBox.Items)
{
if (item.Path.EndsWith(".tga", StringComparison.InvariantCultureIgnoreCase) ||
item.Path.EndsWith(".texture", StringComparison.InvariantCultureIgnoreCase))
textures.Add(item);
else if (item.Path.EndsWith(".lua", StringComparison.InvariantCultureIgnoreCase) ||
item.Path.EndsWith(".script", StringComparison.InvariantCultureIgnoreCase))
scripts.Add(item);
else if (item.Path.EndsWith(".mcfg", StringComparison.InvariantCultureIgnoreCase) || // TODO: add the other config types
item.Path.EndsWith(".config", StringComparison.InvariantCultureIgnoreCase))
configs.Add(item);
else if (item.Path.EndsWith(".lvl", StringComparison.InvariantCultureIgnoreCase) )
lvls.Add(item);
}
builder.Append("ucft\n{\n");
if (configs.Count > 0)
{
builder.Append(" REQN\n");
builder.Append(" {\n");
builder.Append(" \"config\"\n");
for (int i = 0; i < configs.Count; i++)
{
builder.Append(string.Format(
" \"{0}\"\n", configs[i].MungedName));
}
builder.Append(" }\n");
}
if (scripts.Count > 0)
{
builder.Append(" REQN\n");
builder.Append(" {\n");
builder.Append(" \"script\"\n");
for(int i =0; i< scripts.Count; i++)
{
builder.Append(string.Format(
" \"{0}\"\n", scripts[i].MungedName));
}
builder.Append(" }\n");
}
if (textures.Count > 0)
{
builder.Append(" REQN\n");
builder.Append(" {\n");
builder.Append(" \"texture\"\n");
for (int i = 0; i < textures.Count; i++)
{
builder.Append(string.Format(
" \"{0}\"\n", textures[i].MungedName));
}
builder.Append(" }\n");
}
if (lvls.Count > 0)
{
builder.Append(" REQN\n");
builder.Append(" {\n");
builder.Append(" \"lvl\"\n");
for (int i = 0; i < lvls.Count; i++)
{
builder.Append(string.Format(
" \"{0}\"\n", lvls[i].MungedName));
}
builder.Append(" }\n");
}
builder.Append("}");
retVal = builder.ToString();
}
return retVal;
}
private void mCreateButton_Click(object sender, EventArgs e)
{
CreateLvl(mOutputDirTextbox.Text, mOutputFilenameTextbox.Text, mPlatformComboBox.SelectedItem.ToString(), false );
}
private void supportedFileTypesToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("currently supports [.config, .mcfg, .tga, .texture, .lua, .script, .lvl] files ");
}
private void mClearButton_Click(object sender, EventArgs e)
{
mListBox.Items.Clear();
mOutputFilenameTextbox.Clear();
mOutputDirTextbox.Clear();
}
}
public class MungableItem
{
public string Path { get; private set; }
public string FileName { get; private set; }
public string MungedName { get; private set; }
public MungableItem(string path)
{
Path = path;
int targetIndex = path.LastIndexOf('\\') + 1;
FileName = path.Substring(targetIndex);
targetIndex = FileName.LastIndexOf(".");
MungedName = FileName.Substring(0, targetIndex);
}
public override string ToString()
{
return FileName;
}
}
}