forked from Moonpaw/Plugy-EX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedSaveFile.cpp
More file actions
167 lines (132 loc) · 4.45 KB
/
Copy pathExtendedSaveFile.cpp
File metadata and controls
167 lines (132 loc) · 4.45 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
/*=================================================================
File created by Yohann NICOLAS.
Add an extra save file for each characters.
=================================================================*/
#include "extendedSaveFile.h"
#include "infinityStash.h"
#include "customLibraries.h"
#include "extraOptions.h"
#include "common.h"
#include <stdio.h>
#define BUFFER_SIZE 0x4000
#define FILE_VERSION 0x3130 //"01"
#define FILE_EXTENDED 0x4D545343 //"CSTM"
BYTE * readExtendedSaveFile(char* name, DWORD* size)//WORKS
{
char filename[512];
BYTE* data;
d2_assert(!name, "Bad file name (NULL)", __FILE__, __LINE__);
D2FogGetSavePath( filename, 512-5);
strncat(filename, name, 512 - strlen(filename) - 5);
strcat(filename, ".d2x");
log_msg("Extended file to read : %s\n",filename);
FILE* file = fopen(filename, "rb");
if (file)
{
fseek(file, 0, SEEK_END);
*size = ftell(file);
fseek(file, 0, SEEK_SET);
data = (BYTE*)D2FogMemAlloc(*size,__FILE__,__LINE__,0);
DWORD nbRead = fread(data, 1, *size, file);
fclose(file);
d2_assert(nbRead != *size , "nbRead from extented save file != size", __FILE__, __LINE__);
} else {
log_msg("Can't open extented save file in mode \"rb\" (is not an error if it's a new player)\n");
DWORD maxSize = 100;
data = (BYTE*)D2FogMemAlloc(maxSize,__FILE__,__LINE__,0);
*size = 14;
*((DWORD*)&data[0]) = FILE_EXTENDED; //"CSTM"
*((WORD *)&data[4]) = FILE_VERSION;
*((DWORD*)&data[6]) = nbPlayersCommandByDefault - 1;
*((DWORD*)&data[10]) = 0;// number of stash
TCustomDll* currentDll = customDlls;
while (currentDll)
{
currentDll->initExtendedSaveFile(&data, &maxSize, size);
currentDll=currentDll->nextDll;
}
}
return data;
}
int loadExtendedSaveFile(Unit* ptChar, BYTE data[], DWORD maxSize)//WORKS
{
if (!ptChar || !PCPY || !data) return 0;
log_msg("Load extended file\n");
DWORD curSize = 0;
if (*(DWORD*)&data[curSize] != FILE_EXTENDED)
{
log_msg("loadExtendedSaveFile -> bad header\n");
return 9;
}
curSize += 4;
if (*(WORD *)&data[curSize] != FILE_VERSION)
{
log_msg("loadExtendedSaveFile -> bad file version\n");
return 9;
}
curSize += 2;
nbPlayersCommand = (*(BYTE*)&data[curSize]) + 1;
curSize += 1;
curSize += 3;
int ret = loadStashList(ptChar, data, maxSize, &curSize, false);
TCustomDll* currentDll = customDlls;
while (!ret && currentDll)
{
ret = currentDll->loadExtendedSaveFile(ptChar, data, maxSize, &curSize);
currentDll=currentDll->nextDll;
}
PCPY->selfStashIsOpened = true;
return ret;
}
void writeExtendedSaveFile(char* name, BYTE* data, DWORD size)
{
char szTempName[MAX_PATH];
char szSaveName[MAX_PATH];
//Get temporary savefile name.
D2FogGetSavePath(szTempName, MAX_PATH);
strcat(szTempName, name);
strcat(szTempName, ".d2~");
log_msg("Extended temporary file for saving : %s\n",szTempName);
//Write data in savefile.
FILE* customSaveFile = fopen(szTempName, "wb+");
fwrite(data, size, 1, customSaveFile);
fclose(customSaveFile);
//Get real savefile name.
D2FogGetSavePath(szSaveName, MAX_PATH);
strcat(szSaveName, name);
strcat(szSaveName, ".d2x");
log_msg("Extended file for saving : %s\n",szSaveName);
// if (!MoveFileEx(szTempName, szSaveName, MOVEFILE_WRITE_THROUGH|MOVEFILE_REPLACE_EXISTING))
DeleteFile(szSaveName);
if (!MoveFile(szTempName, szSaveName))
log_box("Could not create the extended save file.");
}
void backupExtendedSaveFile(char* name)
{
char szBackupName[MAX_PATH];
char szSaveName[MAX_PATH];
D2FogGetSavePath(szSaveName, MAX_PATH);
strcat(szSaveName, name);
strcat(szSaveName, ".d2x");
D2FogGetSavePath(szBackupName, MAX_PATH);
strcat(szBackupName, name);
strcat(szBackupName, ".d2x.backup");
CopyFile(szSaveName, szBackupName, true);
}
void saveExtendedSaveFile(Unit* ptChar, BYTE** data, DWORD* maxSize, DWORD* curSize)
{
*(DWORD *)(*data + *curSize) = FILE_EXTENDED; //"CSTM"
*curSize += 4;
*(WORD *)(*data + *curSize) = FILE_VERSION;
*curSize += 2;
*(DWORD *)(*data + *curSize) = (BYTE)(nbPlayersCommand - 1);
*curSize += 4;
saveStashList(ptChar, PCPY->selfStash, data, maxSize, curSize);
TCustomDll* currentDll = customDlls;
while (currentDll)
{
currentDll->saveExtendedSaveFile(ptChar, data, maxSize, curSize);
currentDll=currentDll->nextDll;
}
}
/*================================= END OF FILE =================================*/