Skip to content

Commit

Permalink
Merge branch 'main' of github.com:AmbientRun/Ambient into misc-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
philpax committed Sep 22, 2023
2 parents 492c5e5 + 4a5e2b0 commit cffb59b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
8 changes: 6 additions & 2 deletions crates/network/src/web/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct GameClientView {
/// The url to connect to
pub url: String,
pub user_id: String,
pub fail_on_version_mismatch: bool,
pub systems_and_resources: Cb<dyn Fn() -> (SystemGroup, Entity) + Sync + Send>,
/// Invoked when the game client is loaded
///
Expand All @@ -60,6 +61,7 @@ impl ElementComponent for GameClientView {
let Self {
url,
user_id,
fail_on_version_mismatch,
systems_and_resources,
on_loaded,
create_rpc_registry,
Expand Down Expand Up @@ -122,6 +124,7 @@ impl ElementComponent for GameClientView {
conn,
&assets,
user_id,
fail_on_version_mismatch,
move |assets, user_id| {
let (systems, resources) = systems_and_resources();

Expand Down Expand Up @@ -253,6 +256,7 @@ async fn handle_connection(
mut conn: Connection,
assets: &AssetCache,
user_id: String,
fail_on_version_mismatch: bool,
mut on_loaded: impl FnMut(&AssetCache, &str) -> anyhow::Result<(SharedClientGameState, CleanupFunc)>,
control_rx: flume::Receiver<Control>,
proxy_rx: flume::Receiver<ProxyMessage>,
Expand Down Expand Up @@ -289,7 +293,7 @@ async fn handle_connection(
while client.is_pending() {
tracing::info!("Waiting for server to accept connection and send server info");
if let Some(frame) = push_recv.next().await {
client.process_push(&assets, true, frame?)?;
client.process_push(&assets, fail_on_version_mismatch, frame?)?;
}
}

Expand Down Expand Up @@ -321,7 +325,7 @@ async fn handle_connection(
while let ClientProtoState::Connected(connected) = &mut client {
tokio::select! {
Some(frame) = push_recv.next() => {
client.process_push(&assets, true, frame?)?;
client.process_push(&assets, fail_on_version_mismatch, frame?)?;
}

Some(message) = proxy_rx.next() => {
Expand Down
3 changes: 2 additions & 1 deletion web/client/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use ambient_ui_native::cb;
use std::collections::HashMap;

#[element_component]
pub fn MainApp(_hooks: &mut Hooks, server_url: String) -> Element {
pub fn MainApp(_hooks: &mut Hooks, server_url: String, fail_on_version_mismatch: bool) -> Element {
tracing::info!("Connecting to {server_url:?}");

GameClientView {
url: server_url,
user_id: ambient_client_shared::util::random_username(),
fail_on_version_mismatch,
systems_and_resources: cb(|| {
let mut resources = Entity::new();

Expand Down
17 changes: 15 additions & 2 deletions web/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::OnceLock;
use std::sync::{
atomic::{AtomicBool, Ordering},
OnceLock,
};

use ambient_app::App;
use ambient_cameras::UICamera;
Expand Down Expand Up @@ -33,9 +36,12 @@ static APP_CONTROL: OnceLock<flume::Sender<WindowCtl>> = OnceLock::new();
pub struct Settings {
pub enable_logging: bool,
pub enable_panic_hook: bool,
pub allow_version_mismatch: Option<bool>,
pub log_filter: Option<String>,
}

static ALLOW_VERSION_MISMATCH: AtomicBool = AtomicBool::new(false);

/// Initialize ambient
#[wasm_bindgen]
pub fn init(settings: JsValue) -> Result<(), JsValue> {
Expand Down Expand Up @@ -63,6 +69,10 @@ pub fn init(settings: JsValue) -> Result<(), JsValue> {
ambient_sys::set_panic_hook();
}

if let Some(allow_version_mismatch) = settings.allow_version_mismatch {
ALLOW_VERSION_MISMATCH.store(allow_version_mismatch, Ordering::SeqCst);
}

tracing::info!("Hello, Wasm!");

ambient_ecs::init_components();
Expand Down Expand Up @@ -123,7 +133,10 @@ async fn run(target: Option<web_sys::HtmlElement>, server_url: String) -> anyhow
Group(vec![
UICamera.el().with(active_camera(), 0.),
ambient_client_shared::player::PlayerRawInputHandler.el(),
WindowSized::el([MainApp::el(server_url)]),
WindowSized::el([MainApp::el(
server_url,
!ALLOW_VERSION_MISMATCH.load(Ordering::SeqCst),
)]),
])
.el()
.spawn_interactive(world);
Expand Down
1 change: 1 addition & 0 deletions web/dioxus_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn main() {
enable_logging: true,
enable_panic_hook: true,
log_filter: None,
allow_version_mismatch: Some(true),
})
.unwrap(),
)
Expand Down
21 changes: 18 additions & 3 deletions web/www/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ import("ambient_web")
return;
}

ambient.init({ enableLogging: true, enablePanicHook : true, logFilter: "debug" });
ambient.init({
enableLogging: true,
enablePanicHook: true,
logFilter: "debug",
allowVersionMismatch: true,
});

let target = window.document.getElementById("instance-container");

Expand All @@ -92,8 +97,18 @@ import("ambient_web")
}

const urlParams = new URLSearchParams(window.location.search);
const package_id = urlParams.get('package');
const url = package_id && `https://api.ambient.run/servers/ensure-running?package_id=${package_id}` || "https://127.0.0.1:9000";
const packageId = urlParams.get('package');
const context = urlParams.get('context');

let params = new URLSearchParams();
if (packageId) {
params.set('package_id', packageId);
}
if (context) {
params.set('context', context);
}

const url = params.size != 0 && `https://api.ambient.run/servers/ensure-running?${params.toString()}` || "https://127.0.0.1:9000";

console.log(`Connecting to ${url}`)

Expand Down

0 comments on commit cffb59b

Please sign in to comment.