forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.cpp
154 lines (133 loc) · 4.46 KB
/
resources.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
#include <string>
#include <iostream>
#include <fstream>
#include <regex>
#include <vector>
#include <limits.h>
#include "lib/easylogging/easylogging++.h"
#include "lib/json/json.hpp"
#include "helpers.h"
#include "errors.h"
#include "settings.h"
#include "resources.h"
#include "api/debug/debug.h"
#include "api/fs/fs.h"
#if defined(_WIN32)
// ifstream and ofstream do not support UTF-8 file paths on Windows.
// However there is a non-standard extension which allows the use of wide strings.
// So, before we pass the path string to the constructor, we have to convert it to a UTF-16 std::wstring.
#define CONVSTR(S) helpers::str2wstr(S)
#else
#define CONVSTR(S) S
#endif
#define NEU_APP_RES_FILE "/resources.neu"
using namespace std;
using json = nlohmann::json;
namespace resources {
json fileTree = nullptr;
unsigned int asarHeaderSize;
resources::ResourceMode mode = resources::ResourceModeBundle;
pair<int, string> __seekFilePos(const string &path, json node, const string &curpath) {
vector<string> pathSegments = helpers::split(path, '/');
string filename = pathSegments[pathSegments.size() - 1];
json json = node;
for(const auto &pathSegment: pathSegments) {
if(pathSegment.length() == 0 || json.is_null() || json["files"].is_null())
continue;
json = json["files"][pathSegment];
}
if(!json.is_null())
return make_pair<int, string>(json["size"].get<int>(), json["offset"].get<string>());
return make_pair<int, string>(-1, "");
}
// Needs explicit close later
ifstream __openResourceFile() {
ifstream asarArchive;
string resFileName = NEU_APP_RES_FILE;
resFileName = settings::joinAppPath(resFileName);
asarArchive.open(CONVSTR(resFileName), ios::binary);
if(!asarArchive) {
debug::log(debug::LogTypeError, errors::makeErrorMsg(errors::NE_RS_TREEGER, resFileName));
}
return asarArchive;
}
fs::FileReaderResult __getFileFromBundle(const string &filename) {
fs::FileReaderResult fileReaderResult;
pair<int, string> p = __seekFilePos(filename, fileTree, "");
if(p.first != -1) {
ifstream asarArchive = __openResourceFile();
if (!asarArchive) {
fileReaderResult.status = errors::NE_RS_TREEGER;
return fileReaderResult;
}
unsigned int uSize = p.first;
unsigned int uOffset = stoi(p.second);
vector<char>fileBuf ( uSize );
asarArchive.seekg(asarHeaderSize + uOffset);
asarArchive.read(fileBuf.data(), uSize);
string fileContent(fileBuf.begin(), fileBuf.end());
fileReaderResult.data = fileContent;
asarArchive.close();
}
else {
fileReaderResult.status = errors::NE_RS_TREEGER;
}
return fileReaderResult;
}
bool __makeFileTree() {
ifstream asarArchive = __openResourceFile();
if (!asarArchive) {
return false;
}
char *sizeBuf = new char[8];
asarArchive.read(sizeBuf, 8);
unsigned int uSize = *(unsigned int *)(sizeBuf + 4) - 8;
delete[] sizeBuf;
asarHeaderSize = uSize + 16;
vector<char> headerBuf(uSize);
asarArchive.seekg(16);
asarArchive.read(headerBuf.data(), uSize);
json files;
string headerContent(headerBuf.begin(), headerBuf.end());
asarArchive.close();
try {
files = json::parse(headerContent);
}
catch(exception e) {
debug::log(debug::LogTypeError, e.what());
}
fileTree = files;
return fileTree != nullptr;
}
void extractFile(const string &filename, const string &outputFilename) {
fs::FileReaderResult fileReaderResult = resources::getFile(filename);
fs::FileWriterOptions fileWriterOptions;
fileWriterOptions.filename = outputFilename;
fileWriterOptions.data = fileReaderResult.data;
fs::writeFile(fileWriterOptions);
}
fs::FileReaderResult getFile(const string &filename) {
if(resources::getMode() == resources::ResourceModeBundle) {
return __getFileFromBundle(filename);
}
return fs::readFile(settings::joinAppPath(filename));
}
void init() {
if(resources::getMode() == resources::ResourceModeDir) {
return;
}
bool resourceLoaderStatus = __makeFileTree();
if(!resourceLoaderStatus) {
resources::setMode(resources::ResourceModeDir); // fallback to directory mode
}
}
void setMode(const resources::ResourceMode m) {
mode = m;
}
resources::ResourceMode getMode() {
return mode;
}
string getModeString() {
return mode == resources::ResourceModeDir ? "directory" : "bundle";
}
} // namespace resources