Exemplo n.º 1
0
        private static void AugmentShipInfo(Ship ship, List <Ship> storedShips)
        {
            ShipsConfiguration     shipsConfiguration = ShipsConfiguration.FromFile();
            Dictionary <int, Ship> lookup             = shipsConfiguration.Ships.ToDictionary(o => o.LocalId);

            Ship shipConfig;

            // Start with our current ship
            if (lookup.TryGetValue(ship.LocalId, out shipConfig))
            {
                // Already exists; grab the relevant information and supplement it
                // Ship config name might be just whitespace, in which case we unset it
                if (shipConfig.Name != null && shipConfig.Name.Trim().Length > 0)
                {
                    ship.Name = shipConfig.Name.Trim();
                }
                if (shipConfig.PhoneticName != null && shipConfig.PhoneticName.Trim().Length > 0)
                {
                    ship.PhoneticName = shipConfig.PhoneticName.Trim();
                }
                ship.CallSign = shipConfig.CallSign;
                ship.Role     = shipConfig.Role;
            }
            else
            {
                // Doesn't already exist; add a callsign and default role
                ship.CallSign = Ship.generateCallsign();
                ship.Role     = ShipRole.Multipurpose;
            }

            // Work through our shipyard
            foreach (Ship storedShip in storedShips)
            {
                if (lookup.TryGetValue(storedShip.LocalId, out shipConfig))
                {
                    // Already exists; grab the relevant information and supplement it
                    storedShip.Name     = shipConfig.Name;
                    storedShip.CallSign = shipConfig.CallSign;
                    storedShip.Role     = shipConfig.Role;
                }
                else
                {
                    // Doesn't already exist; add a callsign and default role
                    storedShip.CallSign = Ship.generateCallsign();
                    storedShip.Role     = ShipRole.Multipurpose;
                }
            }

            // Update our configuration with the new data (this also removes any old redundant ships)
            shipsConfiguration.Ships = new List <Ship>();
            shipsConfiguration.Ships.Add(ship);
            shipsConfiguration.Ships.AddRange(storedShips);
            shipsConfiguration.ToFile();
        }
        /// <summary>
        /// Obtain ships configuration from a file.  If the file name is not supplied the the default
        /// path of %APPDATA%\EDDI\ships.json is used
        /// </summary>
        public static ShipsConfiguration FromFile(string filename=null)
        {
            if (filename == null)
            {
                String dataDir = Environment.GetEnvironmentVariable("AppData") + "\\EDDI";
                Directory.CreateDirectory(dataDir);
                filename = dataDir + "\\ships.json";
            }

            ShipsConfiguration configuration;
            try
            {
                configuration = JsonConvert.DeserializeObject<ShipsConfiguration>(File.ReadAllText(filename));
            }
            catch
            {
                configuration = new ShipsConfiguration();
            }

            configuration.dataPath = filename;
            return configuration;
        }
        /// <summary>
        /// Obtain ships configuration from a file.  If the file name is not supplied the the default
        /// path of %APPDATA%\EDDI\ships.json is used
        /// </summary>
        public static ShipsConfiguration FromFile(string filename = null)
        {
            if (filename == null)
            {
                String dataDir = Environment.GetEnvironmentVariable("AppData") + "\\EDDI";
                Directory.CreateDirectory(dataDir);
                filename = dataDir + "\\ships.json";
            }

            ShipsConfiguration configuration;

            try
            {
                configuration = JsonConvert.DeserializeObject <ShipsConfiguration>(File.ReadAllText(filename));
            }
            catch
            {
                configuration = new ShipsConfiguration();
            }

            configuration.dataPath = filename;
            return(configuration);
        }
 // Handle changes to the Shipyard tab
 private void setShipyardFromConfiguration()
 {
     shipsConfiguration = new ShipsConfiguration();
     List<Ship> ships = new List<Ship>();
     if (commander != null)
     {
         ships.Add(commander.Ship);
         ships.AddRange(commander.StoredShips);
     }
     shipsConfiguration.Ships = ships;
     shipyardData.ItemsSource = ships;
 }