submodule对gitea不管用,所以直接拉了一份拉格兰
This commit is contained in:
16
Lagrange.Core/Lagrange.Core.Test/Utility/Console.cs
Normal file
16
Lagrange.Core/Lagrange.Core.Test/Utility/Console.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Lagrange.Core.Event.EventArg;
|
||||
|
||||
namespace Lagrange.Core.Test.Utility;
|
||||
|
||||
public static class Console
|
||||
{
|
||||
public static void ChangeColorByTitle(this LogLevel level) => System.Console.ForegroundColor = level switch
|
||||
{
|
||||
LogLevel.Debug => ConsoleColor.White,
|
||||
LogLevel.Verbose => ConsoleColor.DarkGray,
|
||||
LogLevel.Information => ConsoleColor.Blue,
|
||||
LogLevel.Warning => ConsoleColor.Yellow,
|
||||
LogLevel.Fatal => ConsoleColor.Red,
|
||||
_ => System.Console.ForegroundColor
|
||||
};
|
||||
}
|
||||
61
Lagrange.Core/Lagrange.Core.Test/Utility/ProtoGen.cs
Normal file
61
Lagrange.Core/Lagrange.Core.Test/Utility/ProtoGen.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using ProtoBuf;
|
||||
|
||||
namespace Lagrange.Core.Test.Utility;
|
||||
|
||||
internal static class ProtoGen
|
||||
{
|
||||
public static void GenerateProtoFiles()
|
||||
{
|
||||
var assembly = typeof(Lagrange.Core.Utility.ServiceInjector).Assembly;
|
||||
var types = assembly.GetTypes();
|
||||
var sb = new StringBuilder();
|
||||
|
||||
sb.AppendLine("syntax = \"proto3\";");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("package Lagrange.Core;");
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
if (type.Namespace?.StartsWith("Lagrange.Core.Internal.Packets") != true) continue;
|
||||
|
||||
sb.AppendLine($"message {type.Name} {{");
|
||||
var properties = type.GetProperties();
|
||||
foreach (var property in properties)
|
||||
{
|
||||
string typeString = ParseType(property.PropertyType);
|
||||
sb.AppendLine($" {GetLastToken(typeString, '.')} {property.Name} = {property.GetCustomAttribute<ProtoMemberAttribute>()?.Tag};");
|
||||
}
|
||||
sb.AppendLine("}");
|
||||
sb.AppendLine();
|
||||
}
|
||||
|
||||
string proto = sb.ToString();
|
||||
|
||||
File.WriteAllText("packets.proto", proto);
|
||||
}
|
||||
|
||||
private static string ParseType(Type type)
|
||||
{
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
|
||||
{
|
||||
return $"repeated {ParseType(type.GetGenericArguments()[0])}";
|
||||
}
|
||||
|
||||
return type.ToString() switch
|
||||
{
|
||||
"System.UInt64" => "varint",
|
||||
"System.UInt32" => "varint",
|
||||
"System.UInt16" => "varint",
|
||||
"System.Int64" => "varint",
|
||||
"System.Int32" => "varint",
|
||||
"System.String" => "string",
|
||||
"System.Boolean" => "bool",
|
||||
"System.Byte[]" => "bytes",
|
||||
_ => type.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetLastToken(string str, char separator) => str.Split(separator)[^1];
|
||||
}
|
||||
35
Lagrange.Core/Lagrange.Core.Test/Utility/Tlv.cs
Normal file
35
Lagrange.Core/Lagrange.Core.Test/Utility/Tlv.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Text;
|
||||
using Lagrange.Core.Utility.Extension;
|
||||
using static Lagrange.Core.Utility.Binary.BitConverter;
|
||||
|
||||
namespace Lagrange.Core.Test.Utility;
|
||||
|
||||
public static class Tlv
|
||||
{
|
||||
public static Dictionary<string, string> GetTlvDictionary(byte[] tlvs, bool isCommand = true)
|
||||
{
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
using var reader = new BinaryReader(new MemoryStream(tlvs));
|
||||
|
||||
ushort command;
|
||||
if (isCommand)
|
||||
{
|
||||
command = ToUInt16(reader.ReadBytes(2), false);
|
||||
}
|
||||
|
||||
ushort tlvCount = ToUInt16(reader.ReadBytes(2), false);
|
||||
|
||||
for (int i = 0; i < tlvCount; i++)
|
||||
{
|
||||
ushort tlvTag = ToUInt16(reader.ReadBytes(2), false);
|
||||
ushort tlvLength = ToUInt16(reader.ReadBytes(2), false);
|
||||
byte[] tlvValue = reader.ReadBytes(tlvLength);
|
||||
|
||||
result.Add($"0x{tlvTag:X} {tlvLength}", tlvValue.Hex());
|
||||
result.Add($"0x{tlvTag:X} UTF8 {tlvLength}", Encoding.UTF8.GetString(tlvValue));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user