-
Notifications
You must be signed in to change notification settings - Fork 2
/
Patches.cs
270 lines (225 loc) · 13.8 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using System.Reflection;
using BepInEx;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;
namespace FrequencyWalkie
{
public static class Patches
{
public static void ApplyPatches(Harmony harmony)
{
MethodInfo originalSetLocalClientSpeaking = AccessTools.Method(typeof(WalkieTalkie), "SetLocalClientSpeaking");
MethodInfo patchSetLocalClientSpeaking = AccessTools.Method(typeof(Patches), "WalkieTalkie_SetLocalClientSpeaking");
harmony.Patch(originalSetLocalClientSpeaking, new HarmonyMethod(patchSetLocalClientSpeaking));
MethodInfo original__rpc_handler_64994802 = AccessTools.Method(typeof(WalkieTalkie), "__rpc_handler_64994802");
MethodInfo patch__rpc_handler_64994802 = AccessTools.Method(typeof(Patches), "WalkieTalkie_HandlePlayerSpeakingServerRPC");
harmony.Patch(original__rpc_handler_64994802, new HarmonyMethod(patch__rpc_handler_64994802));
MethodInfo original__rpc_handler_2961867446 = AccessTools.Method(typeof(WalkieTalkie), "__rpc_handler_2961867446");
MethodInfo patch__rpc_handler_2961867446 = AccessTools.Method(typeof(Patches), "WalkieTalkie_HandlePlayerSpeakingClientRPC");
harmony.Patch(original__rpc_handler_2961867446, new HarmonyMethod(patch__rpc_handler_2961867446));
MethodInfo originUpdate = AccessTools.Method(typeof(WalkieTalkie), "Update");
MethodInfo patchUpdate = AccessTools.Method(typeof(Patches), "WalkieTalkie_Update");
harmony.Patch(originUpdate, null, new HarmonyMethod(patchUpdate));
MethodInfo originStart = AccessTools.Method(typeof(WalkieTalkie), "Start");
MethodInfo patchStart = AccessTools.Method(typeof(Patches), "WalkieTalkie_Start");
harmony.Patch(originStart, null, new HarmonyMethod(patchStart));
MethodInfo originSwitchWalkieTalkieOn = AccessTools.Method(typeof(WalkieTalkie), "SwitchWalkieTalkieOn");
MethodInfo patchSwitchWalkieTalkieOn = AccessTools.Method(typeof(Patches), "WalkieTalkie_SwitchWalkieTalkieOn");
harmony.Patch(originSwitchWalkieTalkieOn, null, new HarmonyMethod(patchSwitchWalkieTalkieOn));
MethodInfo originEquipItem = AccessTools.Method(typeof(WalkieTalkie), "EquipItem");
MethodInfo patchEquipItem = AccessTools.Method(typeof(Patches), "WalkieTalkie_EquipItem");
harmony.Patch(originEquipItem, null, new HarmonyMethod(patchEquipItem));
MethodInfo originPocketItem = AccessTools.Method(typeof(WalkieTalkie), "PocketItem");
MethodInfo patchPocketItem = AccessTools.Method(typeof(Patches), "WalkieTalkie_PocketItem");
harmony.Patch(originPocketItem, null, new HarmonyMethod(patchPocketItem));
}
private static void WalkieTalkie_EquipItem(WalkieTalkie __instance)
{
// enable canvas if walkie talkie is on
if (__instance.isBeingUsed)
__instance.gameObject.GetComponent<Canvas>().enabled = true;
}
private static void WalkieTalkie_PocketItem(WalkieTalkie __instance)
{
__instance.gameObject.GetComponent<Canvas>().enabled = false;
}
private static void WalkieTalkie_Start(WalkieTalkie __instance)
{
// add walkie talkie to the dictionary
FrequencyWalkie.walkieTalkieFrequencies.Add(__instance.GetInstanceID(), 0);
// add control tip to the walkie talkie
__instance.itemProperties.toolTips = __instance.itemProperties.toolTips.AddToArray(FrequencyWalkie.tooltip);
// we patch the material
// copy texture to a render texture and modify it
RenderTexture rt = new RenderTexture(__instance.onMaterial.mainTexture.width,
__instance.onMaterial.mainTexture.width, 32);
Graphics.Blit(__instance.onMaterial.mainTexture, rt);
RenderTexture.active = rt;
// set pixels
Texture2D tex = new Texture2D(__instance.onMaterial.mainTexture.width,
__instance.onMaterial.mainTexture.height);
tex.ReadPixels(
new Rect(0, 0, tex.width, tex.height), 0,
0);
var colors = new Color[tex.width * tex.height];
for (int i = 0; i < colors.Length; i++)
{
colors[i] = new Color(0x7A, 0x96, 0x79);
}
tex.SetPixels(420, 64, 135, 76, colors);
tex.Apply();
RenderTexture.active = null;
// set texture
__instance.onMaterial.mainTexture = tex;
// var mesh = __instance.mainObjectRenderer.GetComponent<MeshFilter>().mesh;
// var uv = CalculateUVFromPixelCoordinates(tex, 480, 64);
// var pos = MapUVtoObjectSpace(uv, mesh);
// create a new object with a canvas renderer and a text
// and add it as a child to the light
var canvas = __instance.gameObject.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvas.worldCamera = Camera.main;
// set canvas size
var rectTransform = canvas.GetComponent<RectTransform>();
rectTransform.sizeDelta = new Vector2(40, 40);
var text = new GameObject("Text");
text.transform.parent = canvas.transform;
text.transform.localPosition = new Vector3(2.7f, -0.9f, -0.23f);
// rotate the text upwards
// and make horizontal
text.transform.localRotation = Quaternion.Euler(-90, -90, 0);
var circle = new GameObject("Circle");
circle.transform.parent = canvas.transform;
circle.transform.localPosition = new Vector3(3.25f, -0.9f, -0.45f);
circle.transform.localRotation = Quaternion.Euler(-90, -90, 0);
circle.transform.localScale = new Vector3(0.003f, 0.003f, 0.003f);
var circleImage = circle.AddComponent<Image>();
circleImage.sprite = Assets.GetResource<Sprite>("scanCircle2");;
circleImage.color = Color.black;
var speaking = new GameObject("Speaking");
speaking.transform.parent = canvas.transform;
speaking.transform.localPosition = new Vector3(3.25f, -0.9f, 0.2f);
speaking.transform.localRotation = Quaternion.Euler(-90, -90, 0);
var speakingImage = speaking.AddComponent<Image>();
speakingImage.sprite = Assets.GetResource<Sprite>("SpeakingSymbol");;
speakingImage.color = Color.black;
speaking.transform.localScale = new Vector3(0.003f, 0.003f, 0.003f);
var arrowUp = new GameObject("ArrowUp");
arrowUp.transform.parent = canvas.transform;
arrowUp.transform.localPosition = new Vector3(3.25f, -0.9f, -0.2f);
arrowUp.transform.localRotation = Quaternion.Euler(-90, -90, 0);
arrowUp.transform.localScale = new Vector3(0.003f, 0.003f, 0.003f);
var arrowUpImage = arrowUp.AddComponent<Image>();
arrowUpImage.sprite = Assets.GetResource<Sprite>("arrow2");;
arrowUpImage.color = Color.black;
var arrowDown = new GameObject("ArrowDown");
arrowDown.transform.parent = canvas.transform;
arrowDown.transform.localPosition = new Vector3(3.25f, -0.9f, -0.1f);
arrowDown.transform.localRotation = Quaternion.Euler(-90, -90, 0);
arrowDown.transform.localScale = new Vector3(0.003f, 0.003f, 0.003f);
var arrowDownImage = arrowDown.AddComponent<Image>();
arrowDownImage.sprite = Assets.GetResource<Sprite>("arrow2");;
arrowUpImage.transform.Rotate(0, 0, 180);
arrowDownImage.color = Color.black;
var textComponent = text.AddComponent<Text>();
textComponent.text = $"<b><size=40>{FrequencyWalkie.frequencies[FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()]]}</size><i><size=30>MHz</size></i></b>";
textComponent.font = Assets.GetResource<Font>("3270-Regular");;
textComponent.color = Color.black;
textComponent.fontSize = 40;
textComponent.lineSpacing = 0.5f;
text.transform.localScale = new Vector3(0.015f, 0.01f, 0.01f);
// disable canvas by default
// will be enabled when walkie talkie is on
canvas.enabled = false;
__instance.walkieTalkieLight.intensity /= 3;
}
private static void WalkieTalkie_SwitchWalkieTalkieOn(WalkieTalkie __instance)
{
__instance.gameObject.GetComponent<Canvas>().enabled = __instance.isBeingUsed;
if (__instance.playerHeldBy == GameNetworkManager.Instance.localPlayerController && __instance.isBeingUsed)
HUDManager.Instance.DisplayTip("FrequencyWalkie", "Press [R] or [F] to cycle between frequencies. BR.d will broadcast to all walkie talkies.", false, true, "FW_UseTip");
}
private static void WalkieTalkie_Update(WalkieTalkie __instance)
{
var player = GameNetworkManager.Instance.localPlayerController;
var isCurrentItemWalkie = player.ItemSlots[player.currentItemSlot] == __instance;
// player has to be able to use the walkie talkie
if (player.isGrabbingObjectAnimation || player.isTypingChat || player.inTerminalMenu ||
player.inSpecialInteractAnimation) return;
// player has to be holding the walkie talkie
// and walkie talkie has to be turned on
if (!__instance.isBeingUsed || !isCurrentItemWalkie) return;
if (UnityInput.Current.GetKeyUp(KeyCode.F))
{
FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()]--;
if (FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()] < 0)
{
FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()] = FrequencyWalkie.frequencies.Count - 1;
}
__instance.StartCoroutine(FrequencyWalkie.OnFrequencyChanged(__instance, false));
} else if (UnityInput.Current.GetKeyUp(KeyCode.R))
{
FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()] = (FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()] + 1) % FrequencyWalkie.frequencies.Count;
__instance.StartCoroutine(FrequencyWalkie.OnFrequencyChanged(__instance, true));
}
}
private static bool WalkieTalkie_SetLocalClientSpeaking(WalkieTalkie __instance, PlayerControllerB ___previousPlayerHeldBy, bool speaking)
{
if (___previousPlayerHeldBy.speakingToWalkieTalkie != speaking)
{
___previousPlayerHeldBy.speakingToWalkieTalkie = speaking;
if (speaking)
{
FrequencyWalkie.SetPlayerSpeakingOnWalkieTalkieServerRpc(__instance, (int)___previousPlayerHeldBy.playerClientId, FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()]);
return false;
}
__instance.UnsetPlayerSpeakingOnWalkieTalkieServerRpc((int)___previousPlayerHeldBy.playerClientId);
}
return false;
}
// NOTE: need to patch this
private static bool WalkieTalkie_HandlePlayerSpeakingServerRPC(
NetworkBehaviour target,
FastBufferReader reader,
__RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if (networkManager == null || !networkManager.IsListening)
return false;
if ((long) rpcParams.Server.Receive.SenderClientId != (long) target.OwnerClientId)
{
if (networkManager.LogLevel > LogLevel.Normal)
return false;
Debug.LogError((object) "Only the owner can invoke a ServerRpc that requires ownership!");
}
else
{
ByteUnpacker.ReadValueBitPacked(reader, out int playerId);
ByteUnpacker.ReadValueBitPacked(reader, out int frequency);
AccessTools.Field(typeof(WalkieTalkie), "__rpc_exec_stage").SetValue(target, (int)RpcExecStage.Server);
FrequencyWalkie.SetPlayerSpeakingOnWalkieTalkieServerRpc((WalkieTalkie)target, playerId, frequency);
AccessTools.Field(typeof(WalkieTalkie), "__rpc_exec_stage").SetValue(target, (int)RpcExecStage.None);
}
return false;
}
// NOTE: need to patch this
private static bool WalkieTalkie_HandlePlayerSpeakingClientRPC(
NetworkBehaviour target,
FastBufferReader reader,
__RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if (networkManager == null || !networkManager.IsListening)
return false;
ByteUnpacker.ReadValueBitPacked(reader, out int playerId);
ByteUnpacker.ReadValueBitPacked(reader, out int frequency);
AccessTools.Field(typeof(WalkieTalkie), "__rpc_exec_stage").SetValue(target, (int)RpcExecStage.Client);
FrequencyWalkie.SetPlayerSpeakingOnWalkieTalkieClientRpc((WalkieTalkie)target, playerId, frequency);
AccessTools.Field(typeof(WalkieTalkie), "__rpc_exec_stage").SetValue(target, (int)RpcExecStage.None);
return false;
}
}
}