public override string RecordData() { if (!includeInSavedGameData) { return(string.Empty); } var saveData = new SaveData(); for (int i = 0; i < questList.Count; i++) { var quest = questList[i]; if (quest == null) { continue; } if (quest.isProcedurallyGenerated) { saveData.proceduralQuests.Add(JsonUtility.ToJson(new QuestProxy(quest))); } else { saveData.staticQuestIds.Add(StringField.GetStringValue(quest.id)); var bytes = QuestStateSerializer.Serialize(quest); saveData.staticQuestData.Add(new ByteData(bytes)); } } saveData.deletedStaticQuests.AddRange(deletedStaticQuests); return(SaveSystem.Serialize(saveData)); }
public override void ApplyData(string data) { if (!includeInSavedGameData) { return; } if (string.IsNullOrEmpty(data)) { return; } try { QuestMachine.isLoadingGame = true; var saveData = SaveSystem.Deserialize <SaveData>(data); if (saveData == null) { return; } // Clear current quest list: DestroyQuestInstances(); // Adds them to deletedStaticQuests, but that's OK since we're going to restore deletedStaticQuests. // Restore dynamic quests: for (int i = 0; i < saveData.proceduralQuests.Count; i++) { try { var questProxy = JsonUtility.FromJson <QuestProxy>(saveData.proceduralQuests[i]); if (questProxy == null) { continue; } var quest = ScriptableObject.CreateInstance <Quest>(); quest.name = questProxy.displayName; questProxy.CopyTo(quest); AddQuest(quest); } catch (Exception e) { if (Debug.isDebugBuild) { Debug.LogWarning("Quest Machine: Unable to restore quest from serialized quest proxy. Message: " + e.Message + "\nData: " + saveData.proceduralQuests[i], this); } } } // Restore list of deleted static quests: deletedStaticQuests.Clear(); deletedStaticQuests.AddRange(saveData.deletedStaticQuests); // Restore static quests: for (int i = 0; i < Mathf.Min(saveData.staticQuestIds.Count, saveData.staticQuestData.Count); i++) { var questID = saveData.staticQuestIds[i]; var questData = saveData.staticQuestData[i]; if (string.IsNullOrEmpty(questID) || questData == null || questData.bytes == null) { continue; } if (deletedStaticQuests.Contains(questID)) { continue; } var quest = QuestMachine.GetQuestAsset(questID); if (quest == null) { quest = originalQuestList.Find(x => string.Equals(StringField.GetStringValue(x.id), questID)); } if (quest == null && Debug.isDebugBuild) { Debug.LogError("Quest Machine: " + name + " Can't find quest " + saveData.staticQuestIds[i] + ". Is it registered with Quest Machine?", this); } if (quest == null) { continue; } quest = quest.Clone(); try { QuestStateSerializer.DeserializeInto(quest, questData.bytes, true); } catch (Exception e) { try { if (Debug.isDebugBuild) { Debug.LogWarning("Quest Machine: Unable to restore quest: " + quest.name + ". Message: " + e.Message, this); } Destroy(quest); } catch (Exception) { } quest = quest.Clone(); } AddQuest(quest); } QuestMachineMessages.RefreshIndicator(this, QuestMachineMessages.GetID(this)); } finally { QuestMachine.isLoadingGame = false; } }