mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[+] 接入ET8服务端
[+] 接入ET8服务端
This commit is contained in:
24
Assets/GameScripts/DotNet/Core/Helper/AssemblyHelper.cs
Normal file
24
Assets/GameScripts/DotNet/Core/Helper/AssemblyHelper.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class AssemblyHelper
|
||||
{
|
||||
public static Dictionary<string, Type> GetAssemblyTypes(params Assembly[] args)
|
||||
{
|
||||
Dictionary<string, Type> types = new Dictionary<string, Type>();
|
||||
|
||||
foreach (Assembly ass in args)
|
||||
{
|
||||
foreach (Type type in ass.GetTypes())
|
||||
{
|
||||
types[type.FullName] = type;
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Helper/AssemblyHelper.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Helper/AssemblyHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 63fb0b4a59b5b7a499ae6e2b28f11ebb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
129
Assets/GameScripts/DotNet/Core/Helper/ByteHelper.cs
Normal file
129
Assets/GameScripts/DotNet/Core/Helper/ByteHelper.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System.Text;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class ByteHelper
|
||||
{
|
||||
public static string ToHex(this byte b)
|
||||
{
|
||||
return b.ToString("X2");
|
||||
}
|
||||
|
||||
public static string ToHex(this byte[] bytes)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
foreach (byte b in bytes)
|
||||
{
|
||||
stringBuilder.Append(b.ToString("X2"));
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
public static string ToHex(this byte[] bytes, string format)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
foreach (byte b in bytes)
|
||||
{
|
||||
stringBuilder.Append(b.ToString(format));
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
public static string ToHex(this byte[] bytes, int offset, int count)
|
||||
{
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (int i = offset; i < offset + count; ++i)
|
||||
{
|
||||
stringBuilder.Append(bytes[i].ToString("X2"));
|
||||
}
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
public static string ToStr(this byte[] bytes)
|
||||
{
|
||||
return Encoding.Default.GetString(bytes);
|
||||
}
|
||||
|
||||
public static string ToStr(this byte[] bytes, int index, int count)
|
||||
{
|
||||
return Encoding.Default.GetString(bytes, index, count);
|
||||
}
|
||||
|
||||
public static string Utf8ToStr(this byte[] bytes)
|
||||
{
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
public static string Utf8ToStr(this byte[] bytes, int index, int count)
|
||||
{
|
||||
return Encoding.UTF8.GetString(bytes, index, count);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, uint num)
|
||||
{
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
|
||||
bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, ActorId num)
|
||||
{
|
||||
bytes.WriteTo(offset, num.Process);
|
||||
bytes.WriteTo(offset + 4, num.Fiber);
|
||||
bytes.WriteTo(offset + 8, num.InstanceId);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, int num)
|
||||
{
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
|
||||
bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, byte num)
|
||||
{
|
||||
bytes[offset] = num;
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, short num)
|
||||
{
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
}
|
||||
|
||||
public static void WriteTo(this byte[] bytes, int offset, ushort num)
|
||||
{
|
||||
bytes[offset] = (byte)(num & 0xff);
|
||||
bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
|
||||
}
|
||||
|
||||
public static unsafe void WriteTo(this byte[] bytes, int offset, long num)
|
||||
{
|
||||
byte* bPoint = (byte*)#
|
||||
for (int i = 0; i < sizeof(long); ++i)
|
||||
{
|
||||
bytes[offset + i] = bPoint[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static long Hash(this byte[] data, int index, int length)
|
||||
{
|
||||
const int p = 16777619;
|
||||
long hash = 2166136261L;
|
||||
|
||||
for (int i = index; i < index + length; i++)
|
||||
{
|
||||
hash = (hash ^ data[i]) * p;
|
||||
}
|
||||
|
||||
hash += hash << 13;
|
||||
hash ^= hash >> 7;
|
||||
hash += hash << 3;
|
||||
hash ^= hash >> 17;
|
||||
hash += hash << 5;
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/GameScripts/DotNet/Core/Helper/ByteHelper.cs.meta
Normal file
12
Assets/GameScripts/DotNet/Core/Helper/ByteHelper.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33f44962744fcd64d98b9bcd0fc41bf9
|
||||
timeCreated: 1474942922
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,22 @@
|
||||
namespace ET
|
||||
{
|
||||
public static class ETCancelationTokenHelper
|
||||
{
|
||||
public static async ETTask CancelAfter(this ETCancellationToken self, Fiber fiber, long afterTimeCancel)
|
||||
{
|
||||
if (self.IsCancel())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await fiber.TimerComponent.WaitAsync(afterTimeCancel);
|
||||
|
||||
if (self.IsCancel())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
self.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 36b3530e4d4b82d49820f4f1a69ef74f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
30
Assets/GameScripts/DotNet/Core/Helper/EnumHelper.cs
Normal file
30
Assets/GameScripts/DotNet/Core/Helper/EnumHelper.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class EnumHelper
|
||||
{
|
||||
public static int EnumIndex<T>(int value)
|
||||
{
|
||||
int i = 0;
|
||||
foreach (object v in Enum.GetValues(typeof (T)))
|
||||
{
|
||||
if ((int) v == value)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static T FromString<T>(string str)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(T), str))
|
||||
{
|
||||
return default(T);
|
||||
}
|
||||
return (T)Enum.Parse(typeof(T), str);
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/GameScripts/DotNet/Core/Helper/EnumHelper.cs.meta
Normal file
12
Assets/GameScripts/DotNet/Core/Helper/EnumHelper.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1f7baecded0cf041bb61181909a1619
|
||||
timeCreated: 1474948030
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
107
Assets/GameScripts/DotNet/Core/Helper/FileHelper.cs
Normal file
107
Assets/GameScripts/DotNet/Core/Helper/FileHelper.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class FileHelper
|
||||
{
|
||||
public static List<string> GetAllFiles(string dir, string searchPattern = "*")
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
GetAllFiles(list, dir, searchPattern);
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void GetAllFiles(List<string> files, string dir, string searchPattern = "*")
|
||||
{
|
||||
string[] fls = Directory.GetFiles(dir);
|
||||
foreach (string fl in fls)
|
||||
{
|
||||
files.Add(fl);
|
||||
}
|
||||
|
||||
string[] subDirs = Directory.GetDirectories(dir);
|
||||
foreach (string subDir in subDirs)
|
||||
{
|
||||
GetAllFiles(files, subDir, searchPattern);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CleanDirectory(string dir)
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (string subdir in Directory.GetDirectories(dir))
|
||||
{
|
||||
Directory.Delete(subdir, true);
|
||||
}
|
||||
|
||||
foreach (string subFile in Directory.GetFiles(dir))
|
||||
{
|
||||
File.Delete(subFile);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CopyDirectory(string srcDir, string tgtDir)
|
||||
{
|
||||
DirectoryInfo source = new DirectoryInfo(srcDir);
|
||||
DirectoryInfo target = new DirectoryInfo(tgtDir);
|
||||
|
||||
if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
throw new Exception("父目录不能拷贝到子目录!");
|
||||
}
|
||||
|
||||
if (!source.Exists)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!target.Exists)
|
||||
{
|
||||
target.Create();
|
||||
}
|
||||
|
||||
FileInfo[] files = source.GetFiles();
|
||||
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
File.Copy(files[i].FullName, Path.Combine(target.FullName, files[i].Name), true);
|
||||
}
|
||||
|
||||
DirectoryInfo[] dirs = source.GetDirectories();
|
||||
|
||||
for (int j = 0; j < dirs.Length; j++)
|
||||
{
|
||||
CopyDirectory(dirs[j].FullName, Path.Combine(target.FullName, dirs[j].Name));
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReplaceExtensionName(string srcDir, string extensionName, string newExtensionName)
|
||||
{
|
||||
if (Directory.Exists(srcDir))
|
||||
{
|
||||
string[] fls = Directory.GetFiles(srcDir);
|
||||
|
||||
foreach (string fl in fls)
|
||||
{
|
||||
if (fl.EndsWith(extensionName))
|
||||
{
|
||||
File.Move(fl, fl.Substring(0, fl.IndexOf(extensionName)) + newExtensionName);
|
||||
File.Delete(fl);
|
||||
}
|
||||
}
|
||||
|
||||
string[] subDirs = Directory.GetDirectories(srcDir);
|
||||
|
||||
foreach (string subDir in subDirs)
|
||||
{
|
||||
ReplaceExtensionName(subDir, extensionName, newExtensionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/GameScripts/DotNet/Core/Helper/FileHelper.cs.meta
Normal file
12
Assets/GameScripts/DotNet/Core/Helper/FileHelper.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 74cc512af7309484fa066b16175c6331
|
||||
timeCreated: 1474943113
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
19
Assets/GameScripts/DotNet/Core/Helper/MD5Helper.cs
Normal file
19
Assets/GameScripts/DotNet/Core/Helper/MD5Helper.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class MD5Helper
|
||||
{
|
||||
public static string FileMD5(string filePath)
|
||||
{
|
||||
byte[] retVal;
|
||||
using (FileStream file = new FileStream(filePath, FileMode.Open))
|
||||
{
|
||||
MD5 md5 = MD5.Create();
|
||||
retVal = md5.ComputeHash(file);
|
||||
}
|
||||
return retVal.ToHex("x2");
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/GameScripts/DotNet/Core/Helper/MD5Helper.cs.meta
Normal file
12
Assets/GameScripts/DotNet/Core/Helper/MD5Helper.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c37f13dde60869459e624143ae45a3c
|
||||
timeCreated: 1474942922
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
73
Assets/GameScripts/DotNet/Core/Helper/NetworkHelper.cs
Normal file
73
Assets/GameScripts/DotNet/Core/Helper/NetworkHelper.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class NetworkHelper
|
||||
{
|
||||
public static string[] GetAddressIPs()
|
||||
{
|
||||
List<string> list = new List<string>();
|
||||
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
if (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
foreach (UnicastIPAddressInformation add in networkInterface.GetIPProperties().UnicastAddresses)
|
||||
{
|
||||
list.Add(add.Address.ToString());
|
||||
}
|
||||
}
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
// 优先获取IPV4的地址
|
||||
public static IPAddress GetHostAddress(string hostName)
|
||||
{
|
||||
IPAddress[] ipAddresses = Dns.GetHostAddresses(hostName);
|
||||
IPAddress returnIpAddress = null;
|
||||
foreach (IPAddress ipAddress in ipAddresses)
|
||||
{
|
||||
returnIpAddress = ipAddress;
|
||||
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
return ipAddress;
|
||||
}
|
||||
}
|
||||
return returnIpAddress;
|
||||
}
|
||||
|
||||
public static IPEndPoint ToIPEndPoint(string host, int port)
|
||||
{
|
||||
return new IPEndPoint(IPAddress.Parse(host), port);
|
||||
}
|
||||
|
||||
public static IPEndPoint ToIPEndPoint(string address)
|
||||
{
|
||||
int index = address.LastIndexOf(':');
|
||||
string host = address.Substring(0, index);
|
||||
string p = address.Substring(index + 1);
|
||||
int port = int.Parse(p);
|
||||
return ToIPEndPoint(host, port);
|
||||
}
|
||||
|
||||
public static void SetSioUdpConnReset(Socket socket)
|
||||
{
|
||||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const uint IOC_IN = 0x80000000;
|
||||
const uint IOC_VENDOR = 0x18000000;
|
||||
const int SIO_UDP_CONNRESET = unchecked((int)(IOC_IN | IOC_VENDOR | 12));
|
||||
|
||||
socket.IOControl(SIO_UDP_CONNRESET, new[] { Convert.ToByte(false) }, null);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Helper/NetworkHelper.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Helper/NetworkHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be86584128551415eb1b97047a3ae96e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
10
Assets/GameScripts/DotNet/Core/Helper/ObjectHelper.cs
Normal file
10
Assets/GameScripts/DotNet/Core/Helper/ObjectHelper.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace ET
|
||||
{
|
||||
public static class ObjectHelper
|
||||
{
|
||||
public static void Swap<T>(ref T t1, ref T t2)
|
||||
{
|
||||
(t1, t2) = (t2, t1);
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/GameScripts/DotNet/Core/Helper/ObjectHelper.cs.meta
Normal file
12
Assets/GameScripts/DotNet/Core/Helper/ObjectHelper.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 27d07fce480358144b4bf720ff3831ee
|
||||
timeCreated: 1474942922
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
109
Assets/GameScripts/DotNet/Core/Helper/ProcessHelper.cs
Normal file
109
Assets/GameScripts/DotNet/Core/Helper/ProcessHelper.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using Path = System.IO.Path;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class ProcessHelper
|
||||
{
|
||||
public static System.Diagnostics.Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false)
|
||||
{
|
||||
//Log.Debug($"Process Run exe:{exe} ,arguments:{arguments} ,workingDirectory:{workingDirectory}");
|
||||
try
|
||||
{
|
||||
bool redirectStandardOutput = true;
|
||||
bool redirectStandardError = true;
|
||||
bool useShellExecute = false;
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
redirectStandardOutput = false;
|
||||
redirectStandardError = false;
|
||||
useShellExecute = true;
|
||||
}
|
||||
|
||||
if (waitExit)
|
||||
{
|
||||
redirectStandardOutput = true;
|
||||
redirectStandardError = true;
|
||||
useShellExecute = false;
|
||||
}
|
||||
|
||||
ProcessStartInfo info = new ProcessStartInfo
|
||||
{
|
||||
FileName = exe,
|
||||
Arguments = arguments,
|
||||
CreateNoWindow = true,
|
||||
UseShellExecute = useShellExecute,
|
||||
WorkingDirectory = workingDirectory,
|
||||
RedirectStandardOutput = redirectStandardOutput,
|
||||
RedirectStandardError = redirectStandardError,
|
||||
};
|
||||
|
||||
System.Diagnostics.Process process = System.Diagnostics.Process.Start(info);
|
||||
|
||||
if (waitExit)
|
||||
{
|
||||
WaitExitAsync(process).Coroutine();
|
||||
}
|
||||
|
||||
return process;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"dir: {Path.GetFullPath(workingDirectory)}, command: {exe} {arguments}", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static async ETTask WaitExitAsync(System.Diagnostics.Process process)
|
||||
{
|
||||
await process.WaitForExitAsync();
|
||||
#if UNITY
|
||||
Log.Info($"process exit, exitcode: {process.ExitCode} {process.StandardOutput.ReadToEnd()} {process.StandardError.ReadToEnd()}");
|
||||
#endif
|
||||
}
|
||||
|
||||
#if UNITY
|
||||
private static async Task WaitForExitAsync(this System.Diagnostics.Process self)
|
||||
{
|
||||
if (!self.HasExited)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
self.EnableRaisingEvents = true;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
if (self.HasExited)
|
||||
{
|
||||
return;
|
||||
}
|
||||
throw;
|
||||
}
|
||||
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
|
||||
void Handler(object s, EventArgs e) => tcs.TrySetResult(true);
|
||||
|
||||
self.Exited += Handler;
|
||||
|
||||
try
|
||||
{
|
||||
if (self.HasExited)
|
||||
{
|
||||
return;
|
||||
}
|
||||
await tcs.Task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
self.Exited -= Handler;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Helper/ProcessHelper.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Helper/ProcessHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f6f1dd88f9c6457dad5db960404a12b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
96
Assets/GameScripts/DotNet/Core/Helper/RandomGenerator.cs
Normal file
96
Assets/GameScripts/DotNet/Core/Helper/RandomGenerator.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Random = System.Random;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
// 支持多线程
|
||||
public static class RandomGenerator
|
||||
{
|
||||
[StaticField]
|
||||
[ThreadStatic]
|
||||
private static Random random;
|
||||
|
||||
private static Random GetRandom()
|
||||
{
|
||||
return random ??= new Random(Guid.NewGuid().GetHashCode());
|
||||
}
|
||||
|
||||
public static ulong RandUInt64()
|
||||
{
|
||||
int r1 = RandInt32();
|
||||
int r2 = RandInt32();
|
||||
|
||||
return ((ulong)r1 << 32) & (ulong)r2;
|
||||
}
|
||||
|
||||
public static int RandInt32()
|
||||
{
|
||||
return GetRandom().Next();
|
||||
}
|
||||
|
||||
public static uint RandUInt32()
|
||||
{
|
||||
return (uint) GetRandom().Next();
|
||||
}
|
||||
|
||||
public static long RandInt64()
|
||||
{
|
||||
uint r1 = RandUInt32();
|
||||
uint r2 = RandUInt32();
|
||||
return (long)(((ulong)r1 << 32) | r2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取lower与Upper之间的随机数,包含下限,不包含上限
|
||||
/// </summary>
|
||||
/// <param name="lower"></param>
|
||||
/// <param name="upper"></param>
|
||||
/// <returns></returns>
|
||||
public static int RandomNumber(int lower, int upper)
|
||||
{
|
||||
int value = GetRandom().Next(lower, upper);
|
||||
return value;
|
||||
}
|
||||
|
||||
public static bool RandomBool()
|
||||
{
|
||||
return GetRandom().Next(2) == 0;
|
||||
}
|
||||
|
||||
public static T RandomArray<T>(T[] array)
|
||||
{
|
||||
return array[RandomNumber(0, array.Length)];
|
||||
}
|
||||
|
||||
public static T RandomArray<T>(List<T> array)
|
||||
{
|
||||
return array[RandomNumber(0, array.Count)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打乱数组
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="arr">要打乱的数组</param>
|
||||
public static void BreakRank<T>(List<T> arr)
|
||||
{
|
||||
if (arr == null || arr.Count < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < arr.Count; i++)
|
||||
{
|
||||
int index = GetRandom().Next(0, arr.Count);
|
||||
(arr[index], arr[i]) = (arr[i], arr[index]);
|
||||
}
|
||||
}
|
||||
|
||||
public static float RandFloat01()
|
||||
{
|
||||
int a = RandomNumber(0, 1000000);
|
||||
return a / 1000000f;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3fae633c3f0136a49b65fe6e909b4851
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
34
Assets/GameScripts/DotNet/Core/Helper/StringHashHelper.cs
Normal file
34
Assets/GameScripts/DotNet/Core/Helper/StringHashHelper.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class StringHashHelper
|
||||
{
|
||||
// bkdr hash
|
||||
public static long GetLongHashCode(this string str)
|
||||
{
|
||||
const uint seed = 1313; // 31 131 1313 13131 131313 etc..
|
||||
|
||||
ulong hash = 0;
|
||||
for (int i = 0; i < str.Length; ++i)
|
||||
{
|
||||
char c = str[i];
|
||||
byte high = (byte)(c >> 8);
|
||||
byte low = (byte)(c & byte.MaxValue);
|
||||
hash = hash * seed + high;
|
||||
hash = hash * seed + low;
|
||||
}
|
||||
return (long)hash;
|
||||
}
|
||||
|
||||
public static int Mode(this string strText, int mode)
|
||||
{
|
||||
if (mode <= 0)
|
||||
{
|
||||
throw new Exception($"string mode < 0: {strText} {mode}");
|
||||
}
|
||||
return (int)((ulong)strText.GetLongHashCode() % (uint)mode);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 36104fdef2a076547afb95f2edee000c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
104
Assets/GameScripts/DotNet/Core/Helper/StringHelper.cs
Normal file
104
Assets/GameScripts/DotNet/Core/Helper/StringHelper.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class StringHelper
|
||||
{
|
||||
public static IEnumerable<byte> ToBytes(this string str)
|
||||
{
|
||||
byte[] byteArray = Encoding.Default.GetBytes(str);
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
public static byte[] ToByteArray(this string str)
|
||||
{
|
||||
byte[] byteArray = Encoding.Default.GetBytes(str);
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
public static byte[] ToUtf8(this string str)
|
||||
{
|
||||
byte[] byteArray = Encoding.UTF8.GetBytes(str);
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
public static byte[] HexToBytes(this string hexString)
|
||||
{
|
||||
if (hexString.Length % 2 != 0)
|
||||
{
|
||||
throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
|
||||
}
|
||||
|
||||
var hexAsBytes = new byte[hexString.Length / 2];
|
||||
for (int index = 0; index < hexAsBytes.Length; index++)
|
||||
{
|
||||
string byteValue = "";
|
||||
byteValue += hexString[index * 2];
|
||||
byteValue += hexString[index * 2 + 1];
|
||||
hexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
|
||||
}
|
||||
return hexAsBytes;
|
||||
}
|
||||
|
||||
public static string Fmt(this string text, params object[] args)
|
||||
{
|
||||
return string.Format(text, args);
|
||||
}
|
||||
|
||||
public static string ListToString<T>(this List<T> list)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (T t in list)
|
||||
{
|
||||
sb.Append(t);
|
||||
sb.Append(",");
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static string ArrayToString<T>(this T[] args)
|
||||
{
|
||||
if (args == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
string argStr = " [";
|
||||
for (int arrIndex = 0; arrIndex < args.Length; arrIndex++)
|
||||
{
|
||||
argStr += args[arrIndex];
|
||||
if (arrIndex != args.Length - 1)
|
||||
{
|
||||
argStr += ", ";
|
||||
}
|
||||
}
|
||||
|
||||
argStr += "]";
|
||||
return argStr;
|
||||
}
|
||||
|
||||
public static string ArrayToString<T>(this T[] args, int index, int count)
|
||||
{
|
||||
if (args == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
string argStr = " [";
|
||||
for (int arrIndex = index; arrIndex < count + index; arrIndex++)
|
||||
{
|
||||
argStr += args[arrIndex];
|
||||
if (arrIndex != args.Length - 1)
|
||||
{
|
||||
argStr += ", ";
|
||||
}
|
||||
}
|
||||
|
||||
argStr += "]";
|
||||
return argStr;
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/GameScripts/DotNet/Core/Helper/StringHelper.cs.meta
Normal file
12
Assets/GameScripts/DotNet/Core/Helper/StringHelper.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7b4a3306f5899c48966bcc2aea42a61
|
||||
timeCreated: 1474947336
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
21
Assets/GameScripts/DotNet/Core/Helper/WinPeriod.cs
Normal file
21
Assets/GameScripts/DotNet/Core/Helper/WinPeriod.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class WinPeriod
|
||||
{
|
||||
// 一般默认的精度不止1毫秒(不同操作系统有所不同),需要调用timeBeginPeriod与timeEndPeriod来设置精度
|
||||
[DllImport("winmm")]
|
||||
private static extern void timeBeginPeriod(int t);
|
||||
//[DllImport("winmm")]
|
||||
//static extern void timeEndPeriod(int t);
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
timeBeginPeriod(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/GameScripts/DotNet/Core/Helper/WinPeriod.cs.meta
Normal file
11
Assets/GameScripts/DotNet/Core/Helper/WinPeriod.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ad83b047e765b34ea32c6ca7461406d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
97
Assets/GameScripts/DotNet/Core/Helper/ZipHelper.cs
Normal file
97
Assets/GameScripts/DotNet/Core/Helper/ZipHelper.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System.IO;
|
||||
using ICSharpCode.SharpZipLib.Zip.Compression;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class ZipHelper
|
||||
{
|
||||
public static byte[] Compress(byte[] content)
|
||||
{
|
||||
//return content;
|
||||
Deflater compressor = new Deflater();
|
||||
compressor.SetLevel(Deflater.BEST_COMPRESSION);
|
||||
|
||||
compressor.SetInput(content);
|
||||
compressor.Finish();
|
||||
|
||||
using (MemoryStream bos = new MemoryStream(content.Length))
|
||||
{
|
||||
var buf = new byte[1024];
|
||||
while (!compressor.IsFinished)
|
||||
{
|
||||
int n = compressor.Deflate(buf);
|
||||
bos.Write(buf, 0, n);
|
||||
}
|
||||
return bos.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Decompress(byte[] content)
|
||||
{
|
||||
return Decompress(content, 0, content.Length);
|
||||
}
|
||||
|
||||
public static byte[] Decompress(byte[] content, int offset, int count)
|
||||
{
|
||||
//return content;
|
||||
Inflater decompressor = new Inflater();
|
||||
decompressor.SetInput(content, offset, count);
|
||||
|
||||
using (MemoryStream bos = new MemoryStream(content.Length))
|
||||
{
|
||||
var buf = new byte[1024];
|
||||
while (!decompressor.IsFinished)
|
||||
{
|
||||
int n = decompressor.Inflate(buf);
|
||||
bos.Write(buf, 0, n);
|
||||
}
|
||||
return bos.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
|
||||
namespace ET
|
||||
{
|
||||
public static class ZipHelper
|
||||
{
|
||||
public static byte[] Compress(byte[] content)
|
||||
{
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true))
|
||||
{
|
||||
stream.Write(content, 0, content.Length);
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Decompress(byte[] content)
|
||||
{
|
||||
return Decompress(content, 0, content.Length);
|
||||
}
|
||||
|
||||
public static byte[] Decompress(byte[] content, int offset, int count)
|
||||
{
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
using (DeflateStream stream = new DeflateStream(new MemoryStream(content, offset, count), CompressionMode.Decompress, true))
|
||||
{
|
||||
byte[] buffer = new byte[1024];
|
||||
while (true)
|
||||
{
|
||||
int bytesRead = stream.Read(buffer, 0, 1024);
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
ms.Write(buffer, 0, bytesRead);
|
||||
}
|
||||
return ms.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
12
Assets/GameScripts/DotNet/Core/Helper/ZipHelper.cs.meta
Normal file
12
Assets/GameScripts/DotNet/Core/Helper/ZipHelper.cs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75d07d90984a5dc4698c98d2fe3809bf
|
||||
timeCreated: 1474948216
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user