Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/filesystem_find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions src/filesystem_open.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileHandle_t, const char *, const char *, const char *>(FunctionHooks->IBaseFileSystem__Open__index, pFileName, pOptions, pathID);
if (res.shouldRedirect) {
return FunctionHooks->BaseFileSystemReplacer->Call<FileHandle_t, const char *, const char *, const char *>(FunctionHooks->IBaseFileSystem__Open__index, res.redirect.c_str(), pOptions, res.redirectPathID.c_str());
}
else {
return FunctionHooks->BaseFileSystemReplacer->Call<FileHandle_t, const char *, const char *, const char *>(FunctionHooks->IBaseFileSystem__Open__index, pFileName, pOptions, pathID);
}
}


Expand All @@ -30,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;
}
Expand Down
58 changes: 44 additions & 14 deletions src/openresult.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,79 @@

#include <mutex>
#include <vector>
#include <string>
#include "threadtools.h"
#include "GarrysMod/Lua/Interface.h"
#include "vfuncs.h"

struct IOpenResult {
bool shouldOpen;
bool shouldRedirect;
std::string redirect;
std::string redirectPathID;
};

class OpenResult {
public:
OpenResult(const char *relativetobase) {
file = relativetobase;
}

bool GetResult() {
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;
}

bool RunLua() {
void RunLua() {
auto lua = FunctionHooks->lua;
lua->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
lua->GetField(-1, "hook");
lua->GetField(-1, "Run");
lua->PushString("ShouldHideFile");
lua->PushString(file);
lua->Call(2, 1);
bool ret = !lua->GetBool(-1);
lua->Pop(3);
return ret;
lua->Call(2, 2);

if (lua->IsType(-2,GarrysMod::Lua::Type::BOOL)) {
this->result.shouldOpen = !lua->GetBool(-2);
this->result.shouldRedirect = false;
}
else if (lua->IsType(-2,GarrysMod::Lua::Type::STRING)) {
this->result.shouldOpen = true;
this->result.shouldRedirect = true;
this->result.redirect = std::string(lua->GetString(-2));

if (lua->IsType(-1, GarrysMod::Lua::Type::STRING)) {
this->result.redirectPathID = std::string(lua->GetString(-1));
}
else {
this->result.redirectPathID = "BASE_PATH";
}
}
else {
this->result.shouldOpen = true;
this->result.shouldRedirect = false;
}

lua->Pop(4);
}

public:
static std::mutex m;
static std::vector<OpenResult *> waiting_list;
const char *file;
bool has_result;
bool result;
IOpenResult result;
};
#endif // OPENRESULT_H