// // Serilaization // /// <summary> /// Only to be called by the RpgCharacterSerializer /// </summary> public RpgCharacterPacket ExportSerializationPacket() { RpgCharacterPacket newPacket = new RpgCharacterPacket(); newPacket.id = this.id.GuidString; newPacket.name = this.name; newPacket.hp = this.hp; newPacket.maxHp = this.maxHp; newPacket.additionalMaxHp = this.additionalMaxHp; newPacket.unallocatedSpPool = this.unallocatedSpPool; newPacket.assignmentType = this.assignmentType; newPacket.xpDataPacket = this.xpData.ExportSerializationPacket(); foreach (StatData stat in this.appliedStats) { StatPacket newStatPacket = stat.ExportSerializationPacket(); newPacket.appliedStats.Add(newStatPacket); } foreach (AbilityData ability in this.appliedAbilities) { AbilityPacket newAbilityPacket = ability.ExportSerializationPacket(); newPacket.appliedAbilities.Add(newAbilityPacket); } return(newPacket); }
/// <summary> /// Save the RpgCharacter to file. The file will be named based off the character's name and GUID /// </summary> public static void SaveCharacter(RpgCharacterData character) { if (character == null) { Debug.LogError("Cannot save a null RpgCharacter!"); return; } RpgCharacterPacket packet = character.ExportSerializationPacket(); string savePath = RpgCharacterSerializer.GetFullPath(character.Name, character.Id.ToString()); RpgCharacterSerializer.WriteToXml(packet, savePath); }
/// <summary> /// Writes the RpgCharacterPacket into a file at the given path. /// The path should be an XML file /// </summary> private static void WriteToXml(RpgCharacterPacket packet, string path) { // Create the subdiretory if it doesn't exist FileInfo saveFileInfo = new FileInfo(path); saveFileInfo.Directory.Create(); // Does nothing if it already exists // Write actual file var serializer = new XmlSerializer(typeof(RpgCharacterPacket)); using (var stream = new FileStream(path, FileMode.Create)) { serializer.Serialize(stream, packet); } }
/// <summary> /// Deserialization Constructor /// </summary> public RpgCharacterData(RpgCharacterPacket rpgCharacterPacket) { Debug.Assert(rpgCharacterPacket != null, "RpgCharacterData could not be deserialized because the packet was null!"); this.id = new SaveableGuid(rpgCharacterPacket.id); this.name = rpgCharacterPacket.name; this.hp = rpgCharacterPacket.hp; this.maxHp = rpgCharacterPacket.maxHp; this.additionalMaxHp = rpgCharacterPacket.additionalMaxHp; this.unallocatedSpPool = rpgCharacterPacket.unallocatedSpPool; this.assignmentType = rpgCharacterPacket.assignmentType; this.xpData = new XpData(rpgCharacterPacket.xpDataPacket); this.appliedStats = new List <StatData>(); this.appliedAbilities = new List <AbilityData>(); this.numOfStats = rpgCharacterPacket.appliedStats.Count; this.numOfAbilities = rpgCharacterPacket.appliedAbilities.Count; // Deserialize abilties first, so we can just loop through stats later to apply abilities foreach (AbilityPacket abilityPacket in rpgCharacterPacket.appliedAbilities) { AbilityData newAbility = new AbilityData(abilityPacket); this.appliedAbilities.Add(newAbility); } foreach (StatPacket statPacket in rpgCharacterPacket.appliedStats) { StatData newStat = new StatData(statPacket, this.appliedStats); this.appliedStats.Add(newStat); // Apply all abilities that this new stat takes foreach (AbilityData ability in this.appliedAbilities) { newStat.ApplyOneAbility(ability); } } this.RecalculateAllLinkedStatsPools(); this.UpdateReadOnlyStatsList(); this.UpdateReadOnlyAbilitiesList(); }
/// <summary> /// Load an RpgCharacter from file, given its name and it ID (GUID as string). /// It is the developer's responsibility to somehow save the GUID of the player before saving the character. /// Saving the GUID can be done in PlayerPrefs or use the SaveSlot system provided in this project /// </summary> /// <param name="characterName">Character name.</param> /// <param name="characterId">Character GUID as a string.</param> public static RpgCharacterData LoadCharacter(string characterName, string characterId) { if (string.IsNullOrEmpty(characterName)) { Debug.LogError("Cannot load an RpgCharacter with a blank name!"); return(null); } if (string.IsNullOrEmpty(characterId)) { Debug.LogError("Cannot load an RpgCharacter with a blank GUID!"); return(null); } string loadPath = RpgCharacterSerializer.GetFullPath(characterName, characterId); RpgCharacterPacket packet = RpgCharacterSerializer.ReadFromXml(loadPath); RpgCharacterData character = new RpgCharacterData(packet); Debug.Assert(character != null, "RpgCharacter of name \"" + characterName + "\" failed to load!"); return(character); }