Skip to content

Commit

Permalink
Fix accidental event handler overwrite
Browse files Browse the repository at this point in the history
- This caused Scout missions to be unable to be completed, as well as causing issues with the hyperjump planner.
- The constraint on the new Event API is that a module may only register 1 handler per event per file, for the purpose of automatically overwriting the event handler when the module code is hot-reloaded.
  • Loading branch information
sturnclaw authored and Webster Sheets committed Jan 27, 2024
1 parent c647754 commit 216704b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions data/libs/Event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function Event:Register(name, module, func)
for _, cb in ipairs(self.callbacks[name]) do
-- Update registered callback
if cb.module == module then
logWarning("Module {} overwriting event callback {}" % { module, func })
cb.func = func
return
end
Expand Down
24 changes: 14 additions & 10 deletions data/modules/Scout/Scout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,19 @@ local onScanComplete = function (player, scanId)
mission.client.name)
end
end

mission.location = newlocation
Game.player:SetHyperspaceTarget(mission.location:GetStarSystem().path)

if Game.system and mission.location:IsSameSystem(Game.system.path) then
Game.player:SetNavTarget(mission.location)
else
Game.player:SetHyperspaceTarget(mission.location:SystemOnly())
end
end


---@param player Player
---@param station SpaceStation
local onShipDocked = function (player, station)
if not player:IsPlayer() then return end
local scanMgr = player:GetComponent("ScanManager")
Expand Down Expand Up @@ -670,6 +677,11 @@ end
local loaded_data

local onGameStart = function ()
-- If we loaded a saved game, the player may have a ScanManager component already
if not Game.player:GetComponent("ScanManager") then
Game.player:SetComponent("ScanManager", ScanManager.New(Game.player))
end

ads = {}
missions = {}
missionKey = {}
Expand Down Expand Up @@ -765,26 +777,18 @@ local onGameEnd = function ()
nearbysystems = nil
end

Event.Register("onGameStart", onGameStart)
Event.Register("onGameEnd", onGameEnd)
Event.Register("onCreateBB", onCreateBB)
Event.Register("onUpdateBB", onUpdateBB)
Event.Register("onEnterSystem", onEnterSystem)
Event.Register("onShipDocked", onShipDocked)
Event.Register("onGameStart", onGameStart)

Event.Register("onScanRangeEnter", onScanRangeEnter)
Event.Register("onScanRangeExit", onScanRangeExit)
Event.Register("onScanPaused", onScanPaused)
Event.Register("onScanComplete", onScanComplete)

-- Ensure the player ship has a ScanManager available
Event.Register('onGameStart', function()
-- If we loaded a saved game, the player may have a ScanManager component already
if not Game.player:GetComponent("ScanManager") then
Game.player:SetComponent("ScanManager", ScanManager.New(Game.player))
end
end)

Mission.RegisterType('Scout', l.MAPPING, buildMissionDescription)

Serializer:Register("Scout", serialize, unserialize)
20 changes: 10 additions & 10 deletions data/pigui/views/map-sector-view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ end})

Event.Register("onGameStart", onGameStart)
Event.Register("onEnterSystem", function()
hyperJumpPlanner.onEnterSystem()
hyperspaceDetailsCache = {}
end)

Expand All @@ -454,18 +455,17 @@ Event.Register("onGameEnd", function()
searchString = ""
systemPaths = nil
leftBarMode = "SEARCH"
end)

-- events moved from hyperJumpPlanner
Event.Register("onGameEnd", hyperJumpPlanner.onGameEnd)
Event.Register("onEnterSystem", hyperJumpPlanner.onEnterSystem)
Event.Register("onShipEquipmentChanged", hyperJumpPlanner.onShipEquipmentChanged)
hyperJumpPlanner.onGameEnd()
end)

local function clearHyperspaceCache(ship)
if ship and ship == player then hyperspaceDetailsCache = {} end
end
Event.Register("onShipEquipmentChanged", function(ship, ...)
if ship:IsPlayer() then hyperspaceDetailsCache = {} end
hyperJumpPlanner.onShipEquipmentChanged(ship, ...)
end)

Event.Register("onShipEquipmentChanged", clearHyperspaceCache)
Event.Register("onShipTypeChanged", clearHyperspaceCache)
Event.Register("onShipTypeChanged", function(ship, ...)
if ship:IsPlayer() then hyperspaceDetailsCache = {} end
end)

return {}

0 comments on commit 216704b

Please sign in to comment.