Forked from TimTheTerribleCS/CatenaryReplacer.cs
Last active
September 22, 2016 00:34
-
-
Save boformer/3e5367a7f9a36e3fe5e1bf1658f16f24 to your computer and use it in GitHub Desktop.
Catenary Replacer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ICities; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace CatenaryReplacer | |
{ | |
public class CatenaryReplacerMod : LoadingExtensionBase, IUserMod | |
{ | |
private string[] styles = new string[] { "No Catenary", "Dutch A", "Dutch B", "German A", "PRR A" }; | |
/// <summary> | |
/// Saves the changed lane prop state; | |
/// </summary> | |
private class ReplacementState | |
{ | |
public NetInfo prefab; | |
public int laneIndex; | |
public int propIndex; | |
public PropInfo originalProp; | |
public PropInfo replacementProp; | |
} | |
private readonly List<ReplacementState> changes = new List<ReplacementState>(); | |
public string Name | |
{ | |
get { return "Catenary Replacer"; } | |
} | |
public string Description | |
{ | |
get { return "Replaces the catenaries on railroads with those of your choice"; } | |
} | |
public void OnSettingsUI(UIHelperBase helper) | |
{ | |
var config = Configuration<CatenaryReplacerConfiguration>.Load(); | |
UIHelperBase group = helper.AddGroup("CatenaryReplacer"); | |
group.AddDropdown("Catenary Style", styles, config.Style, OnStyleSelect); | |
} | |
private void OnStyleSelect(int c) | |
{ | |
var config = Configuration<CatenaryReplacerConfiguration>.Load(); | |
config.Style = c; | |
Configuration<CatenaryReplacerConfiguration>.Save(); | |
} | |
public override void OnLevelLoaded(LoadMode mode) | |
{ | |
base.OnLevelLoaded(mode); | |
changes.Clear(); | |
var config = Configuration<CatenaryReplacerConfiguration>.Load(); | |
if (config.Style == 0) | |
{ | |
//NONE | |
ReplaceCatenaries(null, null); | |
} | |
else if (config.Style == 1) | |
{ | |
//Dutch Type A | |
ReplaceCatenaries("760273072.Catenary Type NL2A_Data", "760273072.Catenary Type NL1A_Data"); | |
} | |
else if (config.Style == 2) | |
{ | |
//Dutch Type B | |
ReplaceCatenaries("760273072.Catenary Type NL2B_Data", "760273072.Catenary Type NL1B_Data"); | |
} | |
else if (config.Style == 3) | |
{ | |
//German | |
ReplaceCatenaries("760273072.Catenary Type DE2A_Data", "760273072.Catenary Type DE1A_Data"); | |
} | |
else if (config.Style == 4) | |
{ | |
//PRR A | |
ReplaceCatenaries("760273072.Catenary Type PRR 2A_Data", "760273072.Catenary Type PRR 1A_Data"); | |
} | |
} | |
public override void OnLevelUnloading() | |
{ | |
RevertLaneProps(); | |
} | |
private void ReplaceCatenaries(string doubleReplacement, string singeReplacement) | |
{ | |
ReplaceLaneProp("Train Track", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Train Cargo Track", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Train Track Bridge", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Train Track Elevated", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Train Track Tunnel", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Train Cargo Track Elevated", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Oneway Train Track", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Station Track Sunken", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Train Station Track (C)", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Station Track Elevated Narrow (C)", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Station Track Eleva", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Station Track Elevated (C)", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Station Track Elevated Narrow", "RailwayPowerline", doubleReplacement); | |
ReplaceLaneProp("Rail1L", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L Slope", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L Elevated", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L Bridge", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L Tunnel", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L2W", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L2W Slope", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L2W Elevated", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L2W Bridge", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L2W Tunnel", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1L2SidesStation", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
ReplaceLaneProp("Rail1LStation", "724382534.Rail1LPowerLine_Data", singleReplacement); | |
} | |
private void ReplaceLaneProp(string net, string original, string replacement) | |
{ | |
var netInfo = PrefabCollection<NetInfo>.FindLoaded(net); | |
if (netInfo == null) | |
{ | |
Debug.LogWarning("The name '" + net + "' you entered does not belong to a loaded net!"); | |
return; | |
} | |
var replacementProp = replacement != null ? PrefabCollection<PropInfo>.FindLoaded(replacement) : null; | |
if (replacement != null && replacementProp == null) | |
{ | |
Debug.LogWarning("The name '" + replacement + "' you entered does not belong to a loaded prop!"); | |
return; | |
} | |
if (netInfo.m_lanes != null) | |
{ | |
for (int laneIndex = 0; laneIndex < netInfo.m_lanes.Length; laneIndex++) | |
{ | |
var lane = netInfo.m_lanes[laneIndex]; | |
if (lane != null && lane.m_laneProps != null && lane.m_laneProps.m_props != null) | |
{ | |
for (var propIndex = 0; propIndex < lane.m_laneProps.m_props.Length; propIndex++) | |
{ | |
var laneProp = lane.m_laneProps.m_props[propIndex]; | |
if (laneProp != null && laneProp.m_prop != null && laneProp.m_prop.name == original) | |
{ | |
changes.Add(new ReplacementState | |
{ | |
prefab = netInfo, | |
laneIndex = laneIndex, | |
propIndex = propIndex, | |
originalProp = laneProp.m_prop, | |
replacementProp = replacementProp | |
}); | |
laneProp.m_prop = replacementProp; | |
laneProp.m_finalProp = replacementProp; | |
} | |
} | |
} | |
} | |
} | |
} | |
private void RevertLaneProps() | |
{ | |
foreach (var state in changes) | |
{ | |
var laneProp = state.prefab.m_lanes[state.laneIndex].m_laneProps.m_props[state.propIndex]; | |
laneProp.m_prop = state.originalProp; | |
laneProp.m_finalProp = state.originalProp; | |
} | |
changes.Clear(); | |
} | |
} | |
[ConfigurationPath("CatenaryReplacer.xml")] | |
public class CatenaryReplacerConfiguration | |
{ | |
public int Style { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment