Skip to content

Commit

Permalink
SQLLink
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyTech6 committed Nov 24, 2024
1 parent d40dc35 commit a926b5c
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 314 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
`1.0.1`
`1.0.2`
- Changes to how bans are triggered and cleared

`1.0.1`
Expand Down
24 changes: 12 additions & 12 deletions Commands/BanCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@ namespace CrimsonBanned.Commands;
internal static class BanCommands
{
[Command(name: "server", shortHand: "s", adminOnly: true)]
public static void Ban(ChatCommandContext ctx, string name, int length = -1, string denomination = "-", string reason = "")
public static void Ban(ChatCommandContext ctx, string name, int length = -1, string timeunit = "-", string reason = "")
{
var result = HandleBanOperation(ctx, name, length, denomination, Database.Banned, "banned", true);
var result = HandleBanOperation(ctx, name, length, timeunit, Database.Banned, "banned", true);
if (result.Success) Core.StartCoroutine(DelayKick(result.PlayerInfo));
}

[Command(name: "chat", shortHand: "c", adminOnly: true)]
public static void BanFromChat(ChatCommandContext ctx, string name, int length = -1, string denomination = "-", string reason = "")
public static void BanFromChat(ChatCommandContext ctx, string name, int length = -1, string timeunit = "-", string reason = "")
{
HandleBanOperation(ctx, name, length, denomination, Database.ChatBans, "banned from chat");
HandleBanOperation(ctx, name, length, timeunit, Database.ChatBans, "banned from chat");
}

[Command(name: "voice", shortHand: "v", adminOnly: true)]
public static void BanFromVoice(ChatCommandContext ctx, string name, int length = -1, string denomination = "-", string reason = "")
public static void BanFromVoice(ChatCommandContext ctx, string name, int length = -1, string timeunit = "-", string reason = "")
{
HandleBanOperation(ctx, name, length, denomination, Database.VoiceBans, "banned from voice chat");
HandleBanOperation(ctx, name, length, timeunit, Database.VoiceBans, "banned from voice chat");
}

[Command(name: "mute", shortHand: "m", adminOnly: true)]
public static void Mute(ChatCommandContext ctx, string name, int length = -1, string denomination = "-", string reason = "")
public static void Mute(ChatCommandContext ctx, string name, int length = -1, string timeunit = "-", string reason = "")
{
BanFromChat(ctx, name, length, denomination, reason);
BanFromVoice(ctx, name, length, denomination, reason);
BanFromChat(ctx, name, length, timeunit, reason);
BanFromVoice(ctx, name, length, timeunit, reason);
}

private static (bool Success, PlayerInfo PlayerInfo) HandleBanOperation(ChatCommandContext ctx, string name, int length, string denomination,
private static (bool Success, PlayerInfo PlayerInfo) HandleBanOperation(ChatCommandContext ctx, string name, int length, string timeunit,
List<Ban> banList, string banType, bool isGameBan = false, string reason = "")
{
if (!Extensions.TryGetPlayerInfo(name, out PlayerInfo playerInfo))
Expand All @@ -52,7 +52,7 @@ private static (bool Success, PlayerInfo PlayerInfo) HandleBanOperation(ChatComm
}

if(length == -1) length = Settings.DefaultBanLength.Value;
if(denomination == "-") denomination = Settings.DefaultBanDenomination.Value;
if(timeunit == "-") timeunit = Settings.DefaultBanDenomination.Value;

if(playerInfo.User.IsAdmin && Settings.AdminImmune.Value)
{
Expand All @@ -65,7 +65,7 @@ private static (bool Success, PlayerInfo PlayerInfo) HandleBanOperation(ChatComm
return (false, null);
}

var timeSpan = LengthParse(length, denomination);
var timeSpan = LengthParse(length, timeunit);
var bannedTime = DateTime.Now + timeSpan;

if(length == 0) bannedTime = DateTime.MinValue;
Expand Down
4 changes: 2 additions & 2 deletions Commands/BannedCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public static void List(ChatCommandContext ctx, string listName = "server")
[Command(name: "loaduntracked", adminOnly: true)]
public static void Backlog(ChatCommandContext ctx)
{
if (!Settings.MySQLConfigured)
if (Database.SQL == null)
{
ctx.Reply("MySQL is not configured. Please configure MySQL in the config file.");
ctx.Reply("MySQL is not configured. Please configure MySQL using CrimsonSQL.");
return;
}

Expand Down
12 changes: 11 additions & 1 deletion Commands/UnbanCommands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CrimsonBanned.Structs;
using ProjectM;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using VampireCommandFramework;
using static CrimsonBanned.Services.PlayerService;
Expand Down Expand Up @@ -54,10 +55,19 @@ private static void HandleUnbanOperation(ChatCommandContext ctx, string name, Li

ctx.Reply($"{name} has been {unbanType}");

if (unbanType != "unbanned") // Don't send message for game unbans
if (unbanType != "unbanned" && !Settings.ShadowBan.Value) // Don't send message for game unbans
{
ServerChatUtils.SendSystemMessageToClient(Core.EntityManager, playerInfo.User,
$"You have been {unbanType}.");
}
else
{
if (File.Exists(Settings.BanFilePath.Value))
{
var lines = File.ReadAllLines(Settings.BanFilePath.Value).ToList();
lines.RemoveAll(line => line.Trim() == ban.PlayerID.ToString());
File.WriteAllLines(Settings.BanFilePath.Value, lines);
}
}
}
}
22 changes: 11 additions & 11 deletions CrimsonBanned.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<FileVersion>0.1.10.0</FileVersion>
<InformationalVersion>0.1.10+1.Branch.main.Sha.c33d0879382e1a0da34861ec55914ba7e637a80e</InformationalVersion>
</PropertyGroup>
<Target Name="Thunderstore Copy to Dist" AfterTargets="AfterBuild"
Condition=" '$(Configuration' == 'Release'">
<Target Name="Thunderstore Copy to Dist" AfterTargets="AfterBuild" Condition=" '$(Configuration' == 'Release'">
<Copy SourceFiles="$(OutDir)\CrimsonBanned.dll" DestinationFolder="$(SolutionDir)/dist" />
</Target>
<ItemGroup>
Expand All @@ -25,7 +24,7 @@
<PackageReference Include="Fody" Version="6.9.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MySql.Data" Version="9.1.0" />
<PackageReference Include="MySql.Data" Version="8.0.22" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
<PackageReference Include="VRising.Unhollowed.Client" Version="1.0.*" />
<PackageReference Include="VRising.VampireCommandFramework" Version="0.9.0" />
Expand All @@ -39,13 +38,14 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(NuGetPackageRoot)mysql.data\*\lib\net6.0\MySql.Data.dll" />
<EmbeddedResource
Include="$(NuGetPackageRoot)system.diagnostics.diagnosticsource\*\lib\net6.0\System.Diagnostics.DiagnosticSource.dll" />
<EmbeddedResource
Include="$(NuGetPackageRoot)system.security.permissions\*\lib\net6.0\System.Security.Permissions.dll" />
<EmbeddedResource
Include="$(NuGetPackageRoot)system.configuration.configurationmanager\*\lib\net6.0\System.Configuration.ConfigurationManager.dll" />
<EmbeddedResource
Include="$(NuGetPackageRoot)system.text.encoding.codepages\*\lib\net6.0\System.Text.Encoding.CodePages.dll"/>
<EmbeddedResource Include="$(NuGetPackageRoot)system.diagnostics.diagnosticsource\*\lib\net6.0\System.Diagnostics.DiagnosticSource.dll" />
<EmbeddedResource Include="$(NuGetPackageRoot)system.security.permissions\*\lib\net6.0\System.Security.Permissions.dll" />
<EmbeddedResource Include="$(NuGetPackageRoot)system.configuration.configurationmanager\*\lib\net6.0\System.Configuration.ConfigurationManager.dll" />
<EmbeddedResource Include="$(NuGetPackageRoot)system.text.encoding.codepages\*\lib\net6.0\System.Text.Encoding.CodePages.dll" />
</ItemGroup>
<ItemGroup>
<Reference Include="CrimsonSQL">
<HintPath>..\CrimsonSQL\bin\Debug\net6.0\CrimsonSQL.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion Patches/ChatMessagePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public static bool Prefix(ChatMessageSystem __instance)

if (chatEventData.MessageType == ChatMessageType.System) continue;

if (Database.ChatBans.Exists(x => x.PlayerID == userData.PlatformId))
if (Database.Banned.Exists(x => x.PlayerID == userData.PlatformId))
{
Core.Server.EntityManager.DestroyEntity(entity);
}
else if (Database.ChatBans.Exists(x => x.PlayerID == userData.PlatformId))
{
var ban = Database.ChatBans.First(x => x.PlayerID == userData.PlatformId);

Expand Down
65 changes: 65 additions & 0 deletions Patches/OnUserConnectedPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using CrimsonBanned.Structs;
using HarmonyLib;
using ProjectM;
using ProjectM.Network;
using Stunlock.Network;
using System;
using System.Linq;
using Unity.Entities;

namespace CrimsonBanned.Patches;

[HarmonyPatch(typeof(ServerBootstrapSystem), nameof(ServerBootstrapSystem.OnUserConnected))]
public static class OnUserConnectedPatcch
{
public static void Postfix(ServerBootstrapSystem __instance, NetConnectionId netConnectionId)
{
try
{
var em = __instance.EntityManager;
var userIndex = __instance._NetEndPointToApprovedUserIndex[netConnectionId];
var serverClient = __instance._ApprovedUsersLookup[userIndex];
var userEntity = serverClient.UserEntity;
var userData = __instance.EntityManager.GetComponentData<User>(userEntity);
bool isNewVampire = userData.CharacterName.IsEmpty;

if (Database.Banned.Exists(x => x.PlayerID == userData.PlatformId))
{
var ban = Database.Banned.First(x => x.PlayerID == userData.PlatformId);

if (DateTime.Now > ban.TimeUntil)
{
Database.DeleteBan(ban, Database.Banned);
}
else
{
Entity entity = Core.EntityManager.CreateEntity(new ComponentType[3]
{
ComponentType.ReadOnly<NetworkEventType>(),
ComponentType.ReadOnly<SendEventToUser>(),
ComponentType.ReadOnly<KickEvent>()
});

entity.Write(new KickEvent()
{
PlatformId = userData.PlatformId
});
entity.Write(new SendEventToUser()
{
UserIndex = userData.Index
});
entity.Write(new NetworkEventType()
{
EventId = NetworkEvents.EventId_KickEvent,
IsAdminEvent = false,
IsDebugEvent = false
});
}
}
}
catch (Exception e)
{
Core.Log.LogError($"Failure in {nameof(ServerBootstrapSystem.OnUserConnected)}\nMessage: {e.Message} Inner:{e.InnerException?.Message}\n\nStack: {e.StackTrace}\nInner Stack: {e.InnerException?.StackTrace}");
}
}
}
105 changes: 39 additions & 66 deletions Patches/VivoxPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using ProjectM;
using ProjectM.Network;
using System;
using System.IO;
using System.Linq;
using Unity.Collections;
using Unity.Entities;
Expand All @@ -16,90 +15,64 @@ public static class VivoxPatch
[HarmonyPatch(typeof(VivoxConnectionSystem), nameof(VivoxConnectionSystem.OnUpdate))]
public static void Prefix(VivoxConnectionSystem __instance)
{
NativeArray<Entity> entities = __instance.__query_337126773_0.ToEntityArray(Allocator.Temp);
ProcessEntities(__instance.__query_337126773_0);
ProcessEntities(__instance.__query_337126773_1);
ProcessEntities(__instance.__query_337126773_2);
ProcessEntities(__instance.__query_337126773_3);
}

private static void ProcessEntities(EntityQuery query)
{
NativeArray<Entity> entities = query.ToEntityArray(Allocator.Temp);
foreach (var entity in entities)
{
if (entity.Has<FromCharacter>())
{
User user = entity.Read<FromCharacter>().User.Read<User>();
if (Database.VoiceBans.Exists(x => x.PlayerID == user.PlatformId))
{
// check if expired

Core.Server.EntityManager.DestroyEntity(entity);
}
HandleUser(entity, user);
}
}
entities.Dispose();
}

NativeArray<Entity> entities1 = __instance.__query_337126773_1.ToEntityArray(Allocator.Temp);
foreach (var entity in entities1)
private static void HandleUser(Entity entity, User user)
{
if (Database.VoiceBans.Exists(x => x.PlayerID == user.PlatformId))
{
if (entity.Has<FromCharacter>())
var ban = Database.VoiceBans.First(x => x.PlayerID == user.PlatformId);

if(DateTime.Now > ban.TimeUntil && ban.TimeUntil != DateTime.MinValue)
{
User user = entity.Read<FromCharacter>().User.Read<User>();
if (Database.VoiceBans.Exists(x => x.PlayerID == user.PlatformId))
Database.DeleteBan(ban, Database.VoiceBans);
ServerChatUtils.SendSystemMessageToClient(Core.EntityManager, user,
"Your voice ban has expired. Please verify in your social settings that Voice Proximity is re-enabled.");
}
else
{
if(entity.Has<VivoxEvents.ClientEvent>())
{
var ban = Database.VoiceBans.First(x => x.PlayerID == user.PlatformId);

if (DateTime.Now > ban.TimeUntil && ban.TimeUntil != DateTime.MinValue)
{
Database.VoiceBans.Remove(ban);
VivoxEvents.ClientEvent clientEvent = entity.Read<VivoxEvents.ClientEvent>();
clientEvent.Type = VivoxRequestType.ClientLogin;

ServerChatUtils.SendSystemMessageToClient(Core.EntityManager, user,
"Your voice ban has expired. Please verify in your social settings that Voice Proximity is re-enabled.");
}
else
{
Core.Server.EntityManager.DestroyEntity(entity);
}
entity.Write(clientEvent);
}
}
}

NativeArray<Entity> entities2 = __instance.__query_337126773_2.ToEntityArray(Allocator.Temp);
foreach (var entity in entities2)
{
if (entity.Has<FromCharacter>())
{
User user = entity.Read<FromCharacter>().User.Read<User>();
if (Database.VoiceBans.Exists(x => x.PlayerID == user.PlatformId))
if(entity.Has<VivoxEvents.ClientStateEvent>())
{
Core.Server.EntityManager.DestroyEntity(entity);
VivoxEvents.ClientStateEvent clientStateEvent = entity.Read<VivoxEvents.ClientStateEvent>();
clientStateEvent.IsSpeaking = false;

entity.Write(clientStateEvent);
}

Core.Server.EntityManager.DestroyEntity(entity);
return;
}
}

NativeArray<Entity> entities3 = __instance.__query_337126773_3.ToEntityArray(Allocator.Temp);
foreach (var entity in entities3)

if (Database.Banned.Exists(x => x.PlayerID == user.PlatformId))
{
if (entity.Has<FromCharacter>())
{
User user = entity.Read<FromCharacter>().User.Read<User>();
if (Database.VoiceBans.Exists(x => x.PlayerID == user.PlatformId))
{
Core.Server.EntityManager.DestroyEntity(entity);
}
}
Core.Server.EntityManager.DestroyEntity(entity);
}
}

public static void EntityCompomponentDumper(string filePath, Entity entity)
{
File.AppendAllText(filePath, $"--------------------------------------------------" + Environment.NewLine);
File.AppendAllText(filePath, $"Dumping components of {entity.ToString()}:" + Environment.NewLine);

foreach (var componentType in Core.Server.EntityManager.GetComponentTypes(entity))
{ File.AppendAllText(filePath, $"{componentType.ToString()}" + Environment.NewLine); }

File.AppendAllText(filePath, $"--------------------------------------------------" + Environment.NewLine);

File.AppendAllText(filePath, DumpEntity(entity));
}

private static string DumpEntity(Entity entity, bool fullDump = true)
{
var sb = new Il2CppSystem.Text.StringBuilder();
ProjectM.EntityDebuggingUtility.DumpEntity(Core.Server, entity, fullDump, sb);
return sb.ToString();
}
}
2 changes: 2 additions & 0 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BepInEx.Unity.IL2CPP;
using CrimsonBanned.Structs;
using HarmonyLib;
using System;
using System.IO;
using System.Reflection;
using VampireCommandFramework;
Expand All @@ -11,6 +12,7 @@ namespace CrimsonBanned;

[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency("gg.deca.VampireCommandFramework")]
[BepInDependency("CrimsonSQL", BepInDependency.DependencyFlags.SoftDependency)]
public class Plugin : BasePlugin
{
Harmony _harmony;
Expand Down
Loading

0 comments on commit a926b5c

Please sign in to comment.