diff --git a/VERSION b/VERSION index f905682..cb498ab 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.7 +0.4.8 diff --git a/control.lua b/control.lua index b015c73..cc59110 100644 --- a/control.lua +++ b/control.lua @@ -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 @@ -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 diff --git a/evoGUI.lua b/evoGUI.lua index 7df027f..59606dc 100644 --- a/evoGUI.lua +++ b/evoGUI.lua @@ -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 diff --git a/locale/en/locale.cfg b/locale/en/locale.cfg index a0c9a34..12f4337 100644 --- a/locale/en/locale.cfg +++ b/locale/en/locale.cfg @@ -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. diff --git a/remote.lua b/remote.lua index c8968e5..0d18f47 100644 --- a/remote.lua +++ b/remote.lua @@ -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 -- @@ -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, diff --git a/value_sensors/remote_sensor.lua b/value_sensors/remote_sensor.lua index b23bdb0..fa13dc2 100644 --- a/value_sensors/remote_sensor.lua +++ b/value_sensors/remote_sensor.lua @@ -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 @@ -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