Created
July 27, 2017 13:45
-
-
Save TimTheTerribleCS/ea59a0e6656eb08326da8428220e1a0d to your computer and use it in GitHub Desktop.
NMR
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
// C:\Users\Tim\Downloads\NetworkMeshReplacer\NetworkMeshReplacer\NetworkMeshReplacer.dll | |
// NetworkMeshReplacer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | |
// Global type: <Module> | |
// Architecture: AnyCPU (64-bit preferred) | |
// Runtime: .NET 2.0 | |
using ColossalFramework; | |
using ColossalFramework.Plugins; | |
using ColossalFramework.UI; | |
using ICities; | |
using ObjUnity3D; | |
using PrefabHook; | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Runtime.CompilerServices; | |
using System.Runtime.InteropServices; | |
using UnityEngine; | |
[assembly: AssemblyVersion("1.0.0.0")] | |
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] | |
[assembly: AssemblyCompany("")] | |
[assembly: AssemblyConfiguration("")] | |
[assembly: AssemblyCopyright("Copyright © 2015")] | |
[assembly: AssemblyDescription("")] | |
[assembly: AssemblyFileVersion("1.0.0.0")] | |
[assembly: AssemblyProduct("NetworkMeshReplacer")] | |
[assembly: AssemblyTitle("NetworkMeshReplacer")] | |
[assembly: AssemblyTrademark("")] | |
[assembly: CompilationRelaxations(8)] | |
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] | |
[assembly: ComVisible(false)] | |
[assembly: Guid("424fb46d-3d38-419e-80ec-dcdb65f6d762")] | |
namespace NetworkMeshReplacer | |
{ | |
public class NetworkMeshReplacerMod : LoadingExtensionBase, IUserMod, IThreadingExtension | |
{ | |
public string Name | |
{ | |
get | |
{ | |
return "Network Mesh Replacer"; | |
} | |
} | |
public string Description | |
{ | |
get | |
{ | |
return "For testing purposes only"; | |
} | |
} | |
public static string AssemblyDirectory | |
{ | |
get | |
{ | |
PluginManager instance = Singleton<PluginManager>.get_instance(); | |
IEnumerable<PluginManager.PluginInfo> pluginsInfo = instance.GetPluginsInfo(); | |
foreach (PluginManager.PluginInfo current in pluginsInfo) | |
{ | |
try | |
{ | |
IUserMod[] instances = current.GetInstances<IUserMod>(); | |
if (instances.FirstOrDefault<IUserMod>() is NetworkMeshReplacerMod) | |
{ | |
return current.get_modPath(); | |
} | |
} | |
catch | |
{ | |
} | |
} | |
throw new Exception("Failed to find NetworkMeshReplacer assembly!"); | |
} | |
} | |
public override void OnCreated(ILoading loading) | |
{ | |
base.OnCreated(loading); | |
if (!this.IsHooked()) | |
{ | |
return; | |
} | |
NetInfoHook.add_OnPreInitialization(new PrefabEventHandler<NetInfo>(this.OnPreNetInit)); | |
NetInfoHook.Deploy(); | |
} | |
public override void OnLevelLoaded(LoadMode mode) | |
{ | |
base.OnLevelLoaded(mode); | |
if (!this.IsHooked()) | |
{ | |
UIView.get_library().ShowModal<ExceptionPanel>("ExceptionPanel").SetMessage("Missing dependency", this.Name + " requires the 'Prefab Hook' mod to work properly. Please subscribe to the mod and restart the game!", false); | |
} | |
} | |
public override void OnReleased() | |
{ | |
base.OnReleased(); | |
if (!this.IsHooked()) | |
{ | |
return; | |
} | |
NetInfoHook.Revert(); | |
} | |
public void OnPreNetInit(NetInfo prefab) | |
{ | |
if (prefab.get_name() == "Train Track Elevated") | |
{ | |
try | |
{ | |
Mesh mesh = NetworkMeshReplacerMod.LoadMesh("segment.obj", "NMR Segment"); | |
if (mesh != null) | |
{ | |
prefab.m_segments[0].m_mesh = mesh; | |
prefab.m_segments[0].m_segmentMesh = mesh; | |
} | |
Mesh mesh2 = NetworkMeshReplacerMod.LoadMesh("segment_LOD.obj", "NMR Segment LOD"); | |
if (mesh2 != null) | |
{ | |
prefab.m_segments[0].m_lodMesh = mesh2; | |
} | |
Mesh mesh3 = NetworkMeshReplacerMod.LoadMesh("node.obj", "NMR Node"); | |
if (mesh3 != null) | |
{ | |
prefab.m_nodes[0].m_mesh = mesh3; | |
prefab.m_nodes[0].m_nodeMesh = mesh3; | |
} | |
Mesh mesh4 = NetworkMeshReplacerMod.LoadMesh("node_LOD.obj", "NMR Node LOD"); | |
if (mesh4 != null) | |
{ | |
prefab.m_nodes[0].m_lodMesh = mesh4; | |
} | |
prefab.m_segments[0].m_material = NetworkMeshReplacerMod.MakeMaterial("segment", prefab.m_segments[0].m_material); | |
prefab.m_segments[0].m_segmentMaterial = prefab.m_segments[0].m_material; | |
prefab.m_segments[0].m_lodMaterial = NetworkMeshReplacerMod.MakeMaterial("segment_lod", prefab.m_segments[0].m_lodMaterial); | |
prefab.m_nodes[0].m_material = NetworkMeshReplacerMod.MakeMaterial("node", prefab.m_nodes[0].m_material); | |
prefab.m_nodes[0].m_nodeMaterial = prefab.m_nodes[0].m_material; | |
prefab.m_nodes[0].m_lodMaterial = NetworkMeshReplacerMod.MakeMaterial("node_lod", prefab.m_nodes[0].m_lodMaterial); | |
prefab.m_segments = new NetInfo.Segment[] | |
{ | |
prefab.m_segments[0], | |
prefab.m_segments[1] | |
}; | |
prefab.m_nodes = new NetInfo.Node[] | |
{ | |
prefab.m_nodes[0], | |
prefab.m_nodes[1] | |
}; | |
NetInfo.Lane[] lanes = prefab.m_lanes; | |
for (int i = 0; i < lanes.Length; i++) | |
{ | |
NetInfo.Lane lane = lanes[i]; | |
if (lane != null && lane.m_laneProps != null && lane.m_laneProps.m_props != null) | |
{ | |
List<NetLaneProps.Prop> list = new List<NetLaneProps.Prop>(); | |
NetLaneProps.Prop[] props = lane.m_laneProps.m_props; | |
for (int j = 0; j < props.Length; j++) | |
{ | |
NetLaneProps.Prop prop = props[j]; | |
if (prop.m_prop == null || !prop.m_prop.get_name().Contains("RailwayPowerline")) | |
{ | |
list.Add(prop); | |
} | |
} | |
lane.m_laneProps.m_props = list.ToArray(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Debug.LogException(ex); | |
} | |
} | |
} | |
private bool IsHooked() | |
{ | |
foreach (PluginManager.PluginInfo current in Singleton<PluginManager>.get_instance().GetPluginsInfo()) | |
{ | |
if (current.get_publishedFileID().get_AsUInt64() == 530771650uL) | |
{ | |
return true; | |
} | |
} | |
return false; | |
} | |
public static Mesh LoadMesh(string fullPath, string meshName) | |
{ | |
fullPath = Path.Combine(NetworkMeshReplacerMod.AssemblyDirectory, fullPath); | |
if (!File.Exists(fullPath)) | |
{ | |
return null; | |
} | |
Mesh mesh = new Mesh(); | |
using (FileStream fileStream = File.Open(fullPath, FileMode.Open)) | |
{ | |
mesh.LoadOBJ(OBJLoader.LoadOBJ(fileStream)); | |
} | |
mesh.Optimize(); | |
mesh.set_name(meshName); | |
return mesh; | |
} | |
public static Material MakeMaterial(string texturePathBase, Material originalMaterial) | |
{ | |
Texture2D texture2D = null; | |
Texture2D texture2D2 = null; | |
Texture2D texture2D3 = null; | |
string path = Path.Combine(NetworkMeshReplacerMod.AssemblyDirectory, texturePathBase + "_d.png"); | |
if (File.Exists(path)) | |
{ | |
byte[] array = File.ReadAllBytes(path); | |
texture2D = new Texture2D(2, 2); | |
texture2D.set_name("NMR " + texturePathBase + "_d"); | |
texture2D.LoadImage(array); | |
texture2D.set_anisoLevel(8); | |
texture2D.set_filterMode(2); | |
texture2D.Apply(); | |
} | |
path = Path.Combine(NetworkMeshReplacerMod.AssemblyDirectory, texturePathBase + "_xys.png"); | |
if (File.Exists(path)) | |
{ | |
byte[] array = File.ReadAllBytes(path); | |
texture2D2 = new Texture2D(2, 2); | |
texture2D2.set_name("NMR " + texturePathBase + "_xys"); | |
texture2D2.LoadImage(array); | |
texture2D2.set_anisoLevel(8); | |
texture2D2.set_filterMode(2); | |
texture2D2.Apply(); | |
} | |
path = Path.Combine(NetworkMeshReplacerMod.AssemblyDirectory, texturePathBase + "_apr.png"); | |
if (File.Exists(path)) | |
{ | |
byte[] array = File.ReadAllBytes(path); | |
texture2D3 = new Texture2D(2, 2); | |
texture2D3.set_name("NMR " + texturePathBase + "_apr"); | |
texture2D3.LoadImage(array); | |
texture2D3.set_anisoLevel(8); | |
texture2D3.set_filterMode(2); | |
texture2D3.Apply(); | |
} | |
if (texture2D == null && texture2D2 == null && texture2D3 == null) | |
{ | |
return originalMaterial; | |
} | |
Material material = Object.Instantiate<Material>(originalMaterial); | |
if (texture2D != null) | |
{ | |
material.SetTexture("_MainTex", texture2D); | |
} | |
if (texture2D2 != null) | |
{ | |
material.SetTexture("_XYSMap", texture2D2); | |
} | |
if (texture2D3 != null) | |
{ | |
material.SetTexture("_APRMap", texture2D3); | |
} | |
return material; | |
} | |
public void OnAfterSimulationFrame() | |
{ | |
} | |
public void OnAfterSimulationTick() | |
{ | |
} | |
public void OnBeforeSimulationFrame() | |
{ | |
} | |
public void OnBeforeSimulationTick() | |
{ | |
} | |
public void OnCreated(IThreading threading) | |
{ | |
} | |
public void OnUpdate(float realTimeDelta, float simulationTimeDelta) | |
{ | |
if ((Input.GetKey(306) || Input.GetKey(305)) && Input.GetKeyDown(106)) | |
{ | |
this.OnPreNetInit(PrefabCollection<NetInfo>.FindLoaded("Train Track Elevated")); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment