mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Add clusterio module with API for lua code
Add a shared API for lua code through a module named clusterio.
This commit is contained in:
1 parent
221dc855f4
commit
66aa7e1e43
9 files changed
+139
-43
No files matched your search
@@ -0,0 +1,17 @@
|
||||
local api = {}
|
||||
|
||||
|
||||
-- Table of events raised by clusterio
|
||||
api.events = {
|
||||
-- Raised after the name and id of an instance has been updated
|
||||
-- This may occur even if the id and name didn't change.
|
||||
|
||||
-- Event data:
|
||||
-- instance_id: The id of the instance.
|
||||
-- instance_name: the name of the instance.
|
||||
on_instance_updated = script.generate_event_name(),
|
||||
}
|
||||
|
||||
|
||||
return api
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
local api = require('modules/clusterio/api')
|
||||
|
||||
|
||||
local impl = {}
|
||||
|
||||
local function init()
|
||||
if not global.clusterio then
|
||||
global.clusterio = {
|
||||
instance_id = nil,
|
||||
instance_name = nil,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
-- Internal API
|
||||
clusterio_private = {}
|
||||
function clusterio_private.update_instance(new_id, new_name)
|
||||
init()
|
||||
global.clusterio.instance_id = new_id
|
||||
global.clusterio.instance_name = new_name
|
||||
script.raise_event(api.events.on_instance_updated, {
|
||||
instance_id = new_id,
|
||||
instance_name = new_name,
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
function impl.add_remote_interface()
|
||||
remote.add_interface('clusterio_api', {
|
||||
-- Returns a table of events raised by clusterio
|
||||
get_events = function()
|
||||
return api.events
|
||||
end,
|
||||
|
||||
-- Returns the instance id the game is run under
|
||||
-- This may change over time and/or be nil.
|
||||
get_instance_id = function()
|
||||
init()
|
||||
return global.clusterio.instance_id
|
||||
end,
|
||||
|
||||
-- Returns the name of the instance the game runs under.
|
||||
-- This may change over time and/or be nil.
|
||||
get_instance_name = function()
|
||||
init()
|
||||
return global.clusterio.instance_name
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
return impl
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "clusterio",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {},
|
||||
"load": ["impl.lua"]
|
||||
}
|
||||
Reference in new issue
Block a user