-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFF7FieldRSDResource.cs
295 lines (229 loc) · 11.3 KB
/
FF7FieldRSDResource.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
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KimeraCS
{
using Defines;
using static FF7FieldSkeleton;
using static FF7PModel;
using static FF7TEXTexture;
using static Utils;
using static OpenGL32;
using static GDI32;
using static FileTools;
public class FF7FieldRSDResource
{
public struct FieldRSDResource
{
public string ID;
public string res_file;
public PModel Model;
public int numTextures;
public List<TEX> textures;
public FieldRSDResource(string in_res_file, ref List<TEX> textures_pool, string strFolderName)
{
int ti, ti_pool;
bool tex_foundQ;
DialogResult drPModelError;
TEX itmTextureTEX;
Model = new PModel();
string[] rsdString;
string rsdFileName = strFolderName + "\\" + in_res_file + ".RSD";
string polyFileName;
int rsdNTEXpos = 0;
// Let's read RSD file into memory.
// Let's put the ID (@RSD) All .rsd files have this.
ID = "@RSD940102";
// Assign to struct name of resource file
res_file = in_res_file;
numTextures = 0;
textures = new List<TEX>();
// First check if exists
if (!File.Exists(rsdFileName))
{
//throw new FileNotFoundException("Error opening RSD file '" + rsdFileName);
MessageBox.Show("File: " + in_res_file + ".RSD does not exists.");
}
else
{
rsdString = File.ReadAllLines(rsdFileName);
// Let's read PLY
while (rsdString[rsdNTEXpos].Length == 0 || rsdString[rsdNTEXpos][0] != 'P') rsdNTEXpos++;
// Let's read the P model
//polyFileName = (rsdString[rsdNTEXpos].Split('=')[1]).Substring(0, (rsdString[rsdNTEXpos].Split('=')[1]).IndexOf('.')) + ".P";
polyFileName = Path.GetFileNameWithoutExtension(rsdString[rsdNTEXpos].Split('=')[1]) + ".P";
// Let's read the P model into memory.
// First check if exists
if (!File.Exists(strFolderName + "\\" + polyFileName))
{
// We maybe want to load the rest of the model.
// Here we give the choice to load it o cancel loading the model.
drPModelError = MessageBox.Show("The .P Model file '" + polyFileName + "' does not exists. " +
"Do you want to continue loading the other parts of the model?",
"Information", MessageBoxButtons.YesNo);
if (drPModelError == DialogResult.No)
{
// throw new FileNotFoundException("Error opening P file '" + polyFileName + ".P'.");
throw new FileNotFoundException();
}
// We can fill the name of the .P model. This will help to saving.
Model.fileName = polyFileName;
}
else
{
LoadPModel(ref Model, strFolderName, polyFileName, true);
}
// Let's read NTEX
while (rsdString[rsdNTEXpos].Length == 0 || rsdString[rsdNTEXpos][0] != 'N') rsdNTEXpos++;
// Let's get the num textures
numTextures = Int32.Parse(rsdString[rsdNTEXpos].Split('=')[1]);
glTexParameterf(GLTextureTarget.GL_TEXTURE_2D, GLTextureParameter.GL_TEXTURE_MAG_FILTER, (float)GLTextureMagFilter.GL_LINEAR);
glTexParameterf(GLTextureTarget.GL_TEXTURE_2D, GLTextureParameter.GL_TEXTURE_MIN_FILTER, (float)GLTextureMagFilter.GL_LINEAR);
for (ti = 0; ti < numTextures; ti++)
{
// Position to the "TEX[n]" line (check comments or lines not needed)
while (rsdString[rsdNTEXpos].Length == 0 || rsdString[rsdNTEXpos][0] != 'T') rsdNTEXpos++;
// Prepare itmTextureTEX var
itmTextureTEX = new TEX()
{
// Get each texture entry in RSD (TEX[n] entries)
TEXfileName = rsdString[rsdNTEXpos].Split('=')[1].
Substring(0, rsdString[rsdNTEXpos].
Split('=')[1].IndexOf('.')) + ".TEX",
};
// Position for next "TEX[n]" line
rsdNTEXpos++;
ti_pool = 0;
tex_foundQ = false;
while (ti_pool < textures_pool.Count && !tex_foundQ)
{
tex_foundQ = textures_pool[ti_pool].TEXfileName == itmTextureTEX.TEXfileName;
ti_pool++;
}
if (tex_foundQ)
{
itmTextureTEX = textures_pool[ti_pool - 1];
}
else
{
if (ReadTEXTexture(ref itmTextureTEX, strFolderName + "\\" + itmTextureTEX.TEXfileName) == 0)
{
// Load TEX Texture (or other format if existant).
LoadTEXTexture(ref itmTextureTEX);
// Prepare Bitmap from TEX Texture
LoadBitmapFromTEXTexture(ref itmTextureTEX);
}
// Add TEX Texture to the list of RSDResource textures
textures.Add(itmTextureTEX);
}
}
}
}
}
// ---------------------------------------------------------------------------------------------------
// ============================================= SAVING ==============================================
// ---------------------------------------------------------------------------------------------------
public static void MergeFieldRSDResources(ref FieldRSDResource fRSDResourceOut,
FieldRSDResource fRSDResourceIn)
{
int iTextureIdx;
MergePModels(ref fRSDResourceOut.Model, fRSDResourceIn.Model);
// Merge textures
for (iTextureIdx = 0; iTextureIdx < fRSDResourceIn.numTextures; iTextureIdx++)
fRSDResourceOut.textures.Add(fRSDResourceIn.textures[iTextureIdx]);
fRSDResourceOut.numTextures += fRSDResourceIn.numTextures;
}
public static int WriteRSDResource(FieldRSDResource fRSDResource, string fileName)
{
int ti, iWriteRSDResourceResult;
string nameP;
StringBuilder strRSDContent = new StringBuilder();
try
{
strRSDContent.AppendLine(fRSDResource.ID);
nameP = fRSDResource.Model.fileName.Substring(0, fRSDResource.Model.fileName.Length - 2).ToUpper();
strRSDContent.AppendLine("PLY=" + nameP + ".PLY");
strRSDContent.AppendLine("MAT=" + nameP + ".MAT");
strRSDContent.AppendLine("GRP=" + nameP + ".GRP");
strRSDContent.AppendLine("NTEX=" + fRSDResource.numTextures);
for (ti = 0; ti < fRSDResource.numTextures; ti++)
{
strRSDContent.AppendLine("TEX[" + ti.ToString() + "]=" +
fRSDResource.textures[ti].TEXfileName.Substring(0, fRSDResource.textures[ti].TEXfileName.Length - 4).ToUpper() + ".TIM");
WriteTEXTexture(fRSDResource.textures[ti], strGlobalPathSaveSkeletonFolder + "\\" + fRSDResource.textures[ti].TEXfileName.ToUpper());
}
File.WriteAllText(fileName, strRSDContent.ToString());
iWriteRSDResourceResult = 1;
}
catch
{
MessageBox.Show("Error saving RSD file " + fileName, "Error", MessageBoxButtons.OK);
iWriteRSDResourceResult = -1;
}
return iWriteRSDResourceResult;
}
public static int WriteFullRSDResource(FieldBone infBone, string strFullFileName)
{
int iWriteRSDResourceResult;
PModel tmpPModel;
try
{
if (infBone.fRSDResources.Count > 0)
{
// Write RSD Resource
WriteRSDResource(infBone.fRSDResources[0], strFullFileName);
if (infBone.fRSDResources[0].Model.Polys != null)
{
tmpPModel = infBone.fRSDResources[0].Model;
WriteGlobalPModel(ref tmpPModel,
Path.GetDirectoryName(strFullFileName) + "\\" + tmpPModel.fileName.ToUpper());
}
}
iWriteRSDResourceResult = 1;
}
catch (Exception ex)
{
strGlobalExceptionMessage = ex.Message;
iWriteRSDResourceResult = -1;
}
return iWriteRSDResourceResult;
}
public static void CreateDListsFromRSDResource(ref FieldRSDResource Resource)
{
CreateDListsFromPModel(ref Resource.Model);
}
public static void DestroyRSDResources(ref FieldRSDResource Resource)
{
int ti;
uint[] lstTexID = new uint[1];
DestroyPModelResources(ref Resource.Model);
for (ti = 0; ti < Resource.numTextures; ti++)
{
lstTexID[0] = Resource.textures[ti].texID;
glDeleteTextures(1, lstTexID);
DeleteDC(Resource.textures[ti].HDC);
DeleteObject(Resource.textures[ti].HBMP);
}
Resource.textures.Clear();
}
// --------------------------------------------------------------------------------------------------
// ============================================= UTILS ==============================================
// --------------------------------------------------------------------------------------------------
public static FieldRSDResource CopyRSDResource(FieldRSDResource fRSDResourceIn)
{
FieldRSDResource tmpFieldRSDResource = new FieldRSDResource()
{
ID = fRSDResourceIn.ID,
numTextures = fRSDResourceIn.numTextures,
res_file = fRSDResourceIn.res_file,
textures = fRSDResourceIn.textures,
Model = CopyPModel(fRSDResourceIn.Model),
};
return tmpFieldRSDResource;
}
}
}