mirror of
https://github.com/geode-sdk/geode.git
synced 2025-03-24 03:39:56 -04:00
format code and use std::filesystem in updater
This commit is contained in:
parent
1ae1f7df73
commit
d5fe754b65
1 changed files with 27 additions and 28 deletions
|
@ -1,24 +1,24 @@
|
|||
#include <Windows.h>
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <ghc/filesystem.hpp>
|
||||
#include <filesystem>
|
||||
|
||||
ghc::filesystem::path workingDir;
|
||||
ghc::filesystem::path geodeDir;
|
||||
ghc::filesystem::path updatesDir;
|
||||
ghc::filesystem::path resourcesDir;
|
||||
std::filesystem::path workingDir;
|
||||
std::filesystem::path geodeDir;
|
||||
std::filesystem::path updatesDir;
|
||||
std::filesystem::path resourcesDir;
|
||||
|
||||
void showError(std::string const& error) {
|
||||
MessageBoxA(nullptr, error.c_str(), "Error Loading Geode", MB_ICONERROR);
|
||||
}
|
||||
|
||||
bool waitForFile(ghc::filesystem::path const& path) {
|
||||
bool waitForFile(std::filesystem::path const& path) {
|
||||
if (!path.has_filename())
|
||||
return false;
|
||||
|
||||
int delay = 10;
|
||||
HANDLE hFile;
|
||||
while ((hFile = CreateFile(path.string().c_str(), FILE_GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) {
|
||||
while ((hFile = CreateFileA(path.string().c_str(), FILE_GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) {
|
||||
if (GetLastError() == ERROR_SHARING_VIOLATION) {
|
||||
Sleep(delay);
|
||||
// the delay would raise and go up to about 5 seconds, after which it will fail
|
||||
|
@ -38,29 +38,29 @@ bool waitForFile(ghc::filesystem::path const& path) {
|
|||
}
|
||||
|
||||
void updateFile(std::string const& name) {
|
||||
auto error = std::error_code();
|
||||
if (!ghc::filesystem::exists(updatesDir / name, error) || error)
|
||||
std::error_code error;
|
||||
if (!std::filesystem::exists(updatesDir / name, error) || error)
|
||||
return;
|
||||
if (!waitForFile(workingDir / name))
|
||||
return;
|
||||
|
||||
ghc::filesystem::rename(updatesDir / name, workingDir / name, error);
|
||||
std::filesystem::rename(updatesDir / name, workingDir / name, error);
|
||||
if (error) {
|
||||
showError("Unable to update Geode: Unable to move " + name + " - " + error.message());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void removePath(ghc::filesystem::path const& path) {
|
||||
auto error = std::error_code();
|
||||
if (!ghc::filesystem::exists(path, error) || error)
|
||||
void removePath(std::filesystem::path const& path) {
|
||||
std::error_code error;
|
||||
if (!std::filesystem::exists(path, error) || error)
|
||||
return;
|
||||
if (path.has_filename() && !waitForFile(path))
|
||||
return;
|
||||
|
||||
if (ghc::filesystem::is_directory(path) && !ghc::filesystem::is_empty(path))
|
||||
ghc::filesystem::remove_all(path, error);
|
||||
ghc::filesystem::remove(path, error);
|
||||
if (std::filesystem::is_directory(path) && !std::filesystem::is_empty(path))
|
||||
std::filesystem::remove_all(path, error);
|
||||
std::filesystem::remove(path, error);
|
||||
if (error) {
|
||||
if (path.has_filename())
|
||||
showError("Unable to update Geode: Unable to remove " + path.filename().string() + " - " + error.message());
|
||||
|
@ -71,13 +71,13 @@ void removePath(ghc::filesystem::path const& path) {
|
|||
}
|
||||
|
||||
void removeDirectory(std::string const& name) {
|
||||
auto error = std::error_code();
|
||||
if (!ghc::filesystem::exists(workingDir / name, error) || error)
|
||||
std::error_code error;
|
||||
if (!std::filesystem::exists(workingDir / name, error) || error)
|
||||
return;
|
||||
if (!waitForFile(workingDir / name))
|
||||
return;
|
||||
|
||||
ghc::filesystem::remove(workingDir / name, error);
|
||||
std::filesystem::remove(workingDir / name, error);
|
||||
if (error) {
|
||||
showError("Unable to update Geode: Unable to remove " + name + " - " + error.message());
|
||||
return;
|
||||
|
@ -85,17 +85,17 @@ void removeDirectory(std::string const& name) {
|
|||
}
|
||||
|
||||
void updateResources() {
|
||||
auto error = std::error_code();
|
||||
if (!ghc::filesystem::exists(updatesDir / "resources", error) || error)
|
||||
std::error_code error;
|
||||
if (!std::filesystem::exists(updatesDir / "resources", error) || error)
|
||||
return;
|
||||
|
||||
ghc::filesystem::remove_all(resourcesDir / "geode.loader", error);
|
||||
std::filesystem::remove_all(resourcesDir / "geode.loader", error);
|
||||
if (error) {
|
||||
showError("Unable to update Geode resources:" + error.message());
|
||||
return;
|
||||
}
|
||||
|
||||
ghc::filesystem::rename(updatesDir / "resources", resourcesDir / "geode.loader", error);
|
||||
std::filesystem::rename(updatesDir / "resources", resourcesDir / "geode.loader", error);
|
||||
if (error) {
|
||||
showError("Unable to update Geode resources: " + error.message());
|
||||
return;
|
||||
|
@ -103,12 +103,12 @@ void updateResources() {
|
|||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
workingDir = ghc::filesystem::current_path();
|
||||
workingDir = std::filesystem::current_path();
|
||||
geodeDir = workingDir / "geode";
|
||||
updatesDir = geodeDir / "update";
|
||||
resourcesDir = geodeDir / "resources";
|
||||
|
||||
if (ghc::filesystem::exists(geodeDir) && ghc::filesystem::exists(updatesDir)) {
|
||||
if (std::filesystem::exists(geodeDir) && std::filesystem::exists(updatesDir)) {
|
||||
removePath(workingDir / "GeodeBootstrapper.dll");
|
||||
updateFile("XInput9_1_0.dll");
|
||||
updateFile("Geode.dll");
|
||||
|
@ -116,11 +116,10 @@ int main(int argc, char* argv[]) {
|
|||
removePath(updatesDir);
|
||||
}
|
||||
|
||||
if(argc < 2)
|
||||
if (argc < 2)
|
||||
return 0;
|
||||
|
||||
// restart gd using the provided path
|
||||
ShellExecute(NULL, "open", (workingDir / argv[1]).string().c_str(), "", workingDir.string().c_str(), TRUE);
|
||||
|
||||
ShellExecuteA(NULL, "open", (workingDir / argv[1]).string().c_str(), "", workingDir.string().c_str(), TRUE);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue