Skip to content

Commit

Permalink
RMG-Core: move osal functions to File.cpp and Library.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Feb 1, 2025
1 parent f5b0452 commit a396b37
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 216 deletions.
13 changes: 1 addition & 12 deletions Source/RMG-Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ set(RMG_CORE_SOURCES
Callback.cpp
Settings.cpp
Archive.cpp
Library.cpp
Netplay.cpp
Plugins.cpp
Version.cpp
Expand All @@ -58,18 +59,6 @@ set(RMG_CORE_SOURCES
Rom.cpp
)

if (WIN32)
list(APPEND RMG_CORE_SOURCES
osal/osal_dynlib_win32.cpp
osal/osal_files_win32.cpp
)
else(WIN32)
list(APPEND RMG_CORE_SOURCES
osal/osal_dynlib_unix.cpp
osal/osal_files_unix.cpp
)
endif(WIN32)

if (DISCORD_RPC)
list(APPEND RMG_CORE_SOURCES
DiscordRpc.cpp
Expand Down
11 changes: 5 additions & 6 deletions Source/RMG-Core/CachedRomHeaderAndSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#include "Directories.hpp"
#include "RomSettings.hpp"
#include "RomHeader.hpp"

#include "osal/osal_files.hpp"
#include "File.hpp"

#include <algorithm>
#include <cstring>
Expand Down Expand Up @@ -54,7 +53,7 @@
struct l_CacheEntry
{
std::filesystem::path fileName;
osal_files_file_time fileTime;
uint64_t fileTime;

CoreRomType type;
CoreRomHeader header;
Expand All @@ -77,15 +76,15 @@ static std::filesystem::path get_cache_file_name()
std::filesystem::path file;

file = CoreGetUserCacheDirectory();
file += OSAL_FILES_DIR_SEPERATOR_STR;
file += CORE_DIR_SEPERATOR_STR;
file += "RomHeaderAndSettingsCache.cache";

return file;
}

static std::vector<l_CacheEntry>::iterator get_cache_entry_iter(std::filesystem::path file, bool checkFileTime = true)
{
osal_files_file_time fileTime = osal_files_get_file_time(file);
uint64_t fileTime = CoreGetFileTime(file);

auto predicate = [file, fileTime, checkFileTime](const auto& entry)
{
Expand Down Expand Up @@ -298,7 +297,7 @@ bool CoreAddCachedRomHeaderAndSettings(std::filesystem::path file, CoreRomType t
}

cacheEntry.fileName = file;
cacheEntry.fileTime = osal_files_get_file_time(file);
cacheEntry.fileTime = CoreGetFileTime(file);
cacheEntry.type = type;
cacheEntry.header = header;
cacheEntry.settings = settings;
Expand Down
13 changes: 6 additions & 7 deletions Source/RMG-Core/Cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "Cheats.hpp"
#include "Error.hpp"

#include "osal/osal_files.hpp"
#include "m64p/Api.hpp"

#ifdef USE_LIBFMT
Expand Down Expand Up @@ -118,9 +117,9 @@ static std::filesystem::path get_shared_cheat_file_path(CoreRomHeader romHeader,
std::filesystem::path cheatFilePath;

cheatFilePath = CoreGetSharedDataDirectory();
cheatFilePath += OSAL_FILES_DIR_SEPERATOR_STR;
cheatFilePath += CORE_DIR_SEPERATOR_STR;
cheatFilePath += "Cheats";
cheatFilePath += OSAL_FILES_DIR_SEPERATOR_STR;
cheatFilePath += CORE_DIR_SEPERATOR_STR;
cheatFilePath += get_cheat_file_name(romHeader, romSettings);

return cheatFilePath;
Expand All @@ -132,15 +131,15 @@ static std::filesystem::path get_user_cheat_file_path(CoreRomHeader romHeader, C
std::filesystem::path cheatFilePath;

oldCheatFilePath = CoreGetUserDataDirectory();
oldCheatFilePath += OSAL_FILES_DIR_SEPERATOR_STR;
oldCheatFilePath += CORE_DIR_SEPERATOR_STR;
oldCheatFilePath += "Cheats-User";
oldCheatFilePath += OSAL_FILES_DIR_SEPERATOR_STR;
oldCheatFilePath += CORE_DIR_SEPERATOR_STR;
oldCheatFilePath += get_cheat_file_name(romHeader, romSettings);

cheatFilePath = CoreGetUserConfigDirectory();
cheatFilePath += OSAL_FILES_DIR_SEPERATOR_STR;
cheatFilePath += CORE_DIR_SEPERATOR_STR;
cheatFilePath += "Cheats-User";
cheatFilePath += OSAL_FILES_DIR_SEPERATOR_STR;
cheatFilePath += CORE_DIR_SEPERATOR_STR;
cheatFilePath += get_cheat_file_name(romHeader, romSettings);

// try to make the user cheats directory
Expand Down
14 changes: 7 additions & 7 deletions Source/RMG-Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
#endif // DISCORD_RPC
#include "Callback.hpp"
#include "Settings.hpp"
#include "Library.hpp"
#include "Plugins.hpp"
#include "Error.hpp"
#include "Core.hpp"

#include "osal/osal_dynlib.hpp"
#include "m64p/Api.hpp"
#include "m64p/api/version.h"

Expand All @@ -32,7 +32,7 @@
// Local Variables
//

static osal_dynlib_lib_handle l_CoreLibHandle;
static CoreLibraryHandle l_CoreLibHandle;
static char l_CoreContextString[20];

//
Expand All @@ -45,7 +45,7 @@ static std::filesystem::path find_core_lib(void)
{
std::filesystem::path path = entry.path();
if (path.has_extension() &&
path.extension() == OSAL_DYNLIB_LIB_EXT_STR)
path.extension() == CORE_LIBRARY_EXT_STR)
{
return path;
}
Expand Down Expand Up @@ -104,11 +104,11 @@ bool CoreInit(void)
return false;
}

l_CoreLibHandle = osal_dynlib_open(core_file);
l_CoreLibHandle = CoreOpenLibrary(core_file);
if (l_CoreLibHandle == nullptr)
{
error = "osal_dynlib_open Failed: ";
error += osal_dynlib_strerror();
error = "CoreOpenLibrary Failed: ";
error += CoreGetLibraryError();
CoreSetError(error);
return false;
}
Expand Down Expand Up @@ -193,5 +193,5 @@ void CoreShutdown(void)
m64p::Core.Unhook();
m64p::Config.Unhook();

osal_dynlib_close(l_CoreLibHandle);
CoreCloseLibrary(l_CoreLibHandle);
}
6 changes: 6 additions & 0 deletions Source/RMG-Core/Directories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

#include <filesystem>

#ifdef _WIN32
#define CORE_DIR_SEPERATOR_STR "\\"
#else // Unix
#define CORE_DIR_SEPERATOR_STR "/"
#endif // _WIN32

// tries to create the needed directories,
// returns false when failed
bool CoreCreateDirectories(void);
Expand Down
51 changes: 51 additions & 0 deletions Source/RMG-Core/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
#include <cstring>
#include <fstream>

#ifdef _WIN32
#include <windows.h>
#include <fileapi.h>
#else
#include <sys/stat.h>
#endif

//
// Exported Functions
//
Expand Down Expand Up @@ -77,3 +84,47 @@ bool CoreWriteFile(std::filesystem::path file, std::vector<char>& buffer)
fileStream.close();
return true;
}

uint64_t CoreGetFileTime(std::filesystem::path file)
{
#ifdef _WIN32
BOOL ret;
HANDLE file_handle;
FILETIME file_time;
ULARGE_INTEGER ularge_int;

file_handle = CreateFileW(file.wstring().c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file_handle == INVALID_HANDLE_VALUE)
{
return -1;
}

ret = GetFileTime(file_handle, nullptr, nullptr, &file_time);
if (ret != TRUE)
{
return -1;
}

ret = CloseHandle(file_handle);
if (ret != TRUE)
{
return -1;
}

ularge_int.LowPart = file_time.dwLowDateTime;
ularge_int.HighPart = file_time.dwHighDateTime;

return ularge_int.QuadPart;
#else // Linux
int ret;
struct stat file_stat;

ret = stat(file.string().c_str(), &file_stat);
if (ret != 0)
{
return -1;
}

return file_stat.st_mtime;
#endif
}
3 changes: 3 additions & 0 deletions Source/RMG-Core/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ bool CoreReadFile(std::filesystem::path file, std::vector<char>& outBuffer);
// attempts to write the buffer to file
bool CoreWriteFile(std::filesystem::path file, std::vector<char>& buffer);

// attempts to retrieve the file time
uint64_t CoreGetFileTime(std::filesystem::path file);

#endif // CORE_FILE_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,46 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "osal_dynlib.hpp"
#include "Library.hpp"

osal_dynlib_lib_handle osal_dynlib_open(std::filesystem::path path)
#ifndef _WIN32
#include <dlfcn.h>
#endif

//
// Exported Functions
//

CoreLibraryHandle CoreOpenLibrary(std::filesystem::path path)
{
#ifdef _WIN32
return LoadLibraryW(path.wstring().c_str());
#else // Linux
return dlopen(path.string().c_str(), RTLD_LAZY);
#endif
}

osal_dynlib_lib_sym osal_dynlib_sym(osal_dynlib_lib_handle handle, const char* symbol)
CoreLibrarySymbol CoreGetLibrarySymbol(CoreLibraryHandle handle, const char* symbol)
{
#ifdef _WIN32
return GetProcAddress(handle, symbol);
#else
return dlsym(handle, symbol);
#endif // Linux
}

void osal_dynlib_close(osal_dynlib_lib_handle handle)
void CoreCloseLibrary(CoreLibraryHandle handle)
{
#ifdef _WIN32
FreeLibrary(handle);
#else
dlclose(handle);
#endif // Linux
}

std::string osal_dynlib_strerror(void)
std::string CoreGetLibraryError(void)
{
#ifdef _WIN32
DWORD err = GetLastError();
LPSTR buf = nullptr;

Expand All @@ -38,4 +59,7 @@ std::string osal_dynlib_strerror(void)
LocalFree(buf);

return msg;
#else // Linux
return std::string(dlerror());
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef OSAL_DYNLIB_HPP
#define OSAL_DYNLIB_HPP
#ifndef CORE_LIBRARY_HPP
#define CORE_LIBRARY_HPP

#include <string>
#include <filesystem>
#include <string>

#ifdef _WIN32
#include <windows.h>
typedef HMODULE osal_dynlib_lib_handle;
typedef FARPROC osal_dynlib_lib_sym;
#define OSAL_DYNLIB_LIB_EXT_STR ".dll"
typedef HMODULE CoreLibraryHandle;
typedef FARPROC CoreLibrarySymbol;
#define CORE_LIBRARY_EXT_STR ".dll"
#else // Unix
typedef void* osal_dynlib_lib_handle;
typedef void* osal_dynlib_lib_sym;
#define OSAL_DYNLIB_LIB_EXT_STR ".so"
typedef void* CoreLibraryHandle;
typedef void* CoreLibrarySymbol;
#define CORE_LIBRARY_EXT_STR ".so"
#endif // _WIN32

// returns library handle for given filename,
// return nullptr when invalid library
osal_dynlib_lib_handle osal_dynlib_open(std::filesystem::path path);
CoreLibraryHandle CoreOpenLibrary(std::filesystem::path path);

// retrieves symbol handle for given library
// handle, returns nullptr when not found
osal_dynlib_lib_sym osal_dynlib_sym(osal_dynlib_lib_handle, const char* symbol);
CoreLibrarySymbol CoreGetLibrarySymbol(CoreLibraryHandle handle, const char* symbol);

// closes library handle
void osal_dynlib_close(osal_dynlib_lib_handle);
void CoreCloseLibrary(CoreLibraryHandle);

// returns error message
std::string osal_dynlib_strerror(void);
std::string CoreGetLibraryError(void);

#endif // OSAL_DYNLIB_HPP
#endif // CORE_LIBRARY_HPP
Loading

0 comments on commit a396b37

Please sign in to comment.