示例#1
0
        public static VehicleAvailibilityResult IsVehicleAvailable(Vehicle vehicle)
        {
            VehicleAvailibilityResult ret = new VehicleAvailibilityResult
            {
                Available    = false,
                InApFile     = false,
                ApPath       = null,
                PathWithinAp = null
            };

            if (vehicle.IsReskin)
            {
                // We should determine if the reskin itself exists first
                Vehicle reskinAsVehicle = new Vehicle(vehicle.ReskinProvider, vehicle.ReskinProduct, vehicle.ReskinBlueprintId, "Reskin");
                Log.Debug("IsVehicleAvailable: check for reskin {0}", reskinAsVehicle.XmlPath);
                VehicleAvailibilityResult reskinAvailability = IsVehicleAvailable(reskinAsVehicle);
                if (!reskinAvailability.Available)
                {
                    return(ret);
                }
            }
            if (_vehicleTable.ContainsKey(vehicle.XmlPath))
            {
                return(_vehicleTable[vehicle.XmlPath]);
            }
            var xmlPath = vehicle.FullXmlPath;
            var binPath = Path.ChangeExtension(xmlPath, "bin");

            if (File.Exists(binPath))
            {
                ret.Available = true;
                _vehicleTable[vehicle.XmlPath] = ret;
                return(ret);
            }

            var apDirectory = Path.Combine(Properties.Settings.Default.TsPath, "Assets", vehicle.Provider, vehicle.Product);

            if (Directory.Exists(apDirectory))
            {
                var    apFiles     = Directory.GetFiles(apDirectory, "*.ap");
                var    found       = false;
                string foundApPath = "";
                var    binName     = Path.ChangeExtension(vehicle.BlueprintId, "bin").Replace('\\', '/');
                foreach (var apPath in apFiles)
                {
                    var result = false;
                    try
                    {
                        var zipFile = ZipFile.Read(apPath);
                        result = zipFile.Any(entry => entry.FileName.Equals(binName));
                    }
                    catch (ZipException e)
                    {
                        Debug.WriteLine("Error while reading zip file: " + apPath);
                    }

                    if (result)
                    {
                        found       = true;
                        foundApPath = apPath;
                        break;
                    }
                }
                if (found)
                {
                    ret.Available    = true;
                    ret.InApFile     = true;
                    ret.ApPath       = foundApPath;
                    ret.PathWithinAp = binName;
                    _vehicleTable[vehicle.XmlPath] = ret;
                    return(ret);
                }
            }

            _vehicleTable[vehicle.XmlPath] = ret;
            return(ret);
        }
示例#2
0
        public List <Consist> GetConsists(IProgress <int> progress = null)
        {
            progress?.Report(0);
            List <Consist>         ret      = new List <Consist>();
            IEnumerable <XElement> consists = ScenarioXml.Root.Descendants("cConsist");

            foreach (var consistRow in consists.Select((value, i) => (value, i)))
            {
                XElement consist    = consistRow.value;
                int      consistIdx = consistRow.i;
                Consist  consistObj = new Consist();

                XElement driver = consist.Element("Driver").Element("cDriver");
                string   name   = "Loose Consist";
                if (driver != null)
                {
                    IEnumerable <XElement> names = driver.Element("ServiceName").Element("Localisation-cUserLocalisedString").Elements();
                    foreach (XElement nameElement in names)
                    {
                        if (nameElement.Name == "Key" || nameElement.Name == "Other" || nameElement.Value == "")
                        {
                            continue;
                        }
                        name = nameElement.Value;
                        break;
                    }
                    XElement playerDriver = driver.Element("PlayerDriver");
                    if (playerDriver.Value == "1")
                    {
                        consistObj.IsPlayerConsist = true;
                    }
                }

                IEnumerable <XElement> vehicles = consist.Element("RailVehicles").Descendants("cOwnedEntity");
                foreach (var vehicleRow in vehicles.Select((value, i) => (value, i)))
                {
                    int vehicleIdx = vehicleRow.i;

                    // Fill in basic info
                    XElement    vehicle     = vehicleRow.value;
                    XElement    blueprintID = vehicle.Element("BlueprintID").Element("iBlueprintLibrary-cAbsoluteBlueprintID");
                    string      provider    = (string)blueprintID.Element("BlueprintSetID").Element("iBlueprintLibrary-cBlueprintSetID").Element("Provider").Value;
                    string      product     = (string)blueprintID.Element("BlueprintSetID").Element("iBlueprintLibrary-cBlueprintSetID").Element("Product").Value;
                    string      path        = (string)blueprintID.Element("BlueprintID").Value;
                    string      vehicleName = vehicle.Element("Name").Value;
                    VehicleType type;
                    if (vehicle.Descendants("cEngine").Count() > 0)
                    {
                        type = VehicleType.Engine;
                    }
                    else
                    {
                        type = VehicleType.Wagon;
                    }
                    string   number       = "";
                    XElement uniqueNumber = vehicle.Descendants("UniqueNumber").FirstOrDefault();
                    if (uniqueNumber != null)
                    {
                        number = uniqueNumber.Value;
                    }
                    bool     flipped        = false;
                    XElement flippedElement = vehicle.Descendants("Flipped").FirstOrDefault();
                    if (flippedElement != null)
                    {
                        flipped = flippedElement.Value == "1";
                    }

                    ScenarioVehicle v = new ScenarioVehicle(vehicleIdx, provider, product, path, vehicleName, number, flipped);
                    v.Type = type;

                    // Determine if it is a reskin
                    XElement reskinBlueprintIdElement = vehicle.Element("ReskinBlueprintID");
                    try
                    {
                        var reskinProvider = reskinBlueprintIdElement.Descendants("Provider").First().Value;
                        if (reskinProvider != "")
                        {
                            var reskinProduct     = reskinBlueprintIdElement.Descendants("Product").First().Value;
                            var reskinBlueprintId = reskinBlueprintIdElement.Descendants("BlueprintID").First().Value;
                            v.IsReskin          = true;
                            v.ReskinProvider    = reskinProvider;
                            v.ReskinProduct     = reskinProduct;
                            v.ReskinBlueprintId = reskinBlueprintId;
                            Log.Debug("{0} is a reskin at {1}\\{2}\\{3}", v.Name, reskinProvider, reskinProduct, reskinBlueprintId);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Debug("Exception caught when determining whether a ScenarioVehicle is reskin\n{0}", e);
                    }

                    consistObj.Vehicles.Add(v);
                }
                consistObj.Idx  = consistIdx;
                consistObj.Name = name;
                ret.Add(consistObj);
            }

            foreach (var row in ret.Select((value, i) => (value, i)))
            {
                Consist consist = row.value;

                foreach (var vehicle in consist.Vehicles)
                {
                    VehicleAvailibilityResult abilility = VehicleAvailibility.IsVehicleAvailable(vehicle);
                    if (abilility.Available)
                    {
                        if (vehicle.DisplayName != "")
                        {
                            continue;
                        }
                        vehicle.DisplayName = VehicleAvailibility.GetVehicleDisplayName(vehicle);
                        continue;
                    }
                    vehicle.Exists     = VehicleExistance.Missing;
                    consist.IsComplete = VehicleExistance.Missing;
                }
                progress?.Report((int)Math.Ceiling((float)row.i / ret.Count * 100));
            }

            return(ret);
        }