-
Notifications
You must be signed in to change notification settings - Fork 0
/
Munger.cs
158 lines (141 loc) · 5.85 KB
/
Munger.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace LVLTool
{
public static class Munger
{
/// <summary>
/// Throws exception if the selected file is not UCFB or not mungable
/// (currently tga & lua).
/// </summary>
/// <param name="prompt">The text to prompt the user with.</param>
/// <returns>The munged file name.</returns>
public static string GetMungedFile( string prompt )
{
string retVal = null;
OpenFileDialog dlg = new OpenFileDialog();
dlg.RestoreDirectory = true;
dlg.Title = prompt;
if (dlg.ShowDialog() == DialogResult.OK)
retVal = dlg.FileName;
dlg.Dispose();
if (retVal != null)
{
if (IsMungableFile(retVal))
retVal = MungeFile(retVal, Platform.None);
VerifyUcfbFile(retVal); // throws exception if it's not a UCFB file
}
return retVal;
}
public static string EnsureMungedFile(string fileName, Platform platform)
{
string retVal = fileName;
if (IsMungableFile(fileName))
retVal = MungeFile(fileName, platform);
VerifyUcfbFile(retVal); // throws exception if it's not a UCFB file
return retVal;
}
private static bool IsMungableFile(string file)
{
bool retVal = false;
int dotIndex = file.LastIndexOf('.');
string suffix = file.Substring(dotIndex).ToLower();
switch (suffix)
{
case ".tga":
case ".lua":
retVal = true;
break;
}
return retVal;
}
private static string MungeFile(string file, Platform platform)
{
string retVal = null;
int dotIndex = file.LastIndexOf('.');
string suffix = file.Substring(dotIndex).ToLower();
if (!Directory.Exists(Program.TmpDir))
Directory.CreateDirectory(Program.TmpDir);
switch (suffix)
{
case ".tga": retVal = TextureMunge(file, platform); break;
case ".lua": retVal = ScriptMunge(file); break;
}
return retVal;
}
private static string TextureMunge(string file, Platform platform)
{
string retVal = null;
if (File.Exists(file))
{
int index = file.LastIndexOf('\\');
string tmpFile = Program.TmpDir + file.Substring(index + 1);
File.Copy(file, tmpFile, true);
string prog = Path.GetFullPath( Program.ModToolsDir + "\\ToolsFL\\bin\\pc_TextureMunge.exe");
if( !File.Exists(prog))
prog = Path.GetFullPath( Program.ModToolsDir + "\\ToolsFL\\bin\\TextureMunge.exe");
if (!File.Exists(prog))
throw new Exception(
"Could not find texture munge program; ensure modtools directory is set correctly");
if( platform == Platform.None)
platform = PlatformPrompt.PromptForPlatform();
if (platform != Platform.None)
{
string args = String.Format(
"-sourcedir {0} -platform {1} -inputfile {2} -outputdir {3}",
Program.TmpDir, platform.ToString(), tmpFile, Program.TmpDir);
string programOutput = Program.RunCommand(prog, args, true);
string outputFile = tmpFile.Replace(".tga", ".texture");
if (File.Exists(outputFile))
{
retVal = outputFile;
}
Console.WriteLine(programOutput);
}
}
return retVal;
}
/*
scriptmunge -sourcedir . -platform xbox -inputfile addme.lua -outputdir munged\ -checkdate -continue
copy munged\addme.script ..\_LVL_XBOX\
*/
private static string ScriptMunge(string file)
{
string retVal = null;
if (File.Exists(file))
{
int index = file.LastIndexOf('\\');
string tmpFile = Program.TmpDir + file.Substring(index + 1);
File.Copy(file, tmpFile, true);
string prog = Path.GetFullPath( Program.ModToolsDir + "\\ToolsFl\\bin\\ScriptMunge.exe");
string args = String.Format(
// for scripts, platform doesn't matter; just use pc here
"-sourcedir {0} -platform pc -inputfile {1} -outputdir {2}",
".",//Program.TmpDir,
tmpFile, Program.TmpDir);
string programOutput = Program.RunCommand(prog, args, true);
string outputFile = tmpFile.Replace(".lua", ".script");
if (File.Exists(outputFile))
{
retVal = outputFile;
}
Console.WriteLine(programOutput);
}
return retVal;
}
public static void VerifyUcfbFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
byte[] buff = new byte[8];
fs.Read(buff, 0, buff.Length);
fs.Close();
byte[] UCFB = new byte[]{(byte)'u', (byte)'c', (byte)'f', (byte)'b'};
if (!BinUtils.BinCompare(UCFB, buff, 0))
throw new Exception("Error! File is not UCFB! " + filename);
}
}
}