Skip to content

Commit

Permalink
Add support for compiling WebAssembly wasm32-unknown-unknown target (#92
Browse files Browse the repository at this point in the history
)

Note this only is the first step in web support, although the project compiles, it doesn't run!

Merging now to avoid branch divergence, until dependencies can be updated for wasm support.

* Add instructions to build for wasm32-unknown-unknown with wasm-pack in www/

* Update to rust-clipboard fork to compile with emscripten

aweinstock314/rust-clipboard#62

* Exclude reqwest dependency in wasm32

* Exclude compiling clipboard pasting on wasm32

* Exclude reqwest-using code from wasm32

* Install wasm target with rustup in Travis CI

* Update to collision 0.19.0

Fixes wasm incompatibility in deprecated rustc-serialize crate: rustgd/collision-rs#106

error[E0046]: not all trait items implemented, missing: `encode`
    --> github.aaakk.us.kg-1ecc6299db9ec823/rustc-serialize-0.3.24/src/serialize.rs:1358:1

* Increase travis_wait time even further, try 120 minutes

* Set RUST_BACKTRACE=1 in main

* Remove unused unneeded bzip2 features in zip crate

To fix wasm32-unknown-unknown target compile error:
error[E0432]: unresolved imports `libc::c_int`, `libc::c_uint`, `libc::c_void`, `libc::c_char`
 --> src/github.aaakk.us.kg-1ecc6299db9ec823/bzip2-sys-0.1.7/lib.rs:5:12
  |
5 | use libc::{c_int, c_uint, c_void, c_char};
  |            ^^^^^  ^^^^^^  ^^^^^^  ^^^^^^ no `c_char` in the root
  |            |      |       |
  |            |      |       no `c_void` in the root
  |            |      no `c_uint` in the root
  |            no `c_int` in the root

* flate2 use Rust backend

* Add console_error_panic_hook module for wasm backtraces

* Build using wasm-pack, wasm-bindgen, run with wasm-app

* Update to miniz_oxide 0.2.1, remove patch for Frommi/miniz_oxide#42

* Update to official clipboard crate since aweinstock314/rust-clipboard#62 was merged, but git revision pending release

* Update to branch of glutin attempting to build for wasm

iceiix/glutin#1

* Update winit dependency of glutin to git master

iceiix/winit#2

* Update to glutin branch with working (compiles, doesn't run) wasm_stub

* Add app name in title on web page

* Add wasm to Travis-CI test matrix

* Update glutin to fix Windows EGL compilation on AppVeyor

iceiix/glutin@9779735
  • Loading branch information
iceiix authored Mar 3, 2019
1 parent 13fbbaa commit 2f2f358
Show file tree
Hide file tree
Showing 21 changed files with 5,597 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
target/
.rust/
working/
*.log
pkg

# IntelliJ
.idea
Expand Down
18 changes: 15 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@ matrix:
allow_failures:
- rust: nightly
fast_finish: true
cache: cargo
include:
- env: WASM=true
- env: WASM=false
cache:
cargo: true
timeout: 1000
before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sudo chown root:wheel /usr/local/bin/brew ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get update -qq ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install -y gcc libegl1-mesa-dev libgles2-mesa-dev ; fi
script:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/lib" ; fi
- travis_wait 30 cargo build --verbose
- cargo test --verbose
- if [[ "$WASM" == "true" ]]; then
rustup target add wasm32-unknown-unknown;
cargo install wasm-bindgen-cli;
cargo install wasm-pack;
travis_wait 120 wasm-pack build;
else
travis_wait 30 cargo build --verbose;
cargo test --verbose;
fi
os:
- linux
- osx
84 changes: 75 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ description = "Multi-protocol multi-platform Minecraft-compatible client"
repository = "https://github.com/iceiix/stevenarella"
license = "(MIT OR Apache-2.0)"

[lib]
crate-type = ["cdylib", "rlib"]
path = "src/main.rs"

[profile.dev]
# Steven runs horrendously slow with no optimizations, and often freezes.
# However, building with full -O3 optimizations takes too long for a debug build.
# Use an -O1 optimization level strikes a good compromise between build and program performance.
opt-level = 1

[dependencies]
cfg-if = "0.1.2"
wasm-bindgen = "0.2.33"
sha-1 = "0.8.1"
glutin = { git = "https://github.com/tomaka/glutin", rev = "23b3b101e554e521f38c1179f90d3d2f278b857c" }
glutin = { git = "https://github.com/iceiix/glutin", rev = "97797352b5242436cb82d8ecfb44242b69766e4c" }
byteorder = "1.2.7"
reqwest = "0.9.10"
serde = "1.0.89"
serde_json = "1.0.39"
flate2 = { version = "1.0.6", features = ["rust_backend"], default-features = false }
Expand All @@ -33,9 +38,15 @@ collision = "0.19.0"
aes = "0.3.2"
cfb8 = "0.3.1"
rsa_public_encrypt_pkcs1 = "0.2.0"
clipboard = "0.5.0"
clipboard = { git = "https://github.com/aweinstock314/rust-clipboard", rev = "07d080be58a361a5bbdb548fafe9449843d968be" }
# clippy = "*"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = "0.9.10"

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.1"

[dependencies.steven_gl]
path = "./gl"
version = "0"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Just compile:
cargo build --release
```

For progress on web support, see [www/](./www).

## Running

### Standalone
Expand Down
28 changes: 26 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub mod auth;
pub mod model;
pub mod entity;

use cfg_if::cfg_if;
use wasm_bindgen::prelude::*;
use std::sync::{Arc, RwLock, Mutex};
use std::rc::Rc;
use std::marker::PhantomData;
Expand Down Expand Up @@ -166,7 +168,21 @@ impl Game {
}
}

fn main() {
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
extern crate console_error_panic_hook;
pub use console_error_panic_hook::set_once as set_panic_hook;
} else {
#[inline]
pub fn set_panic_hook() {}
}
}

#[wasm_bindgen]
pub fn main() {
set_panic_hook();
std::env::set_var("RUST_BACKTRACE", "1");

let con = Arc::new(Mutex::new(console::Console::new()));
let (vars, vsync) = {
let mut vars = console::Vars::new();
Expand Down Expand Up @@ -215,7 +231,15 @@ fn main() {
let frame_time = 1e9f64 / 60.0;

let mut screen_sys = screen::ScreenSystem::new();
screen_sys.add_screen(Box::new(screen::Login::new(vars.clone())));
#[cfg(not(target_arch = "wasm32"))]
{
screen_sys.add_screen(Box::new(screen::Login::new(vars.clone())));
}

#[cfg(target_arch = "wasm32")]
{
screen_sys.add_screen(Box::new(screen::ServerList::new(None)));
}

let textures = renderer.get_textures();
let dpi_factor = window.get_current_monitor().get_hidpi_factor();
Expand Down
5 changes: 5 additions & 0 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use aes::Aes128;
use cfb8::Cfb8;
use cfb8::stream_cipher::{NewStreamCipher, StreamCipher};
use serde_json;
#[cfg(not(target_arch = "wasm32"))]
use reqwest;

pub mod mojang;
Expand Down Expand Up @@ -764,6 +765,7 @@ pub enum Error {
Disconnect(format::Component),
IOError(io::Error),
Json(serde_json::Error),
#[cfg(not(target_arch = "wasm32"))]
Reqwest(reqwest::Error),
}

Expand All @@ -779,6 +781,7 @@ impl convert::From<serde_json::Error> for Error {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl convert::From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Error {
Error::Reqwest(e)
Expand All @@ -792,6 +795,7 @@ impl ::std::error::Error for Error {
Error::Disconnect(_) => "Disconnect",
Error::IOError(ref e) => e.description(),
Error::Json(ref e) => e.description(),
#[cfg(not(target_arch = "wasm32"))]
Error::Reqwest(ref e) => e.description(),
}
}
Expand All @@ -804,6 +808,7 @@ impl ::std::fmt::Display for Error {
Error::Disconnect(ref val) => write!(f, "{}", val),
Error::IOError(ref e) => e.fmt(f),
Error::Json(ref e) => e.fmt(f),
#[cfg(not(target_arch = "wasm32"))]
Error::Reqwest(ref e) => e.fmt(f),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/protocol/mojang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use sha1::{self, Digest};
use serde_json::json;
#[cfg(not(target_arch = "wasm32"))]
use reqwest;

#[derive(Clone, Debug)]
Expand All @@ -28,6 +29,7 @@ const LOGIN_URL: &str = "https://authserver.mojang.com/authenticate";
const REFRESH_URL: &str = "https://authserver.mojang.com/refresh";
const VALIDATE_URL: &str = "https://authserver.mojang.com/validate";

#[cfg(not(target_arch = "wasm32"))]
impl Profile {
pub fn login(username: &str, password: &str, token: &str) -> Result<Profile, super::Error> {
let req_msg = json!({
Expand Down
6 changes: 6 additions & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,11 @@ impl TextureManager {
]);
}

#[cfg(target_arch = "wasm32")]
fn process_skins(recv: mpsc::Receiver<String>, reply: mpsc::Sender<(String, Option<image::DynamicImage>)>) {
}

#[cfg(not(target_arch = "wasm32"))]
fn process_skins(recv: mpsc::Receiver<String>, reply: mpsc::Sender<(String, Option<image::DynamicImage>)>) {
use reqwest;
let client = reqwest::Client::new();
Expand All @@ -857,6 +862,7 @@ impl TextureManager {
}
}

#[cfg(not(target_arch = "wasm32"))]
fn obtain_skin(client: &::reqwest::Client, hash: &str) -> Result<image::DynamicImage, ::std::io::Error> {
use std::io::Read;
use std::fs;
Expand Down
Loading

0 comments on commit 2f2f358

Please sign in to comment.