summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-tpanglefix.sp
blob: 62977958c3e37ca9f3ec7471fc1d9d5e4a7e221c (plain)
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
271
272
273
274
275
276
277
#include <sourcemod>

#include <dhooks>

#include <gokz/core>
#include <gokz/tpanglefix>

#include <autoexecconfig>

#undef REQUIRE_EXTENSIONS
#undef REQUIRE_PLUGIN
#include <updater>

#pragma newdecls required
#pragma semicolon 1



public Plugin myinfo = 
{
	name = "GOKZ Teleport Angle Fix",
	author = "zer0.k",
	description = "Fix teleporting not modifying player's view angles due to packet loss",
	version = GOKZ_VERSION,
	url = "https://github.com/KZGlobalTeam/gokz"
};

#define UPDATER_URL GOKZ_UPDATER_BASE_URL..."gokz-tpanglefix.txt"

TopMenu gTM_Options;
TopMenuObject gTMO_CatGeneral;
TopMenuObject gTMO_ItemTPAngleFix;
Address gA_ViewAnglePatchAddress;
bool gB_EnableFix[MAXPLAYERS + 1];
DynamicDetour gH_WriteViewAngleUpdate;
int gI_ClientOffset;

// =====[ PLUGIN EVENTS ]=====

public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
{
	RegPluginLibrary("gokz-tpanglefix");
	return APLRes_Success;
}

public void OnPluginStart()
{
	LoadTranslations("gokz-common.phrases");
	LoadTranslations("gokz-tpanglefix.phrases");

	SetupPatch();
	HookEvents();
	RegisterCommands();
}

void SetupPatch()
{
	GameData gamedataConf = LoadGameConfigFile("gokz-tpanglefix.games");
	if (gamedataConf == null)
	{
		SetFailState("Failed to load gokz-tpanglefix gamedata");
	}	
	// Get the patching address
	Address addr = GameConfGetAddress(gamedataConf, "WriteViewAngleUpdate");
	if(addr == Address_Null)
	{
		SetFailState("Can't find WriteViewAngleUpdate address.");	
	}
	
	// Get the offset from the start of the signature to the start of our patch area.
	int offset = GameConfGetOffset(gamedataConf, "WriteViewAngleUpdateReliableOffset");
	if(offset == -1)
	{
		SetFailState("Can't find WriteViewAngleUpdateReliableOffset in gamedata.");
	}
	gA_ViewAnglePatchAddress = view_as<Address>(addr + view_as<Address>(offset));
}

void HookEvents()
{
	GameData gamedataConf = LoadGameConfigFile("gokz-tpanglefix.games");
	if (gamedataConf == null)
	{
		SetFailState("Failed to load gokz-tpanglefix gamedata");
	}
	gH_WriteViewAngleUpdate = DynamicDetour.FromConf(gamedataConf, "CGameClient::WriteViewAngleUpdate");
		
	if (gH_WriteViewAngleUpdate == INVALID_HANDLE)
	{
		SetFailState("Failed to find CGameClient::WriteViewAngleUpdate function signature");
	}
	
	if (!gH_WriteViewAngleUpdate.Enable(Hook_Pre, DHooks_OnWriteViewAngleUpdate_Pre))
	{
		SetFailState("Failed to enable detour on CGameClient::WriteViewAngleUpdate");
	}
	// Prevent the server from crashing.
	FindConVar("sv_parallel_sendsnapshot").SetBool(false);
	FindConVar("sv_parallel_sendsnapshot").AddChangeHook(OnParallelSendSnapshotCvarChanged);

	gI_ClientOffset = gamedataConf.GetOffset("ClientIndexOffset");
	if (gI_ClientOffset == -1)
	{
		SetFailState("Failed to get ClientIndexOffset offset.");
	}
}

void OnParallelSendSnapshotCvarChanged(ConVar convar, const char[] oldValue, const char[] newValue)
{
	if (convar.BoolValue)
	{
		convar.BoolValue = false;
	}
}

public MRESReturn DHooks_OnWriteViewAngleUpdate_Pre(Address pThis)
{
	int client = LoadFromAddress(pThis + view_as<Address>(gI_ClientOffset), NumberType_Int32);
	if (gB_EnableFix[client])
	{
		PatchAngleFix();
	}
	else
	{
		RestoreAngleFix();
	}
	return MRES_Ignored;
}

void PatchAngleFix()
{
	if (LoadFromAddress(gA_ViewAnglePatchAddress, NumberType_Int8) == 0)
	{
		StoreToAddress(gA_ViewAnglePatchAddress, 1, NumberType_Int8);
	}
}

void RestoreAngleFix()
{
	if (LoadFromAddress(gA_ViewAnglePatchAddress, NumberType_Int8) == 1)
	{
		StoreToAddress(gA_ViewAnglePatchAddress, 0, NumberType_Int8);
	}
}

bool ToggleAngleFix(int client)
{
	gB_EnableFix[client] = !gB_EnableFix[client];
	return gB_EnableFix[client];
}

public void OnAllPluginsLoaded()
{
	if (LibraryExists("updater"))
	{
		Updater_AddPlugin(UPDATER_URL);
	}

	TopMenu topMenu;
	if (LibraryExists("gokz-core") && ((topMenu = GOKZ_GetOptionsTopMenu()) != null))
	{
		GOKZ_OnOptionsMenuReady(topMenu);
	}
}

public void OnLibraryAdded(const char[] name)
{
	if (StrEqual(name, "updater"))
	{
		Updater_AddPlugin(UPDATER_URL);
	}

}

// =====[ CLIENT EVENTS ]=====

public void GOKZ_OnOptionChanged(int client, const char[] option, any newValue)
{
	OnOptionChanged_Options(client, option, newValue);
}

// =====[ OTHER EVENTS ]=====

public void GOKZ_OnOptionsMenuReady(TopMenu topMenu)
{
	OnOptionsMenuReady_Options();
	OnOptionsMenuReady_OptionsMenu(topMenu);
}


// =====[ OPTIONS ]=====

void OnOptionsMenuReady_Options()
{
	RegisterOption();
}

void RegisterOption()
{
	GOKZ_RegisterOption(TPANGLEFIX_OPTION_NAME, TPANGLEFIX_OPTION_DESCRIPTION, 
		OptionType_Int, TPAngleFix_Disabled, 0, TPANGLEFIX_COUNT - 1);
}

void OnOptionChanged_Options(int client, const char[] option, any newValue)
{
	if (StrEqual(option, TPANGLEFIX_OPTION_NAME))
	{
		gB_EnableFix[client] = newValue;
		switch (newValue)
		{
			case TPAngleFix_Disabled:
			{
				GOKZ_PrintToChat(client, true, "%t", "Option - TP Angle Fix - Disable");
			}
			case TPAngleFix_Enabled:
			{
				GOKZ_PrintToChat(client, true, "%t", "Option - TP Angle Fix - Enable");
			}
		}
	}
}

// =====[ OPTIONS MENU ]=====

void OnOptionsMenuReady_OptionsMenu(TopMenu topMenu)
{
	if (gTM_Options == topMenu)
	{
		return;
	}

	gTM_Options = topMenu;
	gTMO_CatGeneral = gTM_Options.FindCategory(GENERAL_OPTION_CATEGORY);
	gTMO_ItemTPAngleFix = gTM_Options.AddItem(TPANGLEFIX_OPTION_NAME, TopMenuHandler_TPAngleFix, gTMO_CatGeneral);
}

public void TopMenuHandler_TPAngleFix(TopMenu topmenu, TopMenuAction action, TopMenuObject topobj_id, int param, char[] buffer, int maxlength)
{
	if (topobj_id != gTMO_ItemTPAngleFix)
	{
		return;
	}

	if (action == TopMenuAction_DisplayOption)
	{
		if (GOKZ_GetOption(param, TPANGLEFIX_OPTION_NAME) == TPAngleFix_Disabled)
		{
			FormatEx(buffer, maxlength, "%T - %T", 
				"Options Menu - TP Angle Fix", param, 
				"Options Menu - Disabled", param);
		}
		else
		{
			FormatEx(buffer, maxlength, "%T - %T", 
				"Options Menu - TP Angle Fix", param, 
				"Options Menu - Enabled", param);
		}
	}
	else if (action == TopMenuAction_SelectOption)
	{
		GOKZ_CycleOption(param, TPANGLEFIX_OPTION_NAME);
		gTM_Options.Display(param, TopMenuPosition_LastCategory);
	}
}

// =====[ COMMANDS ]=====

void RegisterCommands()
{
	RegConsoleCmd("sm_tpafix", CommandTPAFix, "[KZ] Toggle teleport angle fix.");
}

public Action CommandTPAFix(int client, int args)
{
	GOKZ_SetOption(client, TPANGLEFIX_OPTION_NAME, ToggleAngleFix(client));
	return Plugin_Handled;
}