public static Tuple <XElement, ScenarioVehicle> GenerateVehicle(XElement prevElem, AvailableVehicle vehicle) { var retVehicle = new ScenarioVehicle(); retVehicle.CopyFrom(vehicle); XDocument doc; XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable()); mgr.AddNamespace("d", "http://www.kuju.com/TnT/2003/Delta"); XmlParserContext ctx = new XmlParserContext(null, mgr, null, XmlSpace.Default); using (XmlReader reader = XmlReader.Create(new StringReader(VehicleTemplates.GetXml(vehicle.Type)), null, ctx)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); doc = XDocument.Parse(xmlDoc.OuterXml); } var cOwnedEntity = doc.Root; XElement typeSpecificElement; switch (vehicle.Type) { case VehicleType.Engine: typeSpecificElement = cOwnedEntity.Element("Component").Element("cEngine"); break; case VehicleType.Wagon: typeSpecificElement = cOwnedEntity.Element("Component").Element("cWagon"); break; case VehicleType.Tender: typeSpecificElement = cOwnedEntity.Element("Component").Element("cTender"); break; default: throw new Exception("Unknown vehicle type!"); } typeSpecificElement.Element("UniqueNumber").SetValue(retVehicle.Number); var followers = typeSpecificElement.Element("Followers"); var prevFollowers = prevElem.Element("Component").Descendants("Followers").First().Elements("Network-cTrackFollower"); foreach (var cTrackFollower in prevFollowers) { var newFollower = new XElement(cTrackFollower); followers.Add(newFollower); } XElement cEntityContainer = cOwnedEntity .Element("Component").Element("cEntityContainer").Element("StaticChildrenMatrix"); for (var i = 0; i < vehicle.EntityCount; i++) { var newNode = Utilities.GenerateEntityContainerItem(); cEntityContainer.Add(newNode); } XElement cCargoComponent = cOwnedEntity .Element("Component").Element("cCargoComponent").Element("InitialLevel"); for (var i = 0; i < vehicle.CargoCount; i++) { var newNode = Utilities.GenerateCargoComponentItem( vehicle.CargoComponents[i].Item1, vehicle.CargoComponents[i].Item2); cCargoComponent.Add(newNode); } var cPosOri = new XElement(prevElem.Element("Component").Element("cPosOri")); cOwnedEntity.Element("Component").Add(cPosOri); cOwnedEntity.Element("Name").SetValue(vehicle.Name); var idElements = cOwnedEntity .DescendantsAndSelf() .Where(elem => elem.Attribute(Namespace + "id") != null); Random idRandom = new Random(); foreach (var elem in idElements) { var id = idRandom.Next(100000000, 999999999); elem.SetAttributeValue(Namespace + "id", id); } var entityId = cOwnedEntity.Element("EntityID"); entityId.Add(Utilities.GenerateCGUID()); XElement cAbsoluteBlueprintID = cOwnedEntity .Element("BlueprintID").Element("iBlueprintLibrary-cAbsoluteBlueprintID"); cAbsoluteBlueprintID .Element("BlueprintSetID").Element("iBlueprintLibrary-cBlueprintSetID").Element("Provider") .SetValue(vehicle.Provider); cAbsoluteBlueprintID .Element("BlueprintSetID").Element("iBlueprintLibrary-cBlueprintSetID").Element("Product") .SetValue(vehicle.Product); cAbsoluteBlueprintID.Element("BlueprintID").SetValue(vehicle.BlueprintId); if (vehicle.IsReskin) { cAbsoluteBlueprintID = cOwnedEntity .Element("ReskinBlueprintID").Element("iBlueprintLibrary-cAbsoluteBlueprintID"); cAbsoluteBlueprintID .Element("BlueprintSetID").Element("iBlueprintLibrary-cBlueprintSetID").Element("Provider") .SetValue(vehicle.ReskinProvider); cAbsoluteBlueprintID .Element("BlueprintSetID").Element("iBlueprintLibrary-cBlueprintSetID").Element("Product") .SetValue(vehicle.ReskinProduct); cAbsoluteBlueprintID.Element("BlueprintID").SetValue(vehicle.ReskinBlueprintId); } return(new Tuple <XElement, ScenarioVehicle>(cOwnedEntity, retVehicle)); }
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); }