-
Notifications
You must be signed in to change notification settings - Fork 3
/
File.cpp
141 lines (125 loc) · 3.42 KB
/
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
#include "File.h"
#include <cassert>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include "zlib.h"
#include "KEnvironment.h"
#include "FatalError.h"
void IOFile::read(void * out, size_t length) {
fread(out, length, 1, file);
}
void IOFile::write(const void * out, size_t length) {
fwrite(out, length, 1, file);
}
void IOFile::seek(size_t pos, int mode) {
fseek(file, pos, mode);
}
size_t IOFile::tell() {
return ftell(file);
}
void IOFile::close() {
if (file) {
fclose(file);
file = nullptr;
}
}
void IOFile::open(const char * name, const char * mode) {
if (file) close();
fopen_s(&file, name, mode);
if (!file) FatalErrorCFormat(L"Failed to open the file %S", name);
}
void IOFile::open(const wchar_t* name, const char* mode)
{
if (file) close();
_wfopen_s(&file, name, std::wstring(mode, mode+strlen(mode)).c_str());
if (!file) FatalErrorCFormat(L"Failed to open the file %s", name);
}
void MemFile::read(void * out, size_t length)
{
memcpy(out, _curptr, length);
_curptr += length;
}
void MemFile::write(const void * out, size_t length)
{
memcpy(_curptr, out, length);
_curptr += length;
}
void MemFile::seek(size_t pos, int mode)
{
if (mode == SEEK_SET)
_curptr = _startptr + pos;
else if (mode == SEEK_CUR)
_curptr += pos;
}
size_t MemFile::tell()
{
return _curptr - _startptr;
}
File * GetResourceFile(const char * resName)
{
auto [ptr, size] = GetResourceContent(resName);
return new MemFile(ptr);
}
std::pair<void*, size_t> GetResourceContent(const char* resName)
{
static bool firstTime = true;
static bool useExternalDir = false;
static const wchar_t* extDirName = L"xec_resources";
if (firstTime) {
DWORD attr = GetFileAttributesW(extDirName);
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
useExternalDir = true;
}
firstTime = false;
}
// First check for file presence in customized resources directory
if (useExternalDir) {
wchar_t path[MAX_PATH];
swprintf_s(path, L"%s\\%S", extDirName, resName);
FILE* file = nullptr;
_wfopen_s(&file, path, L"rb");
if (file) {
fseek(file, 0, SEEK_END);
size_t len = ftell(file);
fseek(file, 0, SEEK_SET);
static void* buffer = nullptr;
if (buffer) free(buffer);
if (buffer = malloc(len)) {
fread(buffer, len, 1, file);
fclose(file);
return { buffer, len };
}
fclose(file);
}
}
// Else check in the executable's resources
HMODULE hmod = GetModuleHandleA(NULL);
HRSRC rs = FindResourceA(hmod, resName, "DATA");
if (!rs) return { nullptr, 0 };
HGLOBAL gl = LoadResource(hmod, rs);
if (!gl) return { nullptr, 0 };
return { LockResource(gl), SizeofResource(hmod, rs) };
}
std::unique_ptr<File> GetGzipFile(const wchar_t* path, const char* mode, KEnvironment& kenv)
{
struct GzipFile : File {
gzFile _gzf;
GzipFile(const wchar_t* path, const char* mode) {
_gzf = gzopen_w(path, mode);
if (!_gzf) FatalErrorCFormat(L"Failed to open the gzip-compressed file %s", path);
}
~GzipFile() {
gzclose(_gzf);
}
virtual void read(void* out, size_t length) override { gzread(_gzf, out, length); }
virtual void write(const void* out, size_t length) override { gzwrite(_gzf, out, length); }
virtual void seek(size_t pos, int mode) override { gzseek(_gzf, pos, mode); }
virtual size_t tell() override { return gztell(_gzf); }
};
if (kenv.version >= KEnvironment::KVERSION_ALICE) {
return std::make_unique<GzipFile>(path, mode);
}
else {
return std::make_unique<IOFile>(path, mode);
}
}