forked from HarbourMasters/Ghostship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameExtractor.cpp
More file actions
215 lines (178 loc) · 6.11 KB
/
Copy pathGameExtractor.cpp
File metadata and controls
215 lines (178 loc) · 6.11 KB
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
#ifdef _WIN32
#include <Windows.h>
#include <winuser.h>
#include <shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
#endif
#include "port/build.h"
#include "GameExtractor.h"
#include <cstdio>
#include <unordered_map>
#include <fstream>
#include "Companion.h"
#include "ship/Context.h"
#include "spdlog/spdlog.h"
#include <port/Engine.h>
#ifdef unix
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#if !defined(__IOS__) && !defined(__ANDROID__) && !defined(__SWITCH__)
#include "portable-file-dialogs.h"
#endif
std::unordered_map<std::string, std::string> mGameList = {
{ "8a20a5c83d6ceb0f0506cfc9fa20d8f438cafe51", "Super Mario 64 (JP)" },
{ "9bef1128717f958171a4afac3ed78ee2bb4e86ce", "Super Mario 64 (US)" },
};
bool GameExtractor::SelectGameFromUI() {
std::vector<std::string> roms;
GetRoms(roms);
bool foundGame = false;
// Store both path and already-read data
std::string romPath;
std::vector<uint8_t> romData;
// Auto detect first baserom with valid hash
for (const auto& rom : roms) {
if (!std::filesystem::exists(rom))
continue;
std::ifstream inFile(rom, std::ios::binary);
if (!inFile.is_open()) {
SPDLOG_INFO("Failed to open ROM at path: {}, continuing", rom);
continue;
}
inFile.seekg(0, std::ios::end);
size_t fileSize = inFile.tellg();
inFile.seekg(0, std::ios::beg);
std::vector<uint8_t> data(fileSize);
if (!inFile.read(reinterpret_cast<char*>(data.data()), fileSize)) {
SPDLOG_INFO("Failed to read ROM at path: {}, continuing", rom);
continue;
}
inFile.close();
std::string hash = Companion::CalculateHash(data);
if (mGameList.find(hash) != mGameList.end()) {
romPath = rom;
romData = std::move(data);
foundGame = true;
break;
}
}
#if !defined(__IOS__) && !defined(__ANDROID__) && !defined(__SWITCH__)
// Desktop: fallback to file dialogue if no baserom found
if (!foundGame) {
if (!pfd::settings::available()) {
SPDLOG_ERROR("portable-file-dialogs is not available on this system.");
return false;
}
auto selection = pfd::open_file("Select a file", ".", { "N64 Roms", "*.z64" }).result();
if (selection.empty())
return false;
romPath = selection[0];
}
#else
// Mobile: fallback to baserom.us.z64
if (!foundGame && !std::filesystem::exists(Ship::Context::GetPathRelativeToAppDirectory("baserom.us.z64"))) {
SPDLOG_ERROR("baserom not found");
return false;
}
if (!foundGame) {
romPath = Ship::Context::GetPathRelativeToAppDirectory("baserom.us.z64");
}
#endif
// Load file if it is not already open
if (romData.empty()) {
if (!std::filesystem::exists(romPath)) {
SPDLOG_ERROR("Failed to find ROM at path: {}", romPath);
return false;
}
std::ifstream inFile(romPath, std::ios::binary);
if (!inFile.is_open())
return false;
romData = std::vector<uint8_t>(std::istreambuf_iterator<char>(inFile), {});
inFile.close();
}
this->mGamePath = romPath;
this->mGameData = std::move(romData);
return true;
}
void GameExtractor::GetRoms(std::vector<std::string>& roms) {
#ifdef _WIN32
WIN32_FIND_DATAA ffd;
HANDLE h = FindFirstFileA(".\\*", &ffd);
do {
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
char* ext = PathFindExtensionA(ffd.cFileName);
// Check for any standard N64 rom file extensions.
if ((strcmp(ext, ".z64") == 0))
roms.push_back(ffd.cFileName);
}
} while (FindNextFileA(h, &ffd) != 0);
// if (h != nullptr) {
// CloseHandle(h);
//}
#elif unix
// Open the directory of the app.
DIR* d = opendir(".");
struct dirent* dir;
if (d != NULL) {
// Go through each file in the directory
while ((dir = readdir(d)) != NULL) {
struct stat path;
auto fullPath = std::filesystem::path(".") / dir->d_name;
auto fullPathString = fullPath.string();
const char* fullPathCStr = fullPathString.c_str();
// Check if current entry is not folder
stat(fullPathCStr, &path);
if (S_ISREG(path.st_mode)) {
// Get the position of the extension character.
char* ext = strrchr(dir->d_name, '.');
if (ext != NULL && (strcmp(ext, ".z64") == 0)) {
roms.push_back(fullPathCStr);
}
}
}
}
closedir(d);
#else
for (const auto& file : std::filesystem::directory_iterator(".")) {
if (file.is_directory())
continue;
if (file.path().extension() == ".z64") {
roms.push_back((file.path()));
}
}
#endif
}
std::optional<std::string> GameExtractor::ValidateChecksum() const {
const auto rom = new N64::Cartridge(this->mGameData);
rom->Initialize();
auto hash = rom->GetHash();
if (mGameList.find(hash) == mGameList.end()) {
return std::nullopt;
}
return mGameList[hash];
}
void GameExtractor::WritePortVersion() {
auto writer = LUS::BinaryWriter();
writer.SetEndianness(Torch::Endianness::Big);
writer.Write((uint16_t)gBuildVersionMajor);
writer.Write((uint16_t)gBuildVersionMinor);
writer.Write((uint16_t)gBuildVersionPatch);
writer.Close();
Companion::Instance->RegisterCompanionFile("portVersion", writer.ToVector());
}
bool GameExtractor::GenerateOTR() {
const std::string assets_path = Ship::Context::GetAppBundlePath();
const std::string game_path = Ship::Context::GetAppDirectoryPath();
Companion::Instance = new Companion(this->mGameData, ArchiveType::O2R, false, assets_path, game_path);
this->WritePortVersion();
try {
Companion::Instance->Init(ExportType::Binary);
} catch (const std::exception& e) {
SPDLOG_INFO("Failed to process O2R {}", e.what());
return false;
}
return true;
}