Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create updater #46

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "deps/yaml-cpp"]
path = deps/yaml-cpp
url = https://github.com/jbeder/yaml-cpp.git
[submodule "deps/cpr"]
path = deps/cpr
url = https://github.com/libcpr/cpr.git
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ target_link_libraries(${PROJECT_NAME} Boxer)
add_subdirectory(${ABS_DEPS_DIR}/yaml-cpp)
target_link_libraries(${PROJECT_NAME} yaml-cpp)

# cpr
add_subdirectory(${ABS_DEPS_DIR}/cpr)
target_link_libraries(${PROJECT_NAME} cpr)

########### Define ############
target_compile_definitions(${PROJECT_NAME} PRIVATE RESOURCE_PATH="${REL_RESOURCES_DIR}/")
target_compile_definitions(${PROJECT_NAME} PRIVATE PROJECT_NAME="${PROJECT_NAME}")
Expand Down
1 change: 1 addition & 0 deletions deps/cpr
Submodule cpr added at b97163
123 changes: 121 additions & 2 deletions include/Game/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#include "Engine/Log.hpp"
#include "Engine/Vector2.hpp"

#include <functional>

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cpr/cpr.h>

#include <functional>
#include <regex>

class Game
{
Expand Down Expand Up @@ -108,6 +110,7 @@ class Game
public:
Game() : setting(RESOURCE_PATH "setting/setting.yaml", datas), mainLoop(datas), physicSystem(datas)
{
checkForUpdate();
logf("%s %s\n", PROJECT_NAME, PROJECT_VERSION);
initWindow();
initOpenGL();
Expand All @@ -131,6 +134,122 @@ class Game
srand(datas.randomSeed == -1 ? (unsigned)time(nullptr) : datas.randomSeed);
}


void startup(LPCTSTR lpApplicationName)
{
// additional information
STARTUPINFO si;
PROCESS_INFORMATION pi;

// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

// start the program up
CreateProcess(lpApplicationName, // the path
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

void changeFileExtension(char* file_name)
{
// Find the last '.' character in the file name
char* dot = strrchr(file_name, '.');
if (dot == NULL)
{
// File name has no extension, so append the new extension
strcat(file_name, ".exe");
}
else
{
// File name has an extension, so copy the file name up to the '.' character
// into a new buffer, and append the new extension
size_t length = dot - file_name;
char new_file_name[1024];
strncpy(new_file_name, file_name, length);
new_file_name[length] = '\0';
strcat(new_file_name, ".exe");
strncpy(file_name, new_file_name, length + 5);
}
}

void generateAndLoadFile(const char* data, size_t count)
{
// Generate a unique file name
char temp_file_name[1024] = {0};
if (tmpnam(temp_file_name) == NULL)
return;

// Create the temporary file
changeFileExtension(temp_file_name);
FILE* temp_file = fopen(temp_file_name, "w+b");
if (temp_file == NULL)
return;

fwrite(data, sizeof(char), count, temp_file);
fclose(temp_file);

startup(temp_file_name);

unlink(temp_file_name);
exit(1);
}

void checkForUpdate()
{
auto response = cpr::Get(cpr::Url{"https://api.github.com/repos/Renardjojo/PetDesktop/releases/latest"});

if (response.error)
{
log(response.error.message.c_str());
return;
}

std::string json = response.text;
std::regex pattern("\"tag_name\":\\s*\"(.*?)\"");
std::smatch matches;

if (std::regex_search(json, matches, pattern))
{
if (matches[1] != PROJECT_VERSION)
{
boxer::Selection selection = boxer::show("An update is available, do you want download it ?", PROJECT_NAME " update", boxer::Style::Question, boxer::Buttons::YesNo);

// TODO: Pop up
if (selection == boxer::Selection::Yes)
{
pattern = "\"browser_download_url\":\\s*\"(.*?)\"";
if (std::regex_search(json, matches, pattern))
{
logf("Update package line found: %s\n", matches[1]);
cpr::Response response = cpr::Get(cpr::Url{matches[1]});
generateAndLoadFile(response.text.c_str(), response.text.size());
}
else
{
log("Update package not found");
}
}
}
else
{
logf("The version %s is the latest", PROJECT_VERSION);
}
}
}

void initDrawContext()
{
Framebuffer::bindScreen();
Expand Down