Repository navigation
Example 02 read a save file
github-actions[bot] edited this page Nov 21, 2026
·
1 revision
Generated from the thetowersdk repository. Edits made here are overwritten on the next push — change the source file instead.
/**
* Example 2 — Read a real save file.
*
* Two steps: decode `tool_export.json` into a save root, then pull typed data
* out of that root. This example uses the asynchronous Node decoder; browser tools use
* `decodeSaveFile` from `@beardedunicorn/thetowersdk/save-decoder`. Only the official game JSON export is supported.
*
* Run it:
* pnpm exec tsx examples/02-read-a-save-file.ts /path/to/tool_export.json
*
* Where to find your save is explained in the README ("Getting a save file").
*/
import { readFile } from 'node:fs/promises'
import { decodePlayerInfoSaveBytesAsync } from '@beardedunicorn/thetowersdk/node'
import {
readCardsFromSaveRoot,
readLabsFromSaveRoot,
readWorkshopFromSaveRoot,
} from '@beardedunicorn/thetowersdk/save'
async function main(): Promise<void> {
const savePath = process.argv[2]
if (!savePath) {
console.error('Usage: tsx examples/02-read-a-save-file.ts <path-to-tool_export.json>')
process.exit(1)
}
// --- Step 1: decode -------------------------------------------------------
const bytes = await readFile(savePath)
const { parsedRoot, wasGzip, battleRunCount } = await decodePlayerInfoSaveBytesAsync(bytes)
console.log(`Decoded ${savePath}`)
console.log(` gzip-compressed: ${wasGzip}`)
console.log(` top-level keys: ${Object.keys(parsedRoot).length}`)
console.log(` battle runs: ${battleRunCount}`)
console.log()
// --- Step 2: extract the parts you care about -----------------------------
// Every extractor returns `null` instead of throwing when a save predates
// that feature, so always check before reading.
const labs = readLabsFromSaveRoot(parsedRoot)
if (labs) {
console.log('Labs')
console.log(` unlocked: ${labs.labsUnlocked}`)
console.log(` researches done: ${labs.researchedCount}`)
console.log(` maxed: ${labs.maxedCount}`)
console.log(` currently running: ${labs.activeQueue.length}`)
if (labs.warnings.length) {
// Extractors report anything they could not make sense of rather than
// silently dropping it -- worth surfacing while developing.
console.log(` warnings: ${labs.warnings.join('; ')}`)
}
} else {
console.log('Labs: not present in this save')
}
console.log()
const workshop = readWorkshopFromSaveRoot(parsedRoot)
console.log(workshop ? 'Workshop: present' : 'Workshop: not present in this save')
const cards = readCardsFromSaveRoot(parsedRoot)
console.log(cards ? 'Cards: present' : 'Cards: not present in this save')
}
main().catch((error: unknown) => {
console.error(error)
process.exit(1)
})Generated from TheTowerSDK — do not edit here. Docs and live demos: https://beardedunicorn.github.io/TheTowerSDK/
- Workshop
- Workshop Enhancements
- Labs
- Cards
- Modules
- Module Rarities and Merging
- Module Substats
- Module Upgrade Costs
- Ultimate Weapons
- Ultimate Weapon Plus
- Bots
- Guardian
- Relics
- Themes
- Tiers
- Battle Conditions
- Enemies
- Perks
- Tournaments
- Milestones
- The Vault
- Daily Missions
- Guild
- Currencies
- Reference Tables
- Glossary
- Irreversible Upgrades
- Game Patch Notes
- Game Version History
SDK
- Quick Start
- Entry Points
- How It Fits Together
- Getting an Official Game Export
- Reading a Save
- Reading The Community Wiki
- Game 29.1 Data
- Formulas
- Builders
- Charts
- Effective Paths
- Examples
- Templates
- Building a Bot On This
- Google Sheets
- Desktop and Mobile
- Optional Add-ons
- Using It With An AI Agent
- Using Your Own Artwork
- Names And Acronyms
- Patch Notes
- Accuracy
- Versioning
- Where the Docs Live
- Contributing
- Credits
- License
Guides
Examples
- 01-browse-game-data.ts
- 02-read-a-save-file.ts
- 03-plan-upgrades-from-a-save.ts
- 04-generate-a-chart.ts
- 05-generate-a-cost-table.ts
- 06-read-the-community-wiki.ts
- 07-format-like-the-game.ts
- 08-build-a-calculator.ts
- 09-build-a-bot.ts
- 10-read-a-sheet.ts
- 11-build-a-knowledge-base.ts
- 12-show-module-and-card-art.ts
Templates