From 63d47bb32ee08e4e75176bedea8067caf4035c65 Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 01:29:15 +0930 Subject: [PATCH 1/8] Add file redirect logic --- src/filesystem_find.cpp | 2 +- src/filesystem_open.cpp | 12 ++++++++++-- src/openresult.h | 27 +++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/filesystem_find.cpp b/src/filesystem_find.cpp index 10d7289..c831e24 100644 --- a/src/filesystem_find.cpp +++ b/src/filesystem_find.cpp @@ -6,7 +6,7 @@ static bool IsOK(const char *name) { OpenResult opener(name); - return opener.GetResult(); + return opener.GetResult().shouldOpen; } static const char *NextFile(FileFindHandle_t pHandle) { diff --git a/src/filesystem_open.cpp b/src/filesystem_open.cpp index ce08662..5e00151 100644 --- a/src/filesystem_open.cpp +++ b/src/filesystem_open.cpp @@ -14,14 +14,22 @@ FileHandle_t VirtualFunctionHooks::IBaseFileSystem__Open(const char *pFileName, g_pFullFileSystem->RelativePathToFullPath(pFileName, pathID, temp, sizeof(temp) - 1); char relative_path[4096]; relative_path[4095] = 0; + IOpenResult res; + if (g_pFullFileSystem->FullPathToRelativePathEx(temp, "BASE_PATH", relative_path, sizeof relative_path - 1)) { OpenResult opener(relative_path); - if (!opener.GetResult()) { + res = opener.GetResult(); + if (!res.shouldOpen) { return 0; } } - return FunctionHooks->BaseFileSystemReplacer->Call(FunctionHooks->IBaseFileSystem__Open__index, pFileName, pOptions, pathID); + if (res.shouldRedirect) { + return FunctionHooks->BaseFileSystemReplacer->Call(FunctionHooks->IBaseFileSystem__Open__index, res.redirect.c_str(), pOptions, pathID); + } + else { + return FunctionHooks->BaseFileSystemReplacer->Call(FunctionHooks->IBaseFileSystem__Open__index, pFileName, pOptions, pathID); + } } diff --git a/src/openresult.h b/src/openresult.h index e0286a6..2cd0d66 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -3,17 +3,24 @@ #include #include +#include #include "threadtools.h" #include "GarrysMod/Lua/Interface.h" #include "vfuncs.h" +struct IOpenResult { + bool shouldOpen; + bool shouldRedirect; + std::string redirect; +}; + class OpenResult { public: OpenResult(const char *relativetobase) { file = relativetobase; } - bool GetResult() { + IOpenResult GetResult() { if (ThreadGetCurrentId() == FunctionHooks->mainthread) { return RunLua(); } @@ -28,7 +35,7 @@ class OpenResult { return this->result; } - bool RunLua() { + IOpenResult RunLua() { auto lua = FunctionHooks->lua; lua->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB); lua->GetField(-1, "hook"); @@ -36,8 +43,20 @@ class OpenResult { lua->PushString("ShouldHideFile"); lua->PushString(file); lua->Call(2, 1); - bool ret = !lua->GetBool(-1); + + IOpenResult ret; + + if (lua->IsType(-1,GarrysMod::Lua::Type::BOOL)) { + ret.shouldOpen = !lua->GetBool(-1); + ret.shouldRedirect = false; + } + else if (lua->IsType(-1,GarrysMod::Lua::Type::STRING)) { + ret.shouldOpen = true; + ret.shouldRedirect = true; + ret.redirect = lua->GetString(-1); + } lua->Pop(3); + return ret; } @@ -46,6 +65,6 @@ class OpenResult { static std::vector waiting_list; const char *file; bool has_result; - bool result; + IOpenResult result; }; #endif // OPENRESULT_H \ No newline at end of file From e7d89ecd7ab1e898eaf99facda68f79cb912a293 Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 01:34:00 +0930 Subject: [PATCH 2/8] im dumb --- src/openresult.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openresult.h b/src/openresult.h index 2cd0d66..b52cd82 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -47,7 +47,7 @@ class OpenResult { IOpenResult ret; if (lua->IsType(-1,GarrysMod::Lua::Type::BOOL)) { - ret.shouldOpen = !lua->GetBool(-1); + ret.shouldOpen = lua->GetBool(-1); ret.shouldRedirect = false; } else if (lua->IsType(-1,GarrysMod::Lua::Type::STRING)) { From eb0f6779d8f1441d785f7513d2f0c47aea90faee Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 01:36:29 +0930 Subject: [PATCH 3/8] what am i doing --- src/openresult.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openresult.h b/src/openresult.h index b52cd82..2cd0d66 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -47,7 +47,7 @@ class OpenResult { IOpenResult ret; if (lua->IsType(-1,GarrysMod::Lua::Type::BOOL)) { - ret.shouldOpen = lua->GetBool(-1); + ret.shouldOpen = !lua->GetBool(-1); ret.shouldRedirect = false; } else if (lua->IsType(-1,GarrysMod::Lua::Type::STRING)) { From e7531df1694ad57708bccda8468b54ec65994a7e Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 01:37:20 +0930 Subject: [PATCH 4/8] this is probably the actual bug --- src/openresult.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/openresult.h b/src/openresult.h index 2cd0d66..7561437 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -55,6 +55,11 @@ class OpenResult { ret.shouldRedirect = true; ret.redirect = lua->GetString(-1); } + else { + ret.shouldOpen = true; + ret.shouldRedirect = false; + } + lua->Pop(3); return ret; From ed9a1b6917cb7afb6bdf5484d37178761d48255f Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 01:42:09 +0930 Subject: [PATCH 5/8] also allow user to specify path ID --- src/filesystem_open.cpp | 2 +- src/openresult.h | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/filesystem_open.cpp b/src/filesystem_open.cpp index 5e00151..b973d8c 100644 --- a/src/filesystem_open.cpp +++ b/src/filesystem_open.cpp @@ -25,7 +25,7 @@ FileHandle_t VirtualFunctionHooks::IBaseFileSystem__Open(const char *pFileName, } if (res.shouldRedirect) { - return FunctionHooks->BaseFileSystemReplacer->Call(FunctionHooks->IBaseFileSystem__Open__index, res.redirect.c_str(), pOptions, pathID); + return FunctionHooks->BaseFileSystemReplacer->Call(FunctionHooks->IBaseFileSystem__Open__index, res.redirect.c_str(), pOptions, res.redirectPathID.c_str()); } else { return FunctionHooks->BaseFileSystemReplacer->Call(FunctionHooks->IBaseFileSystem__Open__index, pFileName, pOptions, pathID); diff --git a/src/openresult.h b/src/openresult.h index 7561437..a71092a 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -12,6 +12,7 @@ struct IOpenResult { bool shouldOpen; bool shouldRedirect; std::string redirect; + std::string redirectPathID; }; class OpenResult { @@ -42,18 +43,24 @@ class OpenResult { lua->GetField(-1, "Run"); lua->PushString("ShouldHideFile"); lua->PushString(file); - lua->Call(2, 1); + lua->Call(2, 2); IOpenResult ret; - if (lua->IsType(-1,GarrysMod::Lua::Type::BOOL)) { + if (lua->IsType(-2,GarrysMod::Lua::Type::BOOL)) { ret.shouldOpen = !lua->GetBool(-1); ret.shouldRedirect = false; } - else if (lua->IsType(-1,GarrysMod::Lua::Type::STRING)) { + else if (lua->IsType(-2,GarrysMod::Lua::Type::STRING)) { ret.shouldOpen = true; ret.shouldRedirect = true; ret.redirect = lua->GetString(-1); + if (lua->IsType(-1, GarrysMod::Lua::Type::STRING)) { + ret.redirectPathID = lua->GetString(-2); + } + else { + ret.redirectPathID = "GAME"; + } } else { ret.shouldOpen = true; From 570d9f1b0861c448369cc106bc17411654b1d1d3 Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 01:46:04 +0930 Subject: [PATCH 6/8] it doesn't help that it's 1:46 am --- src/openresult.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openresult.h b/src/openresult.h index a71092a..bc94165 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -67,7 +67,7 @@ class OpenResult { ret.shouldRedirect = false; } - lua->Pop(3); + lua->Pop(4); return ret; } From 7092df816a1e74c373108a400280c9f4386ec15b Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 02:10:51 +0930 Subject: [PATCH 7/8] very fast --- src/filesystem_open.cpp | 2 +- src/openresult.h | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/filesystem_open.cpp b/src/filesystem_open.cpp index b973d8c..07252e9 100644 --- a/src/filesystem_open.cpp +++ b/src/filesystem_open.cpp @@ -38,7 +38,7 @@ int ThinkHook(lua_State *state) { OpenResult::m.lock(); for (int i = v.size() - 1; i >= 0; i--) { OpenResult *res = v[i]; - res->result = res->RunLua(); + res->RunLua(); v.erase(v.begin() + i); res->has_result = true; } diff --git a/src/openresult.h b/src/openresult.h index bc94165..2150b94 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -23,20 +23,22 @@ class OpenResult { IOpenResult GetResult() { if (ThreadGetCurrentId() == FunctionHooks->mainthread) { - return RunLua(); + RunLua(); } - OpenResult::m.lock(); - this->has_result = false; - OpenResult::waiting_list.push_back(this); - OpenResult::m.unlock(); + else { + OpenResult::m.lock(); + this->has_result = false; + OpenResult::waiting_list.push_back(this); + OpenResult::m.unlock(); - while (!this->has_result) - ThreadSleep(2); + while (!this->has_result) + ThreadSleep(2); + } return this->result; } - IOpenResult RunLua() { + void RunLua() { auto lua = FunctionHooks->lua; lua->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB); lua->GetField(-1, "hook"); @@ -45,31 +47,28 @@ class OpenResult { lua->PushString(file); lua->Call(2, 2); - IOpenResult ret; - if (lua->IsType(-2,GarrysMod::Lua::Type::BOOL)) { - ret.shouldOpen = !lua->GetBool(-1); - ret.shouldRedirect = false; + this->result.shouldOpen = !lua->GetBool(-2); + this->result.shouldRedirect = false; } else if (lua->IsType(-2,GarrysMod::Lua::Type::STRING)) { - ret.shouldOpen = true; - ret.shouldRedirect = true; - ret.redirect = lua->GetString(-1); + this->result.shouldOpen = true; + this->result.shouldRedirect = true; + this->result.redirect = std::string(lua->GetString(-2)); + if (lua->IsType(-1, GarrysMod::Lua::Type::STRING)) { - ret.redirectPathID = lua->GetString(-2); + this->result.redirectPathID = std::string(lua->GetString(-1)); } else { - ret.redirectPathID = "GAME"; + this->result.redirectPathID = "GAME"; } } else { - ret.shouldOpen = true; - ret.shouldRedirect = false; + this->result.shouldOpen = true; + this->result.shouldRedirect = false; } lua->Pop(4); - - return ret; } public: From c5751b7c6ebfa9ce11a5e1a250d8d9fdb0d7572b Mon Sep 17 00:00:00 2001 From: Swad Date: Sat, 12 Aug 2017 02:30:00 +0930 Subject: [PATCH 8/8] okey i make change okey @meepdarknessmeep --- src/openresult.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openresult.h b/src/openresult.h index 2150b94..c9bbdff 100644 --- a/src/openresult.h +++ b/src/openresult.h @@ -60,7 +60,7 @@ class OpenResult { this->result.redirectPathID = std::string(lua->GetString(-1)); } else { - this->result.redirectPathID = "GAME"; + this->result.redirectPathID = "BASE_PATH"; } } else {