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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use std::sync::{Arc, Mutex};

use super::{
    errors::PluginModuleError,
    memory_manager::{EcsAccessManager, EcsWorld},
    CommandResults,
};
use hashbrown::{HashMap, HashSet};
use wasmtime::{
    component::{Component, Linker},
    Config, Engine, Store,
};
use wasmtime_wasi::WasiView;

pub(crate) mod types_mod {
    wasmtime::component::bindgen!({
        path: "../../plugin/wit/veloren.wit",
        world: "common-types",
    });
}

wasmtime::component::bindgen!({
    path: "../../plugin/wit/veloren.wit",
    world: "plugin",
    with: {
        "veloren:plugin/types@0.0.1": types_mod::veloren::plugin::types,
        "veloren:plugin/information@0.0.1/entity": Entity,
    },
});

mod animation_plugin {
    wasmtime::component::bindgen!({
        path: "../../plugin/wit/veloren.wit",
        world: "animation-plugin",
        with: {
            "veloren:plugin/types@0.0.1": super::types_mod::veloren::plugin::types,
        },
    });
}

mod server_plugin {
    wasmtime::component::bindgen!({
        path: "../../plugin/wit/veloren.wit",
        world: "server-plugin",
        with: {
            "veloren:plugin/types@0.0.1": super::types_mod::veloren::plugin::types,
            "veloren:plugin/information@0.0.1/entity": super::Entity,
        },
    });
}

pub struct Entity {
    uid: common::uid::Uid,
}

pub use animation::Body;
use exports::veloren::plugin::animation;
pub use types_mod::veloren::plugin::types::{
    self, CharacterState, Dependency, Skeleton, Transform,
};
use veloren::plugin::{actions, information};

type StoreType = wasmtime::Store<WasiHostCtx>;

/// This enum abstracts over the different types of plugins we defined
enum PluginWrapper {
    Full(Plugin),
    Animation(animation_plugin::AnimationPlugin),
    Server(server_plugin::ServerPlugin),
}

impl PluginWrapper {
    fn load_event<S: wasmtime::AsContextMut>(
        &self,
        store: S,
        mode: common::resources::GameMode,
    ) -> wasmtime::Result<()>
    where
        <S as wasmtime::AsContext>::Data: std::marker::Send,
    {
        let mode = match mode {
            common::resources::GameMode::Server => types::GameMode::Server,
            common::resources::GameMode::Client => types::GameMode::Client,
            common::resources::GameMode::Singleplayer => types::GameMode::SinglePlayer,
        };
        match self {
            PluginWrapper::Full(pl) => pl.veloren_plugin_events().call_load(store, mode),
            PluginWrapper::Animation(pl) => pl.veloren_plugin_events().call_load(store, mode),
            PluginWrapper::Server(pl) => pl.veloren_plugin_events().call_load(store, mode),
        }
    }

    fn command_event<S: wasmtime::AsContextMut>(
        &self,
        store: S,
        name: &str,
        args: &[String],
        player: types::Uid,
    ) -> wasmtime::Result<Result<Vec<String>, String>>
    where
        <S as wasmtime::AsContext>::Data: std::marker::Send,
    {
        match self {
            PluginWrapper::Full(pl) => pl
                .veloren_plugin_server_events()
                .call_command(store, name, args, player),
            PluginWrapper::Animation(_) => Ok(Err("not implemented".into())),
            PluginWrapper::Server(pl) => pl
                .veloren_plugin_server_events()
                .call_command(store, name, args, player),
        }
    }

    fn player_join_event(
        &self,
        store: &mut StoreType,
        name: &str,
        uuid: (types::Uid, types::Uid),
    ) -> wasmtime::Result<types::JoinResult> {
        match self {
            PluginWrapper::Full(pl) => pl
                .veloren_plugin_server_events()
                .call_join(store, name, uuid),
            PluginWrapper::Animation(_) => Ok(types::JoinResult::None),
            PluginWrapper::Server(pl) => pl
                .veloren_plugin_server_events()
                .call_join(store, name, uuid),
        }
    }

    fn create_body(&self, store: &mut StoreType, bodytype: i32) -> Option<animation::Body> {
        match self {
            PluginWrapper::Full(pl) => {
                let body_iface = pl.veloren_plugin_animation().body();
                body_iface.call_constructor(store, bodytype).ok()
            },
            PluginWrapper::Animation(pl) => {
                let body_iface = pl.veloren_plugin_animation().body();
                body_iface.call_constructor(store, bodytype).ok()
            },
            PluginWrapper::Server(_) => None,
        }
    }

    fn update_skeleton(
        &self,
        store: &mut StoreType,
        body: animation::Body,
        dep: types::Dependency,
        time: f32,
    ) -> Option<types::Skeleton> {
        match self {
            PluginWrapper::Full(pl) => {
                let body_iface = pl.veloren_plugin_animation().body();
                body_iface.call_update_skeleton(store, body, dep, time).ok()
            },
            PluginWrapper::Animation(pl) => {
                let body_iface = pl.veloren_plugin_animation().body();
                body_iface.call_update_skeleton(store, body, dep, time).ok()
            },
            PluginWrapper::Server(_) => None,
        }
    }
}

/// This structure represent the WASM State of the plugin.
pub struct PluginModule {
    ecs: Arc<EcsAccessManager>,
    plugin: PluginWrapper,
    store: Mutex<wasmtime::Store<WasiHostCtx>>,
    #[allow(dead_code)]
    name: String,
}

struct WasiHostCtx {
    preview2_ctx: wasmtime_wasi::WasiCtx,
    preview2_table: wasmtime::component::ResourceTable,
    ecs: Arc<EcsAccessManager>,
    registered_commands: HashSet<String>,
    registered_bodies: HashMap<String, types::BodyIndex>,
}

impl wasmtime_wasi::WasiView for WasiHostCtx {
    fn table(&mut self) -> &mut wasmtime::component::ResourceTable { &mut self.preview2_table }

    fn ctx(&mut self) -> &mut wasmtime_wasi::WasiCtx { &mut self.preview2_ctx }
}

impl information::Host for WasiHostCtx {}

impl types::Host for WasiHostCtx {}

impl actions::Host for WasiHostCtx {
    fn register_command(&mut self, name: String) {
        tracing::info!("Plugin registers /{name}");
        self.registered_commands.insert(name);
    }

    fn player_send_message(&mut self, uid: actions::Uid, text: String) {
        tracing::info!("Plugin sends message {text} to player {uid:?}");
    }

    fn register_animation(&mut self, name: String, id: types::BodyIndex) {
        let _ = self.registered_bodies.insert(name, id);
    }
}

impl information::HostEntity for WasiHostCtx {
    fn find_entity(
        &mut self,
        uid: actions::Uid,
    ) -> Result<wasmtime::component::Resource<information::Entity>, types::Error> {
        self.table()
            .push(Entity {
                uid: common::uid::Uid(uid),
            })
            .map_err(|_err| types::Error::RuntimeError)
    }

    fn health(
        &mut self,
        self_: wasmtime::component::Resource<information::Entity>,
    ) -> Result<information::Health, types::Error> {
        let uid = self
            .table()
            .get(&self_)
            .map_err(|_err| types::Error::RuntimeError)?
            .uid;
        // Safety: No reference is leaked out the function so it is safe.
        let world = unsafe { self.ecs.get().ok_or(types::Error::EcsPointerNotAvailable)? };
        let player = world
            .id_maps
            .uid_entity(uid)
            .ok_or(types::Error::EcsEntityNotFound)?;
        world
            .health
            .get(player)
            .map(|health| information::Health {
                current: health.current(),
                base_max: health.base_max(),
                maximum: health.maximum(),
            })
            .ok_or(types::Error::EcsComponentNotFound)
    }

    fn name(
        &mut self,
        self_: wasmtime::component::Resource<information::Entity>,
    ) -> Result<String, types::Error> {
        let uid = self
            .table()
            .get(&self_)
            .map_err(|_err| types::Error::RuntimeError)?
            .uid;
        // Safety: No reference is leaked out the function so it is safe.
        let world = unsafe { self.ecs.get().ok_or(types::Error::EcsPointerNotAvailable)? };
        let player = world
            .id_maps
            .uid_entity(uid)
            .ok_or(types::Error::EcsEntityNotFound)?;
        Ok(world
            .player
            .get(player)
            .ok_or(types::Error::EcsComponentNotFound)?
            .alias
            .to_owned())
    }

    fn drop(
        &mut self,
        rep: wasmtime::component::Resource<information::Entity>,
    ) -> wasmtime::Result<()> {
        Ok(self.table().delete(rep).map(|_entity| ())?)
    }
}

struct InfoStream(String);

impl wasmtime_wasi::HostOutputStream for InfoStream {
    fn write(&mut self, bytes: bytes::Bytes) -> wasmtime_wasi::StreamResult<()> {
        tracing::info!("{}: {}", self.0, String::from_utf8_lossy(bytes.as_ref()));
        Ok(())
    }

    fn flush(&mut self) -> wasmtime_wasi::StreamResult<()> { Ok(()) }

    fn check_write(&mut self) -> wasmtime_wasi::StreamResult<usize> { Ok(1024) }
}

#[wasmtime_wasi::async_trait]
impl wasmtime_wasi::Subscribe for InfoStream {
    async fn ready(&mut self) {}
}

struct ErrorStream(String);

impl wasmtime_wasi::HostOutputStream for ErrorStream {
    fn write(&mut self, bytes: bytes::Bytes) -> wasmtime_wasi::StreamResult<()> {
        tracing::error!("{}: {}", self.0, String::from_utf8_lossy(bytes.as_ref()));
        Ok(())
    }

    fn flush(&mut self) -> wasmtime_wasi::StreamResult<()> { Ok(()) }

    fn check_write(&mut self) -> wasmtime_wasi::StreamResult<usize> { Ok(1024) }
}

#[wasmtime_wasi::async_trait]
impl wasmtime_wasi::Subscribe for ErrorStream {
    async fn ready(&mut self) {}
}

struct LogStream(String, tracing::Level);

impl wasmtime_wasi::StdoutStream for LogStream {
    fn stream(&self) -> Box<dyn wasmtime_wasi::HostOutputStream> {
        if self.1 == tracing::Level::INFO {
            Box::new(InfoStream(self.0.clone()))
        } else {
            Box::new(ErrorStream(self.0.clone()))
        }
    }

    fn isatty(&self) -> bool { true }
}

impl PluginModule {
    /// This function takes bytes from a WASM File and compile them
    pub fn new(name: String, wasm_data: &[u8]) -> Result<Self, PluginModuleError> {
        let ecs = Arc::new(EcsAccessManager::default());

        // configure the wasm runtime
        let mut config = Config::new();
        config.wasm_component_model(true);

        let engine = Engine::new(&config).map_err(PluginModuleError::Wasmtime)?;
        // create a WASI environment (std implementing system calls)
        let wasi = wasmtime_wasi::WasiCtxBuilder::new()
            .stdout(LogStream(name.clone(), tracing::Level::INFO))
            .stderr(LogStream(name.clone(), tracing::Level::ERROR))
            .build();
        let host_ctx = WasiHostCtx {
            preview2_ctx: wasi,
            preview2_table: wasmtime_wasi::ResourceTable::new(),
            ecs: Arc::clone(&ecs),
            registered_commands: HashSet::new(),
            registered_bodies: HashMap::new(),
        };
        // the store contains all data of a wasm instance
        let mut store = Store::new(&engine, host_ctx);

        // load wasm from binary
        let module =
            Component::from_binary(&engine, wasm_data).map_err(PluginModuleError::Wasmtime)?;

        // register WASI and Veloren methods with the runtime
        let mut linker = Linker::new(&engine);
        wasmtime_wasi::add_to_linker_sync(&mut linker).map_err(PluginModuleError::Wasmtime)?;
        Plugin::add_to_linker(&mut linker, |x| x).map_err(PluginModuleError::Wasmtime)?;

        let instance_fut = linker.instantiate(&mut store, &module);
        let instance = (instance_fut).map_err(PluginModuleError::Wasmtime)?;

        let plugin = match Plugin::new(&mut store, &instance) {
            Ok(pl) => Ok(PluginWrapper::Full(pl)),
            Err(_) => match animation_plugin::AnimationPlugin::new(&mut store, &instance) {
                Ok(pl) => Ok(PluginWrapper::Animation(pl)),
                Err(_) => server_plugin::ServerPlugin::new(&mut store, &instance)
                    .map(PluginWrapper::Server),
            },
        }
        .map_err(PluginModuleError::Wasmtime)?;

        Ok(Self {
            plugin,
            ecs,
            store: store.into(),
            name,
        })
    }

    pub fn name(&self) -> &str { &self.name }

    // Implementation of the commands called from veloren and provided in plugins
    pub fn load_event(
        &mut self,
        ecs: &EcsWorld,
        mode: common::resources::GameMode,
    ) -> Result<(), PluginModuleError> {
        self.ecs
            .execute_with(ecs, || {
                self.plugin.load_event(self.store.get_mut().unwrap(), mode)
            })
            .map_err(PluginModuleError::Wasmtime)
    }

    pub fn command_event(
        &mut self,
        ecs: &EcsWorld,
        name: &str,
        args: &[String],
        player: common::uid::Uid,
    ) -> Result<Vec<String>, CommandResults> {
        if !self
            .store
            .get_mut()
            .unwrap()
            .data()
            .registered_commands
            .contains(name)
        {
            return Err(CommandResults::UnknownCommand);
        }
        self.ecs.execute_with(ecs, || {
            match self
                .plugin
                .command_event(self.store.get_mut().unwrap(), name, args, player.0)
            {
                Err(err) => Err(CommandResults::HostError(err)),
                Ok(result) => result.map_err(CommandResults::PluginError),
            }
        })
    }

    pub fn player_join_event(
        &mut self,
        ecs: &EcsWorld,
        name: &str,
        uuid: common::uuid::Uuid,
    ) -> types::JoinResult {
        self.ecs.execute_with(ecs, || {
            match self.plugin.player_join_event(
                self.store.get_mut().unwrap(),
                name,
                uuid.as_u64_pair(),
            ) {
                Ok(value) => {
                    tracing::info!("JoinResult {value:?}");
                    value
                },
                Err(err) => {
                    tracing::error!("join_event: {err:?}");
                    types::JoinResult::None
                },
            }
        })
    }

    pub fn create_body(&mut self, bodytype: &str) -> Option<animation::Body> {
        let store = self.store.get_mut().unwrap();
        let bodytype = store.data().registered_bodies.get(bodytype).copied();
        bodytype.and_then(|bd| self.plugin.create_body(store, bd))
    }

    pub fn update_skeleton(
        &mut self,
        body: &animation::Body,
        dep: &types::Dependency,
        time: f32,
    ) -> Option<types::Skeleton> {
        self.plugin
            .update_skeleton(self.store.get_mut().unwrap(), *body, *dep, time)
    }
}