Skip to content

Commit

Permalink
change Inits from void to bool
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed Dec 16, 2024
1 parent a96a8e2 commit 0f649bd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 50 deletions.
127 changes: 88 additions & 39 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,16 @@ Context::Context(std::string name, std::string shortName, std::string configFile
: mConfigFilePath(std::move(configFilePath)), mName(std::move(name)), mShortName(std::move(shortName)) {
}

void Context::Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validHashes,
bool Context::Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window) {
InitLogging();
InitConfiguration();
InitConsoleVariables();
InitResourceManager(archivePaths, validHashes, reservedThreadCount);
InitControlDeck();
InitCrashHandler();
InitConsole();
InitWindow(window);
InitAudio(audioSettings);
InitGfxDebugger();
}

void Context::InitLogging() {
return InitLogging() && InitConfiguration() && InitConsoleVariables() &&
InitResourceManager(archivePaths, validHashes, reservedThreadCount) && InitControlDeck() &&
InitCrashHandler() && InitConsole() && InitWindow(window) && InitAudio(audioSettings) && InitGfxDebugger();
}

bool Context::InitLogging() {
if (GetLogger() != nullptr) {
return;
return true;
}

try {
Expand Down Expand Up @@ -160,29 +153,44 @@ void Context::InitLogging() {

spdlog::register_logger(GetLogger());
spdlog::set_default_logger(GetLogger());
} catch (const spdlog::spdlog_ex& ex) { std::cout << "Log initialization failed: " << ex.what() << std::endl; }
return true;
} catch (const spdlog::spdlog_ex& ex) { std::cout << "Log initialization failed: " << ex.what() << std::endl; return false; }
}

void Context::InitConfiguration() {
bool Context::InitConfiguration() {
if (GetConfig() != nullptr) {
return;
return true;
}

mConfig = std::make_shared<Config>(GetPathRelativeToAppDirectory(mConfigFilePath));

if (GetConfig() == nullptr) {
SPDLOG_ERROR("Failed to initialize config");
return false;
}

return true;
}

void Context::InitConsoleVariables() {
bool Context::InitConsoleVariables() {
if (GetConsoleVariables() != nullptr) {
return;
return true;
}

mConsoleVariables = std::make_shared<ConsoleVariable>();

if (GetConsoleVariables() == nullptr) {
SPDLOG_ERROR("Failed to initialize console variables");
return false;
}

return true;
}

void Context::InitResourceManager(const std::vector<std::string>& archivePaths,
bool Context::InitResourceManager(const std::vector<std::string>& archivePaths,
const std::unordered_set<uint32_t>& validHashes, uint32_t reservedThreadCount) {
if (GetResourceManager() != nullptr) {
return;
return true;
}

mMainPath = GetConfig()->GetString("Game.Main Archive", GetAppDirectoryPath());
Expand All @@ -207,64 +215,105 @@ void Context::InitResourceManager(const std::vector<std::string>& archivePaths,
// We need this exit to close the app when we dismiss the dialog
exit(0);
#endif
return;
return false;
}

return true;
}

void Context::InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) {
bool Context::InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) {
if (GetControlDeck() != nullptr) {
return;
return true;
}

mControlDeck = std::make_shared<ControlDeck>(additionalBitmasks);

if (GetControlDeck() == nullptr) {
SPDLOG_ERROR("Failed to initialize control deck");
return false;
}

return true;
}

void Context::InitCrashHandler() {
bool Context::InitCrashHandler() {
if (GetCrashHandler() != nullptr) {
return;
return true;
}

mCrashHandler = std::make_shared<CrashHandler>();

if (GetCrashHandler() == nullptr) {
SPDLOG_ERROR("Failed to initialize crash handler");
return false;
}

return true;
}

void Context::InitAudio(AudioSettings settings) {
bool Context::InitAudio(AudioSettings settings) {
if (GetAudio() != nullptr) {
return;
return true;
}

mAudio = std::make_shared<Audio>(settings);

if (GetAudio() == nullptr) {
SPDLOG_ERROR("Failed to initialize audio");
return false;
}

GetAudio()->Init();
return true;
}

void Context::InitGfxDebugger() {
bool Context::InitGfxDebugger() {
if (GetGfxDebugger() != nullptr) {
return;
return true;
}

mGfxDebugger = std::make_shared<Fast::GfxDebugger>();

if (GetGfxDebugger() == nullptr) {
SPDLOG_ERROR("Failed to initialize gfx debugger");
return false;
}

return true;
}

void Context::InitConsole() {
bool Context::InitConsole() {
if (GetConsole() != nullptr) {
return;
return true;
}

mConsole = std::make_shared<Console>();

if (GetConsole() == nullptr) {
SPDLOG_ERROR("Failed to initialize console");
return false;
}

GetConsole()->Init();

return true;
}

void Context::InitWindow(std::shared_ptr<Window> window) {
bool Context::InitWindow(std::shared_ptr<Window> window) {
if (GetWindow() != nullptr) {
return;
return true;
}

if (window == nullptr) {
// todo: better error - quit?
SPDLOG_ERROR("CONTEXT NEEDS WINDOW BADLY");
mWindow = window;

if (GetWindow() == nullptr) {
SPDLOG_ERROR("Failed to initialize window");
return false;
}

mWindow = window;
GetWindow()->Init();

return true;
}

std::shared_ptr<ConsoleVariable> Context::GetConsoleVariables() {
Expand Down
22 changes: 11 additions & 11 deletions src/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Context {
Context(std::string name, std::string shortName, std::string configFilePath);
~Context();

void Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validHashes,
bool Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window = nullptr);

std::shared_ptr<spdlog::logger> GetLogger();
Expand All @@ -55,17 +55,17 @@ class Context {
std::string GetName();
std::string GetShortName();

void InitLogging();
void InitConfiguration();
void InitConsoleVariables();
void InitResourceManager(const std::vector<std::string>& archivePaths = {},
bool InitLogging();
bool InitConfiguration();
bool InitConsoleVariables();
bool InitResourceManager(const std::vector<std::string>& archivePaths = {},
const std::unordered_set<uint32_t>& validHashes = {}, uint32_t reservedThreadCount = 1);
void InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks = {});
void InitCrashHandler();
void InitAudio(AudioSettings settings);
void InitGfxDebugger();
void InitConsole();
void InitWindow(std::shared_ptr<Window> window = nullptr);
bool InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks = {});
bool InitCrashHandler();
bool InitAudio(AudioSettings settings);
bool InitGfxDebugger();
bool InitConsole();
bool InitWindow(std::shared_ptr<Window> window = nullptr);

protected:
Context() = default;
Expand Down

0 comments on commit 0f649bd

Please sign in to comment.