-
Notifications
You must be signed in to change notification settings - Fork 2
/
ONScripter_file.cpp
260 lines (228 loc) · 8.12 KB
/
ONScripter_file.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
/* -*- C++ -*-
*
* ONScripter_file.cpp - FILE I/O of ONScripter
*
* Copyright (c) 2001-2012 Ogapee. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ONScripter.h"
#if defined(LINUX) || defined(MACOSX) || defined(IOS)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#elif defined(WIN32)
#include <windows.h>
#elif defined(MACOS9)
#include <DateTimeUtils.h>
#include <Files.h>
extern "C" void c2pstrcpy(Str255 dst, const char *src); //#include <TextUtils.h>
#elif defined(PSP)
#include <pspiofilemgr.h>
#endif
#define SAVEFILE_MAGIC_NUMBER "ONS"
#define SAVEFILE_VERSION_MAJOR 2
#define SAVEFILE_VERSION_MINOR 8
#define READ_LENGTH 4096
void ONScripter::searchSaveFile( SaveFileInfo &save_file_info, int no )
{
char file_name[256];
script_h.getStringFromInteger( save_file_info.sjis_no, no, (num_save_file >= 10)?2:1 );
#if defined(LINUX) || defined(MACOSX) || defined(IOS)
sprintf( file_name, "%ssave%d.dat", archive_path, no );
struct stat buf;
struct tm *tm;
if ( stat( file_name, &buf ) != 0 ){
save_file_info.valid = false;
return;
}
time_t mtime = buf.st_mtime;
tm = localtime( &mtime );
save_file_info.month = tm->tm_mon + 1;
save_file_info.day = tm->tm_mday;
save_file_info.hour = tm->tm_hour;
save_file_info.minute = tm->tm_min;
#elif defined(WIN32)
sprintf( file_name, "%ssave%d.dat", archive_path, no );
HANDLE handle;
FILETIME tm, ltm;
SYSTEMTIME stm;
#if defined(WINCE)
WCHAR file_nameW[256];
MultiByteToWideChar(CP_ACP, 0, file_name, -1, file_nameW, 256);
handle = CreateFile( file_nameW, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
#else
handle = CreateFile( file_name, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
#endif
if ( handle == INVALID_HANDLE_VALUE ){
save_file_info.valid = false;
return;
}
GetFileTime( handle, NULL, NULL, &tm );
FileTimeToLocalFileTime( &tm, <m );
FileTimeToSystemTime( <m, &stm );
CloseHandle( handle );
save_file_info.month = stm.wMonth;
save_file_info.day = stm.wDay;
save_file_info.hour = stm.wHour;
save_file_info.minute = stm.wMinute;
#elif defined(MACOS9)
sprintf( file_name, "%ssave%d.dat", archive_path, no );
CInfoPBRec pb;
Str255 p_file_name;
FSSpec file_spec;
DateTimeRec tm;
c2pstrcpy( p_file_name, file_name );
if ( FSMakeFSSpec(0, 0, p_file_name, &file_spec) != noErr ){
save_file_info.valid = false;
return;
}
pb.hFileInfo.ioNamePtr = file_spec.name;
pb.hFileInfo.ioVRefNum = file_spec.vRefNum;
pb.hFileInfo.ioFDirIndex = 0;
pb.hFileInfo.ioDirID = file_spec.parID;
if (PBGetCatInfoSync(&pb) != noErr) {
save_file_info.valid = false;
return;
}
SecondsToDate( pb.hFileInfo.ioFlMdDat, &tm );
save_file_info.month = tm.month;
save_file_info.day = tm.day;
save_file_info.hour = tm.hour;
save_file_info.minute = tm.minute;
#elif defined(PSP)
sprintf( file_name, "%ssave%d.dat", archive_path, no );
SceIoStat buf;
if ( sceIoGetstat(file_name, &buf)<0 ){
save_file_info.valid = false;
return;
}
save_file_info.month = buf.st_mtime.month;
save_file_info.day = buf.st_mtime.day;
save_file_info.hour = buf.st_mtime.hour;
save_file_info.minute = buf.st_mtime.minute;
#else
sprintf( file_name, "save%d.dat", no );
FILE *fp;
if ( (fp = fopen( file_name, "rb" )) == NULL ){
save_file_info.valid = false;
return;
}
fclose( fp );
save_file_info.month = 1;
save_file_info.day = 1;
save_file_info.hour = 0;
save_file_info.minute = 0;
#endif
save_file_info.valid = true;
script_h.getStringFromInteger( save_file_info.sjis_month, save_file_info.month, 2 );
script_h.getStringFromInteger( save_file_info.sjis_day, save_file_info.day, 2 );
script_h.getStringFromInteger( save_file_info.sjis_hour, save_file_info.hour, 2 );
script_h.getStringFromInteger( save_file_info.sjis_minute, save_file_info.minute, 2, true );
}
char *ONScripter::readSaveStrFromFile( int no )
{
char filename[16];
sprintf( filename, "save%d.dat", no );
size_t len = loadFileIOBuf( filename );
if (len == 0){
fprintf( stderr, "readSaveStrFromFile: can't open save file %s\n", filename );
return NULL;
}
int p = len - 1;
if ( p < 3 || file_io_buf[p] != '*' || file_io_buf[p-1] != '"' ) return NULL;
p -= 2;
while( file_io_buf[p] != '"' && p>0 ) p--;
if ( file_io_buf[p] != '"' ) return NULL;
len = len - p - 3;
char *buf = new char[len+1];
int i;
for (i=0 ; i<len ; i++)
buf[i] = file_io_buf[p+i+1];
buf[i] = 0;
return buf;
}
int ONScripter::loadSaveFile( int no )
{
char filename[16];
sprintf( filename, "save%d.dat", no );
if (loadFileIOBuf( filename ) == 0){
fprintf( stderr, "can't open save file %s\n", filename );
return -1;
}
/* ---------------------------------------- */
/* Load magic number */
int i;
for ( i=0 ; i<(int)strlen( SAVEFILE_MAGIC_NUMBER ) ; i++ )
if ( readChar() != SAVEFILE_MAGIC_NUMBER[i] ) break;
if ( i != (int)strlen( SAVEFILE_MAGIC_NUMBER ) ){
file_io_buf_ptr = 0;
printf("Save file version is unknown\n" );
return loadSaveFile2( SAVEFILE_VERSION_MAJOR*100 + SAVEFILE_VERSION_MINOR );
}
int file_version = readChar() * 100;
file_version += readChar();
printf("Save file version is %d.%d\n", file_version/100, file_version%100 );
if ( file_version > SAVEFILE_VERSION_MAJOR*100 + SAVEFILE_VERSION_MINOR ){
fprintf( stderr, "Save file is newer than %d.%d, please use the latest ONScripter.\n", SAVEFILE_VERSION_MAJOR, SAVEFILE_VERSION_MINOR );
return -1;
}
if ( file_version >= 200 )
return loadSaveFile2( file_version );
fprintf( stderr, "Save file is too old.\n");
return -1;
}
void ONScripter::saveMagicNumber( bool output_flag )
{
for ( unsigned int i=0 ; i<strlen( SAVEFILE_MAGIC_NUMBER ) ; i++ )
writeChar( SAVEFILE_MAGIC_NUMBER[i], output_flag );
writeChar( SAVEFILE_VERSION_MAJOR, output_flag );
writeChar( SAVEFILE_VERSION_MINOR, output_flag );
}
int ONScripter::saveSaveFile( int no, const char *savestr )
{
// make save data structure on memory
if (no < 0 || (saveon_flag && internal_saveon_flag)){
file_io_buf_ptr = 0;
saveMagicNumber( false );
saveSaveFile2( false );
allocFileIOBuf();
saveMagicNumber( true );
saveSaveFile2( true );
save_data_len = file_io_buf_ptr;
memcpy(save_data_buf, file_io_buf, save_data_len);
}
if ( no >= 0 ){
saveAll();
char filename[16];
sprintf( filename, "save%d.dat", no );
memcpy(file_io_buf, save_data_buf, save_data_len);
file_io_buf_ptr = save_data_len;
if (saveFileIOBuf( filename, 0, savestr )){
fprintf( stderr, "can't open save file %s for writing\n", filename );
return -1;
}
size_t magic_len = strlen(SAVEFILE_MAGIC_NUMBER)+2;
sprintf( filename, RELATIVEPATH "sav%csave%d.dat", DELIMITER, no );
if (saveFileIOBuf( filename, magic_len, savestr ))
fprintf( stderr, "can't open save file %s for writing (not an error)\n", filename );
}
return 0;
}