public async Task LoadBaseDataFromFile(string filePath) { this.FastMoveList.Clear(); this.ChargeMoveList.Clear(); this.Pokedex.Clear(); //Retrieves the data using the serialize attributes BaseDataWrapper data = new BaseDataWrapper(); FileStream file = null; try { file = new FileStream(filePath, FileMode.Open); } catch (IOException) //File does not exist; set everything to defaults { Debug.WriteLine("Error reading base data"); } catch (InvalidOperationException ex) { Debug.WriteLine("Error reading xml:" + ex.Message); } catch (Exception ex) { Debug.WriteLine($"Error reading xml: " + ex.Message); } await this.LoadBaseDataFromFile(file); }
public async Task SaveBaseDataToFile(string filePath) { using (FileStream file = new FileStream(filePath, FileMode.Create)) { using (StreamWriter stream = new StreamWriter(file)) { //Create the DataWrapper object and add the apprpriate data XmlSerializer dataSerializer = new XmlSerializer(typeof(BaseDataWrapper)); BaseDataWrapper data = new BaseDataWrapper(); data.Moves = new MyObservableCollection <Move>(); data.Moves.InsertRange(this.FastMoveList); data.Moves.InsertRange(this.ChargeMoveList); data.PokedexEntries = this.Pokedex; await Task.Run(() => dataSerializer.Serialize(stream, data)); //Saves the data using the attributes defined in each class } } }
public async Task LoadBaseDataFromFile(Stream fileStream) { this.FastMoveList.Clear(); this.ChargeMoveList.Clear(); this.Pokedex.Clear(); //Retrieves the data using the serialize attributes BaseDataWrapper data = new BaseDataWrapper(); try { XmlSerializer dataSerializer = new XmlSerializer(typeof(BaseDataWrapper)); data = await Task.Run(() => dataSerializer.Deserialize(fileStream)) as BaseDataWrapper; } catch (IOException) //File does not exist; set everything to defaults { Debug.WriteLine("Error reading base data"); } catch (InvalidOperationException ex) { Debug.WriteLine("Error reading xml:" + ex.Message); } catch (Exception ex) { Debug.WriteLine($"Error reading xml: " + ex.Message); } //Process the data List <FastMove> tempFastMoves = new List <FastMove>(); List <ChargeMove> tempChargeMoves = new List <ChargeMove>(); foreach (Move move in data.Moves) { if (move.MoveType == MoveType.Fast) { tempFastMoves.Add(move as FastMove); } else if (move.MoveType == MoveType.Charge) { tempChargeMoves.Add(move as ChargeMove); } else { throw new InvalidOperationException("Unknown move type: " + move.MoveType); } } this.FastMoveList.InsertRange(tempFastMoves.OrderBy(x => x.Name)); this.ChargeMoveList.InsertRange(tempChargeMoves.OrderBy(x => x.Name)); List <PokedexEntry> tempPokedex = new List <PokedexEntry>(); foreach (PokedexEntry species in data.PokedexEntries) { //Debug.WriteLine($"{species.Species} has {species.FastMoves.Found fast move {move.Name} for {species.Species}"); for (int i = 0; i < species.FastMoves.Count; i++) { //Debug.WriteLine($"Found fast move {move.Name} for {species.Species}"); PokedexFastMoveWrapper move = species.FastMoves[i]; string moveName = move.FastMove.Name; try { move.FastMove = (this.FastMoveList.Single(x => x.Name.Equals(moveName))); species.FastMoves[i] = move; } catch (Exception ex) { throw new ArgumentException("Cannot find matching fast move " + move.FastMove.Name + " in fast move list.", ex); } } for (int i = 0; i < species.ChargeMoves.Count; i++) { //Debug.WriteLine($"Found fast move {move.Name} for {species.Species}"); PokedexChargeMoveWrapper move = species.ChargeMoves[i]; string moveName = move.ChargeMove.Name; try { move.ChargeMove = (this.ChargeMoveList.Single(x => x.Name.Equals(moveName))); species.ChargeMoves[i] = move; } catch (Exception ex) { throw new ArgumentException("Cannot find matching charge move " + move.ChargeMove.Name + " in charge move list.", ex); } } tempPokedex.Add(species); this.Pokedex.Add(species); } }