Skip to content

Commit b94ebb7

Browse files
committed
lua: crash any% speedrun
1 parent a9af28b commit b94ebb7

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

source/lua.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void Lua::Init(GarrysMod::Lua::ILuaInterface* LUA)
5353
}
5454

5555
g_Lua = LUA;
56+
Lua::CreateLuaData(g_Lua);
5657
g_pModuleManager.LuaInit(false);
5758

5859
std::vector<LuaFindResult> results;
@@ -103,6 +104,8 @@ void Lua::Shutdown()
103104

104105
void Lua::FinalShutdown()
105106
{
107+
Lua::RemoveLuaData(g_Lua);
108+
106109
// g_Lua is bad at this point / the lua_State is already gone so we CAN'T allow any calls
107110
g_Lua = NULL;
108111

@@ -202,30 +205,45 @@ void Lua::DestroyInterface(GarrysMod::Lua::ILuaInterface* LUA)
202205
CloseLuaInterface(LUA);
203206
}
204207

205-
static std::unordered_map<GarrysMod::Lua::ILuaInterface*, Lua::StateData*> g_pLuaStateData;
208+
/*
209+
* Where do we store our StateData?
210+
* In the ILuaInterface itself.
211+
* We abuse the GetPathID var as it's a char[32] but it'll never actually fully use it.
212+
* Why? Because I didn't want to use yet another unordered_map for this, also this should be faster.
213+
*/
206214
Lua::StateData* Lua::GetLuaData(GarrysMod::Lua::ILuaInterface* LUA)
207215
{
208-
auto it = g_pLuaStateData.find(LUA);
209-
if (it == g_pLuaStateData.end())
210-
return NULL;
211-
212-
return it->second;
216+
return *reinterpret_cast<Lua::StateData**>((char*)LUA->GetPathID() + 24);
213217
}
214218

215219
void Lua::CreateLuaData(GarrysMod::Lua::ILuaInterface* LUA)
216220
{
217221
if (Lua::GetLuaData(LUA))
218222
return;
219223

220-
g_pLuaStateData[LUA] = new Lua::StateData;
224+
char* pathID = (char*)LUA->GetPathID();
225+
Lua::StateData* data = new Lua::StateData;
226+
*reinterpret_cast<Lua::StateData**>(pathID + 24) = data;
227+
Msg("holylib - Created thread data %p\n", data);
221228
}
222229

223230
void Lua::RemoveLuaData(GarrysMod::Lua::ILuaInterface* LUA)
224231
{
225-
auto it = g_pLuaStateData.find(LUA);
226-
if (it == g_pLuaStateData.end())
232+
auto data = Lua::GetLuaData(LUA);
233+
if (!data)
234+
return;
235+
236+
delete data;
237+
*reinterpret_cast<Lua::StateData**>((char*)LUA->GetPathID() + 24) = NULL;
238+
Msg("holylib - Removed thread data %p\n", data);
239+
}
240+
241+
242+
static void LuaCheck(const CCommand& args)
243+
{
244+
if (!g_Lua)
227245
return;
228246

229-
delete it->second;
230-
g_pLuaStateData.erase(it);
247+
Msg("holylib - Found data %p\n", Lua::GetLuaData(g_Lua));
231248
}
249+
static ConCommand luacheck("holylib_luacheck", LuaCheck, "Temp", 0);

0 commit comments

Comments
 (0)