-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_init.rs
More file actions
113 lines (92 loc) · 3.24 KB
/
Copy pathtest_init.rs
File metadata and controls
113 lines (92 loc) · 3.24 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
102
103
104
105
106
107
108
109
110
111
112
113
use extactor_lua_embedding::{
script::storage::BoxedStorage, script_loader::ScriptRegister, UnityBundle,
};
use mlua::{Function, Lua, LuaOptions};
use extactor_lua_embedding::script_manager::ScriptManager;
use std::{cell::RefCell, collections::HashMap, convert::Infallible, path::Path, rc::Rc};
#[test]
fn test() {
let script = Path::new("test_lua/test_init.lua");
let storage = Rc::new(MapStorage(RefCell::new(HashMap::new()))) as BoxedStorage;
let lua = Lua::new();
lua.load(script).exec().unwrap();
let init: Function = lua.globals().get("InitScript").unwrap();
// init script
let ret: ScriptRegister = init.call(()).unwrap();
println!("{:?}", ret);
// script and config define
let script = ret.to_script(&storage);
let define = ret.to_config_define();
println!("{:#?}", script);
// start config update
script.configs.iter().for_each(|(k, v)| {
define.update(&script.storage, &k, v.kind.clone()).unwrap();
});
script
.entry_point
.update_config(&lua, script.configs.iter().map(|(k, v)| (&**k, v.value())))
.unwrap();
// user select a unity bundle
let map = script
.entry_point
.get_matched_scripts(UnityBundle::default())
.unwrap()
.unwrap();
println!("{map:?}");
// user using function "A"
let entry = map.get("A").unwrap();
// run function A
let _: () = entry
.call(script.get_config(), UnityBundle::default(), ())
.unwrap();
}
#[test]
fn wrap_init() {
let script = Path::new("test_lua/test_init.lua");
let storage = Rc::new(MapStorage(RefCell::new(HashMap::new()))) as BoxedStorage;
let mut script_manager = ScriptManager::new(LuaOptions::new(), storage).unwrap();
// load script
println!("load script");
script_manager.load_entry(&script).unwrap();
// init script
println!("init script");
script_manager.init_script().unwrap();
//init done
// user select item
let scripts = script_manager
.get_match_scripts(UnityBundle::default())
.unwrap();
for key in scripts.keys() {
println!("match key:{key}")
}
// user press key A
scripts.call_by_key("A", UnityBundle::default()).unwrap();
scripts.call_by_key("B", UnityBundle::default()).unwrap();
println!("done")
}
#[derive(Debug)]
struct MapStorage(RefCell<HashMap<String, Box<[u8]>>>);
impl extactor_lua_embedding::script::storage::Storage for MapStorage {
fn contains_key(&self, script: &str, key: &str) -> Result<bool, Infallible> {
Ok(self.0.borrow().contains_key(&format!("{script}.{key}")))
}
fn load(&self, script: &str, key: &str) -> Result<Option<Vec<u8>>, Infallible> {
println!("get{script}.{key}",);
Ok(self
.0
.borrow()
.get(&format!("{script}.{key}"))
.map(|v| v.clone().into_vec()))
}
fn store(&self, script: &str, key: &str, value: &[u8]) -> Result<(), Infallible> {
println!(
"save {script}.{key}, value: {}",
String::from_utf8_lossy(value)
);
self.0
.borrow_mut()
.insert(format!("{script}.{key}"), value.to_vec().into_boxed_slice());
Ok(())
}
type Error = Infallible;
}