diff --git a/DVCustomCarLoader/CarSpawner_Patches.cs b/DVCustomCarLoader/CarSpawner_Patches.cs new file mode 100644 index 00000000..3f35494c --- /dev/null +++ b/DVCustomCarLoader/CarSpawner_Patches.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using DV; +using HarmonyLib; +using UnityEngine; + +namespace DVCustomCarLoader +{ + [HarmonyPatch(typeof(CommsRadioCarSpawner), nameof(CommsRadioCarSpawner.UpdateCarTypesToSpawn))] + public static class CommsRadioCarSpawner_UpdateCarTypesToSpawn_Patch + { + public static void Prefix( ref bool allowLocoSpawning ) + { + allowLocoSpawning = true; + } + + public static void Postfix( List ___carTypesToSpawn ) + { + var customCarTypes = CustomCarManager.CustomCarTypes.Select(car => car.CarType); + ___carTypesToSpawn.AddRange(customCarTypes); + } + } + + [HarmonyPatch(typeof(CarSpawner))] + public static class CarSpawner_Patches + { + private static Delegate[] carSpawnedDelegates = null; + + private static void RaiseCarSpawned( TrainCar car ) + { + if( carSpawnedDelegates == null ) + { + if( !(AccessTools.Field(typeof(CarSpawner), nameof(CarSpawner.CarSpawned)).GetValue(null) is MulticastDelegate mcd) ) + { + Main.ModEntry.Logger.Error("Couldn't get CarSpawner.CarSpawned delegate"); + return; + } + + carSpawnedDelegates = mcd.GetInvocationList(); + } + + var args = new object[] { car }; + foreach( Delegate d in carSpawnedDelegates ) + { + d.Method.Invoke(d.Target, args); + } + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(CarSpawner.SpawnCar))] + public static bool SpawnCar( + GameObject carToSpawn, RailTrack track, Vector3 position, Vector3 forward, bool playerSpawnedCar, + ref TrainCar __result) + { + TrainCar prefabCar = carToSpawn.GetComponentInChildren(true); + if( !CarTypeInjector.IsCustomTypeRegistered(prefabCar.carType) ) + { + return true; + } + + GameObject carObj = UnityEngine.Object.Instantiate(carToSpawn); + if( !carObj.activeSelf ) + { + carObj.SetActive(true); + } + TrainCar spawnedCar = carObj.GetComponentInChildren(); + + spawnedCar.playerSpawnedCar = playerSpawnedCar; + spawnedCar.InitializeNewLogicCar(); + spawnedCar.SetTrack(track, position, forward); + + //spawnedCar.OnDestroyCar += CustomCarManager.DeregisterCar; + //CustomCarManager.RegisterSpawnedCar(spawnedCar, identifier); + + RaiseCarSpawned(spawnedCar); + + __result = spawnedCar; + return false; + } + + [HarmonyPrefix] + [HarmonyPatch(nameof(CarSpawner.SpawnLoadedCar))] + public static bool SpawnLoadedCar( + GameObject carToSpawn, + string carId, string carGuid, bool playerSpawnedCar, Vector3 position, Quaternion rotation, + bool bogie1Derailed, RailTrack bogie1Track, double bogie1PositionAlongTrack, + bool bogie2Derailed, RailTrack bogie2Track, double bogie2PositionAlongTrack, + bool couplerFCoupled, bool couplerRCoupled, + ref TrainCar __result) + { + TrainCar prefabCar = carToSpawn.GetComponentInChildren(true); + if( !CarTypeInjector.IsCustomTypeRegistered(prefabCar.carType) ) + { + return true; + } + + GameObject carObj = UnityEngine.Object.Instantiate(carToSpawn, position, rotation); + if( !carObj.activeSelf ) + { + carObj.SetActive(true); + } + + TrainCar spawnedCar = carObj.GetComponentInChildren(); + spawnedCar.playerSpawnedCar = playerSpawnedCar; + spawnedCar.InitializeExistingLogicCar(carId, carGuid); + + if( !bogie1Derailed ) + { + spawnedCar.Bogies[0].SetTrack(bogie1Track, bogie1PositionAlongTrack); + } + else + { + spawnedCar.Bogies[0].SetDerailedOnLoadFlag(true); + } + + if( !bogie2Derailed ) + { + spawnedCar.Bogies[1].SetTrack(bogie2Track, bogie2PositionAlongTrack); + } + else + { + spawnedCar.Bogies[1].SetDerailedOnLoadFlag(true); + } + + spawnedCar.frontCoupler.forceCoupleStateOnLoad = true; + spawnedCar.frontCoupler.loadedCoupledState = couplerFCoupled; + spawnedCar.rearCoupler.forceCoupleStateOnLoad = true; + spawnedCar.rearCoupler.loadedCoupledState = couplerRCoupled; + + //spawnedCar.OnDestroyCar += CustomCarManager.DeregisterCar; + //CustomCarManager.RegisterSpawnedCar(spawnedCar, identifier); + + RaiseCarSpawned(spawnedCar); + + __result = spawnedCar; + return false; + } + } +} diff --git a/DVCustomCarLoader/CarTypeInjector.cs b/DVCustomCarLoader/CarTypeInjector.cs index 91f4935e..de676067 100644 --- a/DVCustomCarLoader/CarTypeInjector.cs +++ b/DVCustomCarLoader/CarTypeInjector.cs @@ -23,9 +23,9 @@ public static class CarTypeInjector private static readonly Dictionary idToCustomCar = new Dictionary(StringComparer.CurrentCultureIgnoreCase); private static readonly Dictionary carTypeToCustomCar = new Dictionary(); - public static bool TryGetCarTypeById( string id, out TrainCarType type ) => idToCarType.TryGetValue(id, out type); + public static bool TryGetCarTypeById( string id, out TrainCarType type ) => idToCarType.TryGetValue(id.ToLower(), out type); public static bool TryGetCustomCarByType( TrainCarType carType, out CustomCar car ) => carTypeToCustomCar.TryGetValue(carType, out car); - public static bool TryGetCustomCarById( string id, out CustomCar car ) => idToCustomCar.TryGetValue(id, out car); + public static bool TryGetCustomCarById( string id, out CustomCar car ) => idToCustomCar.TryGetValue(id.ToLower(), out car); public static bool IsCustomTypeRegistered( TrainCarType carType ) => carTypeToCustomCar.ContainsKey(carType); public static bool IsCustomTypeRegistered( string identifier ) => idToCarType.ContainsKey(identifier); diff --git a/DVCustomCarLoader/CommsRadioCarSpawner_Awake_Patch.cs b/DVCustomCarLoader/CommsRadioCarSpawner_Awake_Patch.cs deleted file mode 100644 index 32de9f57..00000000 --- a/DVCustomCarLoader/CommsRadioCarSpawner_Awake_Patch.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using DV; -using HarmonyLib; - -namespace DVCustomCarLoader -{ - [HarmonyPatch(typeof(CommsRadioCarSpawner), "Awake")] - public class CommsRadioCarSpawner_Awake_Patch - { - static void Postfix(CommsRadioCarSpawner __instance) - { - if (!Main.Enabled) - return; - - try - { - //Add trains to car spawner - __instance.UpdateCarTypesToSpawn(true); - - } - catch(Exception e) - { - Main.ModEntry.Logger.Error(e.ToString()); - } - } - } -} \ No newline at end of file diff --git a/DVCustomCarLoader/CommsRadioController_Awake_Patch.cs b/DVCustomCarLoader/CommsRadioController_Awake_Patch.cs deleted file mode 100644 index 41da4a2e..00000000 --- a/DVCustomCarLoader/CommsRadioController_Awake_Patch.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using DV; -using HarmonyLib; - -namespace DVCustomCarLoader -{ - /// - /// After CommsRadioControler finished it's Awake() call, we add our own comms mode after that. - /// - [HarmonyPatch(typeof(CommsRadioController), "Awake")] - public class CommsRadioController_Awake_Patch - { - static void Postfix(CommsRadioController __instance, ref List ___allModes, ref CommsRadioCarSpawner ___carSpawnerControl) - { - if (!Main.Enabled) - return; - - try - { - - if( CustomCarManager.CustomCarTypes.Count <= 0 ) - return; - - //Add our spawner to the comms radio - var ccm = Main.CommsRadioCustomCarManager = __instance.gameObject.AddComponent(); - - //We need to get info from car spawner because we don't have these naturally. - ccm.display = ___carSpawnerControl.display; - ccm.validMaterial = ___carSpawnerControl.validMaterial; - ccm.invalidMaterial = ___carSpawnerControl.invalidMaterial; - ccm.lcdArrow = ___carSpawnerControl.lcdArrow; - ccm.spawnModeEnterSound = ___carSpawnerControl.spawnModeEnterSound; - ccm.spawnVehicleSound = ___carSpawnerControl.spawnVehicleSound; - ccm.confirmSound = ___carSpawnerControl.confirmSound; - ccm.cancelSound = ___carSpawnerControl.cancelSound; - ccm.destinationHighlighterGO = ___carSpawnerControl.destinationHighlighterGO; - ccm.directionArrowsHighlighterGO = ___carSpawnerControl.directionArrowsHighlighterGO; - - //setup script after we add references. - ccm.Setup(); - - //Add our custom mode to comms radio - ___allModes.Add(Main.CommsRadioCustomCarManager); - } - catch(Exception e) - { - Main.ModEntry.Logger.Error(e.ToString()); - } - } - } -} diff --git a/DVCustomCarLoader/CommsRadioCustomCarManager.cs b/DVCustomCarLoader/CommsRadioCustomCarManager.cs deleted file mode 100644 index 3b72ef57..00000000 --- a/DVCustomCarLoader/CommsRadioCustomCarManager.cs +++ /dev/null @@ -1,376 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using DV; -using DV.PointSet; -using UnityEngine; - -namespace DVCustomCarLoader -{ - - /// - /// Allows use of the comms radio to spawn custom cars. - /// - public class CommsRadioCustomCarManager : MonoBehaviour, ICommsRadioMode - { - private static readonly Color laserColor = new Color32(0, 238, 255, 255); - private bool spawnWithTrackDirection = true; - private readonly List potentialTracks = new List(); - private const float POTENTIAL_TRACKS_RADIUS = 200f; - private const float MAX_DISTANCE_FROM_TRACK_POINT = 3f; - private const float TRACK_POINT_POSITION_Y_OFFSET = -1.75f; - private const float SIGNAL_RANGE = 100f; - private const float INVALID_DESTINATION_HIGHLIGHTER_DISTANCE = 20f; - private const float UPDATE_TRACKS_PERIOD = 2.5f; - private const string MODE_NAME = "CUSTOM SPAWNER"; - private const string CONFIRM_TEXT = "confirm"; - private const string CANCEL_TEXT = "cancel"; - public Transform signalOrigin; - public CommsRadioDisplay display; - public Material validMaterial; - public Material invalidMaterial; - public ArrowLCD lcdArrow; - [Header("Sounds")] public AudioClip spawnModeEnterSound; - public AudioClip spawnVehicleSound; - public AudioClip confirmSound; - public AudioClip cancelSound; - [Header("Highlighters")] public GameObject destinationHighlighterGO; - public GameObject directionArrowsHighlighterGO; - private CarDestinationHighlighter destHighlighter; - private RaycastHit hit; - private LayerMask trackMask; - private int selectedCarTypeIndex; - private CustomCar carToSpawn; - private Bounds carBounds; - private bool canSpawnAtPoint; - private RailTrack destinationTrack; - private EquiPointSet.Point? closestPointOnDestinationTrack; - private Coroutine trackUpdateCoro; - private State state; - - - - private void SetState(State newState) - { - if (state == newState) return; - state = newState; - switch (state) - { - case State.EnterSpawnMode: - SetStartingDisplay(); - lcdArrow.TurnOff(); - ButtonBehaviour = ButtonBehaviourType.Regular; - break; - case State.PickCar: - if (trackUpdateCoro == null) - trackUpdateCoro = StartCoroutine(PotentialTracksUpdateCoro()); - ButtonBehaviour = ButtonBehaviourType.Override; - CommsRadioController.PlayAudioFromRadio(spawnModeEnterSound, transform); - break; - case State.PickDestination: - ButtonBehaviour = ButtonBehaviourType.Override; - break; - } - } - - public void Setup() - { - if (!(bool) signalOrigin) - { - Debug.LogError("signalOrigin on CommsRadioCrewVehicle isn't set, using this.transform!", - this); - signalOrigin = transform; - } - - if (display == null) - Debug.LogError("display not set, can't function properly!", this); - if (validMaterial == null || - invalidMaterial == null) - Debug.LogError("Some of the required materials isn't set. Visuals won't be correct.", - this); - if (lcdArrow == null) - Debug.LogError("lcdArrow not set, can't display arrow!", this); - if (destinationHighlighterGO == null || - directionArrowsHighlighterGO == null) - Debug.LogError( - "destinationHighlighterGO or directionArrowsHighlighterGO is not set, can't function properly!!", - this); - if (spawnVehicleSound == null || - spawnModeEnterSound == null || confirmSound == null || cancelSound == null) - Debug.LogError("Not all audio clips set, some sounds won't be played!", - this); - trackMask = LayerMask.GetMask("Default"); - destHighlighter = - new CarDestinationHighlighter(destinationHighlighterGO, directionArrowsHighlighterGO); - } - - private void OnDestroy() - { - if (AppQuitWatcher.isQuitting) return; - destHighlighter.Destroy(); - destHighlighter = null; - } - - public ButtonBehaviourType ButtonBehaviour { get; private set; } - - public void Enable() - { - } - - public void Disable() - { - ClearFlags(); - trackUpdateCoro = null; - StopAllCoroutines(); - } - - public void OverrideSignalOrigin(Transform signalOrigin) - { - this.signalOrigin = signalOrigin; - } - - public void OnUse() - { - switch (state) - { - case State.EnterSpawnMode: - SetCarToSpawn(CustomCarManager.CustomCarTypes[selectedCarTypeIndex]); - SetState(State.PickCar); - break; - case State.PickCar: - if (carToSpawn == null) - { - Debug.LogError("carPrefabToSpawn is null! Something is wrong, can't spawn this car.", - this); - ClearFlags(); - break; - } - - if (canSpawnAtPoint) - { - SetState(State.PickDestination); - CommsRadioController.PlayAudioFromRadio(confirmSound, transform); - break; - } - - ClearFlags(); - CommsRadioController.PlayAudioFromRadio(cancelSound, transform); - break; - case State.PickDestination: - if (canSpawnAtPoint) - { - var forward = closestPointOnDestinationTrack.Value.forward; - if (!spawnWithTrackDirection) forward = -forward; - - var trainCar = carToSpawn.SpawnCar(destinationTrack, - (Vector3)closestPointOnDestinationTrack.Value.position + WorldMover.currentMove, - forward, true); - - if( trainCar != null) - { - SingletonBehaviour.Instance.MarkForDelete(trainCar); - CommsRadioController.PlayAudioFromCar(spawnVehicleSound, trainCar); - CommsRadioController.PlayAudioFromRadio(confirmSound, transform); - canSpawnAtPoint = false; - break; - } - - Debug.LogError("Couldn't spawn car!", carToSpawn.CarPrefab); - ClearFlags(); - break; - } - - ClearFlags(); - CommsRadioController.PlayAudioFromRadio(cancelSound, transform); - break; - } - } - - public void OnUpdate() - { - if (potentialTracks.Count == 0 || (uint) (state - 1) > 1U) return; - if (Physics.Raycast(signalOrigin.position, signalOrigin.forward, out hit, 100f, - trackMask)) - { - var point = hit.point; - foreach (var potentialTrack in potentialTracks) - { - var rangeWithYoffset = - RailTrack.GetPointWithinRangeWithYOffset(potentialTrack, point, 3f, -1.75f); - if (rangeWithYoffset.HasValue) - { - destinationTrack = potentialTrack; - var startingFromIndex = - CarSpawner.FindClosestValidPointForCarStartingFromIndex( - potentialTrack.GetPointSet().points, rangeWithYoffset.Value.index, - carBounds.extents); - var hasValue = startingFromIndex.HasValue; - closestPointOnDestinationTrack = !hasValue ? rangeWithYoffset : startingFromIndex; - canSpawnAtPoint = hasValue; - var position = (Vector3) closestPointOnDestinationTrack.Value.position + - WorldMover.currentMove; - var forward = closestPointOnDestinationTrack.Value.forward; - if (!spawnWithTrackDirection) forward *= -1f; - destHighlighter.Highlight(position, forward, carBounds, - canSpawnAtPoint ? validMaterial : invalidMaterial); - display.SetAction(canSpawnAtPoint ? "confirm" : "cancel"); - if (canSpawnAtPoint && state == State.PickDestination) - { - UpdateLCDRerailDirectionArrow(); - return; - } - - lcdArrow.TurnOff(); - return; - } - } - } - - canSpawnAtPoint = false; - destinationTrack = null; - destHighlighter.Highlight(signalOrigin.position + signalOrigin.forward * 20f, - signalOrigin.right, carBounds, invalidMaterial); - display.SetAction("cancel"); - lcdArrow.TurnOff(); - } - - public bool ButtonACustomAction() - { - switch (state) - { - case State.PickCar: - selectedCarTypeIndex = selectedCarTypeIndex <= 0 - ? CustomCarManager.CustomCarTypes.Count - 1 - : selectedCarTypeIndex - 1; - SetCarToSpawn(CustomCarManager.CustomCarTypes[selectedCarTypeIndex]); - return true; - case State.PickDestination: - if (!canSpawnAtPoint) return false; - spawnWithTrackDirection = !spawnWithTrackDirection; - return true; - default: - Debug.LogError(string.Format("Unexpected state {0}!", state), - this); - return false; - } - } - - public bool ButtonBCustomAction() - { - switch (state) - { - case State.PickCar: - selectedCarTypeIndex = (selectedCarTypeIndex + 1) % CustomCarManager.CustomCarTypes.Count; - SetCarToSpawn(CustomCarManager.CustomCarTypes[selectedCarTypeIndex]); - return true; - case State.PickDestination: - if (!canSpawnAtPoint) return false; - spawnWithTrackDirection = !spawnWithTrackDirection; - return true; - default: - Debug.LogError(string.Format("Unexpected state {0}!", state), - this); - return false; - } - } - - public void SetStartingDisplay() - { - display.SetDisplay("CUSTOM CAR SPAWNER", "Enable custom car spawner?"); - } - - public Color GetLaserBeamColor() - { - return laserColor; - } - - private void SetCarToSpawn(CustomCar car) - { - //carPrefabToSpawn = CarTypes.GetCarPrefab(car.TrainCarType); - carToSpawn = car; - - if( carToSpawn == null) - { - Debug.LogError( - $"Couldn't load car prefab: {car.identifier}! Won't be able to spawn this car.", this); - } - else - { - //var component = carPrefabToSpawn.GetComponent(); - carBounds = car.Bounds; - display.SetContent(car.identifier + "\n" + car.InterCouplerDistance.ToString("F") + "m"); - } - } - - private void UpdatePotentialTracks() - { - potentialTracks.Clear(); - var num = 1f; - while (true) - { - foreach (var allTrack in RailTrackRegistry.AllTracks) - if (RailTrack.GetPointWithinRangeWithYOffset(allTrack, transform.position, num * 200f) - .HasValue) - potentialTracks.Add(allTrack); - - if (potentialTracks.Count <= 0 && num <= 4.0) - { - Debug.LogWarning( - string.Format("No tracks in {0} radius. Expanding radius!", - (float) (num * 200.0)), this); - num += 0.2f; - } - else - { - break; - } - } - - if (potentialTracks.Count != 0) return; - Debug.LogError("No near tracks found. Can't spawn crew vehicle"); - } - - private IEnumerator PotentialTracksUpdateCoro() - { - var CommsRadioCustomCarManager = this; - var lastUpdatedTracksWorldPosition = Vector3.positiveInfinity; - while (true) - { - if ((CommsRadioCustomCarManager.transform.position - WorldMover.currentMove - - lastUpdatedTracksWorldPosition).magnitude > 100.0) - { - CommsRadioCustomCarManager.UpdatePotentialTracks(); - lastUpdatedTracksWorldPosition = - CommsRadioCustomCarManager.transform.position - WorldMover.currentMove; - } - - yield return WaitFor.Seconds(2.5f); - } - } - - private void UpdateLCDRerailDirectionArrow() - { - lcdArrow.TurnOn(Mathf.Sin( - Vector3.SignedAngle( - spawnWithTrackDirection - ? closestPointOnDestinationTrack.Value.forward - : -closestPointOnDestinationTrack.Value.forward, - signalOrigin.forward, Vector3.up) * ((float) Math.PI / 180f)) > 0.0); - } - - private void ClearFlags() - { - destinationTrack = null; - canSpawnAtPoint = false; - destHighlighter.TurnOff(); - SetState(State.EnterSpawnMode); - } - - private enum State - { - EnterSpawnMode, - PickCar, - PickDestination - } - } -} \ No newline at end of file diff --git a/DVCustomCarLoader/CustomCar.cs b/DVCustomCarLoader/CustomCar.cs index 84e4af59..22d6abbb 100644 --- a/DVCustomCarLoader/CustomCar.cs +++ b/DVCustomCarLoader/CustomCar.cs @@ -395,94 +395,6 @@ public bool FinalizePrefab() return true; } - private static Delegate[] carSpawnedDelegates = null; - - private static void RaiseCarSpawned( TrainCar car ) - { - if( carSpawnedDelegates == null ) - { - if( !(AccessTools.Field(typeof(CarSpawner), nameof(CarSpawner.CarSpawned)).GetValue(null) is MulticastDelegate mcd) ) - { - Main.ModEntry.Logger.Error("Couldn't get CarSpawner.CarSpawned delegate"); - return; - } - - carSpawnedDelegates = mcd.GetInvocationList(); - } - - var args = new object[] { car }; - foreach( Delegate d in carSpawnedDelegates ) - { - d.Method.Invoke(d.Target, args); - } - } - - public TrainCar SpawnCar( RailTrack track, Vector3 position, Vector3 forward, bool playerSpawnedCar = false ) - { - GameObject carObj = Object.Instantiate(CarPrefab); - if( !carObj.activeSelf ) - { - carObj.SetActive(true); - } - TrainCar spawnedCar = carObj.GetComponentInChildren(); - - spawnedCar.playerSpawnedCar = playerSpawnedCar; - spawnedCar.InitializeNewLogicCar(); - spawnedCar.SetTrack(track, position, forward); - - spawnedCar.OnDestroyCar += CustomCarManager.DeregisterCar; - CustomCarManager.RegisterSpawnedCar(spawnedCar, identifier); - - RaiseCarSpawned(spawnedCar); - return spawnedCar; - } - - public TrainCar SpawnLoadedCar( - string carId, string carGuid, bool playerSpawnedCar, Vector3 position, Quaternion rotation, - bool bogie1Derailed, RailTrack bogie1Track, double bogie1PositionAlongTrack, - bool bogie2Derailed, RailTrack bogie2Track, double bogie2PositionAlongTrack, - bool couplerFCoupled, bool couplerRCoupled ) - { - GameObject carObj = Object.Instantiate(CarPrefab, position, rotation); - if( !carObj.activeSelf ) - { - carObj.SetActive(true); - } - - TrainCar spawnedCar = carObj.GetComponentInChildren(); - spawnedCar.playerSpawnedCar = playerSpawnedCar; - spawnedCar.InitializeExistingLogicCar(carId, carGuid); - - if( !bogie1Derailed ) - { - spawnedCar.Bogies[0].SetTrack(bogie1Track, bogie1PositionAlongTrack); - } - else - { - spawnedCar.Bogies[0].SetDerailedOnLoadFlag(true); - } - - if( !bogie2Derailed ) - { - spawnedCar.Bogies[1].SetTrack(bogie2Track, bogie2PositionAlongTrack); - } - else - { - spawnedCar.Bogies[1].SetDerailedOnLoadFlag(true); - } - - spawnedCar.frontCoupler.forceCoupleStateOnLoad = true; - spawnedCar.frontCoupler.loadedCoupledState = couplerFCoupled; - spawnedCar.rearCoupler.forceCoupleStateOnLoad = true; - spawnedCar.rearCoupler.loadedCoupledState = couplerRCoupled; - - spawnedCar.OnDestroyCar += CustomCarManager.DeregisterCar; - CustomCarManager.RegisterSpawnedCar(spawnedCar, identifier); - - RaiseCarSpawned(spawnedCar); - return spawnedCar; - } - private float _interCouplerDistance; public float InterCouplerDistance diff --git a/DVCustomCarLoader/CustomCarManager.cs b/DVCustomCarLoader/CustomCarManager.cs index 0f783dac..dd8c02a9 100644 --- a/DVCustomCarLoader/CustomCarManager.cs +++ b/DVCustomCarLoader/CustomCarManager.cs @@ -10,27 +10,27 @@ public static class CustomCarManager { public static List CustomCarTypes = new List(); - private static readonly Dictionary SpawnedCustomCarIds = new Dictionary(); - - public static bool IsRegisteredCustomCar( TrainCar trainCar ) - { - return SpawnedCustomCarIds.ContainsKey(trainCar); - } - - public static bool TryGetCustomCarId( TrainCar trainCar, out string id ) - { - return SpawnedCustomCarIds.TryGetValue(trainCar, out id); - } - - public static void RegisterSpawnedCar( TrainCar car, string identifier ) - { - SpawnedCustomCarIds[car] = identifier; - } - - public static void DeregisterCar( TrainCar car ) - { - SpawnedCustomCarIds.Remove(car); - } + //private static readonly Dictionary SpawnedCustomCarIds = new Dictionary(); + + //public static bool IsRegisteredCustomCar( TrainCar trainCar ) + //{ + // return SpawnedCustomCarIds.ContainsKey(trainCar); + //} + + //public static bool TryGetCustomCarId( TrainCar trainCar, out string id ) + //{ + // return SpawnedCustomCarIds.TryGetValue(trainCar, out id); + //} + + //public static void RegisterSpawnedCar( TrainCar car, string identifier ) + //{ + // SpawnedCustomCarIds[car] = identifier; + //} + + //public static void DeregisterCar( TrainCar car ) + //{ + // SpawnedCustomCarIds.Remove(car); + //} public static void Setup() { diff --git a/DVCustomCarLoader/DVCustomCarLoader.csproj b/DVCustomCarLoader/DVCustomCarLoader.csproj index d62f0376..63488104 100644 --- a/DVCustomCarLoader/DVCustomCarLoader.csproj +++ b/DVCustomCarLoader/DVCustomCarLoader.csproj @@ -74,6 +74,7 @@ + @@ -83,9 +84,6 @@ - - - diff --git a/DVCustomCarLoader/Main.cs b/DVCustomCarLoader/Main.cs index 56012781..4089cd77 100644 --- a/DVCustomCarLoader/Main.cs +++ b/DVCustomCarLoader/Main.cs @@ -10,7 +10,6 @@ namespace DVCustomCarLoader { public static class Main { - public static CommsRadioCustomCarManager CommsRadioCustomCarManager; public static UnityModManager.ModEntry ModEntry; public static bool Enabled; @@ -36,7 +35,8 @@ public static bool Load(UnityModManager.ModEntry modEntry) private static void OnCarChanged( TrainCar newCar ) { - if( newCar && CustomCarManager.IsRegisteredCustomCar(newCar) ) +#if DEBUG + if( newCar && CarTypeInjector.IsCustomTypeRegistered(newCar.carType) ) { // diesel autostart var locoController = newCar.gameObject.GetComponent(); @@ -45,7 +45,8 @@ private static void OnCarChanged( TrainCar newCar ) locoController.EngineRunning = true; } } - } +#endif + } #region Logging diff --git a/DVCustomCarLoader/SaveLoadPatches.cs b/DVCustomCarLoader/SaveLoadPatches.cs index f00162d2..f382b320 100644 --- a/DVCustomCarLoader/SaveLoadPatches.cs +++ b/DVCustomCarLoader/SaveLoadPatches.cs @@ -21,10 +21,10 @@ public static class CarsSaveManager_GetSaveData_Patch { public static void Postfix( TrainCar car, ref JObject __result ) { - if( CustomCarManager.TryGetCustomCarId(car, out string id) ) + if( CarTypeInjector.TryGetCustomCarByType(car.carType, out CustomCar customCar) ) { // custom car detected, save its type - __result.SetString(SaveConstants.CUSTOM_CAR_KEY, id); + __result.SetString(SaveConstants.CUSTOM_CAR_KEY, customCar.identifier); } } } @@ -52,6 +52,8 @@ public static bool Prefix( JObject carData, RailTrack[] tracks, ref TrainCar __r __result = null; return false; } + + // proper custom type, proceed with the spawning // car info string carId = carData.GetString("id"); @@ -97,8 +99,9 @@ public static bool Prefix( JObject carData, RailTrack[] tracks, ref TrainCar __r RailTrack bogie2Track = (!bogie2Derailed) ? tracks[bogie2TrackChildIdx.Value] : null; double bogie2PositionAlongTrack = (!bogie2Derailed) ? bogie2TrackPosition.Value : 0.0; - TrainCar trainCar = customCarType.SpawnLoadedCar( - carId, carGuid, playerSpawnedCar, position.Value + WorldMover.currentMove, Quaternion.Euler(rotation.Value), + TrainCar trainCar = CarSpawner.SpawnLoadedCar( + customCarType.CarPrefab, carId, carGuid, playerSpawnedCar, + position.Value + WorldMover.currentMove, Quaternion.Euler(rotation.Value), bogie1Derailed, bogie1Track, bogie1PositionAlongTrack, bogie2Derailed, bogie2Track, bogie2PositionAlongTrack, coupledFront.Value, coupledRear.Value);