Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Merge #41: auto-remove old remote sensors
Browse files Browse the repository at this point in the history
Includes version bump to v0.4.8.
Attn: #23: more locale
  • Loading branch information
narc0tiq committed Nov 5, 2015
2 parents 5690e13 + fe4d33c commit d3584bc
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 34 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.7
0.4.8
14 changes: 10 additions & 4 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ require "evoGUI"
if not evogui then evogui = {} end

function evogui.log(message)
for i, p in ipairs(game.players) do
p.print(message)
if game then
for i, p in ipairs(game.players) do
p.print(message)
end
else
error(serpent.dump(message, {compact = false, nocode = true, indent = ' '}))
end
end

Expand Down Expand Up @@ -37,14 +41,16 @@ end

script.on_init(evogui.mod_init)
script.on_configuration_changed(evogui.mod_update)

script.on_load(function()
local status, err = pcall(RemoteSensor.initialize)
if err then evogui.log({"err_generic", "on_load", err}) end
end)

script.on_event(defines.events.on_player_created, function(event)
local status, err = pcall(evogui.new_player, event)
if err then evogui.log({"err_generic", "on_player_created", err}) end
end)


script.on_event(defines.events.on_tick, function(event)
local status, err = pcall(evogui.update_gui, event)
if err then evogui.log({"err_generic", "on_tick", err}) end
Expand Down
42 changes: 37 additions & 5 deletions evoGUI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,44 @@ function evogui.mod_init()
end
end


function evogui.mod_update(data)
if data.mod_changes["{{MOD_NAME}}"] then
-- TODO: If a more major migration ever needs doing, do that here.
-- Otherwise, just falling back to mod_init should work fine.
evogui.mod_init()
if data.mod_changes then
if data.mod_changes["{{MOD_NAME}}"] then
-- TODO: If a more major migration ever needs doing, do that here.
-- Otherwise, just falling back to mod_init should work fine.
evogui.mod_init()
end

evogui.validate_sensors(data.mod_changes)
end
end

-- Iterate through all value_sensors, if any are associated with a mod_name that
-- has been removed, remove the sensor from the list of value_sensors.
function evogui.validate_sensors(mod_changes)
for i = #evogui.value_sensors, 1, -1 do
local sensor = evogui.value_sensors[i]
if sensor.mod_name and mod_changes[sensor.mod_name] then
-- mod removed, remove sensor from ui
if mod_changes[sensor.mod_name].new_version == nil then
evogui.hide_sensor(sensor)
table.remove(evogui.value_sensors, i)
end
end
end
end

function evogui.hide_sensor(sensor)
for player_name, data in pairs(global.evogui) do
if data.always_visible then
data.always_visible[sensor["name"]] = false
end
end
for _, player in pairs(game.players) do
local player_settings = global.evogui[player.name]

local sensor_flow = player.gui.top.evoGUI.sensor_flow
evogui.update_av(player, sensor_flow.always_visible)
end
end

Expand Down
4 changes: 3 additions & 1 deletion locale/en/locale.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ err_nosuchplayer=[EvoGUI] No such player: __1__!
err_noplayerdata=[EvoGUI] No player data for __1__!
err_nosensorname=[EvoGUI] Missing a sensor name!
err_no_sensor_data=[EvoGUI] Missing remote sensor data!
err_sensor_missing_field=[EvoGUI] Remote sensor __1__ is missing a required field __2__!
err_nosensortext=[EvoGUI] Missing sensor text for __1__!
err_nosensorcaption=[EvoGUI] Missing caption text for __1__!
err_nosensorfound=[EvoGUI] No sensor was previously created matching the name __1__! Create a sensor first.
err_nosensorfound=[EvoGUI] No sensor matching the name __1__! Create a sensor first.
47 changes: 29 additions & 18 deletions remote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,39 @@ end

--
-- Creates a sensor managed by a remote interface (another mod or script)
-- sensor_name: internal name of the sensor. Should be unique.
-- sensor_text: Text to display in the active gui
-- sensor_caption: Sensor setting name in the EvoGUI settings panel
-- sensor_color: Font color of the text to display in the active gui, optional, may be nil
-- example: remote.call("EvoGUI", "create_remote_sensor", "mymod_my_sensor_name", "Text: Lorem Ipsum", "[My Mod] Lorem Ipsum Text")
local function create_remote_sensor(sensor_name, sensor_text, sensor_caption, sensor_color)
if not sensor_name then
evogui.log({"err_nosensorname"})
-- 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.
-- 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

if not sensor_text then
evogui.log({"err_nosensortext", sensor_name})
return
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

if not sensor_caption then
evogui.log({"err_nosensorcaption", sensor_name})
return
local sensor = RemoteSensor.get_by_name(sensor_data.name)
if not sensor then
RemoteSensor.new(sensor_data)
end

RemoteSensor.new(sensor_name, sensor_text, sensor_caption, sensor_color)
end

--
Expand Down Expand Up @@ -93,8 +104,8 @@ interface = {
if err then evogui.log({"err_generic", "interface.rebuild", err}) end
end,

create_remote_sensor = function(sensor_name, sensor_text, sensor_caption, sensor_color)
local status, err = pcall(create_remote_sensor, sensor_name, sensor_text, sensor_caption, sensor_color)
create_remote_sensor = function(sensor_data)
local status, err = pcall(create_remote_sensor, sensor_data)
if err then evogui.log({"err_generic", "remote.create_remote_sensor", err}) end
end,

Expand Down
29 changes: 24 additions & 5 deletions value_sensors/remote_sensor.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "template"

RemoteSensor = {}
function RemoteSensor.new(name, line, caption, color)
local sensor = ValueSensor.new("remote_sensor_" .. name)
sensor["line"] = line
sensor["display_name"] = caption
sensor["color"] = color
function RemoteSensor.new(sensor_data)
local sensor = ValueSensor.new("remote_sensor_" .. sensor_data.name)

sensor["mod_name"] = sensor_data.mod_name
sensor["line"] = sensor_data.text
sensor["display_name"] = {"", "[", sensor_data.mod_name, "] ", sensor_data.caption}
sensor["color"] = sensor_data.color

function sensor:set_line(text)
self.line = text
Expand All @@ -16,10 +18,27 @@ function RemoteSensor.new(name, line, caption, color)
end

ValueSensor.register(sensor)
if not global.remote_sensors then
global.remote_sensors = {}
end

-- store sensor data for global serialization
global.remote_sensors[sensor_data.name] = sensor_data
end

function RemoteSensor.get_by_name(name)
return ValueSensor.get_by_name("remote_sensor_" .. name)
end

function RemoteSensor.initialize()
-- Initialize any remote sensors that were previously saved
if global.remote_sensors then
for _, sensor_data in pairs(global.remote_sensors) do
if not RemoteSensor.get_by_name(sensor_data.name) then
RemoteSensor.new(sensor_data)
end
end
end
end

return RemoteSensor

0 comments on commit d3584bc

Please sign in to comment.