Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 42 additions & 22 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ members = [
"broodmap-render",
"broodmap-wasm",
]

# Whole-program optimization: the wasm package comes out ~8% smaller (1.86 to 1.71 MB) with no
# loss of speed. Don't trade speed for size with opt-level "s"/"z": broodcasc measured them 3-5%
# smaller but 25-50% slower at decoding.
[profile.release]
lto = true
codegen-units = 1
2 changes: 1 addition & 1 deletion broodmap-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ broodmap-render = { version = "0.1.0", path = "../broodmap-render", features = [
# `AssetRequest` variant (they're dev-time-only inputs to the minimap table generator, never read
# at render time) -- same pinned revision broodmap-render's `casc` feature uses. `cdn-http`
# supplies the ureq transport (plus the fs-feature CachingTransport) behind `--cdn`.
broodcasc = { git = "https://github.com/ShieldBattery/broodcasc", rev = "4c953f89b7e119e19cf0bdca71981e1d5a939de0", features = ["cdn-http"] }
broodcasc = { git = "https://github.com/ShieldBattery/broodcasc", rev = "ea3556da602dca09f3b88517296ccb3772524098", features = ["cdn-http"] }
clap = { version = "4", features = ["derive"] }
anyhow = "1"
2 changes: 1 addition & 1 deletion broodmap-render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ broodmap = { version = "0.1.0", path = "../broodmap" }
broodmap-formats = { version = "0.1.0", path = "../broodmap-formats" }
thiserror = "2.0"
miniz_oxide = "0.8"
broodcasc = { git = "https://github.com/ShieldBattery/broodcasc", rev = "4c953f89b7e119e19cf0bdca71981e1d5a939de0", optional = true }
broodcasc = { git = "https://github.com/ShieldBattery/broodcasc", rev = "ea3556da602dca09f3b88517296ccb3772524098", optional = true }
png = { version = "0.18", optional = true }
serde = { version = "1", features = ["derive"], optional = true }

Expand Down
24 changes: 21 additions & 3 deletions broodmap-render/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,29 @@ impl<P: broodcasc::io::StorageProvider> CascSource<P> {

#[cfg(feature = "casc")]
impl<P: broodcasc::io::StorageProvider> TilesetDataSource for CascSource<P> {
/// Decodes straight into the returned `Arc`, so a large asset isn't held twice while a `Vec`
/// is copied into it.
fn read(&self, req: &AssetRequest) -> Result<std::sync::Arc<[u8]>, SourceError> {
let path = req.casc_path();
// The size comes from the encoding table; bound it before allocating.
let limit = broodcasc::ReadLimits::default().max_decoded_bytes;
let size = usize::try_from(self.storage.file_size(&path).map_err(map_casc_error)?)
.ok()
.filter(|&size| size <= limit)
.ok_or_else(|| {
map_casc_error(broodcasc::CascError::LimitExceeded {
what: "decoded file",
limit,
})
})?;
let mut bytes: std::sync::Arc<[u8]> = std::iter::repeat_n(0, size).collect();
self.storage
.read_file(&req.casc_path())
.map(Into::into)
.map_err(map_casc_error)
.read_file_into(
&path,
std::sync::Arc::get_mut(&mut bytes).expect("a new Arc is unique"),
)
.map_err(map_casc_error)?;
Ok(bytes)
}
}

Expand Down
10 changes: 10 additions & 0 deletions broodmap-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,15 @@ broodmap-render = { version = "0.1.0", path = "../broodmap-render", default-feat
"serde",
] }
wasm-bindgen = "0.2"
js-sys = "0.3"
# CascStorage: CASC parsing over JS file callbacks. No default features: `fs` doesn't build for
# wasm32, and the files come from the host. `fast-inflate` decodes ~12% faster for ~33 KB of wasm.
broodcasc = { git = "https://github.com/ShieldBattery/broodcasc", rev = "ea3556da602dca09f3b88517296ccb3772524098", default-features = false, features = [
"fast-inflate",
] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[dev-dependencies]
# Native CascSource, to check loadAssets against a real install (BROODMAP_TEST_SCR_DIR).
broodmap-render = { version = "0.1.0", path = "../broodmap-render", features = ["casc", "png"] }
29 changes: 29 additions & 0 deletions broodmap-wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@ node examples/node.mjs ../broodmap/assets/lt.scm examples/assets out.png

WASM renders are byte-identical to the native CLI's (verified against `broodmap-cli render`).

### Reading a SC:R install

Where the game install is readable synchronously (Node, Electron), `CascStorage` reads assets
straight from its CASC storage, so no prefetch or extracted asset directory is needed:

```js
const storage = new CascStorage(files) // opens .build.info, indices and catalogs (~55 ms)
const map = new MapRenderer(mapBytes)
const missing = map.loadAssets(storage, opts) // art assets the install lacks; those degrade
const png = map.renderPng(opts)
```

`files` is the sync file access broodcasc reads through, with paths relative to the install
directory (the one containing `.build.info`): `open(path)` returns a handle, `size(handle)`,
`read(handle, buffer, position)` returns the bytes read, `close(handle)`, and `listDir(path)`
returns names (`[]` when the directory doesn't exist). `read` fills a view of WASM memory in
place, so the callbacks mustn't call back into the module. `examples/casc-files.mjs` implements
it over `node:fs`:

```bash
pnpm run build
node examples/node-casc.mjs "C:/Program Files (x86)/StarCraft" ../broodmap/assets/lt.scm
```

Decoded assets are cached per `CascStorage` and shared by every map loaded from it (every render
needs the ~38 MB `SD/mainSD.anim`). `new CascStorage(files, '{"cacheBytes": N}')` bounds the
cache (default 128 MiB, about the whole `original`-style set). `storage.version` is the
install's game version, for keying rendered-image caches. `free()` the storage to close its files.

`pkg/` and `examples/assets/` are build/game-data artifacts and are gitignored;
the assets are Blizzard's and must never be committed.

Expand Down
21 changes: 21 additions & 0 deletions broodmap-wasm/examples/casc-files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// The file callbacks `CascStorage` reads a SC:R install through, over sync `node:fs`.
import { closeSync, fstatSync, openSync, readdirSync, readSync } from 'node:fs'
import path from 'node:path'

/** File access to the SC:R install at `root` (the directory containing `.build.info`). */
export function nodeCascFiles(root) {
return {
open: p => openSync(path.join(root, p), 'r'),
size: fd => fstatSync(fd).size,
read: (fd, buffer, position) => readSync(fd, buffer, 0, buffer.length, position),
close: fd => closeSync(fd),
listDir: p => {
try {
return readdirSync(path.join(root, p))
} catch (err) {
if (err.code === 'ENOENT') return []
throw err
}
},
}
}
35 changes: 35 additions & 0 deletions broodmap-wasm/examples/node-casc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Node usage example: render maps with assets read straight from a SC:R install's CASC storage,
// no prefetch or extracted asset directory. Build the package first: `pnpm run build`.
//
// node examples/node-casc.mjs <install-dir> <map.scm>...
import { readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { CascStorage, MapRenderer } from '@shieldbattery/broodmap'
import { nodeCascFiles } from './casc-files.mjs'

const [installDir, ...mapPaths] = process.argv.slice(2)
if (!installDir || !mapPaths.length) {
console.error('usage: node examples/node-casc.mjs <install-dir> <map.scm>...')
process.exit(1)
}

let start = performance.now()
const storage = new CascStorage(nodeCascFiles(installDir))
console.log(`opened SC:R ${storage.version} in ${(performance.now() - start).toFixed(0)} ms`)

const opts = JSON.stringify({ style: 'original', size: 1024 })
for (const mapPath of mapPaths) {
start = performance.now()
const map = new MapRenderer(readFileSync(mapPath))
const missing = map.loadAssets(storage, opts)
const png = map.renderPng(opts)
map.free()
const outPath = `${path.basename(mapPath, path.extname(mapPath))}.png`
writeFileSync(outPath, png)
console.log(
`wrote ${outPath} in ${(performance.now() - start).toFixed(0)} ms` +
(missing.length ? ` (${missing.length} art assets missing)` : ''),
)
}
console.log(`cache: ${(storage.cachedBytes / 1e6).toFixed(1)} MB`)
storage.free()
33 changes: 33 additions & 0 deletions broodmap-wasm/scripts/package.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createRequire } from 'node:module'
import path from 'node:path'
import { test } from 'node:test'
import * as esm from '@shieldbattery/broodmap'
import { nodeCascFiles } from '../examples/casc-files.mjs'

const require = createRequire(import.meta.url)
const packageDir = path.resolve(import.meta.dirname, '..')
Expand Down Expand Up @@ -69,3 +70,35 @@ test('takes resource limits as options', () => {
test('rejects files that are not maps', () => {
assert.throws(() => esm.summarizeMapJson(new Uint8Array([1, 2, 3])))
})

test('CascStorage rejects incomplete file callbacks and directories that are not installs', () => {
const { open, ...noOpen } = nodeCascFiles(import.meta.dirname)
assert.throws(() => new esm.CascStorage(noOpen), /no `open` function/)
assert.throws(() => new esm.CascStorage(nodeCascFiles(import.meta.dirname)), /failed to open CASC/)
})

test('renders from a SC:R install through CascStorage', async t => {
const installDir = process.env.BROODMAP_TEST_SCR_DIR
if (!installDir) {
t.skip('BROODMAP_TEST_SCR_DIR not set')
return
}
const storage = new esm.CascStorage(nodeCascFiles(installDir))
assert.match(storage.version, /^\d+\.\d+/)

const opts = JSON.stringify({ style: 'original', size: 256 })
const render = () => {
const map = new esm.MapRenderer(lt)
assert.deepEqual(map.loadAssets(storage, opts), [])
const png = map.renderPng(opts)
map.free()
return png
}
const first = render()
assert.deepEqual([...first.subarray(0, 4)], [0x89, 0x50, 0x4e, 0x47])
const cached = storage.cachedBytes
assert.ok(cached > 0)
assert.deepEqual(render(), first)
assert.equal(storage.cachedBytes, cached, 'a second render of the same map reads only from the cache')
storage.free()
})
Loading
Loading