Skip to content

Commit 8eca59a

Browse files
authored
Merge pull request #86 from UncomplicatedCustomServer/dev-labapi
v9.5.0 - LabApi 1.1.7
2 parents b8fa4e2 + b9a7648 commit 8eca59a

6 files changed

Lines changed: 132 additions & 53 deletions

File tree

UncomplicatedCustomRoles/API/Features/SummonedCustomRole.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private void EvaluateRoleBase()
264264

265265
Timing.CallDelayed(3.25f, delegate {
266266
_roleBase.Pooled = false;
267-
DisguiseTeam.RoleBaseList.Add(Player.PlayerId, _roleBase);
267+
DisguiseTeam.RoleBaseList[Player.PlayerId] = _roleBase;
268268
});
269269

270270
}
Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* This file is a part of the UncomplicatedCustomRoles project.
33
*
44
* Copyright (c) 2023-present FoxWorn3365 (Federico Cosma) <me@fcosma.it>
@@ -8,13 +8,46 @@
88
* If not, see <https://www.gnu.org/licenses/>.
99
*/
1010

11+
using System;
12+
using LabApi.Features.Wrappers;
13+
using UncomplicatedCustomRoles.Manager;
14+
1115
namespace UncomplicatedCustomRoles.Integrations
1216
{
1317
internal static class UCT
1418
{
15-
/*public static bool GetCustomTeam(Player player, out uint team)
16-
{ TODO // INTEGRATION WITH UCT IS ON HOLD
17-
object role = DynamicInvoke.GetMethod("UncomplicatedCustomTeams", "UncomplicatedCustomTeams.API.Features.SummonedTeam.SummonedPlayersTryGet"); ON HOLD
18-
}*/
19+
private const string PluginName = "UncomplicatedCustomTeams";
20+
21+
public static bool TryGetCustomTeamId(Player player, out uint teamId)
22+
{
23+
teamId = 0;
24+
try
25+
{
26+
object summonedTeam = DynamicInvoke.GetMethod(PluginName,
27+
"UncomplicatedCustomTeams.API.TeamExtensions.GetCustomTeam", true)?
28+
.Invoke(null, new object[] { player });
29+
30+
if (summonedTeam is null)
31+
return false;
32+
33+
object definition = DynamicInvoke.GetMethod(PluginName,
34+
"UncomplicatedCustomTeams.API.Features.Runtime.SummonedTeam.Definition_get", true)?
35+
.Invoke(summonedTeam, null);
36+
37+
if (definition is null)
38+
return false;
39+
40+
teamId = Convert.ToUInt32(DynamicInvoke.GetMethod(PluginName,
41+
"UncomplicatedCustomTeams.API.Features.Definitions.Team.Id_get", true)?
42+
.Invoke(definition, null));
43+
44+
return true;
45+
}
46+
catch (Exception e)
47+
{
48+
LogManager.Error(e.ToString());
49+
return false;
50+
}
51+
}
1952
}
20-
}
53+
}

UncomplicatedCustomRoles/Manager/SpawnManager.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public static void SummonSubclassApplier(Player Player, ICustomRole Role)
340340
public static KeyValuePair<bool, object>? ParseEscapeRole(Dictionary<string, string> roleAfterEscape, Player player)
341341
{
342342
Dictionary<Team, KeyValuePair<bool, object>?> AsCuffedByInternalTeam = new();
343-
Dictionary<uint, KeyValuePair<bool, object>?> AsCuffedByCustomTeam = new(); //we will add the support to UCT and UIU-RS
343+
Dictionary<uint, KeyValuePair<bool, object>?> AsCuffedByCustomTeam = new();
344344
// cuffed by InternalTeam FoundationForces
345345
// 0 1 2 3 = 4
346346
Dictionary<int, KeyValuePair<bool, object>?> AsCuffedByCustomRole = new();
@@ -387,11 +387,13 @@ public static void SummonSubclassApplier(Player Player, ICustomRole Role)
387387
// Now let's assign
388388
if (!player.IsDisarmed)
389389
return Default;
390-
else if (player.IsDisarmed && player.DisarmedBy is not null)
391-
if (player.DisarmedBy.TryGetSummonedInstance(out SummonedCustomRole role) && AsCuffedByCustomRole.ContainsKey(role.Role.Id))
392-
return AsCuffedByCustomRole[role.Role.Id];
393-
else if (AsCuffedByInternalTeam.ContainsKey(player.DisarmedBy.Team))
394-
return AsCuffedByInternalTeam[player.DisarmedBy.Team];
390+
if (player.IsDisarmed && player.DisarmedBy is not null)
391+
if (player.DisarmedBy.TryGetSummonedInstance(out SummonedCustomRole role) && AsCuffedByCustomRole.TryGetValue(role.Role.Id, out var crEscapeRole))
392+
return crEscapeRole;
393+
else if (UCT.TryGetCustomTeamId(player.DisarmedBy, out uint uctTeamId) && AsCuffedByCustomTeam.TryGetValue(uctTeamId, out var uctEscapeRole))
394+
return uctEscapeRole;
395+
else if (AsCuffedByInternalTeam.TryGetValue(player.DisarmedBy.Team, out var internalEscapeRole))
396+
return internalEscapeRole;
395397

396398
LogManager.Silent($"Returing default type for escaping evaluation of player {player.PlayerId} who's cuffed by {player.DisarmedBy?.Team}");
397399
return Default;
@@ -411,10 +413,9 @@ public static void SummonSubclassApplier(Player Player, ICustomRole Role)
411413

412414
if (Elements[0] is "CustomRole" || Elements[0] is "CR")
413415
return new(true, int.Parse(Elements[1]));
414-
else if ((Elements[0] is "InternalRole" || Elements[0] is "IR") && Enum.TryParse(Elements[1], out RoleTypeId role))
416+
if ((Elements[0] is "InternalRole" || Elements[0] is "IR") && Enum.TryParse(Elements[1], out RoleTypeId role))
415417
return new(false, role);
416-
else
417-
LogManager.Warn($"Function SpawnManager::ParseEscapeString(string escape) failed!\nPossible causes can be:\n- The source is not valid. Allowed: InternalRole / IR / CustomRole / CR. Found: {Elements[0]}\n- The target is not a CustomRole / InternalRole. Found: {Elements[1]} (int32: {int.Parse(Elements[1])}");
418+
LogManager.Warn($"Function SpawnManager::ParseEscapeString(string escape) failed!\nPossible causes can be:\n- The source is not valid. Allowed: InternalRole / IR / CustomRole / CR. Found: {Elements[0]}\n- The target is not a CustomRole / InternalRole. Found: {Elements[1]} (int32: {int.Parse(Elements[1])}");
418419

419420
return new(false, RoleTypeId.Spectator);
420421
}

UncomplicatedCustomRoles/Patches/TeamPatch.cs

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,24 @@
88
* If not, see <https://www.gnu.org/licenses/>.
99
*/
1010

11-
using Achievements.Handlers;
12-
using HarmonyLib;
13-
using Interactables.Interobjects.DoorUtils;
14-
using InventorySystem.Items.ThrowableProjectiles;
15-
using PlayerRoles;
16-
using PlayerRoles.PlayableScps.Scp079.Rewards;
17-
using PlayerRoles.PlayableScps.Scp939.Mimicry;
18-
using System;
1911
using System.Collections.Generic;
2012
using System.Diagnostics;
2113
using System.Linq;
2214
using System.Reflection;
2315
using System.Reflection.Emit;
16+
using Achievements.Handlers;
17+
using HarmonyLib;
18+
using Interactables.Interobjects.DoorUtils;
2419
using InventorySystem.Disarming;
2520
using InventorySystem.Items;
21+
using InventorySystem.Items.ThrowableProjectiles;
2622
using InventorySystem.Searching;
27-
using LabApi.Features.Console;
2823
using MapGeneration.Distributors;
24+
using PlayerRoles;
25+
using PlayerRoles.PlayableScps.HumanTracker;
2926
using PlayerRoles.PlayableScps.Scp079;
27+
using PlayerRoles.PlayableScps.Scp079.Rewards;
28+
using PlayerRoles.PlayableScps.Scp939.Mimicry;
3029
using PlayerStatsSystem;
3130
using UncomplicatedCustomRoles.API.Features;
3231
using UncomplicatedCustomRoles.Manager;
@@ -37,12 +36,13 @@ namespace UncomplicatedCustomRoles.Patches
3736
[HarmonyPatch(typeof(PlayerRoleManager), nameof(PlayerRoleManager.CurrentRole), MethodType.Getter)]
3837
internal class PlayerRoleManagerPatch
3938
{
40-
static bool Prefix(PlayerRoleManager __instance, ref PlayerRoleBase __result)
39+
private static bool Prefix(PlayerRoleManager __instance, ref PlayerRoleBase __result)
4140
{
4241
if (__instance.Hub == null || __instance.Hub.netId == 0)
4342
return true;
4443

45-
if (__instance.Hub is not null && DisguiseTeam.RoleBaseList.TryGetValue(__instance.Hub.PlayerId, out PlayerRoleBase role))
44+
if (__instance.Hub is not null &&
45+
DisguiseTeam.RoleBaseList.TryGetValue(__instance.Hub.PlayerId, out PlayerRoleBase role))
4646
{
4747
if (role is null)
4848
LogManager.Error($"Disguised role for player {__instance.Hub.PlayerId} is null!");
@@ -55,7 +55,7 @@ static bool Prefix(PlayerRoleManager __instance, ref PlayerRoleBase __result)
5555
return true;
5656
}
5757
}
58-
58+
5959
[HarmonyPatch(typeof(PlayerRolesUtils), nameof(PlayerRolesUtils.GetRoleId))]
6060
internal class PlayerRolesUtilsPatch
6161
{
@@ -82,11 +82,11 @@ internal class PlayerRolesUtilsPatch
8282
$"{typeof(Scp079Recontainer)}::{nameof(Scp079Recontainer.OnServerRoleChanged)}"
8383
};
8484

85-
static bool Prefix(ReferenceHub hub, ref RoleTypeId __result)
85+
private static bool Prefix(ReferenceHub hub, ref RoleTypeId __result)
8686
{
8787
if (hub == null)
8888
return true;
89-
89+
9090
if (!DisguiseTeam.List.TryGetValue(hub.PlayerId, out Team team))
9191
return true;
9292

@@ -118,21 +118,22 @@ static bool Prefix(ReferenceHub hub, ref RoleTypeId __result)
118118
[HarmonyPatch(typeof(ExplosionGrenade), nameof(ExplosionGrenade.ExplodeDestructible))]
119119
internal class GrenadeTranspiler
120120
{
121-
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
121+
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
122122
{
123123
List<CodeInstruction> newInstructions = new(instructions);
124124
int index = -1;
125125

126126
for (int i = 0; i < newInstructions.Count; i++)
127-
{
128-
if (newInstructions[i].opcode == OpCodes.Call && newInstructions[i].operand is MethodInfo method && method == Method(typeof(PlayerRolesUtils), nameof(PlayerRolesUtils.GetRoleId), new Type[] { typeof(ReferenceHub) }))
127+
if (newInstructions[i].opcode == OpCodes.Call && newInstructions[i].operand is MethodInfo method &&
128+
method == Method(typeof(PlayerRolesUtils), nameof(PlayerRolesUtils.GetRoleId),
129+
[typeof(ReferenceHub)]))
129130
{
130131
index = i;
131132
break;
132133
}
133-
}
134134

135-
newInstructions[index+1].operand = Method(typeof(PlayerRolesUtils), nameof(PlayerRolesUtils.GetTeam), new Type[] { typeof(ReferenceHub) });
135+
newInstructions[index + 1].operand = Method(typeof(PlayerRolesUtils), nameof(PlayerRolesUtils.GetTeam),
136+
[typeof(ReferenceHub)]);
136137
newInstructions.RemoveAt(index);
137138

138139
return newInstructions;
@@ -142,7 +143,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
142143
[HarmonyPatch(typeof(PickupSearchCompletor), nameof(PickupSearchCompletor.ValidateAny))]
143144
public class PickupSearchCompletorPatch
144145
{
145-
static bool Prefix(PickupSearchCompletor __instance, ref bool __result)
146+
private static bool Prefix(PickupSearchCompletor __instance, ref bool __result)
146147
{
147148
if (!DisguiseTeam.List.TryGetValue(__instance.Hub.PlayerId, out Team team) || team != Team.SCPs ||
148149
__instance.Hub.roleManager.CurrentRole.RoleTypeId.GetTeam() == Team.SCPs) return true;
@@ -151,39 +152,49 @@ static bool Prefix(PickupSearchCompletor __instance, ref bool __result)
151152
return false;
152153
}
153154
}
154-
155+
155156
[HarmonyPatch]
156157
public class DoorPermissionsPolicyPatch
157158
{
158-
static MethodBase TargetMethod()
159+
private static MethodBase TargetMethod()
159160
{
160-
return Method(typeof(DoorPermissionsPolicy), "CheckPermissions", new[] { typeof(ReferenceHub), typeof(IDoorPermissionRequester), typeof(PermissionUsed).MakeByRefType() });
161+
return Method(typeof(DoorPermissionsPolicy), "CheckPermissions",
162+
[
163+
typeof(ReferenceHub), typeof(IDoorPermissionRequester), typeof(PermissionUsed).MakeByRefType()
164+
]);
161165
}
162166

163-
static bool Prefix(DoorPermissionsPolicy __instance, ReferenceHub hub, IDoorPermissionRequester requester, out PermissionUsed callback, ref bool __result)
167+
private static bool Prefix(DoorPermissionsPolicy __instance, ReferenceHub hub,
168+
IDoorPermissionRequester requester,
169+
out PermissionUsed callback, ref bool __result)
164170
{
165171
callback = null;
166172
if (__instance.RequiredPermissions == DoorPermissionFlags.None || hub.serverRoles.BypassMode)
167173
{
168174
__result = true;
169175
return false;
170176
}
177+
171178
if (hub.roleManager.CurrentRole is IDoorPermissionProvider currentRole &&
172179
(!DisguiseTeam.List.TryGetValue(hub.PlayerId, out Team team) || team != Team.SCPs))
173180
{
174181
__result = __instance.CheckPermissions(currentRole, requester, out callback);
175182
return false;
176183
}
184+
177185
ItemBase curInstance = hub.inventory.CurInstance;
178-
__result = curInstance != null && curInstance is IDoorPermissionProvider provider && __instance.CheckPermissions(provider, requester, out callback);
186+
__result = curInstance != null && curInstance is IDoorPermissionProvider provider &&
187+
__instance.CheckPermissions(provider, requester, out callback);
179188
return false;
180189
}
181190
}
182-
183-
[HarmonyPatch(typeof(DoorPermissionsPolicyExtensions), nameof(DoorPermissionsPolicyExtensions.GetCombinedPermissions))]
191+
192+
[HarmonyPatch(typeof(DoorPermissionsPolicyExtensions),
193+
nameof(DoorPermissionsPolicyExtensions.GetCombinedPermissions))]
184194
public class DoorPermissionsPolicyExtensionsPatch
185195
{
186-
static bool Prefix(ReferenceHub hub, IDoorPermissionRequester requester, ref DoorPermissionFlags __result)
196+
private static bool Prefix(ReferenceHub hub, IDoorPermissionRequester requester,
197+
ref DoorPermissionFlags __result)
187198
{
188199
if (hub == null)
189200
{
@@ -212,17 +223,51 @@ static bool Prefix(ReferenceHub hub, IDoorPermissionRequester requester, ref Doo
212223
}
213224
}
214225

226+
[HarmonyPatch(typeof(LastHumanTracker), nameof(LastHumanTracker.TryGetLastTarget))]
227+
public class LastHumanTrackerPatch
228+
{
229+
private static bool Prefix(ref ReferenceHub lastTarget, ref bool __result)
230+
{
231+
lastTarget = null;
232+
int humanCount = 0;
233+
int scpCount = 0;
234+
235+
foreach (ReferenceHub hub in ReferenceHub.AllHubs)
236+
{
237+
Team effectiveTeam = hub.GetRoleId().GetTeam();
238+
if (DisguiseTeam.List.TryGetValue(hub.PlayerId, out Team fakeTeam))
239+
effectiveTeam = fakeTeam;
240+
241+
if (effectiveTeam == Team.SCPs)
242+
{
243+
++scpCount;
244+
}
245+
else if (hub.IsAlive() && effectiveTeam != Team.Dead && effectiveTeam != Team.Dead)
246+
{
247+
++humanCount;
248+
lastTarget = hub;
249+
}
250+
}
251+
252+
__result = humanCount == 1 && scpCount > 0;
253+
return false;
254+
}
255+
}
256+
215257
[HarmonyPatch(typeof(Scp079Recontainer), nameof(Scp079Recontainer.OnServerRoleChanged))]
216258
public class Scp079RecontainerPatch
217259
{
218-
static bool Prefix(Scp079Recontainer __instance, ReferenceHub hub, RoleTypeId newRole, RoleChangeReason reason)
260+
private static bool Prefix(Scp079Recontainer __instance, ReferenceHub hub, RoleTypeId newRole,
261+
RoleChangeReason reason)
219262
{
220263
Team team = hub.GetRoleId().GetTeam();
221264
if (DisguiseTeam.List.TryGetValue(hub.PlayerId, out Team t))
222265
team = t;
223-
Logger.Debug($"Player {hub.PlayerId} changed role to {newRole} for reason {reason}. Checking if recontainment is needed...");
224-
Logger.Debug($"Player's current role: {hub.GetRoleId()}, team: {team}");
225-
if (newRole != RoleTypeId.Spectator || !IsScpButNot079(hub.GetRoleId(), team) || Scp079Role.ActiveInstances.Count == 0 ||
266+
LogManager.Debug(
267+
$"Player {hub.PlayerId} changed role to {newRole} for reason {reason}. Checking if recontainment is needed...");
268+
LogManager.Debug($"Player's current role: {hub.GetRoleId()}, team: {team}");
269+
if (newRole != RoleTypeId.Spectator || !IsScpButNot079(hub.GetRoleId(), team) ||
270+
Scp079Role.ActiveInstances.Count == 0 ||
226271
ReferenceHub.AllHubs.Any(x =>
227272
{
228273
if (x == hub)
@@ -244,7 +289,7 @@ static bool Prefix(Scp079Recontainer __instance, ReferenceHub hub, RoleTypeId ne
244289

245290
private static bool IsScpButNot079(RoleTypeId roleTypeId, Team team)
246291
{
247-
Logger.Debug($"Checking if role {roleTypeId} is an SCP but not 079 for team {team}");
292+
LogManager.Debug($"Checking if role {roleTypeId} is an SCP but not 079 for team {team}");
248293
return team == Team.SCPs && roleTypeId != RoleTypeId.Scp079;
249294
}
250295
}

UncomplicatedCustomRoles/Plugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class Plugin : Plugin<Config>
3636

3737
public override string Author => "FoxWorn3365, Dr.Agenda, MedveMarci";
3838

39-
public override Version Version { get; } = new(9, 4, 0, 0);
39+
public override Version Version { get; } = new(9, 5, 0, 0);
4040

4141
public override Version RequiredApiVersion => new(LabApiProperties.CompiledVersion);
4242

UncomplicatedCustomRoles/UncomplicatedCustomRoles.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
<ItemGroup>
3232
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.3" PrivateAssets="all"/>
33-
<PackageReference Include="Lib.Harmony" Version="2.4.2"/>
34-
<PackageReference Include="Northwood.LabAPI" Version="1.1.6.1"/>
35-
<PackageReference Include="System.Text.Json" Version="10.0.7"/>
33+
<PackageReference Include="Lib.Harmony" Version="2.2.2" />
34+
<PackageReference Include="Northwood.LabAPI" Version="1.1.7" />
35+
<PackageReference Include="System.Text.Json" Version="10.0.8" />
3636
<PackageReference Include="System.ComponentModel" Version="4.3.0"/>
3737
<PackageReference Include="System.Net.Http" Version="4.3.4"/>
3838
<PackageReference Include="YamlDotNet" Version="11.0.1"/>

0 commit comments

Comments
 (0)