Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mono incompatible with WPF. Added minimum character check. #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ releases/
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*/bin/*
*/DLL/*
*/obj/*
bin/*
obj/*
Expand Down
10 changes: 5 additions & 5 deletions NameValidator/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class Config
/// </summary>
public string Action = "kick";

/// <summary>
/// The minimum number of characters in a nickname.
/// </summary>
public int MinimumCharacters = 3;

/// <summary>
/// The reason to use when the action is set to kick/ban.
/// </summary>
Expand All @@ -34,11 +39,6 @@ public class Config
/// </summary>
public List<string> InvalidNameRegexes = new List<string>();

/// <summary>
/// Whether to kick any player using characters that the Terraria font doesn't natively support.
/// </summary>
public bool TerrariaFontOnly = false;

public static Config Read(string path)
{
try
Expand Down
64 changes: 24 additions & 40 deletions NameValidator/NameValidator.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Media;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
Expand All @@ -14,8 +12,6 @@ namespace NameValidator
[ApiVersion(2, 1)]
public class NameValidator : TerrariaPlugin
{
private FontFamily font;

public override string Author => "Enerdy";

public override string Description => "Validate character names on join based on a configuration file.";
Expand All @@ -31,25 +27,13 @@ public NameValidator(Main game)
{
Order = 10;
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.NetGreetPlayer.Register(this, OnJoin);
}
}


public override void Initialize()
{
string fontname = "Andy";
if ((font = Fonts.SystemFontFamilies.FirstOrDefault(f => f.Source.Equals(fontname, StringComparison.OrdinalIgnoreCase))) == null)
TShock.Log.ConsoleError($"The font '{fontname}' was not found.");

string path = Path.Combine(TShock.SavePath, "NameValidator.json");
Config = Config.Read(path);

ServerApi.Hooks.NetGreetPlayer.Register(this, OnJoin);
ServerApi.Hooks.ServerJoin.Register(this, OnJoin);

Commands.ChatCommands.Add(new Command("namevalidator.reload", (args) =>
{
Expand All @@ -58,7 +42,16 @@ public override void Initialize()
}, "nvreload"));
}

private void OnJoin(GreetPlayerEventArgs e)
protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.ServerJoin.Deregister(this, OnJoin);
}
}

//private void OnJoin(GreetPlayerEventArgs e)
private void OnJoin(JoinEventArgs e)
{
if (e.Handled || e.Who < 0 || e.Who > Main.player.Length - 1)
return;
Expand All @@ -67,18 +60,23 @@ private void OnJoin(GreetPlayerEventArgs e)
// If the player's name is null then it most likely isn't a real player
if (!String.IsNullOrEmpty(player?.name))
{
string name = player.name;
string name = player.name.ToLower();
var playerX = new TSPlayer(e.Who);

if (name.Length < Config.MinimumCharacters)
playerX.Kick($"Minimum name length of {Config.MinimumCharacters} characters required!", true, true);

if (!ValidateString(name))
{
switch (Config.Action.ToLowerInvariant())
{
case "ban":
TShock.Utils.Ban(TShock.Players[e.Who], Config.Reason);
TShock.Players[e.Who].Ban(Config.Reason);
TShock.Log.ConsoleInfo($"Player '{name}' was banned for \"{Config.Reason}\".");
e.Handled = true;
return;
case "kick":
TShock.Utils.Kick(TShock.Players[e.Who], Config.Reason, silent: true);
TShock.Players[e.Who].Kick(Config.Reason, silent: true);
TShock.Log.ConsoleInfo($"Player '{name}' was kicked for \"{Config.Reason}\".");
e.Handled = true;
return;
Expand All @@ -88,6 +86,10 @@ private void OnJoin(GreetPlayerEventArgs e)
}
}
}
else
{
TShock.Log.ConsoleInfo($"An attempted connection without a null just occured.");
}
}

/// <summary>
Expand All @@ -97,24 +99,6 @@ private void OnJoin(GreetPlayerEventArgs e)
/// <returns>Whether the string is valid.</returns>
private bool ValidateString(string s)
{
// Font contains check
if (Config.TerrariaFontOnly)
{
foreach (char c in s)
{
ICollection<Typeface> typefaces = font.GetTypefaces();
foreach (Typeface t in typefaces)
{
t.TryGetGlyphTypeface(out GlyphTypeface glyph);
if (glyph != null && !glyph.CharacterToGlyphMap.ContainsKey(Convert.ToInt16(c)))
{
// Spot detected
return false;
}
}
}
}

// Regex check
if (Config.InvalidNameRegexes != null)
{
Expand Down
30 changes: 17 additions & 13 deletions NameValidator/NameValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,22 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<HintPath>refs\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="OTAPI, Version=1.3.4.4, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="OTAPI, Version=1.4.1.2, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>DLL\OTAPI.dll</HintPath>
<HintPath>refs\OTAPI.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="TerrariaServer">
<HintPath>DLL\TerrariaServer.exe</HintPath>
<Reference Include="TerrariaServer, Version=1.4.1.2, Culture=neutral, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>refs\TerrariaServer.exe</HintPath>
</Reference>
<Reference Include="TShockAPI">
<HintPath>DLL\TShockAPI.dll</HintPath>
<Reference Include="TShockAPI, Version=4.4.0.0, Culture=neutral, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>refs\TShockAPI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -79,6 +76,13 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Content Include="refs\Newtonsoft.Json.dll" />
<Content Include="refs\OTAPI.dll" />
<Content Include="refs\TerrariaServer.exe" />
<Content Include="refs\TShockAPI.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Binary file added NameValidator/refs/Newtonsoft.Json.dll
Binary file not shown.
Binary file added NameValidator/refs/OTAPI.dll
Binary file not shown.
Binary file added NameValidator/refs/TShockAPI.dll
Binary file not shown.
Binary file added NameValidator/refs/TerrariaServer.exe
Binary file not shown.