-
Notifications
You must be signed in to change notification settings - Fork 0
/
Patches.cs
147 lines (142 loc) · 6.79 KB
/
Patches.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using ColossalFramework;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Reflection;
using System.Threading;
using UnityEngine;
namespace RoadThemeTextureSwapper
{
[HarmonyPatch]
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0051", Justification = "Called by harmony")]
class Patches
{
[HarmonyDebug]
[HarmonyTranspiler]
[HarmonyPatch(typeof(NetSegment), "RenderInstance", new Type[] { typeof(RenderManager.CameraInfo), typeof(ushort), typeof(int), typeof(NetInfo), typeof(RenderManager.Instance) }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Ref})]
public static IEnumerable<CodeInstruction> RenderInstanceTranspiler(IEnumerable<CodeInstruction> instructions)
{
Debug.Log("transpiling RenderInstance");
foreach(var instruction in instructions)
{
yield return instruction;
if(instruction.opcode == OpCodes.Callvirt)
{
if (instruction.Calls(typeof(MaterialPropertyBlock).GetMethod("Clear"))){
yield return CodeInstruction.Call(typeof(Patches), "ApplyTextures");
}
}
}
}
public static void ApplyTextures()
{
var netManager = Singleton<NetManager>.instance;
var materialBlock = netManager.m_materialBlock;
foreach (Slots slot in Enum.GetValues(typeof(Slots)))
{
materialBlock.SetTexture(slot.ToString(), getTexture(Settings.selection[(int)slot]));
}
materialBlock.SetVector("_TerrainTextureTiling1", new Vector4(
GetTextureTiling(Settings.selection[(int)Slots._TerrainPavementDiffuse]),
GetTextureTiling(Settings.selection[(int)Slots._TerrainRuinedDiffuse]),
1f,
GetTextureTiling(Settings.selection[(int)Slots._TerrainCliffDiffuse])
));
materialBlock.SetVector("_TerrainTextureTiling2", new Vector4(
GetTextureTiling(Settings.selection[(int)Slots._TerrainGrassDiffuse]),
GetTextureTiling(Settings.selection[(int)Slots._TerrainGravelDiffuse]),
1f,
1f
));
}
public static Texture getTexture(GlobalTextures textureId)
{
var terrainProperties = Singleton<TerrainProperties>.instance;
var netProperties = Singleton<NetProperties>.instance;
var buildingProperties = Singleton<BuildingProperties>.instance;
var vehicleProperties = Singleton<VehicleProperties>.instance;
switch (textureId)
{
case GlobalTextures._TerrainGrassDiffuse:
return terrainProperties.m_grassDiffuse;
case GlobalTextures._TerrainRuinedDiffuse:
return terrainProperties.m_ruinedDiffuse;
case GlobalTextures._TerrainPavementDiffuse:
return terrainProperties.m_pavementDiffuse;
case GlobalTextures._TerrainGravelDiffuse:
return terrainProperties.m_gravelDiffuse;
case GlobalTextures._TerrainCliffDiffuse:
return terrainProperties.m_cliffDiffuse;
case GlobalTextures._TerrainOreDiffuse:
return terrainProperties.m_oreDiffuse;
case GlobalTextures._TerrainOilDiffuse:
return terrainProperties.m_oilDiffuse;
case GlobalTextures._TerrainSandDiffuse:
return terrainProperties.m_sandDiffuse;
case GlobalTextures._RoadUpwardDiffuse:
return netProperties.m_upwardDiffuse;
case GlobalTextures._RoadDownwardDiffuse:
return netProperties.m_downwardDiffuse;
case GlobalTextures._BuildingBaseDiffuse:
return buildingProperties.m_baseDiffuse;
case GlobalTextures._BuildingFloorDiffuse:
return buildingProperties.m_floorDiffuse;
case GlobalTextures._BuildingBurnedDiffuse:
return buildingProperties.m_burnedDiffuse;
case GlobalTextures._BuildingAbandonedDiffuse:
return buildingProperties.m_abandonedDiffuse;
case GlobalTextures._VehicleFloorDiffuse:
return vehicleProperties.m_floorDiffuse;
}
return null;
}
public static float GetTextureTiling(GlobalTextures textureId)
{
var terrainProperties = Singleton<TerrainProperties>.instance;
var netProperties = Singleton<NetProperties>.instance;
var buildingProperties = Singleton<BuildingProperties>.instance;
var vehicleProperties = Singleton<VehicleProperties>.instance;
switch (textureId)
{
case GlobalTextures._TerrainGrassDiffuse:
return terrainProperties.m_grassTiling;
case GlobalTextures._TerrainRuinedDiffuse:
return terrainProperties.m_ruinedTiling;
case GlobalTextures._TerrainPavementDiffuse:
return terrainProperties.m_pavementTiling;
case GlobalTextures._TerrainGravelDiffuse:
return terrainProperties.m_gravelTiling;
case GlobalTextures._TerrainCliffDiffuse:
return terrainProperties.m_cliffTiling;
case GlobalTextures._TerrainOreDiffuse:
return terrainProperties.m_oreTiling;
case GlobalTextures._TerrainOilDiffuse:
return terrainProperties.m_oilTiling;
case GlobalTextures._TerrainSandDiffuse:
return terrainProperties.m_sandTiling;
case GlobalTextures._RoadUpwardDiffuse:
return 0.0625f;
case GlobalTextures._RoadDownwardDiffuse:
return 0.0625f;
default:
return Settings.defaultTiling;
}
}
// Force all Nets to refetch their Textures.
public static void RefreshNets()
{
Debug.Log("refreshing nets");
Settings.LogSettings();
NetManager netManager = Singleton<NetManager>.instance;
for (ushort i = 0; i < netManager.m_nodes.m_buffer.Length; i++)
{
netManager.UpdateNodeRenderer(i, false);
}
for (ushort i = 0; i < netManager.m_segments.m_buffer.Length; i++)
{
netManager.UpdateSegmentRenderer(i, false);
}
}
}
}