Skip to content

Commit

Permalink
First Release!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapilarny committed Aug 3, 2023
1 parent 72e72aa commit 7c2a86e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
25 changes: 19 additions & 6 deletions src/exports/JojoAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
#include "utils/mem.h"
#include "utils/hooks.h"

const char* GetModGUID(void* retAddr) {
HMODULE hModule = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCSTR)retAddr, &hModule);

// Get the mod info
auto getModInfo = (ModMeta (*)()) GetProcAddress(hModule, "GetModInfo");
if(!getModInfo) {
return "Unknown";
}

return getModInfo().guid;
}

void JAPI_PatchASBRMem(void* address, void* data, size_t size) {
PatchEx((BYTE*)address, (BYTE*)data, size, GetCurrentProcess());
}
Expand All @@ -22,25 +35,25 @@ bool JAPI_UnhookFunction(Hook* hook) {
}

void JAPI_LogFatal(std::string message) {
LOG_FATAL("", message);
LOG_FATAL(GetModGUID(__builtin_extract_return_addr(__builtin_return_address(0))), message);
}

void JAPI_LogError(std::string message) {
LOG_ERROR("", message);
LOG_ERROR(GetModGUID(__builtin_extract_return_addr(__builtin_return_address(0))), message);
}

void JAPI_LogWarn(std::string message) {
LOG_WARN("", message);
LOG_WARN(GetModGUID(__builtin_extract_return_addr(__builtin_return_address(0))), message);
}

void JAPI_LogInfo(std::string message) {
LOG_INFO("", message);
LOG_INFO(GetModGUID(__builtin_extract_return_addr(__builtin_return_address(0))), message);
}

void JAPI_LogDebug(std::string message) {
LOG_DEBUG("", message);
LOG_DEBUG(GetModGUID(__builtin_extract_return_addr(__builtin_return_address(0))), message);
}

void JAPI_LogTrace(std::string message) {
LOG_TRACE("", message);
LOG_TRACE(GetModGUID(__builtin_extract_return_addr(__builtin_return_address(0))), message);
}
8 changes: 4 additions & 4 deletions src/exports/JojoAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <Windows.h>
#include <string>

#define JEXP __declspec(dllexport)
#define JEXP extern "C" __declspec(dllexport)

typedef struct ModMeta {
std::string name;
std::string guid;
std::string version;
const char* name;
const char* guid;
const char* version;
} ModMeta;

typedef struct Hook {
Expand Down
13 changes: 7 additions & 6 deletions src/japi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#include <Windows.h>
#include <filesystem>

#include "utils/logger.h"
#include "exports/JojoAPI.h"

void JAPI::Init(HINSTANCE hinstDLL) {
instance = std::unique_ptr<JAPI>(new JAPI());
instance->hinstDLL = hinstDLL;

instance->asbrModuleBase = (uint64_t) GetModuleHandle(NULL);

printf("[JojoApi]: Initialized!\n");
JINFO("Initialized JojoAPI!");

instance->LoadMods();
}

Expand All @@ -34,21 +35,21 @@ void JAPI::LoadMods() {
// Load the DLL
auto handle = LoadLibrary(p.path().string().c_str());
if(!handle) {
printf("[ModLoader]: Failed to load mod %s\n", p.path().string().c_str());
LOG_ERROR("ModLoader", "Failed to load mod " + p.path().string());
continue;
}

// Get the mod info
auto getModInfo = (ModMeta (*)()) GetProcAddress(handle, "GetModInfo");
if(!getModInfo) {
printf("[ModLoader]: Failed to get mod info for %s\n", p.path().string().c_str());
LOG_ERROR("ModLoader", "Failed to get mod info for " + p.path().string());
continue;
}

ModMeta modInfo = getModInfo();
auto modInit = (void (*)()) GetProcAddress(handle, "ModInit");
if(!modInit) {
printf("[ModLoader]: Failed to get mod init for %s\n", p.path().string().c_str());
LOG_ERROR("ModLoader", "Failed to get mod init for " + p.path().string());
continue;
}

Expand All @@ -58,7 +59,7 @@ void JAPI::LoadMods() {
}
}

printf("[ModLoader]: Loaded %d mods\n", mods.size());
LOG_INFO("ModLoader", "Loaded " + std::to_string(mods.size()) + " mods!");
}

std::string JAPI::GetModGUID(HANDLE modHandle) {
Expand Down
14 changes: 13 additions & 1 deletion src/utils/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

#include <Windows.h>
#include <string>
#include <fstream>

// Log File
static std::ofstream logFile;

void LogOutput(LogLevel level, const std::string& message, const std::string& pluginGUID) {
static const std::string level_strings[6] = { "[FATAL]: ", "[ERROR]: ", "[WARN]: ", "[INFO]: ", "[DEBUG]: ", "[TRACE]: " };
bool isError = level < 2;

// Add the plugin GUID to the message
std::string finalMessage = level_strings[level] + message;
std::string finalMessage = "[" + pluginGUID + "] " + level_strings[level] + message + "\n";

if (!logFile.is_open()) {
logFile.open("JojoAPI.log", std::ios::out | std::ios::trunc);
}

// Write to log file
logFile << finalMessage;
logFile.flush();

// FATAL,ERROR,WARN,INFO,DEBUG,TRACE
static uint8_t levels[6] = { 64, 4, 6, 2, 1, 8 };
Expand Down
7 changes: 7 additions & 0 deletions src/utils/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ typedef enum LogLevel {

void LogOutput(LogLevel level, const std::string& message, const std::string& pluginGUID);

#define JFATAL(message) LogOutput(LOG_LEVEL_FATAL, message, "JojoAPI");
#define JERROR(message) LogOutput(LOG_LEVEL_ERROR, message, "JojoAPI");
#define JWARN(message) LogOutput(LOG_LEVEL_WARN, message, "JojoAPI");
#define JINFO(message) LogOutput(LOG_LEVEL_INFO, message, "JojoAPI");
#define JDEBUG(message) LogOutput(LOG_LEVEL_DEBUG, message, "JojoAPI");
#define JTRACE(message) LogOutput(LOG_LEVEL_TRACE, message, "JojoAPI");

#define LOG_FATAL(pluginGUID, message) LogOutput(LOG_LEVEL_FATAL, message, pluginGUID);

#define LOG_ERROR(pluginGUID, message) LogOutput(LOG_LEVEL_ERROR, message, pluginGUID);
Expand Down
5 changes: 3 additions & 2 deletions src/utils/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ inline void PatchEx(BYTE* dst, BYTE* src, unsigned int size, HANDLE hProcess)
DWORD oldprotect;
VirtualProtectEx(hProcess, dst, size, PAGE_EXECUTE_READWRITE, &oldprotect);
if (!WriteProcessMemory(hProcess, dst, src, size, nullptr)) {
printf("[JojoAPI]: Failed to write to memory!\n");
JERROR("Failed to write to process memory.!");

// Print the error code.
printf("[JojoAPI]: Error code: %d\n", GetLastError());
DWORD errorCode = GetLastError();
JERROR("Error code: " + std::to_string(errorCode));

return;
}
Expand Down

0 comments on commit 7c2a86e

Please sign in to comment.