<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ColossalFramework;
using UnityEngine;

namespace ElevatedTrainStationTrack
{
    public class Initializer : MonoBehaviour
    {
        public bool isInitialized;
        Dictionary<string, NetInfo> m_customPrefabs;

        public void Awake()
        {
            DontDestroyOnLoad(this);
            m_customPrefabs = new Dictionary<string, NetInfo>();
        }

        public void OnLevelWasLoaded(int level)
        {
            if (level == 6)
            {
                m_customPrefabs.Clear();
                isInitialized = false;
            }
        }

        public void Update()
        {
            if (isInitialized) return;
            try
            {
                GameObject.Find("Public Transport").GetComponent<NetCollection>();
            }
            catch (Exception)
            {
                return;
            }
            Loading.QueueLoadingAction(Loading.ActionWrapper(InitializeImpl));
            isInitialized = true;
        }

        private void InitializeImpl()
        {

            var originalPrefab =
                Resources.FindObjectsOfTypeAll<NetInfo>().
                FirstOrDefault(netInfo => netInfo.name == "Train Station Track");
            if (originalPrefab == null)
            {
                Debug.LogError("ElevatedTrainStationTrack - Station track prefab not found");
                return;
            }
            NetInfo elevatedPrefab = null;
            var stationTrackEleva = "Station Track Eleva";
            if (!m_customPrefabs.ContainsKey(stationTrackEleva))
            {
                elevatedPrefab = ClonePrefab(originalPrefab, stationTrackEleva);
                if (elevatedPrefab != null)
                {
                    SetupElevatedPrefab(elevatedPrefab, originalPrefab);
                    m_customPrefabs.Add(stationTrackEleva, elevatedPrefab);
                }
                else
                {
                    Debug.LogError("ElevatedTrainStationTrack - Couldn't make elevated prefab");
                }  
            }
            var stationTrackTunnel = "Station Track Tunnel";
            if (!m_customPrefabs.ContainsKey(stationTrackTunnel))
            {
                var tunnelPrefab = ClonePrefab(originalPrefab, stationTrackTunnel);
                if (tunnelPrefab != null)
                {
                    SetupTunnelPrefab(tunnelPrefab, originalPrefab, elevatedPrefab);
                    m_customPrefabs.Add(stationTrackTunnel, tunnelPrefab);
                }
                else
                {
                    Debug.LogError("ElevatedTrainStationTrack - Couldn't make tunnel prefab");
                }
            }

            var stationTrackSunken = "Station Track Sunken";
            if (!m_customPrefabs.ContainsKey(stationTrackSunken))
            {
                var sunkenPrefab = ClonePrefab(originalPrefab, stationTrackSunken);
                if (sunkenPrefab != null)
                {
                    SetupSunkenPrefab(sunkenPrefab, originalPrefab);
                    m_customPrefabs.Add(stationTrackSunken, sunkenPrefab);
                }
                else
                {
                    Debug.LogError("ElevatedTrainStationTrack - Couldn't make sunken prefab");
                }
            }

            foreach (var mCustomPrefab in m_customPrefabs)
            {
                PrefabCollection<NetInfo>.InitializePrefabs(mCustomPrefab.Key, new[] { mCustomPrefab.Value }, new string[] { null });
            }
        }

        private static void SetupElevatedPrefab(NetInfo elevatedPrefab, NetInfo originalPrefab)
        {
            var ai = elevatedPrefab.GetComponent<TrainTrackAI>();
            ai.m_elevatedInfo = elevatedPrefab;
            ai.m_outsideConnection = originalPrefab.GetComponent<TrainTrackAI>().m_outsideConnection;
            elevatedPrefab.m_followTerrain = false;
            elevatedPrefab.m_flattenTerrain = false;
            elevatedPrefab.m_createGravel = false;
            elevatedPrefab.m_createPavement = false;
            elevatedPrefab.m_createRuining = false;
            elevatedPrefab.m_requireSurfaceMaps = false;
            elevatedPrefab.m_clipTerrain = false;
            elevatedPrefab.m_snapBuildingNodes = false;
            elevatedPrefab.m_placementStyle = ItemClass.Placement.Procedural;
            elevatedPrefab.m_useFixedHeight = true;
            elevatedPrefab.m_lowerTerrain = true;
            elevatedPrefab.m_availableIn = ItemClass.Availability.GameAndAsset;
            var elevatedTrack = Resources.FindObjectsOfTypeAll<NetInfo>().
                FirstOrDefault(netInfo => netInfo.name == "Train Track Elevated");
            if (elevatedTrack != null)
            {
                var etstMesh = Util.LoadMesh(String.Concat(Util.AssemblyDirectory, "/TTNR.obj"), "ETST ");
                var etstSegmentLodMesh = Util.LoadMesh(String.Concat(Util.AssemblyDirectory, "/TTNR_LOD.obj"), "ETST_SLOD");
                var etstNodeLodMesh = Util.LoadMesh(String.Concat(Util.AssemblyDirectory, "/TTNR_Node_LOD.obj"), "ETST_NLOD");
                elevatedPrefab.m_segments[0].m_segmentMaterial = elevatedTrack.m_segments[0].m_segmentMaterial;
                elevatedPrefab.m_segments[0].m_material = elevatedTrack.m_segments[0].m_material;
                elevatedPrefab.m_segments[0].m_mesh = etstMesh;
                elevatedPrefab.m_segments[0].m_segmentMesh = etstMesh;
                elevatedPrefab.m_segments[0].m_lodMaterial = elevatedTrack.m_segments[0].m_lodMaterial;
                elevatedPrefab.m_segments[0].m_lodMesh = etstSegmentLodMesh;
                elevatedPrefab.m_nodes[0].m_material = elevatedTrack.m_nodes[0].m_material;
                elevatedPrefab.m_nodes[0].m_nodeMaterial = elevatedTrack.m_nodes[0].m_nodeMaterial;
                elevatedPrefab.m_nodes[0].m_lodMaterial = elevatedTrack.m_nodes[0].m_lodMaterial;
                elevatedPrefab.m_nodes[0].m_lodMesh = etstNodeLodMesh;
                elevatedPrefab.m_nodes[0].m_nodeMesh = etstMesh;
                elevatedPrefab.m_nodes[0].m_mesh = etstMesh;
            }
        }

        private static void SetupTunnelPrefab(NetInfo tunnelPrefab, NetInfo originalPrefab, NetInfo elevatedPrefab)
        {<commit_msg>Remove power lines poles from elevated station track<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using ColossalFramework;
using UnityEngine;

namespace ElevatedTrainStationTrack
{
    public class Initializer : MonoBehaviour
    {
        public bool isInitialized;
        Dictionary<string, NetInfo> m_customPrefabs;

        public void Awake()
        {
            DontDestroyOnLoad(this);
            m_customPrefabs = new Dictionary<string, NetInfo>();
        }

        public void OnLevelWasLoaded(int level)
        {
            if (level == 6)
            {
                m_customPrefabs.Clear();
                isInitialized = false;
            }
        }

        public void Update()
        {
            if (isInitialized) return;
            try
            {
                GameObject.Find("Public Transport").GetComponent<NetCollection>();
            }
            catch (Exception)
            {
                return;
            }
            Loading.QueueLoadingAction(Loading.ActionWrapper(InitializeImpl));
            isInitialized = true;
        }

        private void InitializeImpl()
        {

            var originalPrefab =
                Resources.FindObjectsOfTypeAll<NetInfo>().
                FirstOrDefault(netInfo => netInfo.name == "Train Station Track");
            if (originalPrefab == null)
            {
                Debug.LogError("ElevatedTrainStationTrack - Station track prefab not found");
                return;
            }
            NetInfo elevatedPrefab = null;
            var stationTrackEleva = "Station Track Eleva";
            if (!m_customPrefabs.ContainsKey(stationTrackEleva))
            {
                elevatedPrefab = ClonePrefab(originalPrefab, stationTrackEleva);
                if (elevatedPrefab != null)
                {
                    SetupElevatedPrefab(elevatedPrefab, originalPrefab);
                    m_customPrefabs.Add(stationTrackEleva, elevatedPrefab);
                }
                else
                {
                    Debug.LogError("ElevatedTrainStationTrack - Couldn't make elevated prefab");
                }  
            }
            var stationTrackTunnel = "Station Track Tunnel";
            if (!m_customPrefabs.ContainsKey(stationTrackTunnel))
            {
                var tunnelPrefab = ClonePrefab(originalPrefab, stationTrackTunnel);
                if (tunnelPrefab != null)
                {
                    SetupTunnelPrefab(tunnelPrefab, originalPrefab, elevatedPrefab);
                    m_customPrefabs.Add(stationTrackTunnel, tunnelPrefab);
                }
                else
                {
                    Debug.LogError("ElevatedTrainStationTrack - Couldn't make tunnel prefab");
                }
            }

            var stationTrackSunken = "Station Track Sunken";
            if (!m_customPrefabs.ContainsKey(stationTrackSunken))
            {
                var sunkenPrefab = ClonePrefab(originalPrefab, stationTrackSunken);
                if (sunkenPrefab != null)
                {
                    SetupSunkenPrefab(sunkenPrefab, originalPrefab);
                    m_customPrefabs.Add(stationTrackSunken, sunkenPrefab);
                }
                else
                {
                    Debug.LogError("ElevatedTrainStationTrack - Couldn't make sunken prefab");
                }
            }
            PrefabCollection<NetInfo>.InitializePrefabs("Rail Extensions", m_customPrefabs.Values.ToArray(), null);
        }

        private static void SetupElevatedPrefab(NetInfo elevatedPrefab, NetInfo originalPrefab)
        {
            var ai = elevatedPrefab.GetComponent<TrainTrackAI>();
            ai.m_elevatedInfo = elevatedPrefab;
            ai.m_outsideConnection = originalPrefab.GetComponent<TrainTrackAI>().m_outsideConnection;
            elevatedPrefab.m_followTerrain = false;
            elevatedPrefab.m_flattenTerrain = false;
            elevatedPrefab.m_createGravel = false;
            elevatedPrefab.m_createPavement = false;
            elevatedPrefab.m_createRuining = false;
            elevatedPrefab.m_requireSurfaceMaps = false;
            elevatedPrefab.m_clipTerrain = false;
            elevatedPrefab.m_snapBuildingNodes = false;
            elevatedPrefab.m_placementStyle = ItemClass.Placement.Procedural;
            elevatedPrefab.m_useFixedHeight = true;
            elevatedPrefab.m_lowerTerrain = true;
            elevatedPrefab.m_availableIn = ItemClass.Availability.GameAndAsset;
            foreach(var lane in elevatedPrefab.m_lanes)
            {
                var mLaneProps = lane.m_laneProps;
                if (mLaneProps == null)
                {
                    continue;
                }
                var props = mLaneProps.m_props;
                if (props == null)
                {
                    continue;
                }
                mLaneProps.m_props = (from prop in props where prop != null let mProp = prop.m_prop where mProp != null where mProp.name != "RailwayPowerline" select prop).ToArray();
            }
            var elevatedTrack = Resources.FindObjectsOfTypeAll<NetInfo>().
                FirstOrDefault(netInfo => netInfo.name == "Train Track Elevated");
            if (elevatedTrack != null)
            {
                var etstMesh = Util.LoadMesh(String.Concat(Util.AssemblyDirectory, "/TTNR.obj"), "ETST ");
                var etstSegmentLodMesh = Util.LoadMesh(String.Concat(Util.AssemblyDirectory, "/TTNR_LOD.obj"), "ETST_SLOD");
                var etstNodeLodMesh = Util.LoadMesh(String.Concat(Util.AssemblyDirectory, "/TTNR_Node_LOD.obj"), "ETST_NLOD");
                elevatedPrefab.m_segments[0].m_segmentMaterial = elevatedTrack.m_segments[0].m_segmentMaterial;
                elevatedPrefab.m_segments[0].m_material = elevatedTrack.m_segments[0].m_material;
                elevatedPrefab.m_segments[0].m_mesh = etstMesh;
                elevatedPrefab.m_segments[0].m_segmentMesh = etstMesh;
                elevatedPrefab.m_segments[0].m_lodMaterial = elevatedTrack.m_segments[0].m_lodMaterial;
                elevatedPrefab.m_segments[0].m_lodMesh = etstSegmentLodMesh;
                elevatedPrefab.m_nodes[0].m_material = elevatedTrack.m_nodes[0].m_material;
                elevatedPrefab.m_nodes[0].m_nodeMaterial = elevatedTrack.m_nodes[0].m_nodeMaterial;
                elevatedPrefab.m_nodes[0].m_lodMaterial = elevatedTrack.m_nodes[0].m_lodMaterial;
                elevatedPrefab.m_nodes[0].m_lodMesh = etstNodeLodMesh;
                elevatedPrefab.m_nodes[0].m_nodeMesh = etstMesh;
                elevatedPrefab.m_nodes[0].m_mesh = etstMesh;
            }
        }

        private static void SetupTunnelPrefab(NetInfo tunnelPrefab, NetInfo originalPrefab, NetInfo elevatedPrefab)
        {
{'fraction_non_alphanumeric': 0.07954545454545454, 'fraction_numerical': 0.002840909090909091, 'mean_word_length': 1.8904494382022472, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1146166', 'n_tokens_mistral': 3779, 'n_tokens_neox': 3440, 'n_words': 565}