Skip to content

Commit

Permalink
Fix for not finding BP mods and lua mods if there are unicode charact…
Browse files Browse the repository at this point in the history
…er in game path
  • Loading branch information
DeadMor0z committed Sep 18, 2024
1 parent d1a5478 commit 764504d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
22 changes: 11 additions & 11 deletions UE4SS/src/Mod/LuaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,20 +816,20 @@ namespace RC
lua_getglobal(lua_state, "package");

lua_getfield(lua_state, -1, "path");
std::string current_paths = lua_tostring(lua_state, -1);
current_paths.append(fmt::format(";{}\\{}\\Scripts\\?.lua", to_string(m_program.get_mods_directory()).c_str(), to_string(get_name())));
current_paths.append(fmt::format(";{}\\shared\\?.lua", to_string(m_program.get_mods_directory()).c_str()));
current_paths.append(fmt::format(";{}\\shared\\?\\?.lua", to_string(m_program.get_mods_directory()).c_str()));
std::wstring current_paths = to_wstring(lua_tostring(lua_state, -1));
current_paths.append(fmt::format(STR(";{}\\{}\\Scripts\\?.lua"), m_program.get_mods_directory(), get_name()));
current_paths.append(fmt::format(STR(";{}\\shared\\?.lua"), m_program.get_mods_directory()));
current_paths.append(fmt::format(STR(";{}\\shared\\?\\?.lua"), m_program.get_mods_directory()));
lua_pop(lua_state, 1);
lua_pushstring(lua_state, current_paths.c_str());
lua_pushstring(lua_state, to_string(current_paths).c_str());
lua_setfield(lua_state, -2, "path");

lua_getfield(lua_state, -1, "cpath");
std::string current_cpaths = lua_tostring(lua_state, -1);
current_cpaths.append(fmt::format(";{}\\{}\\Scripts\\?.dll", to_string(m_program.get_mods_directory()).c_str(), to_string(get_name())));
current_cpaths.append(fmt::format(";{}\\{}\\?.dll", to_string(m_program.get_mods_directory()).c_str(), to_string(get_name())));
std::wstring current_cpaths = to_wstring(lua_tostring(lua_state, -1));
current_cpaths.append(fmt::format(STR(";{}\\{}\\Scripts\\?.dll"), m_program.get_mods_directory(), get_name()));
current_cpaths.append(fmt::format(STR(";{}\\{}\\?.dll"), m_program.get_mods_directory(), get_name()));
lua_pop(lua_state, 1);
lua_pushstring(lua_state, current_cpaths.c_str());
lua_pushstring(lua_state, to_string(current_cpaths).c_str());
lua_setfield(lua_state, -2, "cpath");

lua_pop(lua_state, 1);
Expand Down Expand Up @@ -2198,7 +2198,7 @@ No overload found for function 'IterateGameDirectories'.
{
throw std::runtime_error{"Couldn't find '__absolute_path' for directory entry."};
}
const auto current_path = std::string{lua.get_string()};
const auto current_path = to_wstring(lua.get_string());
auto files_table = lua.prepare_new_table();
auto index = 1;
for (const auto& item : std::filesystem::directory_iterator(current_path))
Expand Down Expand Up @@ -3364,7 +3364,7 @@ No overload found for function 'FPackageName:IsValidLongPackageName'.
// Don't crash on syntax errors.
try
{
main_lua()->execute_file((m_scripts_path / STR("main.lua")).string());
main_lua()->execute_file((m_scripts_path / STR("main.lua")).wstring());
}
catch (std::runtime_error& e)
{
Expand Down
10 changes: 5 additions & 5 deletions UE4SS/src/UE4SSProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,27 +1359,27 @@ namespace RC

auto UE4SSProgram::get_module_directory() -> File::StringType
{
return ensure_str(m_module_file_path);
return ensure_str(m_module_file_path.wstring());
}

auto UE4SSProgram::get_game_executable_directory() -> File::StringType
{
return ensure_str(m_game_executable_directory);
return ensure_str(m_game_executable_directory.wstring());
}

auto UE4SSProgram::get_working_directory() -> File::StringType
{
return ensure_str(m_working_directory);
return ensure_str(m_working_directory.wstring());
}

auto UE4SSProgram::get_mods_directory() -> File::StringType
{
return ensure_str(m_mods_directory);
return ensure_str(m_mods_directory.wstring());
}

auto UE4SSProgram::get_legacy_root_directory() -> File::StringType
{
return ensure_str(m_legacy_root_directory);
return ensure_str(m_legacy_root_directory.wstring());
}

auto UE4SSProgram::generate_uht_compatible_headers() -> void
Expand Down
1 change: 1 addition & 0 deletions deps/first/LuaRaw/include/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ typedef struct luaL_Stream {



FILE* wfopen(const char *filename, const char *mode);
#endif


13 changes: 12 additions & 1 deletion deps/first/LuaRaw/src/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "lprefix.h"

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>

#include <errno.h>
#include <stdarg.h>
Expand Down Expand Up @@ -785,7 +787,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
}
else {
lua_pushfstring(L, "@%s", filename);
lf.f = fopen(filename, "r");
lf.f = wfopen(filename, "r");
if (lf.f == NULL) return errfile(L, "open", fnameindex);
}
if (skipcomment(&lf, &c)) /* read initial portion */
Expand Down Expand Up @@ -1104,3 +1106,12 @@ LUALIB_API void luaL_checkversion_ (lua_State *L, lua_Number ver, size_t sz) {
(LUAI_UACNUMBER)ver, (LUAI_UACNUMBER)v);
}

FILE* wfopen(const char *filename, const char *mode)
{
wchar_t buff[MAX_PATH+1];
wchar_t modeBuff[MAX_PATH+1];
if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, buff, MAX_PATH) == 0 || MultiByteToWideChar(CP_UTF8, 0, mode, -1, modeBuff, MAX_PATH) == 0)
return fopen(filename, mode);

return _wfopen(buff, modeBuff);
}
4 changes: 2 additions & 2 deletions deps/first/LuaRaw/src/liolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ static LStream *newfile (lua_State *L) {

static void opencheck (lua_State *L, const char *fname, const char *mode) {
LStream *p = newfile(L);
p->f = fopen(fname, mode);
p->f = wfopen(fname, mode);
if (l_unlikely(p->f == NULL))
luaL_error(L, "cannot open file '%s' (%s)", fname, strerror(errno));
}
Expand All @@ -272,7 +272,7 @@ static int io_open (lua_State *L) {
LStream *p = newfile(L);
const char *md = mode; /* to traverse/check mode */
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
p->f = fopen(filename, mode);
p->f = wfopen(filename, mode);
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
}

Expand Down
23 changes: 14 additions & 9 deletions deps/first/LuaRaw/src/loadlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,21 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
** of LUA_EXEC_DIR with the executable's path.
*/
static void setprogdir (lua_State *L) {
wchar_t wbuff[MAX_PATH + 1];
char buff[MAX_PATH + 1];
char *lb;
DWORD nsize = sizeof(buff)/sizeof(char);
DWORD n = GetModuleFileNameA(NULL, buff, nsize); /* get exec. name */
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL)
luaL_error(L, "unable to get ModuleFileName");
wchar_t *lb;
DWORD nsize = sizeof(wbuff)/sizeof(wchar_t);
DWORD n = GetModuleFileNameW(NULL, wbuff, nsize); /* get exec. name */
if (n == 0 || n == nsize || (lb = wcsrchr(wbuff, L'\\')) == NULL)
luaL_error(L, "unable to get ModuleFileName");
else {
*lb = '\0'; /* cut name on the last '\\' to get the path */
luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
lua_remove(L, -2); /* remove original string */
*lb = L'\0'; /* cut name on the last '\\' to get the path */
if (WideCharToMultiByte(CP_UTF8,0,wbuff,-1,buff,MAX_PATH + 1,NULL,NULL) == 0)
luaL_error(L, "unable to get ModuleFileName");
else {
luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff);
lua_remove(L, -2); /* remove original string */
}
}
}

Expand Down Expand Up @@ -432,7 +437,7 @@ static int ll_loadlib (lua_State *L) {


static int readable (const char *filename) {
FILE *f = fopen(filename, "r"); /* try to open file */
FILE *f = wfopen(filename, "r"); /* try to open file */
if (f == NULL) return 0; /* open failed */
fclose(f);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion deps/first/LuaRaw/src/luac.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static int pmain(lua_State* L)
if (listing) luaU_print(f,listing>1);
if (dumping)
{
FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
FILE* D= (output==NULL) ? stdout : wfopen(output,"wb");
if (D==NULL) cannot("open");
lua_lock(L);
luaU_dump(L,f,writer,D,stripping);
Expand Down

0 comments on commit 764504d

Please sign in to comment.