Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
add error system + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
int-72h committed Jun 14, 2024
1 parent 0adfafb commit 17f2f5f
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 28 deletions.
11 changes: 11 additions & 0 deletions Code/binding/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void binding::_bind_methods() {
ADD_SIGNAL(
MethodInfo("progress_update", PropertyInfo(Variant::FLOAT, "progress"), PropertyInfo(Variant::STRING, "game")));
ADD_SIGNAL(MethodInfo("palace_started"));
ADD_SIGNAL(MethodInfo("error", PropertyInfo(Variant::STRING, "error_details")));
ClassDB::bind_method(D_METHOD("desktop_notification"), &binding::desktop_notification);
ClassDB::bind_method(D_METHOD("init_palace"), &binding::init_palace);
ClassDB::bind_method(D_METHOD("sanity_checks"), &binding::sanity_checks);
Expand Down Expand Up @@ -63,11 +64,19 @@ void binding::init_palace() { // yucky hack but we can manually start palace fr
}

void binding::_init_palace() {
A_init_error_system();
A_error_system->RegisterListener(EventType::kOnError, [this](Event& ev) {
emit_signal("error", String(static_cast<ErrorMessage&>(ev).GetMessage().c_str()));
});
UtilityFunctions::print("[binding] Firing up palace!");
p = new palace;
emit_signal("palace_started");
}

void binding::_raise_error(std::string error_str, uint err_level) {
emit_signal("error");
}

// WRAPPERS!
int binding::update_game(godot::String gameName) {
std::thread thread_obj(&binding::_update_game, this, gameName);
Expand Down Expand Up @@ -153,4 +162,6 @@ int binding::desktop_notification(String title, String desc) {
#endif
}



binding::~binding() { UtilityFunctions::print("bye bye"); }
1 change: 1 addition & 0 deletions Code/binding/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class binding : public Node {
godot::String get_latest_version(godot::String gameName);
godot::String is_installed(godot::String gameName);
int desktop_notification(String title, String desc);
void _raise_error(std::string error_str, uint err_level);

private:
void _verify_game(String gameName);
Expand Down
27 changes: 15 additions & 12 deletions Code/emley/kachemak/kachemak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,21 @@ int Kachemak::Install() {
int downloadStatus = torrent::LibTorrentDownload(downloadUri, m_szTempPath.string(),&m_eventSystem);
//std::filesystem::path path = net::download_to_temp(downloadUri, latestVersion.value().szFileName, true,&m_eventSystem);
if (downloadStatus != 0) {
A_printf("[Kachemak/Install] Download failed - ret val %d \n",downloadStatus);
A_error("[Kachemak/Install] Download failed - ret val %d \n",downloadStatus);
return downloadStatus;
}
//A_printf("[Kachemak/Install] Download complete: extracting... \n");
A_printf("[Kachemak/Install] Download complete: extracting... \n");
std::filesystem::create_directory(m_szSourcemodPath.string() / m_szFolderName);
Extract(latestVersion.value().szFileName, m_szSourcemodPath.string(), latestVersion.value().lExtractSize);
int err_c = Extract(latestVersion.value().szFileName, m_szSourcemodPath.string() / m_szFolderName, latestVersion.value().lExtractSize);
//Extract( path.string() , (m_szSourcemodPath/ m_szFolderName).string() , latestVersion.value().lExtractSize);
A_printf("[Kachemak/Install] Extraction done.... \n");
DoSymlink();
m_szInstalledVersion = GetLatestVersion();
WriteVersion();
return 0;
if(err_c == 0) {
A_printf("[Kachemak/Install] Extraction done! \n");
DoSymlink();
m_szInstalledVersion = GetLatestVersion();
WriteVersion();
return 0;
}
else{return 1;}
}

/*
Expand All @@ -230,11 +233,11 @@ int Kachemak::Extract(const std::string& szInputFile, const std::string& szOutpu
A_printf("[Kachemak/Extract] Not enough space. Exiting. \n");
return 1;
}
std::FILE* tmpf = std::tmpfile();
std::string tmpf_loc = std::to_string(fileno(tmpf));
int ret = sys::ExtractZip(szInputFile, szOutputDirectory);
A_printf((m_szTempPath / szInputFile).c_str());
int ret = sys::ExtractZip((m_szTempPath / szInputFile).string(), szOutputDirectory);
if (ret != 0) {
A_printf("[Kachemak/Extract] Extraction Failed - %s\n",zip_strerror(ret));
A_error("[Kachemak/Extract] Extraction Failed - %s\n",zip_strerror(ret));
return -1;
}
m_szInstalledVersion = GetLatestVersion();
WriteVersion();
Expand Down
4 changes: 4 additions & 0 deletions Code/headless/headless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
int main() {
printf(WELCOME_TEXT);
auto p = new palace; // does sanity checks
A_init_error_system();
A_error_system->RegisterListener(EventType::kOnError, [](Event& ev) {
printf(static_cast<ErrorMessage&>(ev).GetMessage().c_str());
});
p->find_sourcemod_path();
if (p->sourcemodsPath.empty()) {
printf("We couldn't find your sourcemod folder. Please input an alternate sourcemods path.\n");
Expand Down
1 change: 1 addition & 0 deletions Code/palace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download
FetchContent_MakeAvailable(json)
include(FetchContent)
IF (GODOT)
add_compile_definitions(-DGODOT)
target_include_directories(palace PUBLIC godot::cpp)
target_link_libraries(palace PUBLIC godot::cpp)
ENDIF ()
Expand Down
2 changes: 2 additions & 0 deletions Code/shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ add_library(shared sha256.cpp adastral_defs.cpp events/error.cpp events/event.cp
target_include_directories(shared PUBLIC ./)
target_include_directories(shared PUBLIC ./events)
IF (GODOT)
target_include_directories(shared PUBLIC ../binding)
add_compile_definitions(-DGODOT)
target_include_directories(shared PUBLIC godot::cpp)
target_link_libraries(shared PUBLIC godot::cpp)
ENDIF ()
8 changes: 5 additions & 3 deletions Code/shared/adastral_defs.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "adastral_defs.h"


void A_init_error_system() {
A_error_system = new EventSystem;
}
void A_printf(const char *const format ,...){
#ifndef GODOT
//#pragma warning ("GODOT DISABLED.")
Expand All @@ -9,7 +11,7 @@ void A_printf(const char *const format ,...){
vfprintf(stdout, format, argptr);
va_end(argptr);
#else
//#pragma warning ("GODOT ENABLED.")
//#pragma warning ("GODOT ENABLED.")
va_list argptr;
va_start(argptr, format);
char* str = (char*)malloc(512);
Expand All @@ -36,4 +38,4 @@ std::string A_SHA256(std::string filename ) {
delete[] buffer;
return digestSHA256.getHash();

}
}
46 changes: 42 additions & 4 deletions Code/shared/adastral_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,55 @@
#include <godot_cpp/godot.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#endif
#include <string>
#include <fstream>

#include <sha256.h>

#include <events/eventsystem.hpp>
#include <events/error.hpp>
#include <fstream>
#include <map>
#include <memory>
#include <string>


inline EventSystem* A_error_system;
enum error_level { INFO, WARNING, SERIOUS, PANIC };
inline std::map<error_level, std::string> error_string_map{
{INFO,"info"},
{WARNING,"warning"},
{SERIOUS,"serious"},
{PANIC,"panic"}
};
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define popen _popen
#define BUTLER "butler.exe"
#define pclose _pclose
#else
#define BUTLER "butler"
#endif

template<typename ... Args>
std::string string_format( const std::string& format, Args ... args );
std::string A_SHA256(std::string filename);
void A_printf(const char *const format ,...);
void A_printf(const char *const format ,...);
template<typename ... Args>
std::string string_format( const std::string& format, Args ... args )
{
int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
auto size = static_cast<size_t>( size_s );
std::unique_ptr<char[]> buf( new char[ size ] );
std::snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
template<typename ... Args>
void A_error(const std::string& format, Args ... args) {
std::string error_detail = string_format(format,args ...);
A_printf("### ERROR: %s\n",error_detail.c_str());
#ifndef GODOT
#else
ErrorMessage e = ErrorMessage(std::move(error_detail));
A_error_system->TriggerEvent(e);
#endif
}
void A_init_error_system();

1 change: 1 addition & 0 deletions Code/sys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FetchContent_MakeAvailable(json)
include(FetchContent)
# Get the name of this folder as the project name
IF (GODOT)
add_compile_definitions(-DGODOT)
target_include_directories(${PROJECT_NAME} PUBLIC godot::cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC godot::cpp)
ENDIF ()
Expand Down
17 changes: 8 additions & 9 deletions Code/torrent/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,31 @@ char const* state(lt::torrent_status::state_t s)
//Adding in extra printf functions to log actions when possible.
int torrent::LibTorrentDownload(const std::string &torrentfileurl, const std::string &path, EventSystem* event) try {
//First, list what are the current parameters.
//A_printf("[torrent] Current torrent file URL: %s", torrentfileurl.c_str());
//A_printf("[torrent] Current path the file is heading to: %s", path.c_str());
A_printf("[torrent] Current torrent file URL: %s", torrentfileurl.c_str());
A_printf("[torrent] Current path the file is heading to: %s", path.c_str());

//Then check the torrent file. Since it is a vector of char, it should bring in
//like weird garbled text to show that there's something being fetched from.
std::vector<char> torrentfile = net().get_bin_data_from_server(torrentfileurl);

//A_printf("[torrent] the torrent file should be loaded.");
A_printf("[torrent] the torrent file should be loaded.");

lt::settings_pack p;
p.set_int(lt::settings_pack::alert_mask, lt::alert_category::status | lt::alert_category::error);
lt::session ses(p);

//A_printf("[torrent] Settings pack should be initialized.");
A_printf("[torrent] Settings pack should be initialized.");

lt::add_torrent_params atp = lt::load_torrent_buffer(torrentfile);
atp.save_path = path; // save in current dir
//A_printf("[torrent] Current atp.save_path: %s", atp.save_path.c_str());

A_printf("[torrent] Current atp.save_path: %s", atp.save_path.c_str());
lt::torrent_handle h = ses.add_torrent(std::move(atp));

for (;;) {
std::vector<lt::alert *> alerts;
ses.pop_alerts(&alerts);

for (lt::alert const *a : alerts) {
//std::cout << a->message() << std::endl;
A_printf(a->message().c_str());
// if we receive the finished alert or an error, we're done
if (lt::alert_cast<lt::torrent_finished_alert>(a)) {
goto done;
Expand Down Expand Up @@ -90,6 +88,7 @@ int torrent::LibTorrentDownload(const std::string &torrentfileurl, const std::st
done:
return 0;
} catch (std::exception &e) {
//A_printf("[torrent] Error: %s\n",e.what());
A_error(e.what());
A_printf("[torrent] Error: %s\n",e.what());
return 1;
}

0 comments on commit 17f2f5f

Please sign in to comment.