-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileResourceManager.java
More file actions
481 lines (473 loc) · 16.9 KB
/
FileResourceManager.java
File metadata and controls
481 lines (473 loc) · 16.9 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package awpsoft.gamemodule;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class FileResourceManager
{
public static final int __FILETYPE_BMP = 0x504D422E;
public static final int __FILETYPE_PNG = 0x474E502E;
public static final int __FILETYPE_JPG = 0x47504A2E;
public static final int __FILETYPE_GIF = 0x4649472E;
public static final int __FILETYPE_TXT = 0x5458542E;
public static final int __FILETYPE_WAV = 0x5641572E;
public static final int __FILETYPE_PCM = 0x4D43502E;
public static final int __FILETYPE_DAT = 0x5441442E;
public static final int __FILETYPE_INI = 0x494E492E;
public static final int __FILETYPE_BIN = 0x4E49422E;
public static final int __FILETYPE_CSV = 0x5653432E;
public static class FileResourceInfo implements Cloneable
{
public byte[] Buffer;
public int FileID;
public int Size; //unsigned int store!
public int Type;
public float Param1, Param2;
FileResourceInfo()
{
Buffer = null;
FileID = -1;
Type = 0x00000000;
Size = 0;
Param1 = Param2 = 0.0f;
}
FileResourceInfo(int fileID, int type, float param1, float param2)
{
Buffer = null;
Size = 0;
FileID = fileID;
Type = type;
Param1 = param1;
Param2 = param2;
}
@Override public FileResourceInfo clone() throws CloneNotSupportedException
{
return (FileResourceInfo)super.clone();
}
};
protected FileResourceInfo[] FileList;
public FileResourceManager()
{
FileList = new FileResourceInfo[0x1000];
}
public FileResourceManager(int maxFileCount)
{
FileList = new FileResourceInfo[maxFileCount];
}
@Override public void finalize()
{
releaseAllFileResource();
}
public int takeOverFileResource(FileResourceInfo fileInfo)
{
if(fileInfo == null) return -1;
if (fileInfo.FileID >= FileList.length || fileInfo.FileID < 0) return -1;
FileList[fileInfo.FileID] = fileInfo;
return fileInfo.FileID;
}
public FileResourceInfo getFileResourceInfo(int fileID)
{
if (fileID >= FileList.length || fileID < 0) return null;
else return FileList[fileID];
}
public FileResourceInfo takeOutFileResource(int fileID)
{
if (fileID >= FileList.length || fileID < 0)
{
return null;
}
else
{
FileResourceInfo temp = FileList[fileID];
FileList[fileID] = null;
return temp;
}
}
public int loadFile(String fileName, int fileID, int fileType) //return fsize,failed:0
{
return loadFile(fileName, fileID, fileType, 0.0f, 0.0f);
}
public int loadFile(String fileName, int fileID, int fileType, float param1) //return fsize,failed:0
{
return loadFile(fileName, fileID, fileType, param1, 0.0f);
}
public int loadFile(String fileName, int fileID, int fileType, float param1, float param2) //return fsize,failed:0
{
if (fileID >= FileList.length || fileID < 0 || fileName == null) return 0;
File fp = new File(fileName);
try(FileInputStream fis = new FileInputStream(fp))
{
FileResourceInfo fri = new FileResourceInfo(fileID, fileType, param1, param1);
long fsize = fp.length();
if((fri.Size & 0xFFFFFFFF80000000L) != 0L) return 0;
fri.Size = (int)fsize;
fri.Buffer = new byte[fri.Size];
fis.read(fri.Buffer);
if (takeOverFileResource(fri) != -1) return fri.Size;
else return 0;
}
catch(Exception ex)
{
return 0;
}
}
public long releaseFileResource(int fileID)
{
if (fileID >= FileList.length || fileID < 0) return 0;
long ts = 0L;
if(FileList[fileID] != null) ts = FileList[fileID].Size;
FileList[fileID] = null;
return ts;
}
public long releaseAllFileResource()
{
long ttSize = 0;
for (int i = 0; i < FileList.length; i++)
{
if (FileList[i] != null)
{
ttSize += FileList[i].Size;
FileList[i] = null;
}
}
System.gc();
return ttSize;
}
public int loadFilesFromPackage(String packageFileName)
{
if(packageFileName == null) return 0;
int scnt = 0;
FileResourceInfo frp = new FileResourceInfo();
File f = new File(packageFileName);
try(FileInputStream fp = new FileInputStream(f))
{
long fsize = f.length();
byte[] byte4 = new byte[4];
fp.read(byte4); frp.Type = ByteBuffer.wrap(byte4).order(ByteOrder.LITTLE_ENDIAN).getInt();
if (frp.Type != 0x50464741) return 0;
fsize -= 4;
byte[] byte20 = new byte[20];
while (fsize > 0L)
{
fp.read(byte20);
ByteBuffer bb = ByteBuffer.wrap(byte20).order(ByteOrder.LITTLE_ENDIAN);
frp.FileID = bb.getInt();
frp.Type = bb.getInt();
frp.Param1 = bb.getFloat();
frp.Param2 = bb.getFloat();
frp.Size = bb.getInt();
fsize -= 20;
if (frp.Size > fsize || frp.Size < 0)
{
break;
}
if (frp.FileID >= FileList.length || frp.FileID < 0)
{
fsize -= frp.Size;
fp.skip(frp.Size);
continue;
}
try
{
frp.Buffer = new byte[frp.Size];
fp.read(frp.Buffer);
fsize -= frp.Size;
if(takeOverFileResource(frp.clone()) >= 0) scnt++;
}
catch (Exception ex)
{
fsize -= frp.Size;
fp.skip(frp.Size);
continue;
}
}
return scnt;
}
catch(Exception ex)
{
return scnt;
}
}
public int loadRangeFilesFromPackage(String packageFileName)
{
return loadRangeFilesFromPackage(packageFileName, 0, 0x7FFFFFFF);
}
public int loadRangeFilesFromPackage(String packageFileName, int minFileID)
{
return loadRangeFilesFromPackage(packageFileName, minFileID, 0x7FFFFFFF);
}
public int loadRangeFilesFromPackage(String packageFileName, int minFileID, int maxFileID)
{
if(packageFileName == null) return 0;
int scnt = 0;
FileResourceInfo frp = new FileResourceInfo();
File f = new File(packageFileName);
try(FileInputStream fp = new FileInputStream(f))
{
long fsize = f.length();
byte[] byte4 = new byte[4];
fp.read(byte4); frp.Type = ByteBuffer.wrap(byte4).order(ByteOrder.LITTLE_ENDIAN).getInt();
if (frp.Type != 0x50464741) return 0;
fsize -= 4;
byte[] byte20 = new byte[20];
while (fsize > 0L)
{
fp.read(byte20);
ByteBuffer bb = ByteBuffer.wrap(byte20).order(ByteOrder.LITTLE_ENDIAN);
frp.FileID = bb.getInt();
frp.Type = bb.getInt();
frp.Param1 = bb.getFloat();
frp.Param2 = bb.getFloat();
frp.Size = bb.getInt();
fsize -= 20;
if (frp.Size > fsize || frp.Size < 0)
{
break;
}
if (frp.FileID < minFileID || frp.FileID > maxFileID || frp.FileID >= FileList.length || frp.FileID < 0)
{
fsize -= frp.Size;
fp.skip(frp.Size);
continue;
}
try
{
frp.Buffer = new byte[frp.Size];
fp.read(frp.Buffer);
fsize -= frp.Size;
if(takeOverFileResource(frp.clone()) >= 0) scnt++;
}
catch (Exception ex)
{
fsize -= frp.Size;
fp.skip(frp.Size);
continue;
}
}
return scnt;
}
catch(Exception ex)
{
return scnt;
}
}
public long loadSingleFileFromPackage(String packageFileName, int fileID)
{
if(packageFileName == null) return 0;
if (fileID >= FileList.length || fileID < 0) return 0;
int ssize = 0;
FileResourceInfo frp = new FileResourceInfo();
File f = new File(packageFileName);
try(FileInputStream fp = new FileInputStream(f))
{
long fsize = f.length();
byte[] byte4 = new byte[4];
fp.read(byte4); frp.Type = ByteBuffer.wrap(byte4).order(ByteOrder.LITTLE_ENDIAN).getInt();
if (frp.Type != 0x50464741) return 0;
fsize -= 4;
byte[] byte20 = new byte[20];
while (fsize > 0L)
{
fp.read(byte20);
ByteBuffer bb = ByteBuffer.wrap(byte20).order(ByteOrder.LITTLE_ENDIAN);
frp.FileID = bb.getInt();
frp.Type = bb.getInt();
frp.Param1 = bb.getFloat();
frp.Param2 = bb.getFloat();
frp.Size = bb.getInt();
fsize -= 20;
if (frp.Size > fsize || frp.Size < 0)
{
break;
}
if (frp.FileID != fileID)
{
fsize -= frp.Size;
fp.skip(frp.Size);
continue;
}
try
{
frp.Buffer = new byte[frp.Size];
fp.read(frp.Buffer);
fsize -= frp.Size;
if(takeOverFileResource(frp.clone()) >= 0)
{
ssize = frp.Size;
break;
}
}
catch (Exception ex)
{
fsize -= frp.Size;
fp.skip(frp.Size);
continue;
}
}
return ssize;
}
catch(Exception ex)
{
return ssize;
}
}
public int saveAsPackage(String targetPackageName)
{
if(targetPackageName == null) return 0;
File f= new File(targetPackageName);
int scnt = 0;
final byte[] c = {'A','G','F','P'};
try(FileOutputStream fos = new FileOutputStream(f))
{
fos.write(c);
byte[] byte20 = new byte[20];
System.out.print("ID\t类型码 \t附属参数1\t附属参数2\t状态 \t文件大小\n");
for (FileResourceInfo finfo: FileList)
{
if (finfo == null || finfo.FileID < 0) continue;
ByteBuffer bb = ByteBuffer.wrap(byte20).order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(finfo.FileID).putInt(finfo.Type).putFloat(finfo.Param1)
.putFloat(finfo.Param2).putInt(finfo.Size);
fos.write(byte20);
fos.write(finfo.Buffer, 0, finfo.Size);
scnt++;
if (finfo.Size / 1024 >= 1024)
{
System.out.printf((Locale)null,"%-6d\t0x%-08X\t%-8f\t%-8f\t%-12s\t打包成功\t%-6.2fMB\n",
finfo.FileID, finfo.Type, finfo.Param1, finfo.Param2, finfo.Size / 1024.0 / 1024.0);
}
else
{
System.out.printf((Locale)null,"%-6d\t0x%-08X\t%-8f\t%-8f\t%-12s\t打包成功\t%-6.2fKB\n",
finfo.FileID, finfo.Type, finfo.Param1, finfo.Param2, finfo.Size / 1024.0);
}
}
}
catch(IOException ex)
{
System.out.print(ex.toString() + "\nIO异常,写入到Package文件失败!\n");
}
finally
{
System.gc();
}
System.out.println("文件打包结束,成功" + scnt + "个。");
return scnt;
}
public int makePackageFromCSV(String manifestFileName, String targetPackageName)
{
return makeDefaultPackageFromCSV(manifestFileName, targetPackageName);
}
public static int makeDefaultPackageFromCSV(String manifestFileName, String targetPackageName)
{
/*
Num,,,,
Id,0xType,FloatPara1,FloatPara2,FileName(CANNOT contain SPACE and CHINESE...!)
......
*/
int scnt = 0, id = 0, num = 0, type = 0;
float p1 = 0.0f, p2 = 0.0f;
String name;
int fsize = 0;
final byte[] c = {'A','G','F','P'};
try(Scanner mfp = new Scanner(new File(manifestFileName));
FileOutputStream pfp = new FileOutputStream(targetPackageName))
{
pfp.write(c);
String[] firstline = mfp.nextLine().split(",");
num = Integer.parseInt(firstline[0].trim());
firstline = null;
System.out.print("共" + num + "个文件\n");
System.out.print("ID\t类型码 \t附属参数1\t附属参数2\t文件名 \t状态 \t文件大小\n");
byte[] byte20 = new byte[20];
for (int i = 1; i <= num; i++)
{
String[] thisline = mfp.nextLine().split(",");
id = Integer.parseInt(thisline[0].trim());
type = (int) Long.parseLong(thisline[1].trim().substring(2), 16);
p1 = Float.parseFloat(thisline[2].trim());
p2 = Float.parseFloat(thisline[3].trim());
name = thisline[4].trim();
thisline = null;
File fp = new File(name);
boolean readsucceed = false;
try(FileInputStream ffp = new FileInputStream(fp))
{
long tl = fp.length();
if((tl & 0xFFFFFFFF80000000L) != 0L)
{
System.out.printf((Locale)null, "%-6d\t0x%-08X\t%-8f\t%-8f\t%-12s\t文件过大,无法载入\n", id, type, p1, p2, name);
continue;
}
fsize = (int)tl;
byte[] buffer = ffp.readAllBytes();
readsucceed =true;
ByteBuffer bb = ByteBuffer.wrap(byte20).order(ByteOrder.LITTLE_ENDIAN);
bb.putInt(id).putInt(type).putFloat(p1).putFloat(p2).putInt(fsize);
pfp.write(byte20);
pfp.write(buffer);
if (fsize / 1024 >= 1024)
{
System.out.printf((Locale)null,"%-6d\t0x%-08X\t%-8f\t%-8f\t%-12s\t打包成功\t%-6.2fMB\n", id, type, p1, p2, name, fsize / 1024.0 / 1024.0);
}
else
{
System.out.printf((Locale)null,"%-6d\t0x%-08X\t%-8f\t%-8f\t%-12s\t打包成功\t%-6.2fKB\n", id, type, p1, p2, name, fsize / 1024.0);
}
scnt++;
buffer = null;
}
catch (OutOfMemoryError re)
{
System.out.println(re.toString() + "\n文件过大,系统内存不足,操作终止!\n");
break;
}
catch(IOException|NullPointerException ex)
{
System.out.printf((Locale)null, "%-6d\t0x%-08X\t%-8f\t%-8f\t%-12s\t文件打开、读取或写入失败\n", id, type, p1, p2, name);
if(readsucceed) break;
else continue;
}
finally
{
System.gc();
}
}
}
catch (FileNotFoundException ex)
{
System.out.print(ex.toString() + "\n未找到文件清单或Package文件创建失败\n");
return 0;
}
catch (SecurityException ex)
{
System.out.print(ex.toString() + "\n权限不足,请先设置相关文件权限\n");
return 0;
}
catch (IndexOutOfBoundsException ex)
{
System.out.print(ex.toString() + "\n文件清单格式不正确,请使用表格类软件填写\n");
}
catch (NoSuchElementException|IllegalStateException ex)
{
System.out.print(ex.toString() + ((num == 0)?"\n文件清单格式不正确\n":"\n文件个数填写错误\n"));
}
catch(NumberFormatException ex)
{
System.out.print(ex.toString() + "\n文件清单数字格式不正确\n");
}
catch (IOException ex)
{
System.out.print(ex.toString() + "\n目标Package文件写入失败\n");
}
catch (NullPointerException ex)
{
ex.printStackTrace();
System.out.print(ex.toString() + "\n未知错误...\n");
}
System.out.print("文件打包结束,成功" + scnt + "个。\n");
return scnt;
}
}