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
use std::path::PathBuf;
use anyhow::{Context, Error};
use log;
use hotg_rune_core::capabilities;
use crate::run::{
Accelerometer, Image, Raw, Sound, accelerometer::Samples,
image::ImageSource, multi::SourceBackedCapability, new_capability_switcher,
runecoral_inference, sound::AudioClip,
};
use hotg_rune_wasmer_runtime::Runtime;
use hotg_runicos_base_runtime::{BaseImage, CapabilityFactory, Random};
#[derive(Debug, Clone, PartialEq, structopt::StructOpt)]
pub struct Run {
rune: PathBuf,
#[structopt(short, long, default_value = "1")]
repeats: usize,
#[structopt(
long = "accelerometer",
aliases = &["accel"],
parse(from_os_str),
help = "A CSV file containing [X, Y, Z] vectors to be returned by the ACCEL capability"
)]
accelerometer: Vec<PathBuf>,
#[structopt(
long,
parse(from_os_str),
help = "A WAV file containing samples returned by the SOUND capability"
)]
sound: Vec<PathBuf>,
#[structopt(
long,
aliases = &["img"],
parse(from_os_str),
help = "An image to be returned by the IMAGE capability"
)]
image: Vec<PathBuf>,
#[structopt(
long,
parse(from_os_str),
help = "A file who's bytes will be returned as-is by the RAW capability"
)]
raw: Vec<PathBuf>,
#[structopt(
long,
aliases = &["rand"],
help = "Seed the runtime's Random Number Generator"
)]
random: Option<u64>,
#[structopt(
long,
env,
help = "The librunecoral.so library to use for hardware acceleration"
)]
librunecoral: Option<PathBuf>,
}
impl Run {
pub fn execute(self) -> Result<(), Error> {
log::info!("Running rune: {}", self.rune.display());
let rune = std::fs::read(&self.rune).with_context(|| {
format!("Unable to read \"{}\"", self.rune.display())
})?;
let env = self
.initialize_image()
.context("Unable to initialize the environment")?;
let mut runtime = Runtime::load(&rune, env)
.context("Unable to initialize the virtual machine")?;
for i in 0..self.repeats {
if i > 0 {
log::info!("Call {}", i + 1);
}
runtime.call().context("Call failed")?;
}
Ok(())
}
fn initialize_image(&self) -> Result<BaseImage, Error> {
let accelerometer = capability_switcher::<Accelerometer, _, _>(
&self.accelerometer,
|p| Samples::from_csv_file(p),
)?;
let image = capability_switcher::<Image, _, _>(&self.image, |p| {
ImageSource::from_file(p)
})?;
let raw = capability_switcher::<Raw, _, _>(&self.raw, |p| {
std::fs::read(p)
.with_context(|| format!("Unable to read \"{}\"", p.display()))
})?;
let sound = capability_switcher::<Sound, _, _>(&self.sound, |p| {
AudioClip::from_wav_file(p)
})?;
let mut img = BaseImage::with_defaults();
img.register_capability(capabilities::ACCEL, accelerometer)
.register_capability(capabilities::IMAGE, image)
.register_capability(capabilities::RAW, raw)
.register_capability(capabilities::SOUND, sound);
runecoral_inference::override_model_handler(
&mut img,
self.librunecoral.as_deref(),
)
.context("Unable to register the librunecoral inference backend")?;
if let Some(seed) = self.random {
img.register_capability(capabilities::RAND, move || {
Ok(Box::new(Random::seeded(seed))
as Box<dyn hotg_rune_runtime::Capability>)
});
}
Ok(img)
}
}
fn capability_switcher<C, T, F>(
items: &[T],
mut make_source: F,
) -> Result<impl CapabilityFactory, Error>
where
C: SourceBackedCapability,
F: FnMut(&T) -> Result<C::Source, Error>,
{
let mut sources = Vec::new();
for item in items {
let source = make_source(item)?;
sources.push(source);
}
Ok(new_capability_switcher::<C, _>(sources))
}