Skip to content

Commit

Permalink
#161 #173 light controller, some mu control readers
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Jan 4, 2024
1 parent 32f6d07 commit 3e6278d
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CCL.Importer/CCL.Importer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="Krafs.Publicizer" Version="2.2.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CCL.Types\CCL.Types.csproj" />
</ItemGroup>
<ItemGroup>
<Publicize Include="Assembly-CSharp" IncludeVirtualMembers="false" />
</ItemGroup>

<ItemGroup>
<Reference Include="Assembly-CSharp" />
Expand Down
3 changes: 2 additions & 1 deletion CCL.Importer/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ private static void Configure(IMapperConfigurationExpression cfg)
.ForMember(c => c.carInstanceIdGenBase, o => o.MapFrom(ccl => ccl.carIdPrefix))
.ForMember(c => c.liveries, o => o.ConvertUsing(new LiveriesConverter()));

cfg.CreateMap<CustomCarType.BrakesSetup, TrainCarType_v2.BrakesSetup>();
cfg.CreateMap<CustomCarType.BrakesSetup, TrainCarType_v2.BrakesSetup>()
.ForMember(b => b.trainBrake, o => o.MapFrom(s => s.brakeValveType));
cfg.CreateMap<CustomCarType.DamageSetup, TrainCarType_v2.DamageSetup>();
cfg.AddMaps(Assembly.GetExecutingAssembly());

Expand Down
11 changes: 9 additions & 2 deletions CCL.Importer/Processing/SimulationProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public override void ExecuteStep(ModelProcessor context)
AddAdditionalControllers(livery.externalInteractablesPrefab);
}

// Add Control Override components
var baseOverrider = livery.prefab.GetComponentInChildren<BaseControlsOverrider>();
if ((livery.prefab.GetComponentsInChildren<OverridableBaseControl>().Length > 0) && !baseOverrider)
{
baseOverrider = livery.prefab.AddComponent<BaseControlsOverrider>();
}
baseOverrider?.OnValidate();

// If we have something that gets referenced through the simConnections decoupling mechanism - these are generally things
// that make ports exist.
var simConnections = livery.prefab.GetComponentInChildren<SimConnectionDefinition>(true);
Expand All @@ -50,8 +58,7 @@ public override void ExecuteStep(ModelProcessor context)
if (needsSimController)
{
var simController = livery.prefab.AddComponent<SimController>();
simController.connectionsDefinition = livery.prefab.GetComponentInChildren<SimConnectionDefinition>(true) ?? AttachSimConnectionsToPrefab(livery.prefab);
simController.otherSimControllers = livery.prefab.GetComponentsInChildren<ASimInitializedController>(true);
simController.OnValidate();
}

// In the event we have a sim controller and *not* a damage controller, we need to add a dummy damage controller
Expand Down
10 changes: 10 additions & 0 deletions CCL.Importer/Proxies/Controllers/PoweredControllerReplacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using CCL.Types.Proxies.Controllers;
using DV.Simulation.Cars;

namespace CCL.Importer.Proxies.Controllers
{
[ProxyMap(typeof(CabLightsControllerProxy), typeof(CabLightsController))]
public class PoweredControllerReplacer : ProxyReplacer
{
}
}
73 changes: 73 additions & 0 deletions CCL.Importer/Proxies/Controls/OverridableControlReplacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using AutoMapper;
using CCL.Types.Proxies.Controls;
using DV.HUD;
using DV.Simulation.Cars;
using DV.Simulation.Controllers;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using UnityEngine;

namespace CCL.Importer.Proxies.Controls
{
[Export(typeof(IProxyReplacer))]
public class OverridableControlReplacer : Profile, IProxyReplacer
{
public OverridableControlReplacer()
{
foreach (var targetType in _controlToImplMap.Values)
{
CreateMap(typeof(OverridableControlProxy), targetType);
}
}

public void CacheAndReplaceProxies(GameObject prefab)
{
foreach (var overridableProxy in prefab.GetComponentsInChildren<OverridableControlProxy>(true))
{
if (_controlToImplMap.TryGetValue(overridableProxy.ControlType, out Type targetType))
{
prefab.StoreComponentsInChildrenInCache(typeof(OverridableControlProxy), targetType, _ => true);
}
}
}

public void MapProxies(GameObject prefab)
{
foreach (var overridableProxy in prefab.GetComponentsInChildren<OverridableControlProxy>(true))
{
if (_controlToImplMap.TryGetValue(overridableProxy.ControlType, out Type targetType))
{
prefab.ConvertFromCache(typeof(OverridableControlProxy), targetType, _ => true);
}
}
}

public void ReplaceProxiesUncached(GameObject prefab)
{

}

private static readonly Dictionary<OverridableControlType, Type> _controlToImplMap = new()
{
{ OverridableControlType.Throttle, typeof(ThrottleControl) },
{ OverridableControlType.TrainBrake, typeof(BrakeControl) },
{ OverridableControlType.Reverser, typeof(ReverserControl) },
{ OverridableControlType.IndBrake, typeof(IndependentBrakeControl) },
{ OverridableControlType.Handbrake, typeof(HandbrakeControl) },
{ OverridableControlType.Sander, typeof(SanderControl) },
{ OverridableControlType.Horn, typeof(HornControl) },
{ OverridableControlType.HeadlightsFront, typeof(HeadlightsControlFront) },
{ OverridableControlType.HeadlightsRear, typeof(HeadlightsControlRear) },
{ OverridableControlType.DynamicBrake, typeof(DynamicBrakeControl) },
{ OverridableControlType.FuelCutoff, typeof(PowerOffControl) },
};
}

[ProxyMap(typeof(InteriorControlsManagerProxy), typeof(InteriorControlsManager))]
[ProxyMap(typeof(BaseControlsOverriderProxy), typeof(BaseControlsOverrider))]
public class InteriorControlReplacer : ProxyReplacer
{

}
}
5 changes: 5 additions & 0 deletions CCL.Importer/Proxies/Indicators/IndicatorProxyMapper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using CCL.Types.Proxies.Indicators;
using DV.Indicators;
using DV.Simulation.Ports;
using System.ComponentModel.Composition;

namespace CCL.Importer.Proxies.Indicators
{
[ProxyMap(typeof(IndicatorPortReaderProxy), typeof(IndicatorPortReader))]
[ProxyMap(typeof(IndicatorBrakeCylinderReaderProxy), typeof(IndicatorBrakeCylinderReader))]
[ProxyMap(typeof(IndicatorBrakePipeReaderProxy), typeof(IndicatorBrakePipeReader))]
[ProxyMap(typeof(IndicatorBrakeReservoirReaderProxy), typeof(IndicatorMainResReader))]

[ProxyMap(typeof(IndicatorEmissionProxy), typeof(IndicatorEmission))]
[ProxyMap(typeof(IndicatorGaugeProxy), typeof(IndicatorGauge))]
[ProxyMap(typeof(IndicatorModelChangerProxy), typeof(IndicatorModelChanger))]
Expand Down
11 changes: 11 additions & 0 deletions CCL.Types/CustomCarType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public class CustomCarType : ScriptableObject, IAssetLoadCallback
public BrakesSetup brakes;
public DamageSetup damage;

[SerializeField, HideInInspector]
private string? brakesJson;
[SerializeField, HideInInspector]
private string? damageJson;

public CustomCarType()
{
NameTranslations = new TranslationData();
Expand All @@ -77,6 +82,9 @@ private void OnValidate()

NameTranslationJson = JSONObject.ToJson(NameTranslations.Items);
CargoTypeJson = CargoTypes.ToJson();

brakesJson = JSONObject.ToJson(brakes);
damageJson = JSONObject.ToJson(damage);
}

public void ForceValidation()
Expand Down Expand Up @@ -116,6 +124,9 @@ public void AfterAssetLoad(AssetBundle bundle)
};
}

brakes = JSONObject.FromJson<BrakesSetup>(brakesJson) ?? new BrakesSetup();
damage = JSONObject.FromJson<DamageSetup>(damageJson) ?? new DamageSetup();

if (liveries != null)
{
foreach (var livery in liveries)
Expand Down
20 changes: 20 additions & 0 deletions CCL.Types/Proxies/Controllers/CabLightsControllerProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEngine;

namespace CCL.Types.Proxies.Controllers
{
public class CabLightsControllerProxy : PoweredControlHandlerBase
{
public Material lightsLit;

public Material lightsUnlit;

[Tooltip("Light components will be enabled/disabled based on threshold")]
public GameObject[] lights;
[Tooltip("Renderers will have material changed between lightsLit/lightsUnlit")]
public Renderer[] lightRenderers;

public float lightsOnControlThreshold = 0.5f;

public float damagedThresholdPercentage = 0.8f;
}
}
13 changes: 13 additions & 0 deletions CCL.Types/Proxies/Controllers/PoweredControlHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CCL.Types.Proxies.Ports;

namespace CCL.Types.Proxies.Controllers
{
public abstract class PoweredControlHandlerBase
{
[PortId(DVPortValueType.CONTROL, false)]
public string controlId;

[FuseId]
public string powerFuseId;
}
}
43 changes: 43 additions & 0 deletions CCL.Types/Proxies/Controls/BaseControlsOverriderProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CCL.Types.Json;
using CCL.Types.Proxies.Ports;
using System;
using UnityEngine;

namespace CCL.Types.Proxies.Controls
{
public class BaseControlsOverriderProxy : MonoBehaviour, ICustomSerialized
{
[Serializable]
public class PortSetter
{
[PortId(null, null)]
public string portId;

public float value;

public PortSetter(string portId, float value)
{
this.portId = portId;
this.value = value;
}
}

public bool propagateNeutralStateToFront;
public bool propagateNeutralStateToRear;

public PortSetter[] neutralStateSetters;
[SerializeField]
[HideInInspector]
private string _neutralStateSettersJson;

public void OnValidate()
{
_neutralStateSettersJson = JSONObject.ToJson(neutralStateSetters);
}

public void AfterImport()
{
neutralStateSetters = JSONObject.FromJson<PortSetter[]>(_neutralStateSettersJson);
}
}
}
70 changes: 70 additions & 0 deletions CCL.Types/Proxies/Controls/DVControlType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CCL.Types.Proxies.Controls
{
public enum OverridableControlType
{
None,
Throttle = DVControlType.Throttle,
TrainBrake = DVControlType.TrainBrake,
Reverser = DVControlType.Reverser,
IndBrake = DVControlType.IndBrake,
Handbrake = DVControlType.Handbrake,
Sander = DVControlType.Sander,
Horn = DVControlType.Horn,
HeadlightsFront = DVControlType.HeadlightsFront,
HeadlightsRear = DVControlType.HeadlightsRear,
DynamicBrake = DVControlType.DynamicBrake,
FuelCutoff = DVControlType.FuelCutoff,
}

public enum DVControlType
{
None,
Throttle,
TrainBrake,
Reverser,
IndBrake,
Handbrake,
Sander,
Horn,
HeadlightsFront,
HeadlightsRear,
StarterFuse,
ElectricsFuse,
TractionMotorFuse,
StarterControl,
DynamicBrake,
CabLight,
Wipers,
FuelCutoff,
ReleaseCyl,
IndHeadlightsTypeFront,
IndHeadlights1Front,
IndHeadlights2Front,
IndHeadlightsTypeRear,
IndHeadlights1Rear,
IndHeadlights2Rear,
IndWipers1,
IndWipers2,
IndCabLight,
IndDashLight,
GearboxA,
GearboxB,
CylCock,
Injector,
Firedoor,
Blower,
Damper,
Blowdown,
CoalDump,
Dynamo,
AirPump,
Lubricator,
Bell
}
}
5 changes: 5 additions & 0 deletions CCL.Types/Proxies/Controls/ExternalControlDefinitionProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public class ExternalControlDefinitionProxy : SimComponentDefinitionProxy
{
public float defaultValue;
public bool saveState;

public override IEnumerable<PortDefinition> ExposedPorts => new[]
{
new PortDefinition(DVPortType.EXTERNAL_IN, DVPortValueType.CONTROL, "EXT_IN")
};
}
}
12 changes: 12 additions & 0 deletions CCL.Types/Proxies/Controls/InteriorControlsManagerProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using UnityEngine;

namespace CCL.Types.Proxies.Controls
{
public class InteriorControlsManagerProxy : MonoBehaviour
{
public bool electricsFuseAffectsIndicators = true;

public List<DVControlType> reverseDirectionList = new List<DVControlType>();
}
}
13 changes: 13 additions & 0 deletions CCL.Types/Proxies/Controls/OverridableControlProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CCL.Types.Proxies.Ports;
using UnityEngine;

namespace CCL.Types.Proxies.Controls
{
public class OverridableControlProxy : MonoBehaviour
{
[PortId(DVPortType.EXTERNAL_IN, DVPortValueType.CONTROL, true)]
public string portId;

public OverridableControlType ControlType;
}
}
22 changes: 22 additions & 0 deletions CCL.Types/Proxies/Indicators/IndicatorBrakeReaderProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CCL.Types.Proxies.Ports;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace CCL.Types.Proxies.Indicators
{
public abstract class IndicatorBrakeReaderProxy : MonoBehaviour
{
[FuseId]
public string fuseId;
}

public class IndicatorBrakeCylinderReaderProxy : IndicatorBrakeReaderProxy { }

public class IndicatorBrakePipeReaderProxy : IndicatorBrakeReaderProxy { }

public class IndicatorBrakeReservoirReaderProxy : IndicatorBrakeReaderProxy { }
}

0 comments on commit 3e6278d

Please sign in to comment.