This repository has been archived by the owner on May 9, 2022. It is now read-only.
forked from Evengard/UniMod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilesystem.cpp
598 lines (575 loc) · 12.9 KB
/
filesystem.cpp
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include "stdafx.h"
//#include <fcntl.h>
//#include <io.h>
#pragma pack(1)
int (__cdecl *noxWriteToFile)(void *Data,int Size,int Count,void *File);
void* (__cdecl *noxFopen)(char *Name,char *Access);
void (__cdecl *noxFclose)(void *File);
extern void *(__cdecl *noxAlloc)(int Size);
extern char *(__cdecl *mapGetName)();
extern int (__cdecl *netSendBySock)(int Player,void *Data,int Size, int Type);
int (__cdecl *mapValidateMB)(char *FileName,DWORD Crc);
DWORD *mapFileSizeServ;
BYTE **mapFileDataServ;
struct TarBlock
{
char name[100];
char mode[8];
char uid[8];
char gid[8];
char size[12];
char mtime[12];
char chksum[8];
char typeflag;
char linkname[100];
char magic[8];
char version[2];
union
{
struct
{
char uname[32];
char gname[32];
char devmajor[8];
char devminor[8];
} TarDefault;
struct
{
int Check;
int Size;
void *Data;
TarBlock* nextBlock;
} Info;
};
char prefix[155];
char Padding[10];
~TarBlock()
{
if (this->Info.Check==123456)
{
if (this->Info.Data!=0)
delete this->Info.Data;
if (this->Info.nextBlock!=0)
delete this->Info.nextBlock;
}
}
static int __cdecl Comparer(const void *A_,const void *B_)
{
TarBlock *A=*((TarBlock**)A_);
TarBlock *B=*((TarBlock**)B_);
int Ret=strcmp(A->prefix,B->prefix);
if (Ret==0)
Ret=strcmp(A->name,B->name);
return Ret;
}
struct PathSearcherData
{
const char *String;
int StrLen;
};
static int __cdecl PathSearcher(const void *A_,const void *B_)
{
PathSearcherData *A=((PathSearcherData*)A_);
TarBlock *B=*((TarBlock**)B_);
int Ret;
const char *P=A->String;
int Len=0;
if (Len=strlen(B->prefix))
{
Ret=strncmp(P,B->prefix,Len);
if (Ret!=0)
return Ret;
P+=Len;
}
Ret=strcmp(P,B->name);
return Ret;
}
};
#include "bzip2\bzlib.h"
#include <vector>
typedef std::vector<TarBlock *> TarList;
namespace
{
TarList MapList; //òóäà âñå êóñêè îò êàðòû - âûãðóçÿòñÿ âìåñòå ñ íåþ
TarList GlobalList;
struct IFile
{
virtual int fread(void *Buf,size_t Size)=0;
virtual DWORD getCrc()=0;
virtual void fclose()=0;
};
struct BZFile:public IFile
{
BZFILE *Data;
int Fd;
int fread(void *Buf,size_t Size)
{
int Error=0;
int Ret=BZ2_bzread(Data,(char*)Buf,Size);
if (Ret==Size)
return 1;
return 0;
}
BZFile(){ }
~BZFile(){fclose();}
void fclose()
{
if (Data) {BZ2_bzclose(Data); Data=0;}
}
DWORD getCrc(){ return 0; }
static IFile* fopen(const char *Filename)
{
BZFile *Ret=new BZFile();
Ret->Data=BZ2_bzopen(Filename,"r");
if (Ret->Data==NULL)
{
delete Ret;
return 0;
}
return Ret;
}
};
struct TarFile :public IFile
{
FILE *F;
int fread(void *Buf,size_t Size)
{
return ::fread(Buf,Size,1,F);
}
DWORD getCrc(){ return 0; }
TarFile(FILE *F_):F(F_){}
~TarFile(){ fclose(); }
void fclose()
{
if (F) {::fclose(F);F=0;}
}
static IFile* fopen(const char *Filename)
{
FILE *F=::fopen(Filename,"rb");
if (F==NULL)
return NULL;
return new TarFile(F);
}
};
int oct2i(const char*Str)
{
int Ret=0;
const char *P=Str;
for (;*P==' ';)
P++;
for( ;(*P>='0') && *P<='7'; P++)
{
Ret<<=3;
Ret+= (*P - '0');
}
return Ret;
}
bool readFile(const char *Filename,TarList &SearchList,DWORD *Crc=NULL)
{
if (sizeof(TarBlock)!=512)
return false;
IFile *F=0;
/* if (0==strcmpi(Filename+strlen(Filename)-4,".tar"))
{
F=TarFile::fopen(Filename);
}else*/ /// òàðôàéëû áûëè íóæíû òîëüêî äëÿ òåñòîâ
if(
(0==strcmpi(Filename+strlen(Filename)-5,".tbz2"))||
(0==strcmpi(Filename+strlen(Filename)-8,".tar.bz2"))
)
{
F=BZFile::fopen(Filename);
}
if (F==NULL)
return false;
TarBlock *Block=new TarBlock,*Start=Block;
bool Error=true;
int FileCount=0;
while(1)
{
if (1!=F->fread(Block,512))
break;
Block->Info.Size=oct2i(Block->size);/// ÷èñëî çàïèñàíî  ÂÎÑÜÌÅÐÈ×ÍÎÉ ñèñòåìå!
Block->Info.Data=0;
Block->Info.nextBlock=0;
Block->Info.Check=123456;
char *Name=Block->name;
int nameLen=strlen(Name);
if (nameLen<1)
{
Error=false;
break; //äóìàþ êîíåö àðõèâà
}
FileCount++;
if (Block->Info.Size==0)
{
if (Name[nameLen-1]!='/')
continue;/// çà÷åì íàì ïóñòîé ôàéë?
Block->Info.nextBlock=new TarBlock;/// ýòî äèðåêòîðèÿ, ïóùàé áóäåò
Block=Block->Info.nextBlock;
continue;
}
int ReadSize=(Block->Info.Size+0x1FF)&0xFFFFFE00;
BYTE *B=new BYTE[ReadSize];
Block->Info.Data=B;
if (1!=F->fread(B,ReadSize))
break;/// îøèáêà ÷òåíèÿ
Block->Info.nextBlock=new TarBlock;
Block=Block->Info.nextBlock;
}
if (Crc!=NULL)
*Crc=F->getCrc();
F->fclose();
delete F;
if (FileCount>0)
{
SearchList.reserve(FileCount);
TarBlock *N=NULL;
for (TarBlock *P=Start;P!=NULL;P=N)
{
N=P->Info.nextBlock;
P->Info.nextBlock=0;
if (0==*P->name) // ïðîñòî ôèíàëüíûé
{
delete P;
break;
}
if (123456!=P->Info.Check)// íåâàëèäíûé áëîê
{
delete P;
break;
}
SearchList.push_back(P);/// ñêëàäûâàåì âñå â ìàññèâ
}
qsort(&SearchList.front(),SearchList.size(),sizeof(TarBlock *),TarBlock::Comparer);
}
return !Error;
}
TarBlock* dirSearch(const char *File,TarList *List)
{
if ( (List==NULL) || List->empty() )
return NULL;
TarBlock::PathSearcherData D={File,strlen(File)};
TarBlock** Ret=(TarBlock**)bsearch(&D,&List->front(),List->size(),sizeof(TarBlock*),
TarBlock::PathSearcher);
return (Ret!=NULL)?*Ret:NULL;
}
int loadFileL(lua_State*L)
{
if (!lua_isstring(L,1))
{
lua_pushstring(L,"wrong args!");
lua_error_(L);
}
lua_pushboolean(L,
readFile(lua_tostring(L,1),lua_toboolean(L,2)?GlobalList:MapList)
?1:0);
return 1;
}
TarBlock *itemByName(const char *P,bool CalledFromCon,char *Buf)
{
if (P==NULL)
return NULL;
if (strlen(P)>200)
return NULL;
TarList *List=&MapList;
if (!CalledFromCon)
{
if (0==strncmp(P,"map/",4))
{
P+=4;
}else
List=&GlobalList;
}
else
{
if (*P=='/') P++;
if (0==strncmp(P,"global/",7)) /// Èç êàðòû âûçûâàåòñÿ ãëîáàëüíûå ñêðèïòû ÷åðåç /global/
{
P+=7;
List=&GlobalList;
}
else
{
if (0!=strncmp(P,"map/",4)) /// Â êàðòå âûçâàíî êàê /map/...
strcpy(Buf,"map/"); /// Â êàðòå âûçâàíî ïðîñòî êàê ...
else
P+=4;
}
}
strcat(Buf,lua_tostring(L,1));
return dirSearch(P,List);
}
int bz2Loader(lua_State*L) /// loadfile èç àðõèâîâ
{
const char *P=lua_tostring(L,1);
char Buf[220]="";
TarBlock *B=itemByName(P,(lua_objlen(L,lua_upvalueindex(1))!=0),Buf);
if ( (B==NULL)|| B->Info.Size<=0)
{
lua_pushstring (L," is not a valid lua file");
lua_concat(L,2);
lua_pushboolean(L,0);
lua_insert(L,1);
return 2;
}
if (0!=luaL_loadbuffer(L,(char*)B->Info.Data,B->Info.Size,Buf))
{
lua_pushstring (L," is not a valid lua file");
lua_concat(L,2);
lua_pushboolean(L,0);
lua_insert(L,1);
return 2;
}
return 1;
}
int cliMapSize=0;/// ðàçìåð ôàéëà êàðòû äëÿ êëèåíòà
int CurrentSize;/// ñêîëüêî óæå ïðèåõàëî
char *MapPath=0;
char *ArchiveName=0;
void **mapFileHandle;
DWORD servTBZ2Crc;
int servArchiveSize=0;
char servArchiveName[MAX_PATH]="";
void *clientOpenFile(char *Name,char *Access)
{
int Size=strlen(Name)+8;// tar.bz2\n
MapPath=new char[Size];
strcpy(MapPath,Name);
CurrentSize=0;
return noxFopen(Name,Access);
}
void clientWriteChunk(BYTE *Data,int Size,int Count,void *File)
{
if (cliMapSize>0)
{
if (CurrentSize+Size>cliMapSize)
{
int DSize=cliMapSize-CurrentSize;
noxWriteToFile(Data,DSize,Count,File);
Size-=DSize;
Data+=DSize;
cliMapSize=0;
char *P=strrchr(MapPath,'.');
if (P!=NULL)
{
noxFclose(File);///ïî èäåå âñåãäà File==mapFileHandle
strcpy(P,".tbz2");
*mapFileHandle=noxFopen(MapPath,"wb");
}
}
}
CurrentSize+=Size;
if (Size>0)
noxWriteToFile(Data,Size,Count,File);
}
int __cdecl mapValidate(char *Name,DWORD Size)
{
int Ret=mapValidateMB(Name,Size);
/* if (0!=servTBZ2Crc)
{
char Buf[MAX_PATH]="";
sprintf(Buf,"maps/%s/%s.",Name,Name);
char *P=strrchr(Buf,'.');
FILE *F=NULL;
strcpy(P,".tbz2");
F=fopen(Buf,"rb");
if (F==NULL)
{
strcpy(P,".tar.bz2");
F=fopen(Buf,"rb");
}
if (F!=0)
{
int Size=0;
fseek(F,0,SEEK_END);
Size=ftell(F);
fseek(F,0,SEEK_SET);
BYTE *B=new BYTE[Size];
fread(B,Size,1,F);
DWORD Crc=0xAAAAAAAA;
fclose(F);
{
DWORD *S=(DWORD*)(B);
Size>>=2;
DWORD *E=S+Size;
for(;S!=E;S++)
{
Crc<<=4;
Crc^=*S;
}
servTBZ2Crc=Crc;
}
delete B;
}
}*/
return Ret;
}
void * __cdecl serverTransferPrepair(int FileSize)
{
servTBZ2Crc=0;
void *Ret;
*servArchiveName=0;
char *Buf=servArchiveName;
sprintf(Buf,"maps/%s/%s.",mapGetName(),mapGetName());
char *P=strrchr(Buf,'.');
FILE *F=NULL;
if (P==NULL)
return noxAlloc(FileSize);
strcpy(P,".tbz2");
F=fopen(Buf,"rb");
if (F==NULL)
{
strcpy(P,".tar.bz2");
F=fopen(Buf,"rb");
}
if (F==NULL)
return noxAlloc(FileSize);
servArchiveSize=0;
fseek(F,0,SEEK_END);
servArchiveSize=ftell(F);
fseek(F,0,SEEK_SET);
Ret=noxAlloc(FileSize+servArchiveSize);
*mapFileSizeServ=FileSize+servArchiveSize;
BYTE *B=(BYTE*)Ret;
B+=FileSize;
fread(B,servArchiveSize,1,F);
DWORD Crc=0xAAAAAAAA;
/* {
DWORD *S=(DWORD*)(B+FileSize);
Size>>=2;
DWORD *E=S+Size;
for(;S!=E;S++)
{
Crc<<=4;
Crc^=*S;
}
servTBZ2Crc=Crc;
}*/
fclose(F);
*mapFileDataServ=B;
return Ret;
}
void __cdecl serverTransferStart(int Player,void *Data,int Size,int Mode)
{
BYTE Buf[MAX_PATH+sizeof(int)+4];
BYTE *P=Buf;
int Len=strlen(servArchiveName);
netUniPacket(upSendArchive,P,sizeof(int)+Len);
*((int*)P)=*mapFileSizeServ-servArchiveSize;/// ðàçìåð òîëüêî êàðòû
P+=4;
memcpy(P,servArchiveName,Len);
P+=Len;
netSendBySock(Player,Buf,P-Buf,Mode);
netSendBySock(Player,Data,Size,Mode);
}
}
bool fsRead(const char *File,void *&Data, size_t &Size)
{
char Buf[220]="";
Data=NULL;Size=0;
TarBlock *B = itemByName(File,true,Buf);
if (!B)
return false;
Data=B->Info.Data;
Size=B->Info.Size;
return true;
}
void netOnSendArchive(int Size,char *Name,char *NameE)
{
cliMapSize=Size;/// ðàçìåð êàðòû
if (ArchiveName)
{
delete ArchiveName;
ArchiveName=0;
}
ArchiveName=new char[NameE-Name+1];
strncpy(ArchiveName,Name,NameE-Name);
ArchiveName[NameE-Name]=0;
}
void netOnAbortDownload()
{
if (MapPath)
{
delete MapPath;
MapPath=0;
}
if (ArchiveName)
{
delete ArchiveName;
ArchiveName=0;
}
cliMapSize=0;
}
void mapLoadFilesystem(const char *MapName)
{
char Buf[200];
do
{
sprintf(Buf,"maps/%s/%s.tbz2",MapName,MapName);
if (readFile(Buf,MapList))
break;
sprintf(Buf,"maps/%s/%s.tar.bz2",MapName,MapName);
if (readFile(Buf,MapList))
break;
return; //íå íàøëè ôàéëà
}while (0);
TarBlock *B=dirSearch("server.lua",&MapList);
if (B==NULL)
return; // íåòó ëóàôàéëà äëÿ ñåðâåðà
if (B->Info.Size>0)
{
int Top=lua_gettop(L);
bool Ok=false;
do
{
if (0!=luaL_loadbuffer(L,(char*)B->Info.Data,B->Info.Size,Buf))
break;
lua_getfield(L,LUA_REGISTRYINDEX,"server");
lua_setfenv(L,-2);
if (0!= lua_pcall(L,0,0,0))
break;
Ok=true;
}while(0);
if (!Ok)
{
conPrintI(lua_tostring(L,-1));
}
lua_settop(L,Top);
}
}
void mapUnloadFilesystem()
{
for (TarList::iterator I=MapList.begin();I!=MapList.end();I++)
{
delete (*I);
}
MapList.clear();
}
extern void InjectOffs(DWORD Addr,void *Fn);
void initFilesystem()
{
/*
ïðè ñìåíå êàðòû
MSG_USE_MAP ïðèõîäèò âñåì, êëèåíòàì âêëþ÷àÿ ñåðâàê
óñòàíàâëèâàåò â gameFlags ôëàã íåîáõîäèìîñòè çàãðóçêè
çàòåì â ãëàâíîì öèêëå ïðè ïðîâåðêå ôëàãà çàãðóæàåò äàííûå
*/
InjectOffs(0x004AB7FD+1,&clientWriteChunk);
InjectOffs(0x004AB8BC+1,&clientWriteChunk);
InjectOffs(0x004ABD64+1,&clientOpenFile);
InjectOffs(0x0043DEF6+1,&mapValidate);
InjectOffs(0x0051A0A1+1,&serverTransferPrepair);
InjectOffs(0x00519AA7+1,&serverTransferStart);
ASSIGN(mapFileHandle,0x007141D4);
ASSIGN(noxWriteToFile,0x00565A02);
ASSIGN(noxFopen,0x0056586C);
ASSIGN(noxFclose,0x00407533);
ASSIGN(mapValidateMB,0x004CF470);
ASSIGN(mapFileDataServ,0x0081B834);
ASSIGN(mapFileSizeServ,0x0081B838);
lua_newtable(L);/// â ýòó òàáëèöó áóäåì ÷òî-íèòü êëàñòü åñëè âåäåì ãëîáàëüíûé ïîèñê
lua_pushcclosure(L,&bz2Loader,1);
lua_setglobal(L,"bz2Loader");
lua_register(L,"fileLoad",&loadFileL);
}