This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
remote.lua
170 lines (144 loc) · 6.09 KB
/
remote.lua
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
if not evogui then evogui = { log = function() end } end
--
-- Rebuilds a given player's EvoGUI, destroying any existing settings.
-- player_name: the name or player index of the player being reset
local function remote_rebuild(player_name)
if not player_name then
evogui.log({"err_needplayername"})
return
end
if not game.players[player_name] then
evogui.log({"err_nosuchplayer", tostring(player_name)})
return
end
if not global.evogui or not global.evogui[player_name] then
evogui.log({"err_noplayerdata", player_name})
return
end
global.evogui[player_name] = nil
evogui.new_player({ player_index = player_name })
end
--
-- Creates a sensor managed by a remote interface (another mod or script)
-- sensor_data: a table with the following fields:
-- mod_name: Name of the mod registering the sensor. Sensor will be removed
-- if the mod is removed from the game.
-- name: Internal name of the sensor. Should be unique (otherwise you're
-- just redefining the existing one).
-- text: Text to display in the active gui (may be localized).
-- caption: Sensor setting name in the EvoGUI settings panel (may be localized).
-- color: Font color of the text to display in the active gui, optional, may be nil.
--
-- example: remote.call("EvoGUI", "create_remote_sensor", { mod_name = "my_mod",
-- name = "my_mod_my_sensor_name",
-- text = "Text: Lorem Ipsum",
-- caption = "Lorem Ipsum Text" })
-- or, with locale:
-- remote.call("EvoGUI", "create_remote_sensor", { mod_name = "my_mod",
-- name = "my_mod_my_sensor_name",
-- text = {"my_mod_sensor_display", 42},
-- caption = {"my_mod_sensor"} })
local function create_remote_sensor(sensor_data)
if not sensor_data then
evogui.log({"err_no_sensor_data"})
return
end
for _, field in pairs({ "mod_name", "name", "text", "caption" }) do
if not sensor_data[field] then
evogui.log({"err_sensor_missing_field", serpent.dump(sensor_data, {compact = false, nocode = true, indent = ' '}), field})
return
end
end
local sensor = RemoteSensor.get_by_name(sensor_data.name)
if not sensor then
RemoteSensor.new(sensor_data)
end
end
--
-- Updates a sensor managed by a remote interface
-- sensor_name: internal name of the sensor. The sensor should have been previously created.
-- sensor_text: Text to display in the active gui
-- sensor_color: Font color of the text to display in the active gui, optional, may be nil
-- example: remote.call("EvoGUI", "update_remote_sensor", "mymod_my_sensor_name", "Text: Lorem Ipsum")
local function update_remote_sensor(sensor_name, sensor_text, sensor_color)
if not sensor_name then
evogui.log({"err_nosensorname"})
return
end
if not sensor_text then
evogui.log({"err_nosensortext", sensor_name})
return
end
local sensor = RemoteSensor.get_by_name(sensor_name)
if not sensor then
evogui.log({"err_nosensorfound", sensor_name})
return
end
sensor["line"] = sensor_text
if sensor_color then
sensor["color"] = sensor_color
end
end
--
-- Checks if a remote sensor exists, returns true if one was does
-- sensor_name: internal name of the sensor.
-- example: remote.call("EvoGUI", "has_remote_sensor", "mymod_my_sensor_name")
local function has_remote_sensor(sensor_name)
if not sensor_name then
evogui.log({"err_nosensorname"})
return
end
local sensor = RemoteSensor.get_by_name(sensor_name)
return sensor ~= nil
end
--
-- Removes a sensor managed by a remote interface, returns true if one was removed
-- sensor_name: internal name of the sensor. If it does not exist, it was already removed.
-- example: remote.call("EvoGUI", "remove_remote_sensor", "mymod_my_sensor_name")
local function remove_remote_sensor(sensor_name)
if not sensor_name then
evogui.log({"err_nosensorname"})
return
end
local sensor = RemoteSensor.get_by_name(sensor_name)
if not sensor then
-- impossible to know if the sensor was removed in advance, so just return a status
return false
end
evogui.hide_sensor(sensor)
for idx, sensor in pairs(evogui.value_sensors) do
if sensor.name == ("remote_sensor_" .. sensor_name) then
table.remove(evogui.value_sensors, idx)
end
end
global.remote_sensors[sensor_name] = nil
return true
end
interface = {
rebuild = function(player_name)
local status, retval = pcall(remote_rebuild, player_name)
if status then return retval
elseif retval then evogui.log({"err_generic", "remote.rebuild", retval}) end
end,
create_remote_sensor = function(sensor_data)
local status, retval = pcall(create_remote_sensor, sensor_data)
if status then return retval
elseif retval then evogui.log({"err_generic", "remote.create_remote_sensor", retval}) end
end,
update_remote_sensor = function(sensor_name, sensor_text, sensor_color)
local status, retval = pcall(update_remote_sensor, sensor_name, sensor_text, sensor_color)
if status then return retval
elseif retval then evogui.log({"err_generic", "remote.update_remote_sensor", retval}) end
end,
has_remote_sensor = function(sensor_name)
local status, retval = pcall(has_remote_sensor, sensor_name)
if status then return retval
elseif retval then evogui.log({"err_generic", "remote.has_remote_sensor", retval}) end
end,
remove_remote_sensor = function(sensor_name)
local status, retval = pcall(remove_remote_sensor, sensor_name)
if status then return retval
elseif retval then evogui.log({"err_generic", "remote.remove_remote_sensor", retval}) end
end
}
remote.add_interface("EvoGUI", interface)