Publish modifications to ZNI Library
This commit is contained in:
parent
62c4eb5850
commit
7b776ecfea
6 changed files with 712 additions and 51 deletions
|
@ -1,42 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI
|
||||
{
|
||||
public class ArgumentParser
|
||||
{
|
||||
public static Arguments Parse(string[] lInputs)
|
||||
{
|
||||
Arguments a = new Arguments();
|
||||
|
||||
for(int i = 0; i < lInputs.Length; i ++ )
|
||||
{
|
||||
if (lInputs[i].StartsWith("-"))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
if (lInputs[i + 1].StartsWith("-"))
|
||||
a[lInputs[i]] = "1";
|
||||
else
|
||||
{
|
||||
a[lInputs[i]] = lInputs[i + 1];
|
||||
}
|
||||
}catch(IndexOutOfRangeException e)
|
||||
{
|
||||
a[lInputs[i]] = "1";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
}
|
||||
public class Arguments : Dictionary<string, string>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
4
HTTP.cs
4
HTTP.cs
|
@ -12,7 +12,9 @@ namespace LibZNI
|
|||
{
|
||||
public class HTTP
|
||||
{
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public static HTTPReplyData performRequest(string url, string sJson)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
HttpRequestMessage hrm = new HttpRequestMessage();
|
||||
hrm.Method = HttpMethod.Post;
|
||||
|
@ -20,7 +22,9 @@ namespace LibZNI
|
|||
hrm.Content = new StringContent(sJson, Encoding.UTF8, "application/json");
|
||||
return HTTP.Request(hrm);
|
||||
}
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public static HTTPReplyData performRequest(string url, string sJson, string xSLOwner)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
HttpRequestMessage hrm = new HttpRequestMessage();
|
||||
hrm.Method = HttpMethod.Post;
|
||||
|
|
|
@ -4,10 +4,8 @@
|
|||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<SignAssembly>True</SignAssembly>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<Configurations>Debug;Release;DebPub;KVPBuild</Configurations>
|
||||
<AssemblyOriginatorKeyFile>C:\Users\tyler\Downloads\ZNI.pfx</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -19,6 +17,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MySqlConnector" Version="2.1.11" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
|
||||
</ItemGroup>
|
||||
|
|
316
PluginSystem.cs
Normal file
316
PluginSystem.cs
Normal file
|
@ -0,0 +1,316 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LibZNI
|
||||
{
|
||||
public interface IPlugin
|
||||
{
|
||||
/// <summary>
|
||||
/// This function is called when the plugin is first activated by the Plugin Loader. Tasks, such as loading configuration should be executed in this function
|
||||
/// </summary>
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public void onActivate();
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
/// <summary>
|
||||
/// Called upon unloading the assembly
|
||||
/// </summary>
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public void onDeactivate();
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
|
||||
/// <summary>
|
||||
/// This function is called every tick. A tick should ideally run every 2 seconds
|
||||
/// </summary>
|
||||
/// <param name="lastTick">The last time a tick was executed by the host program</param>
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public void onTick(TimeSpan lastTick);
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
|
||||
public string PluginName { get; }
|
||||
public VersionNumber PluginVersion { get; }
|
||||
}
|
||||
public class VersionNumber
|
||||
{
|
||||
public int Major;
|
||||
public int Minor;
|
||||
public int Build;
|
||||
public int Revision;
|
||||
|
||||
public static bool operator >(VersionNumber a, VersionNumber b)
|
||||
{
|
||||
bool ret = true;
|
||||
bool f = false;
|
||||
if (a.Major < b.Major) ret=f;
|
||||
if (a.Minor < b.Minor) ret = f;
|
||||
if (a.Build < b.Build) ret = f;
|
||||
if (a.Revision < b.Revision) ret = f;
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
public static bool operator <(VersionNumber a, VersionNumber b)
|
||||
{
|
||||
|
||||
bool ret = true;
|
||||
bool f = false;
|
||||
if (a.Major > b.Major) ret = f;
|
||||
if (a.Minor > b.Minor) ret = f;
|
||||
if (a.Build > b.Build) ret = f;
|
||||
if (a.Revision > b.Revision) ret = f;
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static VersionNumber operator +(VersionNumber a, VersionNumber b)
|
||||
{
|
||||
a.Major += b.Major;
|
||||
a.Minor += b.Minor;
|
||||
a.Build += b.Build;
|
||||
a.Revision += b.Revision;
|
||||
|
||||
return a;
|
||||
}
|
||||
public static VersionNumber operator -(VersionNumber a, VersionNumber b)
|
||||
{
|
||||
a.Major -= b.Major;
|
||||
a.Minor -= b.Minor;
|
||||
a.Build -= b.Build;
|
||||
a.Revision -= b.Revision;
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Major}.{Minor}.{Build}.{Revision}";
|
||||
}
|
||||
|
||||
}
|
||||
public class PluginSystem
|
||||
{
|
||||
public Assembly LoadedASM = null;
|
||||
public Assembly LoadLibrary(string DLL)
|
||||
{
|
||||
LoadedASM = Assembly.LoadFrom(DLL);
|
||||
return LoadedASM;
|
||||
}
|
||||
|
||||
public List<IPlugin> Activate(Assembly asm)
|
||||
{
|
||||
List<IPlugin> ret = new List<IPlugin>();
|
||||
foreach(Type A in asm.GetTypes())
|
||||
{
|
||||
Type check = A.GetInterface("IPlugin");
|
||||
if (check != null)
|
||||
{
|
||||
IPlugin plugin = Activator.CreateInstance(A) as IPlugin;
|
||||
plugin.onActivate();
|
||||
ret.Add(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Scans memory for commands
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Attribute that should be scanned for</typeparam>
|
||||
public static void ScanForCommands<T>()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class CommandRegistry
|
||||
{
|
||||
public static CommandRegistry _reg = null;
|
||||
public static readonly object lckreg = new object();
|
||||
static CommandRegistry() { }
|
||||
|
||||
public static CommandRegistry Master
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_reg != null) return _reg;
|
||||
else
|
||||
{
|
||||
lock (lckreg)
|
||||
{
|
||||
if (_reg == null) _reg = new CommandRegistry();
|
||||
return _reg;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We now want to have as universal of a command registry as possible.
|
||||
// The structure of this should be equivalent to a Hive
|
||||
// All functions should return a CommandResult instance which will provide a error code, and response message.
|
||||
// The CommandResult should also be able to convey if it is meant to be responded to privately, or in the same context where the command was issued.
|
||||
|
||||
/// <summary>
|
||||
/// This provides the actual registry.
|
||||
/// </summary>
|
||||
private Hive Registry { get; set; } = new Hive();
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public void register(CommandBase baseApi)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
Registry.register(baseApi);
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public CommandBase getCommand(string sCmd)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
return Registry.get(sCmd);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The registry hive. Subject to change to a real registry format.
|
||||
/// </summary>
|
||||
public class Hive
|
||||
{
|
||||
// This functionality, is just a registry hive
|
||||
private Dictionary<string, CommandBase> commands = new Dictionary<string, CommandBase>();
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public void register(CommandBase api)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
commands[api.Command] = api;
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public CommandBase get(string sCmd)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
if (commands.ContainsKey(sCmd) == false) return null;
|
||||
return commands[sCmd];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class KVP<T>
|
||||
{
|
||||
public string Key;
|
||||
public T Value;
|
||||
}
|
||||
|
||||
public class CommandArguments
|
||||
{
|
||||
public Dictionary<string, object> args = new Dictionary<string, object>();
|
||||
public int AuthorityLevel;
|
||||
|
||||
public CommandArguments(params KVP<object>[] arguments)
|
||||
{
|
||||
foreach(KVP<object> arg in arguments)
|
||||
{
|
||||
args.Add(arg.Key, arg.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum ErrorCodes
|
||||
{
|
||||
AUTH_LEVEL=0x0001,
|
||||
SUCCESS=0x0002,
|
||||
EXPECTED_PARAMETERS_MISSING=0x0003
|
||||
}
|
||||
|
||||
|
||||
public class CommandResult
|
||||
{
|
||||
public enum ResultType
|
||||
{
|
||||
OK,
|
||||
ERROR
|
||||
}
|
||||
public enum ResponseType
|
||||
{
|
||||
SAFE,
|
||||
SENSITIVE
|
||||
}
|
||||
public ResultType Type;
|
||||
public string ResponseText;
|
||||
public ResponseType Resp_Type;
|
||||
public int ResponseCode;
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] // Allow alias commands
|
||||
public class CommandBase : Attribute
|
||||
{
|
||||
[Flags]
|
||||
public enum Destinations
|
||||
{
|
||||
ANY = 1,
|
||||
LOCAL = 2,
|
||||
GROUP = 4,
|
||||
IM = 8,
|
||||
DISCORD = 16
|
||||
}
|
||||
public string Command;
|
||||
public int MinLevel;
|
||||
public MethodInfo assignedMethod;
|
||||
public CommandHelp usage;
|
||||
public Destinations cmdType;
|
||||
|
||||
public CommandBase(string cmd, int level, string hlp, Destinations dest)
|
||||
{
|
||||
Command = cmd;
|
||||
MinLevel = level;
|
||||
usage = new CommandHelp(cmd, level, hlp, dest);
|
||||
cmdType = dest;
|
||||
}
|
||||
}
|
||||
|
||||
public class CommandHelp
|
||||
{
|
||||
public string Name;
|
||||
public int lvl;
|
||||
public string Text;
|
||||
public string sources;
|
||||
public CommandBase.Destinations Dests;
|
||||
|
||||
public bool HasGroupFlag()
|
||||
{
|
||||
return ((Dests & CommandBase.Destinations.GROUP) == CommandBase.Destinations.GROUP);
|
||||
}
|
||||
public static readonly string NoArgs = "This command does not take any arguments";
|
||||
|
||||
public string GetUsageAsString()
|
||||
{
|
||||
return $"_\nCommand [{Name}]\n{sources}\nLevel Required: {lvl}\nUsage: \n\n{Text}";
|
||||
}
|
||||
|
||||
public CommandHelp(string cmdname, int lvls, string htext, CommandBase.Destinations dst)
|
||||
{
|
||||
Name = cmdname;
|
||||
lvl = lvls;
|
||||
List<string> rss = new List<string>();
|
||||
if ((Dests & CommandBase.Destinations.GROUP) == CommandBase.Destinations.GROUP) rss.Add("Group");
|
||||
if ((Dests & CommandBase.Destinations.LOCAL) == CommandBase.Destinations.LOCAL) rss.Add("Local");
|
||||
if ((Dests & CommandBase.Destinations.IM) == CommandBase.Destinations.IM) rss.Add("IM");
|
||||
if ((Dests & CommandBase.Destinations.DISCORD) == CommandBase.Destinations.DISCORD) rss.Add("Discord");
|
||||
if ((Dests & CommandBase.Destinations.ANY) == CommandBase.Destinations.ANY)
|
||||
{
|
||||
rss.Clear();
|
||||
rss.Add("Any");
|
||||
}
|
||||
|
||||
Dests = dst;
|
||||
Text = htext;
|
||||
sources = $"Command can be used in: {String.Join(", ", rss)}";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -19,7 +19,9 @@ namespace LibZNI
|
|||
DELTA = int.Parse("0x9E3779B9", System.Globalization.NumberStyles.HexNumber);
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public int hex2int(string hex)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
if(hex.Substring(0,2) == "0x")
|
||||
{
|
||||
|
@ -34,12 +36,16 @@ namespace LibZNI
|
|||
return int.Parse("0x" + hex, System.Globalization.NumberStyles.HexNumber);
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
void set_xtea_key(int[] key)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
xtea_key = key;
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public int[] make_key(string str)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
MD5 sum = MD5.Create();
|
||||
str = Convert.ToHexString(sum.ComputeHash(Encoding.UTF8.GetBytes(str)));
|
||||
|
@ -65,7 +71,9 @@ namespace LibZNI
|
|||
return sb.ToString();
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public string encipher(int v0, int v1)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
int num = rounds;
|
||||
int sum = 0;
|
||||
|
@ -82,7 +90,9 @@ namespace LibZNI
|
|||
return ConvertToBase64Arithmetic((uint)v0).Substring(0, 6) + ConvertToBase64Arithmetic((uint)v1).Substring(0, 6);
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public string decipher(int v0, int v1)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
int num = rounds;
|
||||
int sum = DELTA * num;
|
||||
|
@ -100,7 +110,9 @@ namespace LibZNI
|
|||
return ConvertToBase64Arithmetic((uint)v0).Substring(0, 5) + ConvertToBase64Arithmetic((uint)v1).Substring(0, 5);
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public string encrypt_string(string str)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
str = Tools.Base64Encode(str);
|
||||
int i = str.IndexOf('=');
|
||||
|
|
386
Tools.cs
386
Tools.cs
|
@ -5,12 +5,151 @@ using System.Numerics;
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace LibZNI
|
||||
{
|
||||
public class Tools
|
||||
{
|
||||
{
|
||||
/// <summary>
|
||||
/// Finds whether the sum can even be calculated!
|
||||
/// </summary>
|
||||
/// <param name="targetSum">Target</param>
|
||||
/// <param name="nums">List of numbers to use</param>
|
||||
/// <returns>True if it is possible</returns>
|
||||
public static bool canSum(int targetSum, List<int> nums, Dictionary<int, bool> memo)
|
||||
{
|
||||
if (memo == null) memo = new Dictionary<int, bool>();
|
||||
if (memo.ContainsKey(targetSum)) return memo[targetSum];
|
||||
|
||||
if (targetSum == 0) return true;
|
||||
if (targetSum < 0) return false;
|
||||
|
||||
foreach (int element in nums)
|
||||
{
|
||||
int remain = targetSum - element;
|
||||
if (canSum(remain, nums, memo))
|
||||
{
|
||||
memo[targetSum] = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
memo[targetSum] = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds whether the sum can even be calculated!
|
||||
/// </summary>
|
||||
/// <param name="targetSum">Target</param>
|
||||
/// <param name="nums">List of numbers to use</param>
|
||||
/// <returns>True if it is possible</returns>
|
||||
public static bool canSum(BigInteger targetSum, List<BigInteger> nums, Dictionary<BigInteger,bool> memo, int recurse=0)
|
||||
{
|
||||
if (memo == null) memo = new Dictionary<BigInteger, bool>();
|
||||
if (memo.ContainsKey(targetSum)) return memo[targetSum];
|
||||
|
||||
if (recurse >= 20) throw new Exception("Fatal: Too nested");
|
||||
|
||||
if (targetSum == 0) return true;
|
||||
if (targetSum < 0) return false;
|
||||
|
||||
foreach(BigInteger element in nums)
|
||||
{
|
||||
BigInteger remain = targetSum - element;
|
||||
if (canSum(remain, nums, memo, recurse+1))
|
||||
{
|
||||
memo[targetSum] = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
memo[targetSum] = false;
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Finds the shortest possible way to create the sum and returns that list
|
||||
/// </summary>
|
||||
/// <param name="elements">The list of items to create {sum} from</param>
|
||||
/// <param name="sum">The magical number you want to recreate</param>
|
||||
/// <param name="memo">A memory list to reduce the time taken finding the best way to make the sum!</param>
|
||||
/// <returns>The list of {elements} recursively that can make the sum</returns>
|
||||
public static List<int> BestSum(List<int> elements, int sum, Dictionary<int, List<int>> memo)
|
||||
{
|
||||
if (memo != null)
|
||||
{
|
||||
if (memo.ContainsKey(sum)) return memo[sum];
|
||||
}
|
||||
else memo = new Dictionary<int, List<int>>();
|
||||
|
||||
if (sum == 0) return new List<int>();
|
||||
if (sum < 0) return null;
|
||||
if (!canSum(sum, elements, null)) return null;
|
||||
|
||||
|
||||
List<int> shortestCombo = null;
|
||||
|
||||
foreach (int element in elements)
|
||||
{
|
||||
int remainder = sum - element;
|
||||
|
||||
List<int> remCombo = BestSum(elements, remainder, memo);
|
||||
if (remCombo != null)
|
||||
{
|
||||
List<int> combo = new List<int>(remCombo);
|
||||
combo.Add(element);
|
||||
if (shortestCombo == null || combo.Count < shortestCombo.Count)
|
||||
{
|
||||
shortestCombo = combo;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
memo[sum] = shortestCombo;
|
||||
return shortestCombo;
|
||||
}
|
||||
/// <summary>
|
||||
/// Finds the shortest possible way to create the sum and returns that list
|
||||
/// </summary>
|
||||
/// <param name="elements">The list of items to create {sum} from</param>
|
||||
/// <param name="sum">The magical number you want to recreate</param>
|
||||
/// <param name="memo">A memory list to reduce the time taken finding the best way to make the sum!</param>
|
||||
/// <returns>The list of {elements} recursively that can make the sum</returns>
|
||||
public static List<BigInteger> BestSum(List<BigInteger> elements, BigInteger sum, Dictionary<BigInteger, List<BigInteger>> memo, int recursion=0)
|
||||
{
|
||||
if (memo != null)
|
||||
{
|
||||
if (memo.ContainsKey(sum)) return memo[sum];
|
||||
}
|
||||
else memo = new Dictionary<BigInteger, List<BigInteger>>();
|
||||
if (recursion >= 20) throw new Exception("Fatal: Too nested");
|
||||
if (sum == 0) return new List<BigInteger>();
|
||||
if (sum < 0) return null;
|
||||
if (!canSum(sum, elements, null)) return null;
|
||||
|
||||
List<BigInteger> shortestCombo = null;
|
||||
|
||||
foreach (BigInteger element in elements)
|
||||
{
|
||||
BigInteger remainder = sum - element;
|
||||
List<BigInteger> remCombo = BestSum(elements, remainder, memo, recursion+1);
|
||||
if (remCombo != null)
|
||||
{
|
||||
List<BigInteger> combo = new List<BigInteger>(remCombo);
|
||||
combo.Add(element);
|
||||
if (shortestCombo == null || combo.Count < shortestCombo.Count)
|
||||
{
|
||||
shortestCombo = combo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memo[sum] = shortestCombo;
|
||||
return shortestCombo;
|
||||
}
|
||||
/// <summary>
|
||||
/// This function is meant for aiding in attacking the TripleDES encryption for the EDRA algorithm. We intentionally want to break TripleDES since we know for a fact we have one of the correct answers that was used as the encryption key. By doing this, we find the right encryption key with zero knowledge
|
||||
/// </summary>
|
||||
|
@ -46,7 +185,9 @@ namespace LibZNI
|
|||
|
||||
}
|
||||
|
||||
public static Int32 getTimestamp()
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public static Int32 getTimestamp()
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
return int.Parse(DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString());
|
||||
}
|
||||
|
@ -152,6 +293,16 @@ namespace LibZNI
|
|||
return pathListStr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Format:
|
||||
/// w = week
|
||||
/// d = day
|
||||
/// h = hour
|
||||
/// m = minute
|
||||
/// s = second
|
||||
/// </summary>
|
||||
/// <param name="TimeStr"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan DecodeTimeNotation(string TimeStr)
|
||||
{
|
||||
|
||||
|
@ -200,6 +351,69 @@ namespace LibZNI
|
|||
}
|
||||
return F;
|
||||
}
|
||||
|
||||
public static int GetUnixDifference(TimeSpan ts)
|
||||
{
|
||||
return (int)ts.TotalSeconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a timestamp or difference into ZNI Notation
|
||||
/// </summary>
|
||||
/// <param name="ts"></param>
|
||||
/// <returns>ZNI Time Notation</returns>
|
||||
public static string EncodeTimeNotation(TimeSpan ts)
|
||||
{
|
||||
return EncodeTimeNotation(GetUnixDifference(ts));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a unix timestamp or difference into ZNI Notation
|
||||
/// </summary>
|
||||
/// <param name="ts"></param>
|
||||
/// <returns>ZNI Time Notation</returns>
|
||||
public static string EncodeTimeNotation(int ts)
|
||||
{
|
||||
var ONE_DAY = ((60 * 60) * 24);
|
||||
|
||||
var Days = ts / ONE_DAY;
|
||||
ts -= (ONE_DAY * Days);
|
||||
var Hours = ts / 60 / 60;
|
||||
ts -= (Hours * 60 * 60);
|
||||
var Minutes = ts / 60;
|
||||
ts -= (Minutes * 60);
|
||||
|
||||
var Weeks = Days / 7;
|
||||
Days -= Weeks * 7;
|
||||
|
||||
List<string> X = new List<string>();
|
||||
if (Weeks > 0)
|
||||
{
|
||||
X.Add($"{Weeks}");
|
||||
X.Add($"w");
|
||||
}
|
||||
if (Days > 0)
|
||||
{
|
||||
X.Add($"{Days}");
|
||||
X.Add($"d");
|
||||
}
|
||||
if (Hours > 0)
|
||||
{
|
||||
X.Add($"{Hours}");
|
||||
X.Add($"h");
|
||||
}
|
||||
if (Minutes > 0)
|
||||
{
|
||||
X.Add($"{Minutes}");
|
||||
X.Add($"m");
|
||||
}
|
||||
if (ts > 0)
|
||||
{
|
||||
X.Add($"{ts}");
|
||||
X.Add($"s");
|
||||
}
|
||||
return String.Join("", X);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable()]
|
||||
|
@ -307,7 +521,68 @@ namespace LibZNI
|
|||
}
|
||||
|
||||
public static class LinqExtensions
|
||||
{
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that the bit (bitToSet) is set on (i)
|
||||
/// </summary>
|
||||
/// <param name="i"></param>
|
||||
/// <param name="bitToSet"></param>
|
||||
/// <returns>(i) with (bitToSet) set</returns>
|
||||
public static void SetBit(this ref int i, int bitToSet)
|
||||
{
|
||||
if (!i.BitSet(bitToSet))
|
||||
{
|
||||
i += bitToSet;
|
||||
}
|
||||
}
|
||||
public static bool BitSet(this ref int i, int bit)
|
||||
{
|
||||
return (i & bit) != 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// Ensures that the bit (bitToSet) is not set on (i)
|
||||
/// </summary>
|
||||
/// <param name="bitToSet"></param>
|
||||
/// <returns>(i) with (bitToSet) not set</returns>
|
||||
public static void UnsetBit(this ref int i, int bitToSet)
|
||||
{
|
||||
if (i.BitSet(bitToSet))
|
||||
{
|
||||
i -= bitToSet;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that the bit (bitToSet) is set on (i)
|
||||
/// </summary>
|
||||
/// <param name="i"></param>
|
||||
/// <param name="bitToSet"></param>
|
||||
/// <returns>(i) with (bitToSet) set</returns>
|
||||
public static void SetBit(this ref byte i, byte bitToSet)
|
||||
{
|
||||
if (!i.BitSet(bitToSet))
|
||||
{
|
||||
i += bitToSet;
|
||||
}
|
||||
}
|
||||
public static bool BitSet(this ref byte i, byte bit)
|
||||
{
|
||||
return (i & bit) != 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// Ensures that the bit (bitToSet) is not set on (i)
|
||||
/// </summary>
|
||||
/// <param name="bitToSet"></param>
|
||||
/// <returns>(i) with (bitToSet) not set</returns>
|
||||
public static void UnsetBit(this ref byte i, byte bitToSet)
|
||||
{
|
||||
if (i.BitSet(bitToSet))
|
||||
{
|
||||
i -= bitToSet;
|
||||
}
|
||||
}
|
||||
|
||||
public static string ReplaceAtIndex(this string a, int b, string c)
|
||||
{
|
||||
string sSplice = "";
|
||||
|
@ -365,12 +640,20 @@ namespace LibZNI
|
|||
return true;
|
||||
}
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public static List<string> llParseString2List(this string item, string[] opts, string[] keepopts)
|
||||
{
|
||||
return ParseString2List(item, opts, keepopts);
|
||||
}
|
||||
return ParseString2List(item, opts, keepopts, false);
|
||||
}
|
||||
public static List<string> llParseStringKeepNulls(this string item, string[] opts, string[] keepopts)
|
||||
{
|
||||
return ParseString2List(item, opts, keepopts, true);
|
||||
}
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
|
||||
#pragma warning disable IDE1006 // Naming Styles
|
||||
public static string llDumpList2String<T>(this List<T> items, string delimiter)
|
||||
#pragma warning restore IDE1006 // Naming Styles
|
||||
{
|
||||
return String.Join(delimiter, items.Select(t => t.ToString()).ToArray());
|
||||
}
|
||||
|
@ -391,8 +674,97 @@ namespace LibZNI
|
|||
}
|
||||
return buffer.ToArray();
|
||||
}
|
||||
public static List<string> ParseString2List(this string item, string[] opts, string[] keepopts)
|
||||
/// <summary>
|
||||
/// This function was partially taken from the OpenSimulator code and modified to no longer be LSL related and purely function the same way for my own sanity's sake
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="opts"></param>
|
||||
/// <param name="keepopts"></param>
|
||||
/// <returns></returns>
|
||||
public static List<string> ParseString2List(this string item, string[] opts, string[] keepopts, bool keepNulls)
|
||||
{
|
||||
int sourceLen = item.Length;
|
||||
int optionsLength = opts.Length;
|
||||
int spacersLen = keepopts.Length;
|
||||
|
||||
int dellen = 0;
|
||||
string[] delArray = new string[optionsLength + spacersLen];
|
||||
|
||||
int outlen = 0;
|
||||
string[] outArray = new string[sourceLen * 2 + 1];
|
||||
|
||||
int i, j;
|
||||
string d;
|
||||
|
||||
|
||||
for(i=0;i<optionsLength; i++)
|
||||
{
|
||||
d = opts[i].ToString();
|
||||
if(d.Length > 0)
|
||||
{
|
||||
delArray[dellen++] = d;
|
||||
}
|
||||
}
|
||||
optionsLength = dellen;
|
||||
|
||||
|
||||
for (i = 0; i < spacersLen; i++)
|
||||
{
|
||||
d = keepopts[i].ToString();
|
||||
if(d.Length > 0)
|
||||
{
|
||||
delArray[dellen++] = d;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(i=0; ;)
|
||||
{
|
||||
int earliestDel = -1;
|
||||
int earliestSrc = sourceLen;
|
||||
string earliestStr = null;
|
||||
|
||||
for(j = 0;j < dellen; j++)
|
||||
{
|
||||
d = delArray[j];
|
||||
if(d != null)
|
||||
{
|
||||
int idx = item.IndexOf(d, i);
|
||||
if(idx < 0)
|
||||
{
|
||||
delArray[j] = null;
|
||||
}
|
||||
else if(idx < earliestSrc)
|
||||
{
|
||||
earliestSrc = idx;
|
||||
earliestDel = j;
|
||||
earliestStr = d;
|
||||
if (idx == i) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(keepNulls || (earliestSrc > i))
|
||||
{
|
||||
outArray[outlen++] = item.Substring(i, earliestSrc - i);
|
||||
}
|
||||
if (earliestDel < 0) break;
|
||||
if(earliestDel >= optionsLength)
|
||||
{
|
||||
outArray[outlen++] = earliestStr;
|
||||
}
|
||||
i = earliestSrc + earliestStr.Length;
|
||||
}
|
||||
|
||||
List<string> outList = new List<string>();
|
||||
for(i = 0;i<outlen; i++)
|
||||
{
|
||||
outList.Add(outArray[i]);
|
||||
}
|
||||
|
||||
return outList;
|
||||
/*
|
||||
List<string> entries = new List<string>();
|
||||
List<string> tmpBuffer = new List<string>();
|
||||
List<string> buffer = new List<string>();
|
||||
|
@ -444,7 +816,7 @@ namespace LibZNI
|
|||
entries = buffer;
|
||||
buffer = new List<string>();
|
||||
|
||||
return entries;
|
||||
return entries;*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue