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 {
    /// The Rune to run.
    rune: PathBuf,
    /// The number of times to execute this rune
    #[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> {
        // Load the source files for each kind of Capability and create a
        // CapabilityFactory which will instantiate Capabilities depending on
        // the "source" index provided as a capability parameter.
        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)
    }
}

/// Create a new [`CapabilityFactory`] which uses the `"source"` parameter set
/// by a Rune at runtime to switch between inputs within the same type of
/// capability.
///
/// For example, imagine passing the path for 3 images to the `rune` CLI.
/// Inside the Rune, we'll instantiate a [`Capability`] object and set the
/// `"source"` parameter to `1`. This then tells the [`CapabilityFactory`] that
/// we want to read from the second image.
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))
}