mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +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
@@ -1,4 +1,5 @@
|
||||
const plugin = require("lib/plugin");
|
||||
const luaTools = require("lib/luaTools");
|
||||
|
||||
|
||||
/**
|
||||
@@ -8,20 +9,6 @@ function removeTags(content) {
|
||||
return content.replace(/(\[gps=-?\d+,-?\d+\]|\[train=\d+\])/g, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes a string for inclusion into a lua string
|
||||
*/
|
||||
function escapeLuaString(content) {
|
||||
return content
|
||||
.replace(/\\/g, "\\\\")
|
||||
.replace(/"/g, '\\"')
|
||||
.replace(/'/g, "\\'")
|
||||
.replace(/\0/g, "\\0")
|
||||
.replace(/\n/g, "\\n")
|
||||
.replace(/\r/g, "\\r")
|
||||
;
|
||||
}
|
||||
|
||||
class InstancePlugin extends plugin.BaseInstancePlugin {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
@@ -34,7 +21,7 @@ class InstancePlugin extends plugin.BaseInstancePlugin {
|
||||
async chatEventHandler(message) {
|
||||
// TODO check if cross server chat is enabled
|
||||
let content = `[${message.data.instance_name}] ${removeTags(message.data.content)}`;
|
||||
await this.instance.server.sendRcon(`/sc game.print('${escapeLuaString(content)}')`, true);
|
||||
await this.instance.server.sendRcon(`/sc game.print('${luaTools.escapeString(content)}')`, true);
|
||||
}
|
||||
|
||||
async onOutput(output) {
|
||||
@@ -54,5 +41,4 @@ module.exports = {
|
||||
|
||||
// For testing only
|
||||
_removeTags: removeTags,
|
||||
_escapeLuaString: escapeLuaString,
|
||||
}
|
||||
Reference in new issue
Block a user