Skip to content

Commit

Permalink
Merge pull request #17 from Devwarlt/dev
Browse files Browse the repository at this point in the history
Support to various .NET frameworks + new distribution (PSTk.Redis)
  • Loading branch information
Devwarlt committed Mar 6, 2021
2 parents 8accfe0 + d00a36f commit 6bbeb3a
Show file tree
Hide file tree
Showing 26 changed files with 747 additions and 523 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# IDE0063: Use simple 'using' statement
csharp_prefer_simple_using_statement = false:suggestion
7 changes: 7 additions & 0 deletions PSTk Distribution.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Deployment Scripts", "Deplo
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{923F2816-ACF9-4BBE-8D75-E17F3F0E7376}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
LICENSE = LICENSE
Expand All @@ -47,6 +48,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Articles", "Articles", "{D2
docs\articles\toc.yml = docs\articles\toc.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSTk.Redis", "PSTk.Redis\PSTk.Redis.csproj", "{70734C00-A57D-44C2-A9AF-8C53438867C9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -73,6 +76,10 @@ Global
{3023C538-B4F6-45C4-A683-923BF7AF2E09}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3023C538-B4F6-45C4-A683-923BF7AF2E09}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3023C538-B4F6-45C4-A683-923BF7AF2E09}.Release|Any CPU.Build.0 = Release|Any CPU
{70734C00-A57D-44C2-A9AF-8C53438867C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70734C00-A57D-44C2-A9AF-8C53438867C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70734C00-A57D-44C2-A9AF-8C53438867C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70734C00-A57D-44C2-A9AF-8C53438867C9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 6 additions & 5 deletions PSTk.Core/PSTk.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net472;net5.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Authors>PSTk Core Team</Authors>
<Copyright>Copyright © PSTk Core Team 2020</Copyright>
<Copyright>Copyright © PSTk Core Team 2021</Copyright>
<Company>PSTk Core Team</Company>
<Product>PSTk.Core</Product>
<Description>PSTk is a collection of features available in .NET Core that enhances your game server performance. This toolkit contains optimized code and utilities commonly used on game development.</Description>
Expand All @@ -21,18 +21,18 @@
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<PackageId>PSTk.Core</PackageId>
<ApplicationIcon>ICON.ico</ApplicationIcon>
<Version>1.0.2</Version>
<Version>1.1.2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DocumentationFile>bin\Debug\netcoreapp3.1\PSTk.Core.xml</DocumentationFile>
<DocumentationFile>bin\Debug\$(TargetFramework)\PSTk.Core.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PlatformTarget>x64</PlatformTarget>
<DocumentationFile>bin\Release\netcoreapp3.1\PSTk.Core.xml</DocumentationFile>
<DocumentationFile>bin\Release\$(TargetFramework)\PSTk.Core.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -47,6 +47,7 @@
<CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies>
<ReferenceOutputAssembly>true</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\PSTk.Redis\PSTk.Redis.csproj" />
<ProjectReference Include="..\PSTk.Threading\PSTk.Threading.csproj">
<Private>true</Private>
<CopyLocalSatelliteAssemblies>true</CopyLocalSatelliteAssemblies>
Expand Down
24 changes: 15 additions & 9 deletions PSTk.Core/Tools/Google/GMailValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ public GMailValidator(string emailRegistry)
/// <returns></returns>
public bool IsValid(string email)
{
using var client = new TcpClient(SMTP_DNS, SMTP_PORT);
using var stream = client.GetStream();
using var rdr = new StreamReader(stream);
PerformHELO(stream, rdr);
PerformMAIL(emailRegistry, stream, rdr);
PerformRCPT(stream, rdr, email, out var isValid);
PerformQUITE(stream);
client.Close();
return isValid;
using (var client = new TcpClient(SMTP_DNS, SMTP_PORT))
{
using (var stream = client.GetStream())
{
using (var rdr = new StreamReader(stream))
{
PerformHELO(stream, rdr);
PerformMAIL(emailRegistry, stream, rdr);
PerformRCPT(stream, rdr, email, out var isValid);
PerformQUITE(stream);
client.Close();
return isValid;
}
}
}
}

private static void PerformHELO(NetworkStream stream, StreamReader rdr)
Expand Down
32 changes: 17 additions & 15 deletions PSTk.Core/Tools/SerialNumberGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,25 @@ public SerialNumberGenerator(string secret, int pieceSize = 4, char pieceSeparat
/// <returns></returns>
public string Create(string text)
{
using var sha256 = new SHA256Managed();
var buffer = Encoding.UTF8.GetBytes(text + secret);
var hash = sha256.ComputeHash(buffer);
var encoder = Convert.ToBase64String(hash);
var byteHash = EightByteHash(encoder);
var byteHashAbs = Math.Abs(byteHash) * 10f + (byteHash < 0 ? 1d : 0d);
var key = new StringBuilder();
var hashStr = byteHashAbs.ToString();
var pieces = hashStr.ChunkSplit(pieceSize).ToArray();
for (var i = 0; i < pieces.Length; i++)
using (var sha256 = new SHA256Managed())
{
var piece = ushort.Parse(pieces[i]);
key.Append($"{piece:X4}");
if (i + 1 < pieces.Length)
key.Append(pieceSeparator);
var buffer = Encoding.UTF8.GetBytes(text + secret);
var hash = sha256.ComputeHash(buffer);
var encoder = Convert.ToBase64String(hash);
var byteHash = EightByteHash(encoder);
var byteHashAbs = Math.Abs(byteHash) * 10f + (byteHash < 0 ? 1d : 0d);
var key = new StringBuilder();
var hashStr = byteHashAbs.ToString();
var pieces = hashStr.ChunkSplit(pieceSize).ToArray();
for (var i = 0; i < pieces.Length; i++)
{
var piece = ushort.Parse(pieces[i]);
key.Append($"{piece:X4}");
if (i + 1 < pieces.Length)
key.Append(pieceSeparator);
}
return key.ToString();
}
return key.ToString();
}

private int EightByteHash(string text)
Expand Down
17 changes: 17 additions & 0 deletions PSTk.Diagnostics/Logging/LogSlim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ public void OnUnhandledException(object sender, UnhandledExceptionEventArgs args
public void PrintWarn(string message) => OnConsoleLog(Wrn, message, ConsoleColor.Yellow);

private static string GenerateTimePattern(LogTimeOptions options)
#if NET472
{
switch (options)
{
case LogTimeOptions.Classic: return "MM/dd/yyyy hh:mm tt";
case LogTimeOptions.ClassicRegular: return "MM/dd/yyyy hh:mm:ss tt";
case LogTimeOptions.ClassicScientific: return "MM/dd/yyyy hh:mm:ss:ffff tt";
case LogTimeOptions.FullRegular: return "dddd, dd MMMM yyyy - HH:mm:ss tt";
case LogTimeOptions.FullScientific: return "dddd, dd MMMM yyyy - HH:mm:ss:ffff";
case LogTimeOptions.Regular: return "HH:mm:ss tt";
case LogTimeOptions.Scientific: return "HH:mm:ss:ffff";
default: return string.Empty;
}
}
#else
=> options switch
{
LogTimeOptions.Classic => "MM/dd/yyyy hh:mm tt",
Expand All @@ -110,6 +125,8 @@ private static string GenerateTimePattern(LogTimeOptions options)
_ => string.Empty
};

#endif

private string LogHeader(string level) => $"[{LogTimer()}] | [{level}] | <{typeof(T).Name}> -> ";

private string LogTimer() => DateTime.Now.ToString(timePattern);
Expand Down
10 changes: 5 additions & 5 deletions PSTk.Diagnostics/PSTk.Diagnostics.csproj
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net472;net5.0</TargetFrameworks>
<Description>Contains diagnostic utilities part of PSTk.Core toolkit.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<Authors>PSTk Core Team</Authors>
<Copyright>Copyright © PSTk Core Team 2020</Copyright>
<Copyright>Copyright © PSTk Core Team 2021</Copyright>
<PackageProjectUrl>https://github.com/Devwarlt/pstk-core</PackageProjectUrl>
<RepositoryUrl>https://github.com/Devwarlt/pstk-core</RepositoryUrl>
<PackageIcon>ICON.png</PackageIcon>
<RepositoryType>Git</RepositoryType>
<PackageTags>dotnet;toolkit;game-tools;pserver</PackageTags>
<ApplicationIcon>ICON.ico</ApplicationIcon>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<Optimize>false</Optimize>
<DocumentationFile>bin\Debug\netcoreapp3.1\PSTk.Diagnostics.xml</DocumentationFile>
<DocumentationFile>bin\Debug\$(TargetFramework)\PSTk.Diagnostics.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DocumentationFile>bin\Release\netcoreapp3.1\PSTk.Diagnostics.xml</DocumentationFile>
<DocumentationFile>bin\Release\$(TargetFramework)\PSTk.Diagnostics.xml</DocumentationFile>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

Expand Down
29 changes: 14 additions & 15 deletions PSTk.Diagnostics/Profiling/TimedProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace PSTk.Diagnostics
/// </summary>
public sealed class TimedProfiler : IDisposable
{
private string Message { get; }
private Func<bool> Condition { get; }
private Action<string> Output { get; }
private Stopwatch Stopwatch { get; }

/// <summary>
/// Create a new instance of <see cref="TimedProfiler"/>.
/// </summary>
Expand All @@ -17,32 +22,26 @@ public sealed class TimedProfiler : IDisposable
/// <param name="output"></param>
public TimedProfiler(string message, Func<bool> condition = null, Action<string> output = null)
{
this.message = message;
this.condition = condition;
this.output = output;
stopwatch = Stopwatch.StartNew();
Message = message;
Condition = condition;
Output = output;
Stopwatch = Stopwatch.StartNew();
}

private Func<bool> condition { get; }
private string message { get; }
private Action<string> output { get; }
private Stopwatch stopwatch { get; }

/// <summary>
/// Called automatically at the end of the scope when used along side a using statment or explicitly called in the code
/// It will only print out the elapsed time when the condition is met if there is a condition to the desired output if set
/// otherwise it will log to the console
/// </summary>
public void Dispose()
{
if (condition != null && !condition.Invoke())
if (Condition != null && !Condition.Invoke())
return;

stopwatch.Stop();
Stopwatch.Stop();

var result = $"{message} | Elapsed: {stopwatch.Elapsed} ({stopwatch.ElapsedMilliseconds}ms)";
output?.Invoke(result);
if (output == null)
var result = $"{Message} | Elapsed: {Stopwatch.Elapsed} ({Stopwatch.ElapsedMilliseconds}ms)";
Output?.Invoke(result);
if (Output == null)
Console.WriteLine(result);
}
}
Expand Down
11 changes: 6 additions & 5 deletions PSTk.Extensions/PSTk.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net472;net5.0</TargetFrameworks>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<Authors>PSTk Core Team</Authors>
<Company>PSTk Core Team</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Contains extension utilities part of PSTk.Core toolkit.</Description>
<Copyright>Copyright © PSTk Core Team 2020</Copyright>
<Copyright>Copyright © PSTk Core Team 2021</Copyright>
<PackageProjectUrl>https://github.com/Devwarlt/pstk-core</PackageProjectUrl>
<RepositoryUrl>https://github.com/Devwarlt/pstk-core</RepositoryUrl>
<PackageIcon>ICON.png</PackageIcon>
<RepositoryType>Git</RepositoryType>
<PackageTags>dotnet;toolkit;game-tools;pserver</PackageTags>
<ApplicationIcon>ICON.ico</ApplicationIcon>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DocumentationFile>bin\Debug\netcoreapp3.1\PSTk.Extensions.xml</DocumentationFile>
<DocumentationFile>bin\Debug\$(TargetFramework)\PSTk.Extensions.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DocumentationFile>bin\Release\netcoreapp3.1\PSTk.Extensions.xml</DocumentationFile>
<DocumentationFile>bin\Release\$(TargetFramework)\PSTk.Extensions.xml</DocumentationFile>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
<None Include="..\ICON.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
Expand Down
30 changes: 20 additions & 10 deletions PSTk.Extensions/Utils/BufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ public static byte[] GZipCompress(this byte[] buffer)
if (buffer == null)
throw new ArgumentNullException(nameof(buffer), "Cannot compress a null buffer.");

using var stream = new MemoryStream();
using var zip = new GZipStream(stream, CompressionMode.Compress);
zip.Write(buffer, 0, buffer.Length);
zip.Close();
return stream.ToArray();
using (var stream = new MemoryStream())
{
using (var zip = new GZipStream(stream, CompressionMode.Compress))
{
zip.Write(buffer, 0, buffer.Length);
zip.Close();
return stream.ToArray();
}
}
}

/// <summary>
Expand All @@ -38,11 +42,17 @@ public static byte[] GZipDecompress(this byte[] buffer)
if (buffer == null)
throw new ArgumentNullException(nameof(buffer), "Cannot decompress a null buffer.");

using var zipStream = new MemoryStream(buffer);
using var stream = new MemoryStream();
using var unzip = new GZipStream(zipStream, CompressionMode.Decompress);
unzip.CopyTo(stream);
return stream.ToArray();
using (var zipStream = new MemoryStream(buffer))
{
using (var stream = new MemoryStream())
{
using (var unzip = new GZipStream(zipStream, CompressionMode.Decompress))
{
unzip.CopyTo(stream);
return stream.ToArray();
}
}
}
}
}
}
4 changes: 4 additions & 0 deletions PSTk.Extensions/Utils/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ public static string GetElapsedTime(this DateTime date, string header)
}

sb.Append(" and ");
#if NET472
sb.Append(format[format.Count - 1]);
#else
sb.Append(format[^1]);
#endif
}
else
{
Expand Down
9 changes: 8 additions & 1 deletion PSTk.Extensions/Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public static string ToHumanReadable(this string text)
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string ToUpperFirst(this string text) => char.ToUpper(text[0]) + text[1..];
public static string ToUpperFirst(this string text)
=> char.ToUpper(text[0]) +
#if NET472
text.Substring(1);
#else
text[1..];

#endif
}
}
Loading

0 comments on commit 6bbeb3a

Please sign in to comment.