forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.cpp
145 lines (125 loc) · 4.07 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
#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 "settings.h"
#include "resources.h"
#include "api/debug/debug.h"
#include "api/filesystem/filesystem.h"
#define APP_RES_FILE "/resources.neu"
using namespace std;
using json = nlohmann::json;
namespace resources {
json fileTree = nullptr;
unsigned int asarHeaderSize;
bool loadResFromDir = false;
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 = APP_RES_FILE;
resFileName = settings::joinAppPath(resFileName);
asarArchive.open(resFileName, ios::binary);
if(!asarArchive) {
debug::log("ERROR", "Resource file tree generation error: " + resFileName + " is missing.");
}
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.hasError = true;
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.hasError = true;
}
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("ERROR", 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() == "bundle") {
return __getFileFromBundle(filename);
}
fs::FileReaderResult fileReaderResult = fs::readFile(settings::joinAppPath(filename));
if(fileReaderResult.hasError) {
debug::log("ERROR", fileReaderResult.error);
}
return fileReaderResult;
}
void init() {
if(resources::getMode() == "directory") {
return;
}
bool resourceLoaderStatus = __makeFileTree();
if(!resourceLoaderStatus) {
resources::setMode("directory"); // fallback to directory mode
}
}
void setMode(const string &mode) {
loadResFromDir = mode == "directory";
}
string getMode() {
return loadResFromDir ? "directory" : "bundle";
}
} // namespace resources