-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathplugin.rs
65 lines (49 loc) · 1.85 KB
/
plugin.rs
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
use std::{collections::HashMap, io::Write};
use rbx_xml::EncodeError;
use rbx_dom_weak::{RbxInstanceProperties, RbxTree, RbxValue};
static PLUGIN_TEMPLATE: &'static str = include_str!("plugin_main_template.lua");
pub struct RunInRbxPlugin<'a> {
pub port: u16,
pub server_id: &'a str,
pub lua_script: &'a str,
}
impl<'a> RunInRbxPlugin<'a> {
pub fn write<W: Write>(&self, output: W) -> Result<(), EncodeError> {
let tree = self.build_plugin();
let root_id = tree.get_root_id();
rbx_xml::to_writer_default(output, &tree, &[root_id])
}
fn build_plugin(&self) -> RbxTree {
let complete_source = PLUGIN_TEMPLATE
.replace("{{PORT}}", &self.port.to_string())
.replace("{{SERVER_ID}}", self.server_id);
let plugin_script = RbxInstanceProperties {
name: "run-in-roblox-plugin".to_owned(),
class_name: "Script".to_owned(),
properties: {
let mut properties = HashMap::new();
properties.insert(
"Source".to_owned(),
RbxValue::String {
value: complete_source,
},
);
properties
},
};
let main_source = format!("return function()\n{}\nend", self.lua_script);
let injected_main = RbxInstanceProperties {
name: "Main".to_owned(),
class_name: "ModuleScript".to_owned(),
properties: {
let mut properties = HashMap::new();
properties.insert("Source".to_owned(), RbxValue::String { value: main_source });
properties
},
};
let mut tree = RbxTree::new(plugin_script);
let root_id = tree.get_root_id();
tree.insert_instance(injected_main, root_id);
tree
}
}