part of 'game_state.dart'; final _gameSaveStateLog = Logger('GameSaveState'); class GameSaveState { String? _savedState; String getState() { final state = _savedState; if (state == null) throw StateError('GameSaveState: no saved state'); return state; } void save(GameState gameState) { _savedState = gameState.toString(); } void load(GameState gameState) { if (_savedState != null) { try { final data = json.decode(_savedState ?? '') as Map; gameState._level = data['level'] as int; gameState._scenario = data['scenario']; // as String; if (data.containsKey('toastMessage')) { gameState._toastMessage = data['toastMessage']; // as String; } if (data.containsKey('scenarioSectionsAdded')) { final List scenarioSectionsAdded = data['scenarioSectionsAdded'] as List; gameState._scenarioSectionsAdded.clear(); for (final item in scenarioSectionsAdded) { gameState._scenarioSectionsAdded.add(item as String); } } if (data.containsKey('scenarioSpecialRules')) { final scenarioSpecialRulesList = data['scenarioSpecialRules'] as List; gameState._scenarioSpecialRules.clear(); for (Map item in scenarioSpecialRulesList) { gameState._scenarioSpecialRules.add(SpecialRule.fromJson(item)); } } gameState._scenarioSectionsVersion++; gameState._currentCampaign = data['currentCampaign']; gameState._round = data['round'] as int; if (data.containsKey('totalRounds')) { gameState._totalRounds = data['totalRounds'] as int; } else { gameState._totalRounds = gameState._round; } final roundStateIdx = data['roundState'] as int?; if (roundStateIdx != null && roundStateIdx >= 0 && roundStateIdx < RoundState.values.length) { gameState._roundState = RoundState.values[roundStateIdx]; } gameState._solo = data['solo'] as bool; if (data.containsKey('autoScenarioLevel')) { gameState._autoScenarioLevel = data['autoScenarioLevel'] as bool; } else { gameState._autoScenarioLevel = true; } if (data.containsKey("allyDeckInOGGloom")) { gameState._allyDeckInOGGloom = data["allyDeckInOGGloom"] as bool; } else { gameState._allyDeckInOGGloom = true; } if (data.containsKey('difficulty')) { gameState._difficulty = data['difficulty'] as int; } else { gameState._difficulty = 0; } //main list — update existing objects in-place final list = data['currentList'] as List; final List newList = []; for (Map item in list) { if (item["noteRow"] == true) { final String itemId = item["id"] as String; final NoteRow? existing = gameState._currentList .whereType() .firstWhereOrNull((e) => e.id == itemId); if (existing != null) { existing.updateFromJson(item); newList.add(existing); } else { newList.add(NoteRow.fromJson(item)); } } else if (item["characterClass"] != null) { final String itemId = item["id"] as String; final Character? existing = gameState._currentList .whereType() .firstWhereOrNull((e) => e.id == itemId); if (existing != null) { existing.updateFromJson(item); newList.add(existing); } else { newList.add(Character.fromJson(item)); } } else if (item["type"] != null) { final String itemId = item["id"] as String; final Monster? existing = gameState._currentList .whereType() .firstWhereOrNull((e) => e.id == itemId); if (existing != null) { existing.updateFromJson(item); newList.add(existing); } else { newList.add(Monster.fromJson(item)); } } } gameState._currentList = newList; gameState._notifyCurrentList(); final unlockedClassesList = data['unlockedClasses'] as List; gameState._unlockedClasses.clear(); for (String item in unlockedClassesList) { gameState._unlockedClasses.add(item); } gameState._unlockedClassesVersion++; //ability decks — update existing instances in-place final decks = data['currentAbilityDecks'] as List; final List newAbilityDecks = []; for (Map item in decks) { final String deckName = item["name"] as String; // Prototype has the full initialized card set; used only for nr lookups. final proto = MonsterAbilityState(deckName); final List newDrawList = []; final List drawPile = item["drawPile"] as List; for (final cardItem in drawPile) { final int nr = cardItem["nr"]; for (final card in proto._drawPile.getList()) { if (card.nr == nr) { newDrawList.add(card); break; } } } final List newDiscardList = []; for (final cardItem in item["discardPile"] as List) { final int nr = cardItem["nr"]; for (final card in proto._drawPile.getList()) { if (card.nr == nr) { newDiscardList.add(card); break; } } } // Reuse existing state; fall back to the prototype for monsters // newly added from a remote device. final state = gameState._currentAbilityDecks.firstWhereOrNull( (s) => s.name == deckName, ) ?? proto; if (item.containsKey("lastRoundDrawn")) { state._lastRoundDrawn = item["lastRoundDrawn"] as int; } state._drawPile.clear(); state._discardPile.clear(); state._drawPile.setList(newDrawList); state._discardPile.setList(newDiscardList); state.drawPileVersion++; newAbilityDecks.add(state); } gameState._currentAbilityDecks.clear(); gameState._currentAbilityDecks.addAll(newAbilityDecks); _loadModifierDeck('modifierDeck', data, gameState); _loadModifierDeck('modifierDeckAllies', data, gameState); _loadLootDeck(data, gameState); if (data["sanctuaryDeck"] != null) { gameState._sanctuaryDeck = SanctuaryDeck.fromJson( data["sanctuaryDeck"], ); } //this is not really a setting, but a scenario command? if (data["showAllyDeck"] != null) { gameState._showAllyDeck = data["showAllyDeck"]; } //////elements final Map elementData = data['elementState']; for (final element in Elements.values) { final raw = elementData[element.index.toString()] as int?; final idx = (raw != null && raw >= 0 && raw < ElementState.values.length) ? raw : ElementState.inert.index; gameState._elementState[element] = ElementState.values[idx]; } } catch (e, stack) { // Deserialization failure: log always (not just debug) so it surfaces // in release builds and can be caught by crash-reporting tools. _gameSaveStateLog.warning('GameSaveState.load error: $e\n$stack'); } } } void saveToDisk(GameState gameState) { if (_savedState == null) { save(gameState); } getIt().write(_savedState!); } void loadFromDisk(GameState gameState) { //have to call after init or element state overridden final String? data = getIt().read(); if (data != null) { _savedState = data; load(gameState); } else { save(gameState); } } void loadFromData(String data, GameState gameState) { //have to call after init or element state overridden _savedState = data; load(gameState); } void _loadLootDeck(Map data, GameState gameState) { gameState._lootDeck.updateFromJson(data["lootDeck"]); } void _loadModifierDeck( String identifier, Map data, GameState gameState, ) { final modifierDeckData = data[identifier] as Map; if (identifier == 'modifierDeck') { gameState._modifierDeck.updateFromJson(modifierDeckData); } else { gameState._modifierDeckAllies.updateFromJson(modifierDeckData); } } }