|
| 1 | +#include "module.h" |
| 2 | +#include "LuaInterface.h" |
| 3 | +#include "lua.h" |
| 4 | +#include "detours.h" |
| 5 | + |
| 6 | +#include <unordered_map> |
| 7 | + |
| 8 | +#include "tier0/memdbgon.h" |
| 9 | + |
| 10 | +class CAutoRefreshModule : public IModule |
| 11 | +{ |
| 12 | +public: |
| 13 | + virtual void LuaInit(GarrysMod::Lua::ILuaInterface* pLua, bool bServerInit) OVERRIDE; |
| 14 | + virtual void LuaShutdown(GarrysMod::Lua::ILuaInterface* pLua) OVERRIDE; |
| 15 | + virtual void InitDetour(bool bPreServer) OVERRIDE; |
| 16 | + virtual const char* Name() { return "autorefresh"; }; |
| 17 | + virtual int Compatibility() { return LINUX32 | LINUX64; }; |
| 18 | +}; |
| 19 | + |
| 20 | +CAutoRefreshModule g_pAutoRefreshModule; |
| 21 | +IModule* pAutoRefreshModule = &g_pAutoRefreshModule; |
| 22 | + |
| 23 | +static std::unordered_map<std::string, bool> blockedLuaFilesMap = {}; |
| 24 | +LUA_FUNCTION_STATIC(DenyLuaAutoRefresh) |
| 25 | +{ |
| 26 | + LUA->CheckType(1, GarrysMod::Lua::Type::String); |
| 27 | + LUA->CheckType(2, GarrysMod::Lua::Type::Bool); |
| 28 | + |
| 29 | + const char* inputFilePath = LUA->GetString(1); |
| 30 | + bool blockStatus = LUA->GetBool(2); |
| 31 | + char normalizedPath[260]; |
| 32 | + V_FixupPathName(normalizedPath, sizeof(normalizedPath), inputFilePath); |
| 33 | + blockedLuaFilesMap.insert_or_assign(std::string(normalizedPath), blockStatus); |
| 34 | + |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +static Detouring::Hook detour_CAutoRefresh_HandleChange_Lua; |
| 39 | +static bool hook_CAutoRefresh_HandleChange_Lua(const std::string* pfileRelPath, const std::string* pfileName, const std::string* pfileExt) |
| 40 | +{ |
| 41 | + auto trampoline = detour_CAutoRefresh_HandleChange_Lua.GetTrampoline<Symbols::GarrysMod_AutoRefresh_HandleChange_Lua>(); |
| 42 | + if (!g_Lua || !pfileRelPath || !pfileName || !pfileExt) |
| 43 | + { |
| 44 | + return trampoline(pfileRelPath, pfileName, pfileExt); |
| 45 | + } |
| 46 | + |
| 47 | + if (std::string(pfileExt->substr(0, 3)) != "lua") |
| 48 | + { |
| 49 | + return trampoline(pfileRelPath, pfileName, pfileExt); |
| 50 | + } |
| 51 | + |
| 52 | + bool bDenyRefresh = false; |
| 53 | + if (Lua::PushHook("HolyLib:PreLuaAutoRefresh")) |
| 54 | + { |
| 55 | + g_Lua->PushString(pfileRelPath->c_str()); |
| 56 | + g_Lua->PushString(pfileName->c_str()); |
| 57 | + |
| 58 | + if (g_Lua->CallFunctionProtected(3, 1, true)) |
| 59 | + { |
| 60 | + bDenyRefresh = g_Lua->GetBool(-1); |
| 61 | + g_Lua->Pop(1); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (!blockedLuaFilesMap.empty() && !bDenyRefresh) |
| 66 | + { |
| 67 | + char fullPath[260]; |
| 68 | + V_ComposeFileName(pfileRelPath->c_str(), pfileName->c_str(), fullPath, sizeof(fullPath)); |
| 69 | + V_SetExtension(fullPath, "lua", sizeof(fullPath)); |
| 70 | + if (auto fileSearch = blockedLuaFilesMap.find(fullPath); fileSearch != blockedLuaFilesMap.end()) |
| 71 | + { |
| 72 | + bDenyRefresh = fileSearch->second; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (bDenyRefresh) |
| 77 | + { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + bool originalResult = trampoline(pfileRelPath, pfileName, pfileExt); |
| 82 | + |
| 83 | + if (Lua::PushHook("HolyLib:PostLuaAutoRefresh")) |
| 84 | + { |
| 85 | + g_Lua->PushString(pfileRelPath->c_str()); |
| 86 | + g_Lua->PushString(pfileName->c_str()); |
| 87 | + |
| 88 | + g_Lua->CallFunctionProtected(3, 0, true); |
| 89 | + } |
| 90 | + |
| 91 | + return originalResult; |
| 92 | +}; |
| 93 | + |
| 94 | +void CAutoRefreshModule::LuaInit(GarrysMod::Lua::ILuaInterface* pLua, bool bServerInit) |
| 95 | +{ |
| 96 | + if (bServerInit) |
| 97 | + return; |
| 98 | + |
| 99 | + Util::StartTable(pLua); |
| 100 | + Util::AddFunc(pLua, DenyLuaAutoRefresh, "DenyLuaAutoRefresh"); |
| 101 | + Util::FinishTable(pLua, "autorefresh"); |
| 102 | +} |
| 103 | + |
| 104 | +void CAutoRefreshModule::LuaShutdown(GarrysMod::Lua::ILuaInterface* pLua) |
| 105 | +{ |
| 106 | + Util::NukeTable(pLua, "autorefresh"); |
| 107 | +} |
| 108 | + |
| 109 | +void CAutoRefreshModule::InitDetour(bool bPreServer) |
| 110 | +{ |
| 111 | + if (bPreServer) |
| 112 | + return; |
| 113 | + |
| 114 | + SourceSDK::FactoryLoader server_loader("server"); |
| 115 | + Detour::Create( |
| 116 | + &detour_CAutoRefresh_HandleChange_Lua, "CAutoRefresh_HandleChange_Lua", |
| 117 | + server_loader.GetModule(), Symbols::GarrysMod_AutoRefresh_HandleChange_LuaSym, |
| 118 | + (void*)hook_CAutoRefresh_HandleChange_Lua, m_pID |
| 119 | + ); |
| 120 | +} |
0 commit comments