Ejemplo n.º 1
0
        public override void OnFileFound(string path, string name, bool isMod)
        {
            VehicleEffectsDefinition vehicleEffectsDef = null;

            var xmlSerializer = new XmlSerializer(typeof(VehicleEffectsDefinition));

            try
            {
                using (var streamReader = new System.IO.StreamReader(path))
                {
                    vehicleEffectsDef = xmlSerializer.Deserialize(streamReader) as VehicleEffectsDefinition;
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
                vehicleEffectsDefParseErrors.Add(name + " - " + e.Message);
                return;
            }

            // Add config to loaded list
            vehicleEffectsDef.LoadedFromMod = isMod;
            loadedDefinitions.Add(vehicleEffectsDef);
            definitionPackages.Add(vehicleEffectsDef, name);
        }
Ejemplo n.º 2
0
        public VehicleEffectsDefinition Copy()
        {
            VehicleEffectsDefinition copy = null;

            try
            {
                using (System.IO.StringWriter stringWriter = new System.IO.StringWriter())
                {
                    XmlSerializer s = new XmlSerializer(typeof(VehicleEffectsDefinition));
                    s.Serialize(stringWriter, this);
                    using (System.IO.StringReader stringReader = new System.IO.StringReader(stringWriter.ToString()))
                    {
                        copy = (VehicleEffectsDefinition)s.Deserialize(stringReader);
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogError("Exception trying to copy VehicleEffectsDefinition:\r\n" + e.Message + "\r\n" + e.StackTrace);
            }
            return(copy);
        }
Ejemplo n.º 3
0
        private void UpdateVehicleEffects()
        {
            eventVehicleUpdateStart?.Invoke();

            try
            {
                effectPlacementRequests      = new List <VehicleEffectsDefinition.Vehicle>();
                vehicleEffectsDefParseErrors = new HashSet <string>();
                var lockedVehicles = new HashSet <VehicleInfo>();
                var checkedPaths   = new List <string>();

                var loadedDefinitions  = new List <VehicleEffectsDefinition>();
                var definitionPackages = new Dictionary <VehicleEffectsDefinition, string>();

                // Load definitions from mod folders
                foreach (var current in ColossalFramework.Plugins.PluginManager.instance.GetPluginsInfo())
                {
                    if (current.isEnabled)
                    {
                        var vehicleEffectsDefPath = Path.Combine(current.modPath, filename);

                        // skip files which were already parsed
                        if (checkedPaths.Contains(vehicleEffectsDefPath))
                        {
                            continue;
                        }
                        checkedPaths.Add(vehicleEffectsDefPath);

                        if (!File.Exists(vehicleEffectsDefPath))
                        {
                            continue;
                        }

                        VehicleEffectsDefinition vehicleEffectsDef = null;

                        var xmlSerializer = new XmlSerializer(typeof(VehicleEffectsDefinition));
                        try
                        {
                            using (var streamReader = new System.IO.StreamReader(vehicleEffectsDefPath))
                            {
                                vehicleEffectsDef = xmlSerializer.Deserialize(streamReader) as VehicleEffectsDefinition;
                            }
                        }
                        catch (Exception e)
                        {
                            Logging.LogException(e);
                            vehicleEffectsDefParseErrors.Add(current.name + " - " + e.Message);
                            continue;
                        }

                        Logging.Log("Loading definition from Mod " + current.name);

                        vehicleEffectsDef.LoadedFromMod = true;
                        loadedDefinitions.Add(vehicleEffectsDef);
                        definitionPackages.Add(vehicleEffectsDef, current.name);
                    }
                }

                // Load definitions from prefabs
                for (uint i = 0; i < PrefabCollection <VehicleInfo> .LoadedCount(); i++)
                {
                    var prefab = PrefabCollection <VehicleInfo> .GetLoaded(i);

                    if (prefab == null)
                    {
                        continue;
                    }

                    // search for VehicleEffectsDefinition.xml
                    var asset = PackageManager.FindAssetByName(prefab.name);

                    var crpPath = asset?.package?.packagePath;
                    if (crpPath == null)
                    {
                        continue;
                    }

                    var vehicleEffectsDefPath = Path.Combine(Path.GetDirectoryName(crpPath) ?? "",
                                                             filename);

                    // skip files which were already parsed
                    if (checkedPaths.Contains(vehicleEffectsDefPath))
                    {
                        continue;
                    }
                    checkedPaths.Add(vehicleEffectsDefPath);

                    if (!File.Exists(vehicleEffectsDefPath))
                    {
                        continue;
                    }

                    VehicleEffectsDefinition vehicleEffectsDef = null;

                    var xmlSerializer = new XmlSerializer(typeof(VehicleEffectsDefinition));
                    try
                    {
                        using (var streamReader = new System.IO.StreamReader(vehicleEffectsDefPath))
                        {
                            vehicleEffectsDef = xmlSerializer.Deserialize(streamReader) as VehicleEffectsDefinition;
                        }
                    }
                    catch (Exception e)
                    {
                        Logging.LogException(e);
                        vehicleEffectsDefParseErrors.Add(asset.package.packageName + " - " + e.Message);
                        continue;
                    }

                    loadedDefinitions.Add(vehicleEffectsDef);
                    definitionPackages.Add(vehicleEffectsDef, asset.package.packageName);
                }

                // Apply the effects
                foreach (var vehicleEffectsDef in loadedDefinitions)
                {
                    if (vehicleEffectsDef?.Vehicles == null || vehicleEffectsDef.Vehicles.Count == 0)
                    {
                        vehicleEffectsDefParseErrors.Add(definitionPackages[vehicleEffectsDef] + " - vehicleEffectDef is null or empty.");
                        continue;
                    }

                    foreach (var vehicleDef in vehicleEffectsDef.Vehicles)
                    {
                        ParseVehicleDefinition(vehicleDef, definitionPackages[vehicleEffectsDef], ref changes, ref vehicleEffectsDefParseErrors, ignoreModVehicleParseErrors ? vehicleEffectsDef.LoadedFromMod : false);  // Mods can be used as packs, not all vehicles may be loaded so we surpress vehicle parse errors
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }

            if (vehicleEffectsDefParseErrors?.Count > 0 && showParseErrors)
            {
                var errorMessage = vehicleEffectsDefParseErrors.Aggregate("Error while parsing vehicle effect definition file(s). Assets will work but may have effects missing. Contact the author of the asset(s). \n" + "List of errors:\n", (current, error) => current + (error + '\n'));

                UIView.library.ShowModal <ExceptionPanel>("ExceptionPanel").SetMessage("Vehicle Effects", errorMessage, false);
            }

            vehicleEffectsDefParseErrors = null;
            hasChangedPrefabs            = true;

            eventVehicleUpdateFinished?.Invoke();
        }