storage
garnet-storage
Per-user files for Garnet games: platform data directories, crash-safe writes, versioned save slots, and a settings resource.
require "garnet-storage"
world.add_plugin(Garnet::Storage.new("com.example.mygame"))
settings = world.resource(Garnet::Settings)
settings.set_volume("music", 0.4)
Data directories
Garnet::Storage::Paths.data(app) and .cache(app) follow each platform's conventions:
| Platform | Data (saves, settings) | Cache |
|---|---|---|
| macOS | ~/Library/Application Support/<app> |
~/Library/Caches/<app> |
| iOS | <sandbox>/Library/Application Support/<app> |
<sandbox>/Library/Caches/<app> |
| Linux | $XDG_DATA_HOME/<app> or ~/.local/share/<app> |
$XDG_CACHE_HOME/<app> or ~/.cache/<app> |
| Windows | %APPDATA%\<organization>\<app> |
%LOCALAPPDATA%\<organization>\<app>\Cache |
app is a single directory name such as "com.example.mygame"; organization is optional and only used on Windows. Pass platform: and env: to compute another platform's path (for tools and tests).
Storage
Garnet::Storage.new(app, organization: nil, root: nil) stores files under the data directory, or under root for tests, portable builds and consoles.
storage = Garnet::Storage.new("com.example.mygame")
storage.write("replays/last.bin", bytes) # atomic
storage.read?("replays/last.bin") # => String?
storage.path("replays", "last.bin") # rejects absolute paths and ".."
Garnet::Storage.write_atomic(path, content) writes a temporary file in the same directory, fsyncs it, renames it over path and fsyncs the directory, so a crash leaves either the old or the new file. Failures raise Garnet::Storage::Error (a Garnet::Error).
Save slots
class Progress
include JSON::Serializable
property level = 1
property coins = 0
def initialize
end
end
saves = storage.saves(Progress, version: 2)
saves.migrate(1) { |data| data.as_h["coins"] = JSON::Any.new(0_i64); data }
saves.save("slot1", progress)
progress = saves.load?("slot1") || Progress.new
saves.slots # => [SlotInfo(name, version, saved_at, path)], newest first
saves.delete("slot1")
Each slot is saves/<slot>.json containing {"version", "saved_at", "data"}. Loading an older version runs each migrate(from) block in turn on the raw JSON. Corrupt files, versions newer than the current one, missing migrations, and data that doesn't match the type raise Storage::Error.
Settings
Garnet::Settings holds display preferences (window_mode, width, height, vsync, max_fps, ui_scale, monitor), audio volumes (master, muted, and a volume per named bus), control bindings as an opaque JSON::Any owned by the input system, and game-specific custom values.
settings.set_volume("music", 0.4)
settings.master_volume = 0.8
mixer.gain = settings.effective_volume("music") # 0 when muted
display = settings.display
display.vsync = false
settings.display = display
settings.controls = input.bindings_json
settings.set_custom("language", "fr")
settings.on_change { |section| puts "#{section} changed" }
Volumes (master_volume, volume(bus), effective_volume) are Float32 fractions from 0 to 1, not percentages: set_volume("music", 40) stores 1. Convert at the UI if the game shows percentages.
Custom values are stored as JSON but read and written with types:
settings.set_custom("brightness", 160, clamp: 70..140) # => 140
settings.custom(Int32, "brightness", default: 100, clamp: 70..140)
settings.custom(Bool, "shake", default: true)
settings.custom(Tuple(Int32, Int32), "fullscreen_size", default: {0, 0})
settings.custom(String, "language", default: "en") { |code| code if LANGUAGES.includes?(code) }
settings.set_custom("language", input) { |code| code if LANGUAGES.includes?(code) } # nil raises ArgumentError
custom(type, key, default:) returns default when the key is missing, has another type, or the validation block returns nil. It reads Bool, String, Int32, Int64, Float32, Float64 (numbers convert leniently; non-finite floats are rejected) and anything with from_json. clamp: clamps reads and writes into a range. custom?(key) and set_custom(key, JSON::Any) still give raw access.
Writes clamp to valid ranges (volumes 0..1, window size 320×240 to 16384×16384, ui_scale 0.5..4, max_fps 0 or 15..1000) and notify listeners only when something changed. Settings.from_json is lenient: unknown keys are ignored and missing, mistyped or out-of-range values fall back to defaults or are clamped, so an old or hand-edited file never stops the game from starting.
World plugin
world.add_plugin(Garnet::Storage.new(app)) adds the storage and its Settings (loaded from settings.json) as resources. Each settings change emits Garnet::SettingsChanged(section); the storage.settings system (PostUpdate, order 1000) saves changed settings once per frame, and the plugin saves again when the world closes. Pass autosave: false to save manually with storage.save_settings.
storage
- 0
- 0
- 0
- 1
- 1
- about 5 hours ago
- November 30, 2026
MIT License
Thu, 03 Dec 2026 02:11:07 GMT