/// <summary> /// Writes a new Character to file in the form of a serialized XML. /// </summary> /// <param name="user">The Discord user object of the new player.</param> /// <param name="charName">Name of the new character</param> /// <param name="special">S.P.E.C.I.A.L. stat of the new character</param> /// <param name="skills">The initial Skills of the new player WITH 3 Tag'd Skills</param> /// <param name="traits">The new character's starting traits.</param> public void SaveNewCharacter(SocketUser user, string charName, CharacterStats.SPECIAL special, string skillTag1, string skillTag2, string skillTag3, List <CharacterStats.Trait> traits) { var skills = CalculateInitialSkills(special); if (!(Enum.TryParse(skillTag1.ToLower(), out Character.SkillEnum skillTag1Enum))) { throw new ArgumentException("Couldn't parse given skillTag string.", "skillTag1"); } if (!(Enum.TryParse(skillTag2.ToLower(), out Character.SkillEnum skillTag2Enum))) { throw new ArgumentException("Couldn't parse given skillTag string.", "skillTag2"); } if (!(Enum.TryParse(skillTag3.ToLower(), out Character.SkillEnum skillTag3Enum))) { throw new ArgumentException("Couldn't parse given skillTag string.", "skillTag3"); } var newChar = new Character(user, charName, special, skills, skillTag1Enum, skillTag2Enum, skillTag3Enum, traits); string filePath = Character.saveLoc + user.Id + fileExtension; try { BinarySerialization.WriteToBinaryFile <Character>(filePath, newChar, append: false); } catch (Exception e) { Console.WriteLine("[ERROR] Failed to save new character to disk."); Console.WriteLine(e); return; } }
public static bool OverwriteCharacter(Character newCharacter) { string filePath = Character.saveLoc + newCharacter.DiscordId + Character.fileExtension; try { BinarySerialization.WriteToBinaryFile <Character>(filePath, newCharacter, append: false); } catch (Exception e) { Console.WriteLine("[ERROR] Failed to overwrite character!"); Console.WriteLine(e); return(false); } return(true); }
public static Character LoadCharacter(SocketUser user) { string filePath = Character.saveLoc + user.Id + Character.fileExtension; if (File.Exists(filePath)) { try { return(BinarySerialization.ReadFromBinaryFile <Character>(filePath)); } catch (Exception e) { Console.WriteLine("[ERROR] Failed to load character!"); Console.WriteLine(e); return(null); } } else { return(null); } }