Skip to content

Commit d5a45f8

Browse files
committed
Lua Execute content autosave/load
I got fed up of having to rewrite when the panel reloads (separate file for cl/sv)
1 parent 42ad2f7 commit d5a45f8

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

lua/blobsprofiler/shared/modules/bp_lua.lua

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,51 @@ blobsProfiler.RegisterSubModule("Lua", "Globals", {
102102
RefreshButton = "Re-scan" -- TODO: I couldn't get this to play nice, so I gave up for now
103103
})
104104

105+
local function luaExecuteFilesInit()
106+
if not file.Exists("blobsProfiler", "DATA") then
107+
file.CreateDir("blobsProfiler")
108+
end
109+
110+
if not file.Exists("blobsProfiler/Client_LuaExecute.txt", "DATA") then
111+
file.Write("blobsProfiler/Client_LuaExecute.txt", [[print("Hello client!")]])
112+
end
113+
114+
if not file.Exists("blobsProfiler/Server_LuaExecute.txt", "DATA") then
115+
file.Write("blobsProfiler/Server_LuaExecute.txt", [[print("Hello server!")]])
116+
end
117+
end
118+
119+
if CLIENT then
120+
luaExecuteFilesInit()
121+
end
122+
105123
blobsProfiler.RegisterSubModule("Lua", "Execute", {
106124
Icon = "icon16/script_code.png",
107125
OrderPriority = 2,
108126
CustomPanel = function(luaState, parentPanel)
109-
local dhtmlPanel = blobsProfiler.generateAceEditorPanel(parentPanel)
127+
luaExecuteFilesInit()
128+
129+
local preLoadContent = file.Read("blobsProfiler/"..luaState.."_LuaExecute.txt")
130+
local dhtmlPanel = blobsProfiler.generateAceEditorPanel(parentPanel, preLoadContent or [[print("Hello world!")]])
110131

111132
dhtmlPanel:Dock(FILL)
133+
dhtmlPanel.lastCode = preLoadContent
134+
135+
dhtmlPanel:AddFunction("gmod", "saveContentsToFile", function(value)
136+
if value ~= dhtmlPanel.lastCode then -- prevent unnecessary writes
137+
blobsProfiler.Log(blobsProfiler.L_DEBUG, "Writing Lua Execute editor content to: 'blobsProfiler/"..luaState.."_LuaExecute.txt'")
138+
file.Write("blobsProfiler/"..luaState.."_LuaExecute.txt", value)
139+
dhtmlPanel.lastCode = value
140+
end
141+
end)
112142

113143
dhtmlPanel:AddFunction("gmod", "receiveEditorContent", function(value)
144+
if value ~= dhtmlPanel.lastCode then -- prevent unnecessary writes
145+
blobsProfiler.Log(blobsProfiler.L_DEBUG, "Writing Lua Execute editor content to: 'blobsProfiler/"..luaState.."_LuaExecute.txt'")
146+
file.Write("blobsProfiler/"..luaState.."_LuaExecute.txt", value)
147+
dhtmlPanel.lastCode = value
148+
end
149+
114150
if luaState == "Client" then
115151
RunString(value)
116152
elseif luaState == "Server" then
@@ -132,9 +168,30 @@ blobsProfiler.RegisterSubModule("Lua", "Execute", {
132168
gmod.receiveEditorContent(value);
133169
]])
134170
end
171+
172+
dhtmlPanel.attemptSaveContentsToFile = function()
173+
dhtmlPanel:RunJavascript([[
174+
var value = getEditorValue();
175+
gmod.saveContentsToFile(value);
176+
]])
177+
end
178+
179+
parentPanel.codePanel = dhtmlPanel
135180
end
136181
})
137182

183+
timer.Create("blobsProfiler:LuaExecute_SaveToFile", 15, 0, function() -- TODO: Make this configurable once I do settings - and module settings
184+
local moduleTbl = blobsProfiler.GetModule("Lua.Execute")
185+
if not moduleTbl then return end
186+
187+
if moduleTbl.ClientTab and moduleTbl.ClientTab.codePanel and moduleTbl.ClientTab.codePanel.attemptSaveContentsToFile then
188+
moduleTbl.ClientTab.codePanel.attemptSaveContentsToFile()
189+
end
190+
if moduleTbl.ServerTab and moduleTbl.ServerTab.codePanel and moduleTbl.ServerTab.codePanel.attemptSaveContentsToFile then
191+
moduleTbl.ServerTab.codePanel.attemptSaveContentsToFile()
192+
end
193+
end)
194+
138195
local function prettyProfilerTimeFormat(seconds)
139196
if seconds >= 0.5 then
140197
return string.format("%.2fs", seconds)

0 commit comments

Comments
 (0)