Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:4thfever Cultivation world simulator Section Based Serialization

From Leeroopedia
Knowledge Sources
Domains Persistence, Software_Architecture
Last Updated 2026-10-22 14:00 GMT

Overview

A persistence pattern that decomposes save/load into independent, pluggable sections, each responsible for serializing or deserializing one domain of game state into a JSON-keyed partition.

Description

Section-Based Serialization addresses the problem of serializing a complex, multi-domain game state (world config, avatars, events, simulator state, custom content) without a monolithic serializer that becomes unmaintainable as the game grows.

The pattern introduces:

  • SaveSection and LoadSection protocols: each section has a `key` (string) and a `dump(context)` or `load(context)` method
  • SaveContext / LoadContext: dataclasses holding all state needed by sections (world, simulator, sects, file paths, raw save data)
  • SAVE_SECTIONS / LOAD_SECTIONS tuples: ordered registries of section instances
  • dump_save_data / restore_loaded_game: top-level functions that iterate sections and assemble/disassemble the save dict

Each section writes to one key in the JSON save dict (e.g., `"meta"`, `"world"`, `"avatars"`, `"events"`, `"simulator"`). The save file is a flat JSON object with these top-level keys.

This design allows:

  • New domains to be added by implementing a new section class and adding it to the tuple
  • Save and load sections to be maintained independently (load sections handle migration/restoration)
  • Testing each section in isolation
  • The event database (SQLite) to be handled separately from the JSON save (copied as a file)

Usage

Use this principle when:

  • Game state spans many independent domains that need to be saved and loaded
  • You want to add new save-able domains without modifying existing serialization code
  • Some domains use different persistence backends (JSON vs. SQLite)
  • Save/load ordering matters (e.g., world core must be loaded before avatars reference it)
  • Development phase does not require backward compatibility, so sections can be rewritten freely

Theoretical Basis

The core structure is a section registry with context carrier:

# Abstract algorithm description (NOT real implementation)
@dataclass
class SaveContext:
    world: World
    simulator: Simulator
    existed_sects: list[Sect]
    save_path: Path
    events_db_path: Path

@dataclass
class LoadContext:
    save_path: Path
    save_data: dict
    world: Any = None
    simulator: Any = None

class SaveSection(Protocol):
    key: str
    def dump(self, ctx: SaveContext) -> Any: ...

class LoadSection(Protocol):
    key: str
    def load(self, ctx: LoadContext) -> None: ...

# Save: iterate sections, build dict
def dump_save_data(ctx):
    return {section.key: section.dump(ctx) for section in SAVE_SECTIONS}

# Load: iterate sections, mutate context
def restore_loaded_game(ctx):
    for section in LOAD_SECTIONS:
        section.load(ctx)
    return ctx.world, ctx.simulator, ctx.existed_sects

Key design properties:

  • Flat JSON partition: each section owns one top-level key; no nesting
  • Separate save/load registries: save sections and load sections are different classes with potentially different ordering
  • Context as data carrier: sections communicate through the shared context dataclass, not through return values
  • Event DB handled separately: events are persisted in SQLite, not in the JSON save; the save process copies the SQLite file
  • No backward compatibility (dev phase): load sections are written for the current model; old saves are not supported

Practical Guide

When implementing section-based serialization:

  • Order load sections by dependency: world core before avatars, run config before everything else
  • Use a single context object: pass it to all sections; sections set their results on it (e.g., `ctx.world = ...`)
  • Keep JSON-only types: save data must be JSON-serializable (no custom objects, use IDs for cross-references)
  • Handle events via file copy: don't serialize events into JSON; copy the SQLite database file
  • Don't persist runtime state: roleplay sessions, pending requests, and conversation sessions are runtime-only and excluded from saves
  • Development phase rule: when the model changes, update load sections to match the new model; don't maintain migration paths

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment