Refactor save manager validation and update state structure

- Updated the save manager to enforce a new format version (2) and improved validation checks for the save data structure.
- Enhanced the `validate_data` function to ensure all required fields are present and valid.
- Adjusted the handling of legacy saves and introduced new checks for various state fields.
- Improved number formatting in the global script for better readability of large numbers.
- Updated tests to reflect changes in the save manager and validation logic, ensuring compatibility with the new state structure.
- Added new tests for group indexing and production transactions to ensure correctness of entity management and resource production.
This commit is contained in:
Kirill
2026-09-20 20:56:15 +03:00
parent 26347fca30
commit a09e3df220
15 changed files with 295 additions and 154 deletions
+3 -22
View File
@@ -50,10 +50,10 @@ func run() -> void:
for invalid in [0.0, -1.0, NAN, INF, 1e308]:
world.evolute(invalid)
check(world.state == before, "Invalid/overflow interval rejected")
check(saves.validate_data({"state": world.state}) == "", "Fractional save is valid")
check(saves.validate_data({"format_version": 2, "app_version": "", "ui": {"current_page": 0}, "state": world.state}) == "", "Fractional save is valid")
var invalid_save: Dictionary = world.state.duplicate(true)
invalid_save.time.year = 0.25
check(saves.validate_data({"state": invalid_save}) != "", "Reject inconsistent fractional clock")
check(saves.validate_data({"format_version": 2, "app_version": "", "ui": {"current_page": 0}, "state": invalid_save}) != "", "Reject inconsistent fractional clock")
dispose(world)
for fps in [30, 60, 144]:
@@ -76,26 +76,7 @@ func run() -> void:
dispose(world)
world = fixture()
world.state.version = "0.1.0"
world.state.time = {"datetime": 0.45, "year": 0.0}
check(saves.validate_data({"state": world.state}) == "", "Accept legacy integer-year save")
world.migrate_time_precision()
check(world.state.version == "0.1.1", "Migrate schema version")
check(world.state.time.year == 0.4 and world.state.time.datetime == 0.45, "Keep unprocessed sub-tenth remainder")
check(world.state.amount.H == 4.0, "Migration processes pending legacy time")
check(saves.validate_data({"state": world.state}) == "", "Migrated state validates")
before = world.state.duplicate(true)
world.migrate_time_precision()
check(world.state == before, "Migration is idempotent")
# Verify in-memory save roundtrip without touching the player's save.
var restored = fixture()
restored.state = bytes_to_var(var_to_bytes(world.state))
world.evolute(0.05)
restored.evolute(0.05)
check(world.state == restored.state, "Save continuation preserves time and production")
check(world.state.time.year == 0.5 and world.state.amount.H == 5.0, "No double-counted migration time")
dispose(world)
dispose(restored)
world = fixture()
world.entities.N.output_and = {"He": 1.0}
@@ -106,5 +87,5 @@ func run() -> void:
dispose(world)
check(Clock.processed_year(0.099) == 0.0, "Do not round a genuine incomplete tenth forward")
check(Clock.processed_year(1e9 + 0.45) == 1e9 + 0.4, "Large ages preserve fractional precision")
print("Time migration: %d checks, %d failures" % [checks, failures])
print("Time state: %d checks, %d failures" % [checks, failures])
quit(0 if failures == 0 else 1)