SecondBot/SecondBotEvents/Static/Helpers.cs
Madpeter 050ef3c133 Replace Newtonsoft.Json with System.Text.Json
Migrated all serialization and deserialization from Newtonsoft.Json to System.Text.Json across the codebase for improved performance and reduced dependencies. Updated package references and refactored helper method names for consistency (e.g., IsEmpty, NotEmpty, InRange). Also updated NuGet package versions in project files.
2025-10-03 23:12:20 +01:00

184 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
namespace SecondBotEvents
{
public static class StringExtensions
{
public static string FirstCharToUpper(this string input)
{
switch (input)
{
case null: throw new ArgumentNullException(nameof(input));
case "": throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
default: return input.First().ToString().ToUpper() + input.Substring(1);
}
}
}
public static class SecondbotHelpers
{
//
public static byte[] ReadResourceFileBinary(Assembly targetenv, string filename)
{
try
{
byte[] dataset;
string[] files = targetenv.GetManifestResourceNames();
string resourceName = files.Single(str => str.EndsWith(filename));
using (Stream stream = targetenv.GetManifestResourceStream(resourceName))
using (BinaryReader br = new(stream))
{
const int bufferSize = 4096;
using (var ms = new MemoryStream())
{
byte[] buffer = new byte[bufferSize];
int count;
while ((count = br.Read(buffer, 0, buffer.Length)) != 0)
ms.Write(buffer, 0, count);
dataset = ms.ToArray();
}
}
return dataset;
}
catch (Exception e)
{
return Encoding.UTF8.GetBytes(e.Message);
}
}
public static string ReadResourceFile(Assembly targetenv,string filename)
{
try
{
string[] files = targetenv.GetManifestResourceNames();
string resourceName = files.Single(str => str.EndsWith(filename));
string result = "";
using (Stream stream = targetenv.GetManifestResourceStream(resourceName))
using (StreamReader reader = new(stream))
{
result = reader.ReadToEnd();
}
return result;
}
catch (Exception e)
{
return e.Message;
}
}
public static bool InRange(float current,float min,float max)
{
if(current <= max)
{
if(current >= min)
{
return true;
}
}
return false;
}
public static bool InRange(double current, double min, double max)
{
if (current <= max)
{
if (current >= min)
{
return true;
}
}
return false;
}
public static bool Inbox(float expected, float current, float drift)
{
return DistanceCheck(expected, current, drift, true);
}
public static bool DistanceCheck(float expected, float current, float drift, bool current_value)
{
if (current > (expected + drift))
{
return false;
}
else if (current < (expected - drift))
{
return false;
}
return current_value;
}
public static string GetSHA1(string text)
{
SHA1 hash = SHA1CryptoServiceProvider.Create();
byte[] plainTextBytes = Encoding.UTF8.GetBytes(text);
byte[] hashBytes = hash.ComputeHash(plainTextBytes);
string localChecksum = BitConverter.ToString(hashBytes)
.Replace("-", "").ToLowerInvariant();
return localChecksum;
}
public static string[] ParseSLurl(string url)
{
if (url != null)
{
url = url.Replace("http://maps.secondlife.com/secondlife/", "");
url = url.Replace("%20", " ");
string[] bits = url.Split('/');
if (bits.Length == 4)
{
return bits;
}
}
return null;
}
public static string RegionnameFromSLurl(string url)
{
string[] bits = ParseSLurl(url);
if (bits != null)
{
if (bits.Length == 4)
{
return bits[0];
}
}
return "";
}
public static long UnixTimeNow()
{
var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
return (long)timeSpan.TotalSeconds;
}
public static bool NotEmpty(string V)
{
if (V != null)
{
if (V.Length > 0)
{
return true;
}
}
return false;
}
public static bool IsEmpty(string V)
{
if (V != null)
{
if (V.Length > 0)
{
return false;
}
}
return true;
}
public static bool NotEmpty(string[] V)
{
if (V != null)
{
return true;
}
return false;
}
}
}