-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscriptsManager.lua
More file actions
194 lines (167 loc) · 5.67 KB
/
scriptsManager.lua
File metadata and controls
194 lines (167 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
dofile('utils.lua')
ScriptsManager = {}
local scriptsManagerWindow
local scriptsManagerPanel
local scriptsManagerButton
scriptsManagerEvents = {}
scriptsManagerLastId = 0
function ScriptsManager.init()
scriptsManagerButton = modules.client_topmenu.addLeftButton('scriptsManagerButton', 'Scripts Manager', 'scriptsManager.png', ScriptsManager.popupMenu)
scriptsManagerWindow = g_ui.displayUI('scriptsManager.otui')
scriptsManagerWindow:hide()
scriptsManagerPanel = scriptsManagerWindow:getChildById('scriptsPanel')
for id = 1, 50 do
local scriptBox = g_ui.createWidget('ScriptBox', scriptsManagerPanel)
scriptBox.scriptId = id
scriptBox:setId('script_' .. id)
scriptBox:getChildById('numberId'):setText('#'..id)
if id == 1 then
scriptBox:addAnchor(AnchorTop, 'parent', AnchorTop)
end
end
ScriptsManager.load()
end
function ScriptsManager.terminate()
for i,v in pairs(scriptsManagerPanel:getChildren()) do
if v.scriptId then
local checkbox = v:getChildById('statusCheck')
ScriptsManager.scriptBoxSet(checkbox, false)
end
end
scriptsManagerPanel:destroy()
scriptsManagerPanel = nil
scriptsManagerButton:destroy()
scriptsManagerButton = nil
scriptsManagerWindow:destroy()
scriptsManagerWindow = nil
scriptsManagerEvents = {}
scriptsManagerLastId = 0
end
function ScriptsManager.popupMenu()
local scriptsManagerPopup = g_ui.createWidget('PopupMenu')
scriptsManagerPopup:addOption("Scripts Manager", function () ScriptsManager.show() end)
scriptsManagerPopup:addSeparator()
for i,v in pairs(scriptsManagerPanel:getChildren()) do
if v.scriptId then
local status = v:getChildById('statusCheck')
local listas = v:getChildById('listasText'):getText()
if listas ~= '' then
scriptsManagerPopup:addOption(listas, function() status:setChecked(not status:isChecked()) end, status:isChecked() and 'On' or 'Off')
end
end
end
scriptsManagerPopup:display(g_window.getMousePosition())
end
function ScriptsManager.show()
scriptsManagerWindow:show()
end
function ScriptsManager.hide()
scriptsManagerWindow:hide()
end
function ScriptsManager.save()
local patch = "/scriptsManager/default.otml"
local file = g_configs.load(patch)
if not file then
file = g_configs.create(patch)
end
local obj = {}
for i,v in pairs(scriptsManagerPanel:getChildren()) do
if v.scriptId then
obj[v.scriptId] = {listas = v:getChildById('listasText'):getText(), script = v:getChildById('scriptText'):getText()}
end
end
file:setNode('scripts', obj)
file:save()
end
function ScriptsManager.load()
if not g_resources.directoryExists("/scriptsManager") then
g_resources.makeDir("/scriptsManager")
end
local patch = "/scriptsManager/default.otml"
local file = g_configs.load(patch)
if file then
local obj = file:getNode('scripts')
if obj then
for i,v in pairs(scriptsManagerPanel:getChildren()) do
if v.scriptId then
local sObj = obj[tostring(v.scriptId)]
if sObj then
v:getChildById('listasText'):setText(sObj.listas)
v:getChildById('scriptText'):setText(sObj.script)
end
end
end
end
end
end
-- script's execution functions
local hookFunctions = {"cycleEvent", "g_keyboard.bindKeyPress", "onTalkContains", "onScriptCallback"}
function ScriptsManager.hook()
for i,fName in pairs(hookFunctions) do
local hfName = "_"..fName:gsub("%.","%_")
loadstring(hfName .. " = " .. fName)()
loadstring(fName .. " = function(...) scriptsManagerEvents[scriptsManagerLastId] = {func = ".. hfName ..", args = {...}, name = \"".. fName .."\", ret = ".. hfName .."(...)} end")()
end
end
function ScriptsManager.unhook()
for i,fName in pairs(hookFunctions) do
local hfName = "_"..fName:gsub("%.","%_")
loadstring(fName .. " = " .. hfName)()
end
end
function ScriptsManager.call(func)
ScriptsManager.hook()
local success, ret = pcall(func)
ScriptsManager.unhook()
return ret
end
-- window handlers
function ScriptsManager.close()
ScriptsManager.save()
ScriptsManager.hide()
end
function ScriptsManager.scriptBoxSet(widget, option)
local id = widget:getParent().scriptId
local listasWidget = scriptsManagerPanel:getChildById('script_' .. id):getChildById('listasText')
local scriptWidget = scriptsManagerPanel:getChildById('script_' .. id):getChildById('scriptText')
local listas = listasWidget:getText()
local script = scriptWidget:getText()
if option == true then
if script ~= '' then
local ret = loadstring(script)
if type(ret) == "function" then
scriptsManagerLastId = id
ScriptsManager.call(ret)
else
displayErrorBox("Scripts Manager", "Error script #".. id .." could not be run.")
option = false
end
if scriptsManagerEvents[id] == nil then
option = false
end
if option then
listasWidget:disable()
scriptWidget:disable()
end
else
option = false
displayErrorBox("Script Manager", "Error script #".. id .." is empty.")
end
else
listasWidget:enable()
scriptWidget:enable()
if scriptsManagerEvents[id] then
if scriptsManagerEvents[id].func == cycleEvent then
removeEvent(scriptsManagerEvents[id].ret)
elseif scriptsManagerEvents[id].func == g_keyboard.bindKeyPress then
g_keyboard.unbindKeyPress(scriptsManagerEvents[id].args[1], scriptsManagerEvents[id].args[2])
elseif scriptsManagerEvents[id].func == onTalkContains then
disconnect(g_game, {onTalk = scriptsManagerEvents[id].ret})
elseif scriptsManagerEvents[id].func == onScriptCallback then
pcall(scriptsManagerEvents[id].args[2])
end
scriptsManagerEvents[id] = nil
end
end
widget:setChecked(option)
end