oxygengine_script_web/integration/
core.rs1use crate::{
2 interface::{ResourceAccess, ResourceModify},
3 json_asset_protocol::JsonAsset,
4 scriptable::{ScriptableMap, ScriptableValue},
5};
6use core::prelude::*;
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
10pub struct AppLifeCycleScripted {
11 pub running: bool,
12 pub delta_time_seconds: Scalar,
13 pub current_state_token: StateToken,
14}
15
16impl From<&AppLifeCycle> for AppLifeCycleScripted {
17 fn from(value: &AppLifeCycle) -> Self {
18 Self {
19 running: value.running,
20 delta_time_seconds: value.delta_time_seconds(),
21 current_state_token: value.current_state_token(),
22 }
23 }
24}
25
26impl ResourceModify<AppLifeCycleScripted> for AppLifeCycle {
27 fn modify_resource(&mut self, source: AppLifeCycleScripted) {
28 self.running = source.running;
29 }
30}
31
32impl ResourceAccess for AppLifeCycle {
33 fn access_resource(&mut self, _value: ScriptableValue) -> ScriptableValue {
34 ScriptableValue::Null
35 }
36}
37
38#[derive(Debug, Default, Clone, Serialize, Deserialize)]
39pub struct AssetsDatabaseLoaderScripted {
40 pub ready: bool,
41 pub loaded_count: usize,
42 pub loaded_paths: Vec<String>,
43 pub loading_count: usize,
44 pub loading_paths: Vec<String>,
45 pub lately_loaded_paths: Vec<String>,
46 pub lately_unloaded_paths: Vec<String>,
47}
48
49impl From<&AssetsDatabase> for AssetsDatabaseLoaderScripted {
50 fn from(value: &AssetsDatabase) -> Self {
51 Self {
52 ready: value.is_ready(),
53 loaded_count: value.loaded_count(),
54 loaded_paths: value.loaded_paths(),
55 loading_count: value.loading_count(),
56 loading_paths: value.loading_paths(),
57 lately_loaded_paths: value
58 .lately_loaded_paths()
59 .map(|p| p.to_owned())
60 .collect::<Vec<_>>(),
61 lately_unloaded_paths: value
62 .lately_unloaded_paths()
63 .map(|p| p.to_owned())
64 .collect::<Vec<_>>(),
65 }
66 }
67}
68
69impl ResourceModify<AssetsDatabaseLoaderScripted> for AssetsDatabase {
70 fn modify_resource(&mut self, _: AssetsDatabaseLoaderScripted) {}
71}
72
73#[derive(Debug, Default, Clone, Serialize, Deserialize)]
74pub struct AssetsDatabaseScripted {
75 #[serde(default)]
76 pub load: Option<Vec<String>>,
77 #[serde(default)]
78 pub unload: Option<Vec<String>>,
79}
80
81impl From<&AssetsDatabase> for AssetsDatabaseScripted {
82 fn from(_: &AssetsDatabase) -> Self {
83 Self {
84 load: None,
85 unload: None,
86 }
87 }
88}
89
90impl ResourceModify<AssetsDatabaseScripted> for AssetsDatabase {
91 fn modify_resource(&mut self, _source: AssetsDatabaseScripted) {}
92}
93
94impl ResourceAccess for AssetsDatabase {
95 fn access_resource(&mut self, value: ScriptableValue) -> ScriptableValue {
96 match value {
97 ScriptableValue::Object(object) => {
98 if let Some(ScriptableValue::Array(load)) = object.get("load") {
99 for path in load {
100 if let ScriptableValue::String(path) = path {
101 drop(self.load(&path));
102 }
103 }
104 return ScriptableValue::Bool(true);
105 } else if let Some(ScriptableValue::Array(load)) = object.get("unload") {
106 for path in load {
107 if let ScriptableValue::String(path) = path {
108 self.remove_by_path(&path);
109 }
110 }
111 return ScriptableValue::Bool(true);
112 } else if let Some(ScriptableValue::String(path)) = object.get("serve-pack") {
113 if let Some(asset) = self.asset_by_path(path) {
114 if let Some(pack) = asset.get::<PackAsset>() {
115 let engine = pack.make_fetch_engine();
116 self.push_fetch_engine(Box::new(engine));
117 return ScriptableValue::Bool(true);
118 }
119 }
120 } else if let Some(ScriptableValue::Array(paths)) = object.get("loaded") {
121 let map = paths
122 .iter()
123 .filter_map(|path| {
124 if let ScriptableValue::String(path) = path {
125 let path = path.to_string();
126 let result =
127 ScriptableValue::Bool(self.id_by_path(&path).is_some());
128 Some((path, result))
129 } else {
130 None
131 }
132 })
133 .collect::<ScriptableMap<_, _>>();
134 return ScriptableValue::Object(map);
135 }
136 }
137 ScriptableValue::Array(paths) => {
138 let map = paths
139 .into_iter()
140 .filter_map(|path| {
141 let path = path.to_string();
142 if let Some(asset) = self.asset_by_path(&path) {
143 if let Some(protocol) = path.split("://").next() {
144 let data = match protocol {
145 "text" => ScriptableValue::String(
146 asset.get::<TextAsset>().unwrap().get().to_owned(),
147 ),
148 "json" => asset.get::<JsonAsset>().unwrap().get().clone(),
149 "binary" => ScriptableValue::Array(
150 asset
151 .get::<BinaryAsset>()
152 .unwrap()
153 .get()
154 .iter()
155 .map(|byte| ScriptableValue::Number((*byte).into()))
156 .collect::<Vec<_>>(),
157 ),
158 _ => return None,
159 };
160 return Some((path, data));
161 }
162 }
163 None
164 })
165 .collect::<ScriptableMap<_, _>>();
166 return ScriptableValue::Object(map);
167 }
168 _ => {}
169 }
170 ScriptableValue::Null
171 }
172}