add utils::game::restart, reset => forceReset

This commit is contained in:
ConfiG 2023-08-08 21:59:13 +03:00
parent 53b52eaeeb
commit 7f449b996e
No known key found for this signature in database
GPG key ID: 44DA1983F524C11B
8 changed files with 54 additions and 17 deletions
loader
include/Geode/utils
launcher/windows
src

View file

@ -127,3 +127,7 @@ namespace geode::utils::clipboard {
GEODE_DLL bool write(std::string const& data);
GEODE_DLL std::string read();
}
namespace geode::utils::game {
GEODE_DLL void restart();
}

View file

@ -107,6 +107,11 @@ int main(int argc, char* argv[]) {
if (argc < 2)
return 0;
if (!waitForFile(workingDir / argv[1])) {
showError("There was an error restarting GD. Please, restart the game manually.");
return 0;
}
// restart gd using the provided path
ShellExecuteA(NULL, "open", (workingDir / argv[1]).string().c_str(), "", workingDir.string().c_str(), TRUE);
return 0;

View file

@ -66,7 +66,7 @@ int geodeEntry(void* platformData) {
"There was a fatal error setting up "
"the internal mod and Geode can not be loaded: " + internalSetupRes.unwrapErr()
);
LoaderImpl::get()->reset();
LoaderImpl::get()->forceReset();
return 1;
}
@ -84,7 +84,7 @@ int geodeEntry(void* platformData) {
"the loader and Geode can not be loaded. "
"(" + setupRes.unwrapErr() + ")"
);
LoaderImpl::get()->reset();
LoaderImpl::get()->forceReset();
return 1;
}

View file

@ -430,9 +430,8 @@ bool Loader::Impl::didLastLaunchCrash() const {
return crashlog::didLastLaunchCrash();
}
void Loader::Impl::reset() {
void Loader::Impl::forceReset() {
this->closePlatformConsole();
for (auto& [_, mod] : m_mods) {
delete mod;
}
@ -447,7 +446,7 @@ bool Loader::Impl::isReadyToHook() const {
}
void Loader::Impl::addInternalHook(Hook* hook, Mod* mod) {
m_internalHooks.push_back({hook, mod});
m_internalHooks.emplace_back(hook, mod);
}
bool Loader::Impl::loadHooks() {

View file

@ -116,7 +116,7 @@ namespace geode {
Result<Mod*> loadModFromInfo(ModInfo const& info);
Result<> setup();
void reset();
void forceReset();
Result<> saveData();
Result<> loadData();

View file

@ -10,6 +10,7 @@ using namespace geode::prelude;
#include <Geode/utils/web.hpp>
#include <Geode/utils/file.hpp>
#include <Geode/utils/cocos.hpp>
#include <Geode/binding/GameManager.hpp>
bool utils::clipboard::write(std::string const& data) {
[[NSPasteboard generalPasteboard] clearContents];
@ -180,7 +181,7 @@ ghc::filesystem::path dirs::getGameDir() {
_NSGetExecutablePath(gddir.data(), &out);
ghc::filesystem::path gdpath = gddir.data();
auto currentPath = gdpath.parent_path().parent_path();
auto currentPath = gdpath.parent_path().parent_path();
return currentPath;
}();
@ -200,4 +201,15 @@ ghc::filesystem::path dirs::getSaveDir() {
return path;
}
void geode::utils::game::restart() {
if (CCApplication::sharedApplication() &&
(GameManager::get()->m_playLayer || GameManager::get()->m_levelEditorLayer)) {
log::error("Cannot restart in PlayLayer or LevelEditorLayer!");
return;
}
// TODO: implement restarting on mac
log::warn("Restarting is not yet implemented on macOS!");
}
#endif

View file

@ -24,16 +24,7 @@ void updateGeode() {
ghc::filesystem::exists(updatesDir / "GeodeUpdater.exe"))
ghc::filesystem::rename(updatesDir / "GeodeUpdater.exe", workingDir / "GeodeUpdater.exe");
wchar_t buffer[MAX_PATH];
GetModuleFileNameW(nullptr, buffer, MAX_PATH);
const auto gdName = ghc::filesystem::path(buffer).filename().string();
// launch updater
const auto updaterPath = (workingDir / "GeodeUpdater.exe").string();
ShellExecuteA(nullptr, "open", updaterPath.c_str(), gdName.c_str(), workingDir.string().c_str(), false);
// quit gd before it can even start
exit(0);
utils::game::restart();
}
int WINAPI gdMainHook(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {

View file

@ -158,4 +158,30 @@ ghc::filesystem::path dirs::getSaveDir() {
return path;
}
void geode::utils::game::restart() {
if (CCApplication::sharedApplication() &&
(GameManager::get()->m_playLayer || GameManager::get()->m_levelEditorLayer)) {
log::error("Cannot restart in PlayLayer or LevelEditorLayer!");
return;
}
const auto workingDir = dirs::getGameDir();
wchar_t buffer[MAX_PATH];
GetModuleFileNameW(nullptr, buffer, MAX_PATH);
const auto gdName = ghc::filesystem::path(buffer).filename().string();
// launch updater
const auto updaterPath = (workingDir / "GeodeUpdater.exe").string();
ShellExecuteA(nullptr, "open", updaterPath.c_str(), gdName.c_str(), workingDir.string().c_str(), false);
if (CCApplication::sharedApplication())
// please forgive me..
// manually set the closed flag
// TODO: actually call glfwSetWindowShouldClose
*reinterpret_cast<bool*>(reinterpret_cast<uintptr_t>(CCEGLView::sharedOpenGLView()->getWindow()) + 0xa) = true;
else
exit(0);
}
#endif