-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
101 lines (85 loc) · 2.9 KB
/
Copy pathcontrol.lua
File metadata and controls
101 lines (85 loc) · 2.9 KB
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
-- Container Cargo Ship - runtime integration with Cargo Ships
-- Defensive, crash-free.
-- Register on the first post-init/configuration tick so Cargo Ships has already
-- finished rebuilding its ship database.
local RETRY_INTERVAL_TICKS = 1
local MOD_NAME = "container-cargo-ship"
local CARGO_SHIPS_MOD_NAME = "cargo-ships"
local CARGO_SHIPS_INTERFACE_NAME = "cargo-ships"
local SHIP_NAME = "container_cargo_ship"
local INDEP_SHIP_NAME = "indep-container_cargo_ship"
local function ensure_global()
global = global or {}
global.schema = global.schema or {}
global.schema.version = global.schema.version or 1
global.schema.registered = global.schema.registered or false
global.schema.register_attempts = global.schema.register_attempts or 0
end
local function try_register()
ensure_global()
if global.schema.registered then return true end
-- Wait until Cargo Ships interface is available
local cargo_ships_interface = remote and remote.interfaces and remote.interfaces[CARGO_SHIPS_INTERFACE_NAME]
if not (cargo_ships_interface and cargo_ships_interface.add_ship and cargo_ships_interface.add_boat) then
return false
end
global.schema.register_attempts = (global.schema.register_attempts or 0) + 1
local ok_ship = pcall(function()
remote.call(CARGO_SHIPS_INTERFACE_NAME, "add_ship", {
name = SHIP_NAME,
engine = "cargo_ship_engine",
engine_scale = 1,
engine_at_front = false,
})
end)
local ok_boat = pcall(function()
remote.call(CARGO_SHIPS_INTERFACE_NAME, "add_boat", {
name = INDEP_SHIP_NAME,
rail_version = SHIP_NAME,
})
end)
if ok_ship and ok_boat then
global.schema.registered = true
return true
end
return false
end
local function stop_retry_handler()
script.on_nth_tick(RETRY_INTERVAL_TICKS, nil)
end
local function retry_register_on_nth_tick(_)
ensure_global()
if try_register() then
stop_retry_handler()
end
end
local function ensure_registered_with_retry()
ensure_global()
global.schema.registered = false
script.on_nth_tick(RETRY_INTERVAL_TICKS, retry_register_on_nth_tick)
end
local function configuration_change_affects_registration(event)
local mod_changes = event and event.mod_changes
if not mod_changes then
return true
end
return mod_changes[MOD_NAME] ~= nil or mod_changes[CARGO_SHIPS_MOD_NAME] ~= nil
end
script.on_init(function()
ensure_registered_with_retry()
end)
script.on_load(function()
if global and global.schema and not global.schema.registered then
script.on_nth_tick(RETRY_INTERVAL_TICKS, retry_register_on_nth_tick)
end
end)
script.on_configuration_changed(function(event)
ensure_global()
-- Re-register only when this mod or Cargo Ships actually changed.
if not configuration_change_affects_registration(event) then
return
end
-- Re-register in case Cargo Ships or this mod was updated/reset.
global.schema.registered = false
ensure_registered_with_retry()
end)