Skip to content

Commit

Permalink
fix: use hash for custom URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 1, 2024
1 parent 9abb0fe commit 21f0541
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
1 change: 0 additions & 1 deletion client/build
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"
yarn install

trap 'kill $(jobs -p)' EXIT
yarn serve
25 changes: 14 additions & 11 deletions client/scripts/serve
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DIST_DIR="$SCRIPT_DIR/../dist"
SRC_DIR="$SCRIPT_DIR/../src"
Expand All @@ -14,17 +16,18 @@ ensure_link() {
fi
}

ensure_link "$SRC_DIR/index.html" "$DIST_DIR/index.html"
ensure_link "$SRC_DIR/favicon.ico" "$DIST_DIR/favicon.ico"
ensure_link "$SRC_DIR/background.png" "$DIST_DIR/background.png"
if [ -z "$IN_DOCKER" ]; then
ensure_link "$SRC_DIR/../../../build/game" "$DIST_DIR/game"
ensure_link "$SRC_DIR/../../../build/assets" "$DIST_DIR/assets"
SITE_DIR="$SCRIPT_DIR/../../pkg/server/static/site"

if [ -d "$SITE_DIR" ]; then
rm -rf "$SITE_DIR"
fi
mkdir -p "$SITE_DIR"

cd "$SCRIPT_DIR/../"
yarn build

export SOUR_CONFIG="$(cat $SCRIPT_DIR/../../../config/config.json | jq '.client')"
yarn serve:site &
cd dist
trap 'kill $(jobs -p)' EXIT
cat
cp "$SRC_DIR/index.html" "$SITE_DIR/index.html"
cp "$SRC_DIR/favicon.ico" "$SITE_DIR/favicon.ico"
cp "$SRC_DIR/background.png" "$SITE_DIR/background.png"
cp -r "$SRC_DIR/../../game/dist/game" "$SITE_DIR"
cp "$DIST_DIR"/* "$SITE_DIR"
26 changes: 13 additions & 13 deletions client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ const DELAY_AFTER_LOAD: CubeMessageType[] = [
CubeMessageType.N_SPAWN,
]

const SERVER_URL_REGEX = /\/server\/([\w.]+)\/?(\d+)?/
const MAP_URL_REGEX = /\/map\/(\w+)/
const DEMO_URL_REGEX = /\/demo\/(\w+)/
const SERVER_URL_REGEX = /#\/server\/([\w.]+)\/?(\d+)?/
const MAP_URL_REGEX = /#\/map\/(\w+)/
const DEMO_URL_REGEX = /#\/demo\/(\w+)/

let loadedMods: string[] = []
let failedMods: string[] = []
Expand Down Expand Up @@ -597,11 +597,11 @@ function App() {
}

const {
location: { search: params, pathname, hash },
location: { search: params, hash },
} = window

const serverDestination = SERVER_URL_REGEX.exec(pathname)
const mapDestination = MAP_URL_REGEX.exec(pathname)
const serverDestination = SERVER_URL_REGEX.exec(hash)
const mapDestination = MAP_URL_REGEX.exec(hash)
if (serverDestination != null) {
const [, hostname, port] = serverDestination
if (port == null) {
Expand All @@ -612,14 +612,14 @@ function App() {
} else if (mapDestination != null) {
const [, mapId] = mapDestination
BananaBread.execute(`map ${mapId}`)
} else if (pathname.startsWith('/demo/')) {
} else if (hash.startsWith('/demo/')) {
// First check the fragment for a URL
if (hash.length !== 0) {
const url = hash.slice(1)
remoteConnected = true
playDemoURL(url, 'from-url')
} else {
const demoDestination = DEMO_URL_REGEX.exec(pathname)
const demoDestination = DEMO_URL_REGEX.exec(hash)
if (demoDestination != null) {
const [, demoId] = demoDestination
const [serverURL] = CONFIG.servers
Expand All @@ -628,12 +628,12 @@ function App() {
remoteConnected = true
playDemoURL(demoURL, demoId)
} else {
pushURLState('/')
pushURLState('#')
}
}
} else {
// It should not be anything else
pushURLState('/')
pushURLState('#')
}

const parsedParams = new URLSearchParams(params)
Expand All @@ -658,11 +658,11 @@ function App() {
const updateServerURL = (name: string, port: number) => {
// Sour server
if (port === 0) {
pushURLState(`/server/${name}`)
pushURLState(`#/server/${name}`)
return
}

pushURLState(`/server/${name}/${port}`)
pushURLState(`#/server/${name}/${port}`)
}

Module.onConnect = () => {}
Expand All @@ -673,7 +673,7 @@ function App() {

Module.loadedMap = (name: string) => {
if (remoteConnected) return
pushURLState(`/map/${name}`)
pushURLState(`#/map/${name}`)
}

let lastPointer: number = 0
Expand Down

0 comments on commit 21f0541

Please sign in to comment.