/// <summary>
        /// This initializer is called when we are making a new copy of a car already
        /// loaded in memory.  We use this one to speed up loading by eliminating the
        /// need to parse the wag file multiple times.
        /// NOTE:  you must initialize all the same variables as you parsed above
        /// </summary>
        public override void Copy(MSTSWagon copy)
        {
            base.Copy(copy);  // each derived level initializes its own variables

            // for example
            //CabSoundFileName = locoCopy.CabSoundFileName;
            //CVFFileName = locoCopy.CVFFileName;
            MSTSElectricLocomotive locoCopy = (MSTSElectricLocomotive)copy;

            PowerSupply.Copy(locoCopy.PowerSupply);
        }
Exemple #2
0
        public static TrainCar Load(Simulator simulator, string wagFilePath, bool initialize = true)
        {
            GenericWAGFile wagFile = SharedGenericWAGFileManager.Get(wagFilePath);
            TrainCar       car;

            if (wagFile.OpenRails != null &&
                wagFile.OpenRails.DLL != null)
            {  // wag file specifies an external DLL
                try
                {
                    // TODO search the path list
                    string   wagFolder = Path.GetDirectoryName(wagFilePath);
                    string   dllPath   = FindTrainCarPlugin(wagFolder, wagFile.OpenRails.DLL);
                    Assembly customDLL = Assembly.LoadFrom(dllPath);
                    object[] args      = new object[] { wagFilePath };
                    car = (TrainCar)customDLL.CreateInstance("ORTS.CustomCar", true, BindingFlags.CreateInstance, null, args, null, null);
                    return(car);
                }
                catch (Exception error)
                {
                    Trace.WriteLine(new FileLoadException(wagFile.OpenRails.DLL, error));
                    // on error, fall through and try loading without the custom dll
                }
            }
            if (!wagFile.IsEngine)
            {
                // its an ordinary MSTS wagon
                car = new MSTSWagon(simulator, wagFilePath);
            }
            else
            {
                // its an ordinary MSTS engine of some type.
                if (wagFile.Engine.Type == null)
                {
                    throw new InvalidDataException(wagFilePath + "\r\n\r\nEngine type missing");
                }

                switch (wagFile.Engine.Type.ToLower())
                {
                // TODO complete parsing of proper car types
                case "electric": car = new MSTSElectricLocomotive(simulator, wagFilePath); break;

                case "steam": car = new MSTSSteamLocomotive(simulator, wagFilePath); break;

                case "diesel": car = new MSTSDieselLocomotive(simulator, wagFilePath); break;

                case "control": car = new MSTSControlTrailerCar(simulator, wagFilePath); break;

                default: throw new InvalidDataException(wagFilePath + "\r\n\r\nUnknown engine type: " + wagFile.Engine.Type);
                }
            }

            MSTSWagon wagon = car as MSTSWagon;

            if (car != null)
            {
                wagon.Load();

                if (initialize)
                {
                    wagon.Initialize();
                }
            }

            return(car);
        }