Example #1
0
 public void writeShips()
 {
     lock (shipyardLock)
     {
         // Write ship configuration with current inventory
         ShipMonitorConfiguration configuration = new ShipMonitorConfiguration()
         {
             currentshipid = currentShipId,
             shipyard      = shipyard
         };
         configuration.ToFile();
     }
 }
Example #2
0
 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);
 }
Example #3
0
        /// <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);
        }