]> git.somenet.org - factorio/some-luaconsole.git/blob - control.lua
RELEASE 2.0.3 - Overall better error handling
[factorio/some-luaconsole.git] / control.lua
1 function exec_command(player)
2     if not player.admin then
3         player.print('You are not an admin. You may not use this mod. :(')
4         return
5     end
6
7     local f, lserr, pcs, pcerr, cmd
8
9     if player.gui.screen.some_luaconsole then
10         storage.cmd = player.gui.screen.some_luaconsole.input.text
11     end
12
13     cmd = storage.cmd or ""
14     cmd = cmd:gsub('game%.player([^s])', 'game.players['..player.index..']%1')
15
16     f, lserr = loadstring('local ipcs,ipcr ipcs,ipcr=pcall(function() '..cmd..' end) if not ipcs then game.players['..player.index..'].print(ipcr) end')
17     if not f then
18 --        player.print("loadstring1 failed (this may be ok)")
19         f, lserr = loadstring('game.players['..player.index..'].print('..cmd..')')
20     end
21
22     if f then
23         pcs, pcerr = pcall(f)
24         if not pcs then
25 --            player.print("pcall failed")
26             player.print(pcerr:sub(1, pcerr:find('\n')))
27         end
28     else
29 --        player.print("syntax error2")
30         player.print(lserr:sub(1, lserr:find('\n')))
31     end
32 end
33
34
35 function toggleGui (player)
36     if player.gui.screen.some_luaconsole then
37         storage.cmd = player.gui.screen.some_luaconsole.input.text
38         player.gui.screen.some_luaconsole.destroy()
39     else
40         frame = player.gui.screen.add{type = 'frame',
41             name = 'some_luaconsole',
42             direction = 'vertical',
43             caption = {'some_luaconsole.title'}
44         }
45
46         frame.add{type = 'label', caption = {'some_luaconsole.inputlabel'}}
47         input = frame.add{type = 'text-box',
48             name = 'input',
49             style='some_luaconsole_input_textbox'
50         }
51         input.word_wrap = true
52         input.style.maximal_height = (player.display_resolution.height/player.display_scale*0.6)
53         input.text = storage.cmd or ""
54
55         horizontal_flow = frame.add{type='flow', direction='horizontal'}
56         horizontal_flow.add{type = 'button',
57             name = 'some_luaconsole_close',
58             style='back_button',
59             caption = {'some_luaconsole.close'},
60             tooltip = {'some_luaconsole.close_tooltip'}
61         }
62         horizontal_flow.add{type = 'button',
63             name = 'some_luaconsole_exec',
64             style='confirm_button',
65             caption = {'some_luaconsole.exec'},
66             tooltip = {'some_luaconsole.exec_tooltip'}
67         }
68
69         if not player.admin then
70             player.gui.screen.some_luaconsole.input.text = 'You are not an admin. You may not use this mod. :('
71             player.gui.screen.some_luaconsole.input.enabled = false
72         end
73         frame.force_auto_center()
74     end
75 end
76
77
78
79 script.on_event(defines.events.on_gui_click, function(event)
80     if event.element.name == 'some_luaconsole_exec' then
81         exec_command(game.players[event.player_index])
82     elseif event.element.name == 'some_luaconsole_close' then
83         toggleGui(game.players[event.player_index])
84     end
85 end)
86
87
88 script.on_event('some_luaconsole_toggle', function(event)
89     toggleGui(game.players[event.player_index])
90 end)
91 script.on_event('some_luaconsole_exec', function(event)
92     exec_command(game.players[event.player_index])
93 end)