Files
DanhengServer-OpenSource/Common/Util/Extensions.cs
2024-03-31 17:06:46 +08:00

113 lines
3.6 KiB
C#

using EggLink.DanhengServer.Proto;
using Newtonsoft.Json;
using System.Buffers.Binary;
namespace EggLink.DanhengServer.Util;
public static class Extensions
{
public static string JoinFormat<T>(this IEnumerable<T> list, string separator,
string formatString)
{
formatString = string.IsNullOrWhiteSpace(formatString) ? "{0}" : formatString;
return string.Join(separator,
list.Select(item => string.Format(formatString, item)));
}
public static void WriteConvID(this BinaryWriter bw, long convId)
{
//bw.Write(convId);
bw.Write((int)(convId >> 32));
bw.Write((int)(convId & 0xFFFFFFFF));
}
public static long GetNextAvailableIndex<T>(this SortedList<long, T> sortedList)
{
long key = 1;
long count = sortedList.Count;
long counter = 0;
do
{
if (count == 0) break;
long nextKeyInList = sortedList.Keys.ElementAt((Index)counter++);
if (key != nextKeyInList) break;
key = nextKeyInList + 1;
} while (count != 1 && counter != count && key == sortedList.Keys.ElementAt((Index)counter));
return key;
}
public static long AddNext<T>(this SortedList<long, T> sortedList, T item)
{
long key = sortedList.GetNextAvailableIndex();
sortedList.Add(key, item);
return key;
}
public static int ReadInt32BE(this BinaryReader br) => BinaryPrimitives.ReadInt32BigEndian(br.ReadBytes(sizeof(int)));
public static uint ReadUInt32BE(this BinaryReader br) => BinaryPrimitives.ReadUInt32BigEndian(br.ReadBytes(sizeof(uint)));
public static ushort ReadUInt16BE(this BinaryReader br) => BinaryPrimitives.ReadUInt16BigEndian(br.ReadBytes(sizeof(ushort)));
public static void WriteUInt16BE(this BinaryWriter bw, ushort value)
{
Span<byte> data = stackalloc byte[sizeof(ushort)];
BinaryPrimitives.WriteUInt16BigEndian(data, value);
bw.Write(data);
}
public static void WriteInt32BE(this BinaryWriter bw, int value)
{
Span<byte> data = stackalloc byte[sizeof(int)];
BinaryPrimitives.WriteInt32BigEndian(data, value);
bw.Write(data);
}
public static void WriteUInt32BE(this BinaryWriter bw, uint value)
{
Span<byte> data = stackalloc byte[sizeof(uint)];
BinaryPrimitives.WriteUInt32BigEndian(data, value);
bw.Write(data);
}
public static void WriteUInt64BE(this BinaryWriter bw, ulong value)
{
Span<byte> data = stackalloc byte[sizeof(ulong)];
BinaryPrimitives.WriteUInt64BigEndian(data, value);
bw.Write(data);
}
public static Position ToPosition(this Vector vector)
{
return new Position
{
X = vector.X,
Y = vector.Y,
Z = vector.Z
};
}
public static T RandomElement<T> (this List<T> values)
{
var index = new Random().Next(values.Count);
return values[index];
}
public static int RandomInt (int from, int to)
{
return new Random().Next(from, to);
}
public static long GetUnixSec()
{
return DateTimeOffset.UtcNow.ToUnixTimeSeconds();
}
public static long GetUnixMs()
{
return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
public static string ToArrayString(this List<string> list)
{
return list.JoinFormat(", ", "");
}
public static string ToJsonString(this Dictionary<string, string> dic)
{
return JsonConvert.SerializeObject(dic);
}
}