Skip to content

Commit 50082a0

Browse files
Dezashqaisjp
authored andcommitted
Improve debug info for garbage collected files (#312)
Closes #309 * Improve debug info for garbage collected files * Remove todo comment * Fix indentation
1 parent daabf09 commit 50082a0

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

Client/mods/deathmatch/logic/CScriptFile.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CScriptFile : public CClientEntity
4848
// Get the owning resource
4949
CResource* GetResource(void);
5050

51-
// Only call functions belw this if you're sure that the file is loaded.
51+
// Only call functions below this if you're sure that the file is loaded.
5252
// Or you will crash.
5353
bool IsEOF(void);
5454
long GetPointer(void);
@@ -60,6 +60,10 @@ class CScriptFile : public CClientEntity
6060
long Read(unsigned long ulSize, CBuffer& outBuffer);
6161
long Write(unsigned long ulSize, const char* pData);
6262

63+
// Debug info for garbage collected files
64+
const SLuaDebugInfo& GetLuaDebugInfo(void) { return m_LuaDebugInfo; };
65+
void SetLuaDebugInfo(const SLuaDebugInfo& luaDebugInfo) { m_LuaDebugInfo = luaDebugInfo; };
66+
6367
private:
6468
void DoResourceFileCheck(void);
6569

@@ -71,6 +75,7 @@ class CScriptFile : public CClientEntity
7175
unsigned int m_uiScriptId;
7276
CResource* m_pResource;
7377
eAccessType m_accessType;
78+
SLuaDebugInfo m_LuaDebugInfo;
7479
};
7580

7681
#endif

Server/mods/deathmatch/logic/CScriptFile.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ class CScriptFile : public CElement
5454
long Read(unsigned long ulSize, CBuffer& outBuffer);
5555
long Write(unsigned long ulSize, const char* pData);
5656

57+
// Debug info for garbage collected files
58+
const SLuaDebugInfo& GetLuaDebugInfo(void) { return m_LuaDebugInfo; };
59+
void SetLuaDebugInfo(const SLuaDebugInfo& luaDebugInfo) { m_LuaDebugInfo = luaDebugInfo; };
60+
5761
private:
5862
CResource* m_pResource;
5963
FILE* m_pFile;
6064
uint m_uiScriptId;
6165
SString m_strFilename;
6266
unsigned long m_ulMaxSize;
67+
SLuaDebugInfo m_LuaDebugInfo;
6368
};

Shared/mods/deathmatch/logic/luadefs/CLuaFileDefs.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ int CLuaFileDefs::fileOpen(lua_State* luaVM)
206206
#ifdef MTA_CLIENT
207207
// Make it a child of the resource's file root
208208
pFile->SetParent(pResource->GetResourceDynamicEntity());
209+
pFile->SetLuaDebugInfo(g_pClientGame->GetScriptDebugging()->GetLuaDebugInfo(luaVM));
210+
#else
211+
pFile->SetLuaDebugInfo(g_pGame->GetScriptDebugging()->GetLuaDebugInfo(luaVM));
209212
#endif
210213
// Grab its owner resource
211214
CResource* pParentResource = pLuaMain->GetResource();
@@ -310,6 +313,9 @@ int CLuaFileDefs::fileCreate(lua_State* luaVM)
310313
#ifdef MTA_CLIENT
311314
// Make it a child of the resource's file root
312315
pFile->SetParent(pResource->GetResourceDynamicEntity());
316+
pFile->SetLuaDebugInfo(g_pClientGame->GetScriptDebugging()->GetLuaDebugInfo(luaVM));
317+
#else
318+
pFile->SetLuaDebugInfo(g_pGame->GetScriptDebugging()->GetLuaDebugInfo(luaVM));
313319
#endif
314320

315321
// Add it to the scrpt resource element group
@@ -923,16 +929,13 @@ int CLuaFileDefs::fileCloseGC(lua_State* luaVM)
923929

924930
if (!argStream.HasErrors())
925931
{
926-
// Close the file and delete it
927-
pFile->Unload();
928-
m_pElementDeleter->Delete(pFile);
929-
930932
// This file wasn't closed, so we should warn
931933
// the scripter that they forgot to close it.
932-
m_pScriptDebugging->LogWarning(luaVM, "Unclosed file (%s) was garbage collected. Check your resource for dereferenced files.", *pFile->GetFilePath());
933-
// TODO: The debug info reported when Lua automatically garbage collects will
934-
// actually be the exact point Lua pauses for collection. Find a way to
935-
// remove the line number & script file completely.
934+
m_pScriptDebugging->LogWarning(pFile->GetLuaDebugInfo(), "Unclosed file (%s) was garbage collected. Check your resource for dereferenced files.", *pFile->GetFilePath());
935+
936+
// Close the file and delete it from elements
937+
pFile->Unload();
938+
m_pElementDeleter->Delete(pFile);
936939

937940
lua_pushboolean(luaVM, true);
938941
return 1;

0 commit comments

Comments
 (0)