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

loadlib for linux #130

Merged
merged 1 commit into from
Dec 28, 2024
Merged
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
86 changes: 67 additions & 19 deletions dll/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,58 @@ unsigned int file_size_(const std::string &full_path)


#ifdef EMU_EXPERIMENTAL_BUILD

static std::vector<void*> loaded_libs{};

static void load_dlls()
{
constexpr static const char LIB_EXTENSION[] =
#ifdef __WINDOWS__
".dll"
#else
".so"
#endif
;

std::string path(Local_Storage::get_game_settings_path() + "load_dlls" + PATH_SEPARATOR);

std::vector<std::string> paths(Local_Storage::get_filenames_path(path));
for (auto & p: paths) {
std::string full_path(path + p);
if (!common_helpers::ends_with_i(full_path, LIB_EXTENSION)) continue;

PRINT_DEBUG("loading '%s'", full_path.c_str());
auto lib_handle =
#ifdef __WINDOWS__
LoadLibraryW(utf8_decode(full_path).c_str());
#else
dlopen(full_path.c_str(), RTLD_NOW | RTLD_LOCAL);
#endif

if (lib_handle != nullptr) {
loaded_libs.push_back(reinterpret_cast<void *>(lib_handle));
PRINT_DEBUG(" LOADED");
} else {
#ifdef __WINDOWS__
PRINT_DEBUG(" FAILED, error code 0x%X", GetLastError());
#else
PRINT_DEBUG(" FAILED, error string '%s'", dlerror());
#endif
}
}
}

static void unload_dlls()
{
for (auto lib_handle : loaded_libs) {
#ifdef __WINDOWS__
FreeLibrary(reinterpret_cast<HMODULE>(lib_handle));
#else
dlclose(lib_handle);
#endif
}
}

#ifdef __WINDOWS__

struct ips_test {
Expand Down Expand Up @@ -463,23 +515,6 @@ static void load_crack_dll()
}

#include "dll/local_storage.h"
static void load_dlls()
{
std::string path(Local_Storage::get_game_settings_path() + "load_dlls" + PATH_SEPARATOR);

std::vector<std::string> paths(Local_Storage::get_filenames_path(path));
for (auto & p: paths) {
std::string full_path(path + p);
if (!common_helpers::ends_with_i(full_path, ".dll")) continue;

PRINT_DEBUG("loading '%s'", full_path.c_str());
if (LoadLibraryW(utf8_decode(full_path).c_str())) {
PRINT_DEBUG(" LOADED");
} else {
PRINT_DEBUG(" FAILED, error 0x%X", GetLastError());
}
}
}

//For some reason when this function is optimized it breaks the shogun 2 prophet (reloaded) crack.
#pragma optimize( "", off )
Expand Down Expand Up @@ -613,6 +648,8 @@ BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID )
}
DetourTransactionCommit();
}

unload_dlls();
break;
}

Expand All @@ -621,18 +658,29 @@ BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID )

#else

__attribute__((__constructor__)) static void lib_base_entry()
{
load_dlls();
}

__attribute__((__destructor__)) static void lib_base_exit()
{
unload_dlls();
}

void set_whitelist_ips(uint32_t *from, uint32_t *to, unsigned num_ips)
{

}

#endif
#endif // __WINDOWS__

#else

void set_whitelist_ips(uint32_t *from, uint32_t *to, unsigned num_ips)
{

}

#endif
#endif // EMU_EXPERIMENTAL_BUILD