/
Context.cpp
321 lines (269 loc) · 9.91 KB
/
Context.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "Context.h"
#include <iostream>
#include "spdlog/spdlog.h"
#include <spdlog/async.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include "log/spd/ConsoleSink.h"
#include "misc/Hooks.h"
#ifdef __APPLE__
#include "misc/OSXFolderManager.h"
#elif defined(__SWITCH__)
#include "port/switch/SwitchImpl.h"
#elif defined(__WIIU__)
#include "port/wiiu/WiiUImpl.h"
#endif
namespace LUS {
std::weak_ptr<Context> Context::mContext;
std::shared_ptr<Context> Context::GetInstance() {
return mContext.lock();
}
std::shared_ptr<Context> Context::CreateInstance(const std::string name, const std::string shortName,
const std::vector<std::string>& otrFiles,
const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount) {
if (mContext.expired()) {
auto shared = std::make_shared<Context>(name, shortName);
mContext = shared;
shared->Init(otrFiles, validHashes, reservedThreadCount);
return shared;
}
SPDLOG_DEBUG("Trying to create a context when it already exists. Returning existing.");
return GetInstance();
}
Context::Context(std::string name, std::string shortName) : mName(std::move(name)), mShortName(std::move(shortName)) {
}
void Context::CreateDefaultSettings() {
if (GetConfig()->isNewInstance) {
GetConfig()->setInt("Window.Width", 640);
GetConfig()->setInt("Window.Height", 480);
GetConfig()->setString("Window.GfxBackend", "");
GetConfig()->setString("Window.GfxApi", "");
GetConfig()->setString("Window.AudioBackend", "");
GetConfig()->setBool("Window.Fullscreen.Enabled", false);
GetConfig()->setInt("Window.Fullscreen.Width", 1920);
GetConfig()->setInt("Window.Fullscreen.Height", 1080);
GetConfig()->setString("Game.SaveName", "");
GetConfig()->setString("Game.Main Archive", "");
GetConfig()->setString("Game.Patches Archive", "");
GetConfig()->setInt("Shortcuts.Fullscreen", 0x044);
GetConfig()->setInt("Shortcuts.Console", 0x029);
GetConfig()->save();
}
}
void Context::Init(const std::vector<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount) {
InitLogging();
InitConfiguration();
InitConsoleVariables();
InitResourceManager(otrFiles, validHashes, reservedThreadCount);
CreateDefaultSettings();
InitControlDeck();
InitCrashHandler();
InitAudioPlayer(GetConfig()->getString("Window.AudioBackend"));
InitWindow();
LUS::RegisterHook<ExitGame>([this]() { mControlDeck->SaveSettings(); });
}
void Context::InitLogging() {
try {
// Setup Logging
spdlog::init_thread_pool(8192, 1);
std::vector<spdlog::sink_ptr> sinks;
auto consoleSink = std::make_shared<spdlog::sinks::lus_sink_mt>();
// consoleSink->set_level(spdlog::level::trace);
sinks.push_back(consoleSink);
#if (!defined(_WIN32) && !defined(__WIIU__)) || defined(_DEBUG)
#if defined(_DEBUG) && defined(_WIN32)
// LLVM on Windows allocs a hidden console in its entrypoint function.
// We free that console here to create our own.
FreeConsole();
if (AllocConsole() == 0) {
throw std::system_error(GetLastError(), std::generic_category(), "Failed to create debug console");
}
SetConsoleOutputCP(CP_UTF8);
FILE* fDummy;
freopen_s(&fDummy, "CONOUT$", "w", stdout);
freopen_s(&fDummy, "CONOUT$", "w", stderr);
freopen_s(&fDummy, "CONIN$", "r", stdin);
std::cout.clear();
std::clog.clear();
std::cerr.clear();
std::cin.clear();
HANDLE hConOut = CreateFile(_T("CONOUT$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hConIn = CreateFile(_T("CONIN$"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
SetStdHandle(STD_OUTPUT_HANDLE, hConOut);
SetStdHandle(STD_ERROR_HANDLE, hConOut);
SetStdHandle(STD_INPUT_HANDLE, hConIn);
std::wcout.clear();
std::wclog.clear();
std::wcerr.clear();
std::wcin.clear();
#endif
auto systemConsoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
// systemConsoleSink->set_level(spdlog::level::trace);
sinks.push_back(systemConsoleSink);
#endif
#ifndef __WIIU__
auto logPath = GetPathRelativeToAppDirectory(("logs/" + GetName() + ".log").c_str());
auto fileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logPath, 1024 * 1024 * 10, 10);
#ifdef _DEBUG
fileSink->set_level(spdlog::level::trace);
#else
fileSink->set_level(spdlog::level::debug);
#endif
sinks.push_back(fileSink);
#endif
mLogger = std::make_shared<spdlog::async_logger>(GetName(), sinks.begin(), sinks.end(), spdlog::thread_pool(),
spdlog::async_overflow_policy::block);
#ifdef _DEBUG
GetLogger()->set_level(spdlog::level::trace);
#else
GetLogger()->set_level(spdlog::level::debug);
#endif
#if defined(_DEBUG)
GetLogger()->flush_on(spdlog::level::trace);
#endif
#ifndef __WIIU__
GetLogger()->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%@] [%l] %v");
#else
GetLogger()->set_pattern("[%s:%#] [%l] %v");
#endif
spdlog::register_logger(GetLogger());
spdlog::set_default_logger(GetLogger());
} catch (const spdlog::spdlog_ex& ex) { std::cout << "Log initialization failed: " << ex.what() << std::endl; }
}
void Context::InitConfiguration() {
mConfig = std::make_shared<Mercury>(GetPathRelativeToAppDirectory("shipofharkinian.json"));
}
void Context::InitConsoleVariables() {
mConsoleVariables = std::make_shared<ConsoleVariable>();
}
void Context::InitResourceManager(const std::vector<std::string>& otrFiles,
const std::unordered_set<uint32_t>& validHashes, uint32_t reservedThreadCount) {
mMainPath = GetConfig()->getString("Game.Main Archive", GetAppDirectoryPath());
mPatchesPath = mConfig->getString("Game.Patches Archive", GetAppDirectoryPath() + "/mods");
if (otrFiles.empty()) {
mResourceManager =
std::make_shared<ResourceManager>(GetInstance(), mMainPath, mPatchesPath, validHashes, reservedThreadCount);
} else {
mResourceManager = std::make_shared<ResourceManager>(GetInstance(), otrFiles, validHashes, reservedThreadCount);
}
if (!mResourceManager->DidLoadSuccessfully()) {
#if defined(__SWITCH__)
printf("Main OTR file not found!\n");
#elif defined(__WIIU__)
LUS::WiiU::ThrowMissingOTR(mMainPath.c_str());
#else
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "OTR file not found",
"Main OTR file not found. Please generate one", nullptr);
SPDLOG_ERROR("Main OTR file not found!");
#endif
mOtrFileExists = false;
return;
}
mOtrFileExists = true;
#ifdef __SWITCH__
LUS::Switch::Init(PostInitPhase);
#endif
}
void Context::InitControlDeck() {
mControlDeck = std::make_shared<ControlDeck>();
}
void Context::InitCrashHandler() {
mCrashHandler = std::make_shared<CrashHandler>();
}
void Context::InitAudioPlayer(std::string backend) {
// Param can override
#ifdef _WIN32
if (backend == "wasapi") {
mAudioPlayer = std::make_shared<WasapiAudioPlayer>(backend);
return;
}
#endif
#if defined(__linux)
if (backend == "pulse") {
mAudioPlayer = std::make_shared<PulseAudioPlayer>(backend);
return;
}
#endif
if (backend == "sdl") {
mAudioPlayer = std::make_shared<SDLAudioPlayer>(backend);
return;
}
// Defaults if not on list above
#ifdef _WIN32
mAudioPlayer = std::make_shared<WasapiAudioPlayer>(backend);
#elif defined(__linux)
mAudioPlayer = std::make_shared<PulseAudioPlayer>(backend);
#else
mAudioPlayer = std::make_shared<SDLAudioPlayer>(backend);
#endif
}
void Context::InitWindow() {
mWindow = std::make_shared<Window>(GetInstance());
GetWindow()->Init();
}
std::shared_ptr<ConsoleVariable> Context::GetConsoleVariables() {
return mConsoleVariables;
}
std::shared_ptr<spdlog::logger> Context::GetLogger() {
return mLogger;
}
std::shared_ptr<Mercury> Context::GetConfig() {
return mConfig;
}
std::shared_ptr<ResourceManager> Context::GetResourceManager() {
return mResourceManager;
}
std::shared_ptr<ControlDeck> Context::GetControlDeck() {
return mControlDeck;
}
std::shared_ptr<CrashHandler> Context::GetCrashHandler() {
return mCrashHandler;
}
std::shared_ptr<AudioPlayer> Context::GetAudioPlayer() {
return mAudioPlayer;
}
std::shared_ptr<Window> Context::GetWindow() {
return mWindow;
}
std::string Context::GetName() {
return mName;
}
std::string Context::GetShortName() {
return mShortName;
}
std::string Context::GetAppBundlePath() {
#ifdef __APPLE__
FolderManager folderManager;
return folderManager.getMainBundlePath();
#endif
#ifdef __linux__
char* fpath = std::getenv("SHIP_BIN_DIR");
if (fpath != NULL) {
return std::string(fpath);
}
#endif
return ".";
}
std::string Context::GetAppDirectoryPath() {
#if defined(__linux__) || defined(__APPLE__)
char* fpath = std::getenv("SHIP_HOME");
if (fpath != NULL) {
return std::string(fpath);
}
#endif
return ".";
}
std::string Context::GetPathRelativeToAppBundle(const char* path) {
return GetAppBundlePath() + "/" + path;
}
std::string Context::GetPathRelativeToAppDirectory(const char* path) {
return GetAppDirectoryPath() + "/" + path;
}
bool Context::DoesOtrFileExist() {
return mOtrFileExists;
}
} // namespace LUS