Basic groundwork for viperid, a fantasy console

This commit is contained in:
2024-04-21 22:10:48 -07:00
commit 13b61a6def
23 changed files with 1541 additions and 0 deletions

1
crates/editor/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

24
crates/editor/Cargo.lock generated Normal file
View File

@ -0,0 +1,24 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
[[package]]
name = "editor"
version = "0.1.0"
dependencies = [
"anyhow",
"shared",
]
[[package]]
name = "shared"
version = "0.1.0"
dependencies = [
"anyhow",
]

11
crates/editor/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "editor"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.82"
player = { path = "../player" }
viperid = { path = "../.." }

View File

@ -0,0 +1,80 @@
use std::{fs::File, io::Read, path::{Path, PathBuf}, process::Command};
use viperid::VResult;
pub struct GoBuilder {
project_directory: PathBuf,
entry_point: PathBuf
}
impl GoBuilder {
pub fn new(project_directory: &Path) -> VResult<GoBuilder> {
// make sure Go is a supported version
let output = Command::new("go").arg("version").output()?;
if !output.status.success() {
anyhow::bail!("Go is not installed or did not start: {:?}", output.status)
}
// TODO: Support more Go versions
let text = output.stdout;
if !text.starts_with(b"go version go1.21") {
anyhow::bail!("unsupported Go version (must be 1.21): {}", String::from_utf8_lossy(&text).trim())
}
// make sure the project is really here
if !project_directory.exists() {
anyhow::bail!("project directory must exist: {}", project_directory.to_string_lossy())
}
let entry_point = project_directory.join("main.go");
if !entry_point.exists() {
anyhow::bail!("entry point must exist: {}", entry_point.to_string_lossy())
}
// OK, we should be able to do things with Go without any errors
Ok(GoBuilder {
project_directory: PathBuf::from(project_directory),
entry_point
})
}
pub fn build(&self) -> VResult<Vec<u8>> {
// NOTE: We've checked that the project directory exists
if !self.project_directory.exists() {
anyhow::bail!(
"project directory disappeared: {}",
self.project_directory.to_string_lossy()
);
}
if !self.entry_point.exists() {
anyhow::bail!(
"entry point disappeared: {}",
self.entry_point.to_string_lossy()
);
}
let project_directory = &self.project_directory;
let entry_point = &self.entry_point;
let build_directory = project_directory.join("build");
let wasm_name = build_directory.join("game.wasm");
let mut child = Command::new("go")
.args(["build", "-o"]).arg(&wasm_name)
.env("GOOS", "wasip1")
.env("GOARCH", "wasm")
.arg("-trimpath")
.arg(entry_point)
.spawn()?;
let status = child.wait()?;
if !status.success() {
anyhow::bail!("go failed");
}
let mut buf = vec![];
File::open(wasm_name)?.read_to_end(&mut buf)?;
Ok(buf)
}
}

14
crates/editor/src/main.rs Normal file
View File

@ -0,0 +1,14 @@
use std::path::PathBuf;
use viperid::VResult;
use crate::go_builder::GoBuilder;
mod go_builder;
fn main() -> VResult<()> {
let builder = GoBuilder::new(&PathBuf::from("example_project"))?;
let wasm = builder.build()?;
player::run_entry_point(&wasm)?;
Ok(())
}