private void readShips() { lock (shipyardLock) { // Obtain current inventory from configuration ShipMonitorConfiguration configuration = ShipMonitorConfiguration.FromFile(); // Build a new shipyard List <Ship> newShipyard = new List <Ship>(); foreach (Ship ship in configuration.shipyard) { newShipyard.Add(ship); } // Now order the list by model newShipyard = newShipyard.OrderBy(s => s.model).ToList(); // Update the shipyard shipyard.Clear(); foreach (Ship ship in newShipyard) { AddShip(ship); } currentShipId = configuration.currentshipid; } }
public void writeShips() { lock (shipyardLock) { // Write ship configuration with current inventory ShipMonitorConfiguration configuration = new ShipMonitorConfiguration() { currentshipid = currentShipId, shipyard = shipyard }; configuration.ToFile(); } }
private void readShips() { lock (shipyardLock) { // Obtain current inventory from configuration ShipMonitorConfiguration configuration = ShipMonitorConfiguration.FromFile(); // Build a new shipyard List <Ship> newShiplist = configuration.shipyard.OrderBy(s => s.model).ToList(); // Update the shipyard shipyard = new ObservableCollection <Ship>(newShiplist); currentShipId = configuration.currentshipid; } }
private void writeShips() { lock (shipyardLock) { // Write ship configuration with current inventory ShipMonitorConfiguration configuration = new ShipMonitorConfiguration() { currentshipid = currentShipId, shipyard = shipyard }; configuration.ToFile(); } // Make sure the UI is up to date RaiseOnUIThread(ShipyardUpdatedEvent, shipyard); }
/// <summary> /// Obtain ships configuration from a file. If the file name is not supplied the the default /// path of Constants.Data_DIR\shipmonitor.json is used /// </summary> public static ShipMonitorConfiguration FromFile(string filename = null) { if (filename == null) { filename = Constants.DATA_DIR + @"\shipmonitor.json"; } ShipMonitorConfiguration configuration = new ShipMonitorConfiguration(); if (File.Exists(filename)) { try { string data = Files.Read(filename); if (data != null) { configuration = JsonConvert.DeserializeObject <ShipMonitorConfiguration>(data); } } catch (Exception ex) { Logging.Debug("Failed to read ship configuration", ex); } } else { // Used to be in a separate 'ships' file so try that to allow migration string oldFilename = Constants.DATA_DIR + @"\ships.json"; if (File.Exists(oldFilename)) { try { string oldData = Files.Read(oldFilename); if (oldData != null) { Dictionary <string, ObservableCollection <Ship> > oldShipsConfiguration = JsonConvert.DeserializeObject <Dictionary <string, ObservableCollection <Ship> > >(oldData); // At this point the old file is confirmed to have been there - migrate it // There was a bug that caused null entries to be written to the ships configuration; remove these if present ObservableCollection <Ship> oldShips = new ObservableCollection <Ship>(oldShipsConfiguration["ships"].Where(x => x.role != null)); configuration.shipyard = oldShips; File.Delete(oldFilename); configuration.ToFile(); } } catch { // There was a problem parsing the old file, just press on } } } if (configuration == null) { configuration = new ShipMonitorConfiguration(); } // Populate static information from definitions foreach (Ship ship in configuration.shipyard) { ship.Augment(); } configuration.dataPath = filename; return(configuration); }