Skip to content

Commit

Permalink
Integrate custom types to base car spawner, fix save/load
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Aug 16, 2021
1 parent 4a9131f commit 4f2405b
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 577 deletions.
140 changes: 140 additions & 0 deletions DVCustomCarLoader/CarSpawner_Patches.cs
Original file line number Diff line number Diff line change
@@ -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<TrainCarType> ___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<TrainCar>(true);
if( !CarTypeInjector.IsCustomTypeRegistered(prefabCar.carType) )
{
return true;
}

GameObject carObj = UnityEngine.Object.Instantiate(carToSpawn);
if( !carObj.activeSelf )
{
carObj.SetActive(true);
}
TrainCar spawnedCar = carObj.GetComponentInChildren<TrainCar>();

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<TrainCar>(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<TrainCar>();
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;
}
}
}
4 changes: 2 additions & 2 deletions DVCustomCarLoader/CarTypeInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static class CarTypeInjector
private static readonly Dictionary<string, CustomCar> idToCustomCar = new Dictionary<string, CustomCar>(StringComparer.CurrentCultureIgnoreCase);
private static readonly Dictionary<TrainCarType, CustomCar> carTypeToCustomCar = new Dictionary<TrainCarType, CustomCar>();

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);
Expand Down
28 changes: 0 additions & 28 deletions DVCustomCarLoader/CommsRadioCarSpawner_Awake_Patch.cs

This file was deleted.

52 changes: 0 additions & 52 deletions DVCustomCarLoader/CommsRadioController_Awake_Patch.cs

This file was deleted.

Loading

0 comments on commit 4f2405b

Please sign in to comment.