-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert from from cargo-screeps to node/rollup/babel (#59)
- Loading branch information
1 parent
c610edf
commit d7a01d7
Showing
10 changed files
with
413 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
servers: | ||
# Deploy to the main MMO server - note that tokens are | ||
# the only supported auth method for official servers (mmo, season, and ptr) | ||
mmo: | ||
host: screeps.com | ||
secure: true | ||
token: your-auth-token-here | ||
branch: default | ||
# The public test realm can be a good place to test your code | ||
ptr: | ||
host: screeps.com | ||
secure: true | ||
token: your-auth-token-here | ||
path: /ptr | ||
branch: default | ||
# Seasonal server configuration - this environment has unique mechanics each | ||
# season, so it might make sense to have feature flag(s) for different mechanics | ||
season: | ||
host: screeps.com | ||
secure: true | ||
token: your-auth-token-here | ||
path: /season | ||
branch: default | ||
private-server: | ||
host: 127.0.0.1 | ||
port: 21025 | ||
secure: false | ||
username: user | ||
password: password | ||
branch: default | ||
configs: | ||
# Whether to minify generated javascript for each configured server | ||
terser: | ||
# The special '*'' key sets a default for all servers which | ||
# will be **overridden** by an applicable per-server config | ||
'*': false | ||
ptr: false | ||
localhost: false | ||
# Additional options to pass to wasm-pack to customize the build for each server | ||
wasm-pack-options: | ||
# The special '*'' key sets flags applied to all servers, which | ||
# will be **concatenated** with any applicable per-server config | ||
'*': [] | ||
# This setting enables the `mmo` crate feature for these destinations, | ||
# which enables the API functions for intershard communication and pixel | ||
# generation, which are specific to MMO | ||
mmo: ["--features", "mmo"] | ||
ptr: ["--features", "mmo"] | ||
# Other servers can each have their own build flags, including crate features: | ||
#season: ["--features", "my-season-7-feature"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"use strict"; | ||
import 'fastestsmallesttextencoderdecoder-encodeinto/EncoderDecoderTogether.min.js'; | ||
|
||
import * as bot from '../pkg/screeps_starter_rust.js'; | ||
// replace this with the name of your module | ||
const MODULE_NAME = "screeps_starter_rust"; | ||
const BUCKET_BOOT_THRESHOLD = 1500; | ||
|
||
// This provides the function `console.error` that wasm_bindgen sometimes expects to exist, | ||
// especially with type checks in debug mode. An alternative is to have this be `function () {}` | ||
// and let the exception handler log the thrown JS exceptions, but there is some additional | ||
// information that wasm_bindgen only passes here. | ||
// | ||
// There is nothing special about this function and it may also be used by any JS/Rust code as a convenience. | ||
function console_error() { | ||
const processedArgs = _.map(arguments, (arg) => { | ||
if (arg instanceof Error) { | ||
// On this version of Node, the `stack` property of errors contains | ||
// the message as well. | ||
return arg.stack; | ||
} else { | ||
return arg; | ||
} | ||
}).join(' '); | ||
console.log("ERROR:", processedArgs); | ||
Game.notify(processedArgs); | ||
} | ||
|
||
// track whether running wasm loop for each tick completes, to detect errors or aborted execution | ||
let running = false; | ||
|
||
function loaded_loop() { | ||
// need to freshly override the fake console object each tick | ||
console.error = console_error; | ||
if (running) { | ||
// we've had an error on the last tick; skip execution during the current tick, asking to | ||
// have our IVM immediately destroyed so we get a fresh environment next tick; | ||
// workaround for https://github.com/rustwasm/wasm-bindgen/issues/3130 | ||
Game.cpu.halt(); | ||
} else { | ||
try { | ||
running = true; | ||
bot.loop(); | ||
// if execution doesn't get to this point for any reason (error or out-of-CPU | ||
// cancellation), setting to false won't happen which will cause a halt() next tick | ||
running = false; | ||
} catch (error) { | ||
console.log(`caught exception, will halt next tick: ${error}`); | ||
// not logging stack since we've already logged the stack trace from rust via the panic | ||
// hook and that one is generally better, but if we need it, uncomment: | ||
|
||
// if (error.stack) { | ||
// console.log("js stack:", error.stack); | ||
// } | ||
} | ||
} | ||
} | ||
|
||
// cache for each step of the wasm module's initialization | ||
let wasm_bytes, wasm_module, wasm_instance; | ||
|
||
module.exports.loop = function() { | ||
// need to freshly override the fake console object each tick | ||
console.error = console_error; | ||
// temporarily need to polyfill this too because there's a bug causing the warn | ||
// in initSync to fire in bindgen 0.2.93 | ||
console.warn = console.log; | ||
|
||
// attempt to load the wasm only if there's lots of bucket | ||
if (Game.cpu.bucket < BUCKET_BOOT_THRESHOLD) { | ||
console.log(`startup deferred; ${Game.cpu.bucket} / ${BUCKET_BOOT_THRESHOLD} required bucket`); | ||
return; | ||
} | ||
|
||
// run each step of the load process, saving each result so that this can happen over multiple ticks | ||
if (!wasm_bytes) wasm_bytes = require(MODULE_NAME); | ||
if (!wasm_module) wasm_module = new WebAssembly.Module(wasm_bytes); | ||
if (!wasm_instance) wasm_instance = bot.initSync(wasm_module); | ||
|
||
// remove the bytes from the heap and require cache, we don't need 'em anymore | ||
wasm_bytes = null; | ||
delete require.cache[MODULE_NAME]; | ||
// replace this function with the post-load loop for next tick | ||
module.exports.loop = loaded_loop; | ||
console.log(`loading complete, CPU used: ${Game.cpu.getUsed()}`) | ||
} |
Oops, something went wrong.