-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataUtils.cs
206 lines (198 loc) · 5.44 KB
/
DataUtils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShaderEditor
{
internal class DataUtils
{
public enum ePlatform
{
Xenon,
PC
}
public enum eGame
{
IV,
BlueDudeBamboozle = 2,
MP3
}
public static string ReadString(BinaryReader br)
{
byte size = br.ReadByte();
char[] tmp = new char[size - 1];
tmp = br.ReadChars(size - 1);
size = br.ReadByte();
string tempstring = new string(tmp);
return tempstring;
}
public static string ReadStringAtOffset(uint p, BinaryReader br)
{
uint oldPos = (uint)br.BaseStream.Position;
br.BaseStream.Position = p;
string tempstring = "";
char tmp;
for (int a = 0; a < 1;)
{
tmp = br.ReadChar();
if (tmp != 0) tempstring += tmp;
else break;
}
br.BaseStream.Position = oldPos;
return tempstring;
}
public static void WriteString(BinaryWriter bw, string value)
{
byte size = (byte)(value.Length+1);
byte[] array2 = Encoding.ASCII.GetBytes(value);
Array.Resize<byte>(ref array2, size);
bw.Write(size);
bw.Write(array2);
}
public static bool FileExists(string filename)
{
return System.IO.File.Exists(filename);
}
public static string TypeAsString(byte type)
{
switch (type)
{
case 1: return "int";
case 2: return "float";
case 3: return "float2";
case 4: return "float3";
case 5: return "float4";
case 6: return "sampler";
case 7: return "bool";
case 8: return "float4x3";
case 9: return "float4x4";
}
return "unkType";
}
public static byte TypeAsByte(string type)
{
switch (type)
{
case "int": return 1;
case "float": return 2;
case "float2": return 3;
case "float3": return 4;
case "float4": return 5;
case "sampler": return 6;
case "bool": return 7;
case "float4x3": return 8;
case "float4x4": return 9;
}
return 0;
}
public static byte AnnotationTypeAsByte(string type)
{
switch (type)
{
case "int": return 0;
case "float": return 1;
case "string": return 2;
}
return 3;
}
public static void Align(ref uint currentPos,uint value)
{
while (currentPos % value != 0)
currentPos += 1;
}
public static void WriteValueToByteArray(ref byte[] array, string str, ref uint pos)
{
byte[] array2 = Encoding.ASCII.GetBytes(str);
Array.Resize<byte>(ref array2, array2.Length + 1);
WriteToByteArray(ref array, array2, ref pos);
}
public static void WriteValueToByteArray(ref byte[] array, uint value, ref uint pos)
{
WriteToByteArray(ref array, BitConverter.GetBytes(value), ref pos);
}
public static void WriteValueToByteArray(ref byte[] array, int value, ref uint pos)
{
WriteToByteArray(ref array, BitConverter.GetBytes(value), ref pos);
}
public static void WriteValueToByteArray(ref byte[] array, short value, ref uint pos)
{
WriteToByteArray(ref array, BitConverter.GetBytes(value), ref pos);
}
public static void WriteValueToByteArray(ref byte[] array, ushort value, ref uint pos)
{
WriteToByteArray(ref array, BitConverter.GetBytes(value), ref pos);
}
public static void WriteValueToByteArray(ref byte[] array, byte value, ref uint pos)
{
WriteToByteArray(ref array, new byte[] { value }, ref pos);
}
public static void WriteValueToByteArray(ref byte[] array, sbyte value, ref uint pos)
{
WriteToByteArray(ref array, new byte[] { (byte)value }, ref pos);
}
public static void WriteValueToByteArray(ref byte[] array, float value, ref uint pos)
{
WriteToByteArray(ref array, BitConverter.GetBytes(value), ref pos);
}
//public static void WriteValueToByteArray(ref byte[] array, Vector4 value, ref uint pos)
//{
// WriteValueToByteArray(ref array, value.X, ref pos);
// WriteValueToByteArray(ref array, value.Y, ref pos);
// WriteValueToByteArray(ref array, value.Z, ref pos);
// WriteValueToByteArray(ref array, value.W, ref pos);
//}
//public static void WriteValueToByteArray(ref byte[] array, Matrix value, ref uint pos)
//{
// WriteValueToByteArray(ref array, value.m0, ref pos);
// WriteValueToByteArray(ref array, value.m1, ref pos);
// WriteValueToByteArray(ref array, value.m2, ref pos);
// WriteValueToByteArray(ref array, value.m3, ref pos);
//}
public static void WriteToByteArray(ref byte[] array, byte[] array2, ref uint pos)
{
for (int a = 0; a < array2.Length; a++)
array[pos + a] = array2[a];
pos += (uint)array2.Length;
}
}
public static class FileInfo
{
public static string fileName;
public static string GetFileMask(string fileName)
{
return System.IO.Path.GetExtension(fileName);
}
public static string fileMask
{
get
{
if (fileName.Contains('.')) return GetFileMask(fileName);
else return "";
}
}
public static string baseFileName
{
get
{
return Path.GetFileName(FileInfo.fileName.Substring(0, FileInfo.fileName.Length - FileInfo.fileMask.Length));
}
}
public static string filePath
{
get
{
return Path.GetDirectoryName(FileInfo.fileName.Substring(0, FileInfo.fileName.Length - FileInfo.fileMask.Length));
}
}
public static string exePath
{
get
{
return System.Reflection.Assembly.GetEntryAssembly().Location;
}
}
//
}
}