Refactor savefile path handling

This commit is contained in:
kichikuou
2025-06-06 16:13:02 +09:00
parent bce04adf82
commit febee3f5a3
8 changed files with 29 additions and 30 deletions
-9
View File
@@ -9,9 +9,7 @@
#define SYSTEM3_VERSION "1.6.1"
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
@@ -33,13 +31,6 @@ typedef uint32_t uint32;
[[noreturn]] void sys_error(const char* format, ...);
void sys_warning(const char* format, ...);
#ifndef WIN32
#define _MAX_PATH PATH_MAX
#define sscanf_s sscanf
#endif // !WIN32
#ifdef _MSC_VER
#define strcasecmp _stricmp
#endif
+14 -2
View File
@@ -20,6 +20,12 @@ std::string FILEIO::savedir = ".";
namespace {
std::string savefile_name(int index) {
std::string name = "ASLEEP_A.DAT";
name[7] += index - 1;
return name;
}
std::string FindFile(const std::string& dir, const char* filename) {
std::string path = dir + "/" + filename;
@@ -47,9 +53,9 @@ void FILEIO::set_savedir(const std::string& dir)
savedir.pop_back();
}
int FILEIO::stat_save(const char* filename, struct stat* buf)
int FILEIO::stat_save(int index, struct stat* buf)
{
return stat(FindFile(savedir, filename).c_str(), buf);
return stat(FindFile(savedir, savefile_name(index).c_str()).c_str(), buf);
}
FILEIO::~FILEIO(void)
@@ -78,6 +84,12 @@ std::unique_ptr<FILEIO> FILEIO::open(const char *file_name, int mode)
return fp ? std::unique_ptr<FILEIO>(new FILEIO(fp, mode)) : nullptr;
}
//static
std::unique_ptr<FILEIO> FILEIO::open_save(int index, int mode)
{
return open(savefile_name(index).c_str(), mode | FILEIO_SAVEDATA);
}
std::string FILEIO::gets()
{
std::string s;
+2 -1
View File
@@ -30,8 +30,9 @@ public:
~FILEIO();
static void set_savedir(const std::string& dir);
static int stat_save(const char* filename, struct stat* buf);
static int stat_save(int index, struct stat* buf);
static std::unique_ptr<FILEIO> open(const char *file_name, int mode);
static std::unique_ptr<FILEIO> open_save(int index, int mode);
int seek(long offset, int whence) { return fseek(fp, offset, whence); }
long tell() { return ftell(fp); }
bool read(void* buffer, size_t size) {
+1
View File
@@ -5,6 +5,7 @@
*/
#include <memory>
#include <limits.h>
#include <SDL.h>
#include <SDL_mixer.h>
+3 -2
View File
@@ -1,3 +1,4 @@
#include <limits.h>
#ifdef _WIN32
#include <windows.h>
#undef ERROR
@@ -21,8 +22,8 @@ SDL_RWops* open_resource(const char* name, const char* type) {
#else
// On Android, read from APK assets.
// On other platforms, read from a file under RESOURCE_PATH.
char path[_MAX_PATH];
snprintf(path, _MAX_PATH, "%s%s/%s", RESOURCE_PATH, type, name);
char path[PATH_MAX];
snprintf(path, PATH_MAX, "%s%s/%s", RESOURCE_PATH, type, name);
return SDL_RWFromFile(path, "rb");
#endif
}
+1
View File
@@ -5,6 +5,7 @@
*/
#include <algorithm>
#include <limits.h>
#include <string.h>
#include "ags.h"
#include "nact.h"
+6 -8
View File
@@ -20,6 +20,10 @@
#include "game_id.h"
#include "debugger/debugger.h"
#ifndef WIN32
#define sscanf_s sscanf
#endif
extern SDL_Window* g_window;
// 初期化
@@ -533,10 +537,7 @@ void NACT::text_wait()
bool NACT::load(int index)
{
char file_name[_MAX_PATH];
snprintf(file_name, _MAX_PATH, "ASLEEP_%c.DAT", 'A' + index - 1);
auto fio = FILEIO::open(file_name, FILEIO_READ_BINARY | FILEIO_SAVEDATA);
auto fio = FILEIO::open_save(index, FILEIO_READ_BINARY);
if (!fio)
return false;
fio->seek(112, SEEK_SET);
@@ -576,10 +577,7 @@ bool NACT::load(int index)
bool NACT::save(int index, const char header[112])
{
char file_name[_MAX_PATH];
snprintf(file_name, _MAX_PATH, "ASLEEP_%c.DAT", 'A' + index - 1);
auto fio = FILEIO::open(file_name, FILEIO_WRITE_BINARY | FILEIO_SAVEDATA);
auto fio = FILEIO::open_save(index, FILEIO_WRITE_BINARY);
if (!fio)
return false;
+2 -8
View File
@@ -494,10 +494,7 @@ void NACT_Sys3::cmd_l()
RND = 0;
} else if (101 <= index && index <= 126) {
// ASLEEP_A.DAT - ASLEEP_Z.DAT
char file_name[_MAX_PATH];
snprintf(file_name, _MAX_PATH, "ASLEEP_%c.DAT", 'A' + index - 101);
auto fio = FILEIO::open(file_name, FILEIO_READ_BINARY | FILEIO_SAVEDATA);
auto fio = FILEIO::open_save(index - 100, FILEIO_READ_BINARY);
if (fio) {
fio->seek(112 + 16, SEEK_SET);
RND = fio->getw();
@@ -1017,11 +1014,8 @@ void NACT_Sys3::cmd_y()
break;
case 239:
if(1 <= param && param <= 26) {
char file_name[_MAX_PATH];
snprintf(file_name, _MAX_PATH, "ASLEEP_%c.DAT", 'A' + param - 1);
struct stat statbuf;
if (FILEIO::stat_save(file_name, &statbuf) != -1) {
if (FILEIO::stat_save(param, &statbuf) != -1) {
struct tm *t = localtime(&statbuf.st_mtime);
D01 = t->tm_year + 1900;
D02 = t->tm_mon + 1;