// --- constructors --- /// <summary>Creates a new train without any devices installed.</summary> /// <param name="panel">The array of panel variables.</param> /// <param name="playSound">The delegate to play sounds.</param> internal Train(int[] panel, PlaySoundDelegate playSound) { this.PluginInitializing = false; this.Specs = new VehicleSpecs(0, BrakeTypes.ElectromagneticStraightAirBrake, 0, false, 0); this.State = new VehicleState(0.0, new Speed(0.0), 0.0, 0.0, 0.0, 0.0, 0.0); this.Handles = new ReadOnlyHandles(new Handles(0, 0, 0, false)); this.Doors = DoorStates.None; this.Panel = panel; this.Sounds = new Sounds(playSound); this.AI = new AI(this); }
// --- functions --- internal override bool Load(VehicleSpecs specs, InitializationModes mode) { LoadProperties properties = new LoadProperties(this.PluginFolder, this.TrainFolder, this.PlaySound); bool success; #if !DEBUG try { #endif success = this.Api.Load(properties); base.SupportsAI = properties.AISupport == AISupport.Basic; #if !DEBUG } catch (Exception ex) { base.LastException = ex; throw; } #endif if (success) { base.Panel = properties.Panel ?? new int[] { }; #if !DEBUG try { #endif Api.SetVehicleSpecs(specs); Api.Initialize(mode); #if !DEBUG } catch (Exception ex) { base.LastException = ex; throw; } #endif UpdatePower(); UpdateBrake(); UpdateReverser(); return true; } if (properties.FailureReason != null) { Debug.AddMessage(Debug.MessageType.Error, false, "The train plugin " + base.PluginTitle + " failed to load for the following reason: " + properties.FailureReason); return false; } Debug.AddMessage(Debug.MessageType.Error, false, "The train plugin " + base.PluginTitle + " failed to load for an unspecified reason."); return false; }
// --- functions --- /// <summary>Called to load and initialize the plugin.</summary> /// <param name="specs">The train specifications.</param> /// <param name="mode">The initialization mode of the train.</param> /// <returns>Whether loading the plugin was successful.</returns> internal abstract bool Load(VehicleSpecs specs, InitializationModes mode);
/// <summary>Loads the specified plugin for the specified train.</summary> /// <param name="train">The train to attach the plugin to.</param> /// <param name="pluginFile">The file to the plugin.</param> /// <param name="trainFolder">The train folder.</param> /// <returns>Whether the plugin was loaded successfully.</returns> private static bool LoadPlugin(TrainManager.Train train, string pluginFile, string trainFolder) { string pluginTitle = System.IO.Path.GetFileName(pluginFile); if (!System.IO.File.Exists(pluginFile)) { Interface.AddMessage(Interface.MessageType.Error, true, "The train plugin " + pluginTitle + " could not be found."); return false; } /* * Unload plugin if already loaded. * */ if (train.Plugin != null) { UnloadPlugin(train); } /* * Prepare initialization data for the plugin. * */ BrakeTypes brakeType = (BrakeTypes)train.Cars[train.DriverCar].Specs.BrakeType; int brakeNotches; int powerNotches; bool hasHoldBrake; if (brakeType == BrakeTypes.AutomaticAirBrake) { brakeNotches = 2; powerNotches = train.Specs.MaximumPowerNotch; hasHoldBrake = false; } else { brakeNotches = train.Specs.MaximumBrakeNotch + (train.Specs.HasHoldBrake ? 1 : 0); powerNotches = train.Specs.MaximumPowerNotch; hasHoldBrake = train.Specs.HasHoldBrake; } int cars = train.Cars.Length; VehicleSpecs specs = new VehicleSpecs(powerNotches, brakeType, brakeNotches, hasHoldBrake, cars); InitializationModes mode = (InitializationModes)Game.TrainStart; /* * Check if the plugin is a .NET plugin. * */ Assembly assembly; try { assembly = Assembly.LoadFile(pluginFile); } catch (BadImageFormatException) { assembly = null; } catch (Exception ex) { Interface.AddMessage(Interface.MessageType.Error, false, "The train plugin " + pluginTitle + " could not be loaded due to the following exception: " + ex.Message); return false; } if (assembly != null) { Type[] types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { foreach (Exception e in ex.LoaderExceptions) { Interface.AddMessage(Interface.MessageType.Error, false, "The train plugin " + pluginTitle + " raised an exception on loading: " + e.Message); } return false; } foreach (Type type in types) { if (typeof(IRuntime).IsAssignableFrom(type)) { IRuntime api = assembly.CreateInstance(type.FullName) as IRuntime; train.Plugin = new NetPlugin(pluginFile, trainFolder, api, train); if (train.Plugin.Load(specs, mode)) { return true; } else { train.Plugin = null; return false; } } } Interface.AddMessage(Interface.MessageType.Error, false, "The train plugin " + pluginTitle + " does not export a train interface and therefore cannot be used with openBVE."); return false; } /* * Check if the plugin is a Win32 plugin. * */ try { if (!CheckWin32Header(pluginFile)) { Interface.AddMessage(Interface.MessageType.Error, false, "The train plugin " + pluginTitle + " is of an unsupported binary format and therefore cannot be used with openBVE."); return false; } } catch (Exception ex) { Interface.AddMessage(Interface.MessageType.Error, false, "The train plugin " + pluginTitle + " could not be read due to the following reason: " + ex.Message); return false; } if (!Program.CurrentlyRunningOnWindows | IntPtr.Size != 4) { Interface.AddMessage(Interface.MessageType.Warning, false, "The train plugin " + pluginTitle + " can only be used on 32-bit Microsoft Windows or compatible."); return false; } if (Program.CurrentlyRunningOnWindows && !System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\AtsPluginProxy.dll")) { Interface.AddMessage(Interface.MessageType.Warning, false, "AtsPluginProxy.dll is missing or corrupt- Please reinstall."); return false; } train.Plugin = new Win32Plugin(pluginFile, train); if (train.Plugin.Load(specs, mode)) { return true; } else { train.Plugin = null; Interface.AddMessage(Interface.MessageType.Error, false, "The train plugin " + pluginTitle + " does not export a train interface and therefore cannot be used with openBVE."); return false; } }
/// <summary>Is called after loading to inform the plugin about the specifications of the train.</summary> /// <param name="specs">The specifications of the train.</param> public void SetVehicleSpecs(VehicleSpecs specs) { // TODO: Your old SetVehicleSpec code goes here. }
/// <summary>Is called after loading to inform the plugin about the specifications of the train.</summary> /// <param name="specs">The specifications of the train.</param> public void SetVehicleSpecs(VehicleSpecs specs) { this.Train.Specs = specs; }
// --- functions --- internal override bool Load(VehicleSpecs specs, InitializationModes mode) { int result; try { result = Win32LoadDLL(this.PluginFile, this.PluginFile); } catch (Exception ex) { base.LastException = ex; throw; } if (result == 0) { return false; } try { Win32Load(); } catch (Exception ex) { base.LastException = ex; throw; } int version; try { version = Win32GetPluginVersion(); } catch (Exception ex) { base.LastException = ex; throw; } if (version != 131072) { Debug.AddMessage(Debug.MessageType.Error, false, "The train plugin " + base.PluginTitle + " is of an unsupported version."); try { Win32Dispose(); } catch (Exception ex) { base.LastException = ex; throw; } Win32UnloadDLL(); return false; } try { Win32VehicleSpec win32Spec; win32Spec.BrakeNotches = specs.BrakeNotches; win32Spec.PowerNotches = specs.PowerNotches; win32Spec.AtsNotch = specs.AtsNotch; win32Spec.B67Notch = specs.B67Notch; win32Spec.Cars = specs.Cars; Win32SetVehicleSpec(ref win32Spec.BrakeNotches); } catch (Exception ex) { base.LastException = ex; throw; } try { Win32Initialize((int)mode); } catch (Exception ex) { base.LastException = ex; throw; } UpdatePower(); UpdateBrake(); UpdateReverser(); if (PanelHandle.IsAllocated) { PanelHandle.Free(); } if (SoundHandle.IsAllocated) { SoundHandle.Free(); } PanelHandle = GCHandle.Alloc(Panel, GCHandleType.Pinned); SoundHandle = GCHandle.Alloc(Sound, GCHandleType.Pinned); return true; }
/// <summary>Is called after loading to inform the plugin about the specifications of the train.</summary> /// <param name="specs">The specifications of the train.</param> public void SetVehicleSpecs(VehicleSpecs specs) { trainSpecs = specs; }
internal void SetVechicleSpecs(VehicleSpecs vs) { vSpec = vs; }
internal void SetVehicleSpecs(VehicleSpecs specs) { vSpec = specs; rm.SetVehicleSpecs(specs); atp2.SetVehicleSpecs(specs); ato.SetVehicleSpecs(specs); }
/// <summary>Is called after loading to inform the plugin about the specifications of the train.</summary> /// <param name="specs">The specifications of the train.</param> public void SetVehicleSpecs(VehicleSpecs specs) { this.Specs = specs; LoadScripts(); }
/// <summary>Is called after loading to inform the plugin about the specifications of the train.</summary> /// <param name="specs">The specifications of the train.</param> public void SetVehicleSpecs(VehicleSpecs specs) { ms.SetVehicleSpecs(specs); tims.SetVechicleSpecs(specs); }
internal void SetVehicleSpecs(VehicleSpecs specs) { vSpec = specs; double[] newBrakeRate = new Double[vSpec.BrakeNotches + 1]; for (int i = 0; i < vSpec.BrakeNotches + 1; i++) { newBrakeRate[i] = -1 * MaxSvcBrkRate / vSpec.BrakeNotches * i; } BrakeRate = newBrakeRate; }
internal void SetVehicleSpecs(VehicleSpecs specs) { vSpec = specs; }
// --- functions --- internal override bool Load(VehicleSpecs specs, InitializationModes mode) { int result; try { result = Win32LoadDLL(this.PluginFile, this.PluginFile); } catch (Exception ex) { base.LastException = ex; throw; } if (result == 0) { int errorCode = Marshal.GetLastWin32Error(); string errorMessage = new Win32Exception(errorCode).Message; Interface.AddMessage(Interface.MessageType.Error, true, String.Format("Error loading Win32 plugin: {0} (0x{1})", errorMessage, errorCode.ToString("x"))); return false; } try { Win32Load(); } catch (Exception ex) { base.LastException = ex; throw; } int version; try { version = Win32GetPluginVersion(); } catch (Exception ex) { base.LastException = ex; throw; } if (version != 131072) { Interface.AddMessage(Interface.MessageType.Error, false, "The train plugin " + base.PluginTitle + " is of an unsupported version."); try { Win32Dispose(); } catch (Exception ex) { base.LastException = ex; throw; } Win32UnloadDLL(); return false; } try { Win32VehicleSpec win32Spec; win32Spec.BrakeNotches = specs.BrakeNotches; win32Spec.PowerNotches = specs.PowerNotches; win32Spec.AtsNotch = specs.AtsNotch; win32Spec.B67Notch = specs.B67Notch; win32Spec.Cars = specs.Cars; Win32SetVehicleSpec(ref win32Spec.BrakeNotches); } catch (Exception ex) { base.LastException = ex; throw; } try { Win32Initialize((int)mode); } catch (Exception ex) { base.LastException = ex; throw; } UpdatePower(); UpdateBrake(); UpdateReverser(); if (PanelHandle.IsAllocated) { PanelHandle.Free(); } if (SoundHandle.IsAllocated) { SoundHandle.Free(); } PanelHandle = GCHandle.Alloc(Panel, GCHandleType.Pinned); SoundHandle = GCHandle.Alloc(Sound, GCHandleType.Pinned); return true; }