Skip to content

Commit

Permalink
Update Refactoring of Lagrange.OneBot
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Aug 22, 2023
1 parent 5b187c3 commit 2d71b5c
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 45 deletions.
13 changes: 2 additions & 11 deletions Lagrange.Core.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
using System.Text.Json;
using Lagrange.Core.Test.Tests;
using Lagrange.Core.Utility.Binary.JceStruct;
using Lagrange.Core.Utility.Extension;


var jce = "10022C3C4C56235151536572766963652E436F6E666967507573685376632E4D61696E53657276616E746607507573685265717D000104050800010607507573685265711800010612436F6E666967507573682E507573685265711D000103DD0A10012D000103CB19000A0A160D33392E3135362E3132352E32302136B030014C5C600870018602746A9602636DAC0B0A160D33362E3135352E3136332E32352101BB30014C5C60087001860273689602636DAC0B0A160E33392E3135362E3134342E313133211F9030014C5C600870018602746A9602636DAC0B0A160E3132302E3233332E31372E313437205030014C5C600870018602737A9602636DAC0B0A160E33362E3135352E3230372E3233302136B030014C5C60087001860273689602636DAC0B0A160E33392E3135362E3132362E3137382101BB30014C5C600870018602746A9602636DAC0B0A160E3132302E3233322E33312E323530205030014C5C600870018602737A9602636DAC0B0A160D33362E3135352E3234352E3136205030014C5C60087001860273689602636DAC0B0A16116D7366776966692E33672E71712E636F6D211F9030014C5C60087C86066F746865727396066F7468657273AC0B0A160E33392E3135362E3132362E313738205030014C5C600870018602746A9602636DAC0B39000A0A160D33392E3135362E3132352E32302136B030014C5C600870018602746A9602636DAC0B0A160D33362E3135352E3136332E32352101BB30014C5C60087001860273689602636DAC0B0A160E33392E3135362E3134342E313133211F9030014C5C600870018602746A9602636DAC0B0A160E3132302E3233332E31372E313437205030014C5C600870018602737A9602636DAC0B0A160E33362E3135352E3230372E3233302136B030014C5C60087001860273689602636DAC0B0A160E33392E3135362E3132362E3137382101BB30014C5C600870018602746A9602636DAC0B0A160E3132302E3233322E33312E323530205030014C5C600870018602737A9602636DAC0B0A160D33362E3135352E3234352E3136205030014C5C60087001860273689602636DAC0B0A16116D7366776966692E33672E71712E636F6D211F9030014C5C60087C86066F746865727396066F7468657273AC0B0A160E33392E3135362E3132362E313738205030014C5C600870018602746A9602636DAC0B4C5C6C70018900020A160E33362E3135352E3231332E313832205030014C500360087C860273689602636DAC0B0A160F3132302E3233322E3133302E313032205030014C500360087C8602737A9602636DAC0B9900020A160E33362E3135352E3231332E313832205030014C500360087C860273689602636DAC0B0A160F3132302E3233322E3133302E313032205030014C500360087C8602737A9602636DAC0BA90CB90CC90CD90CE002FC0FF61014514449414F5F504F4C4943595F44454641554C54F01101330000000A8D41253D0B8C980CA80C".UnHex();
var decoded = new UniPacket(jce);
var data = decoded.Buffer;

File.WriteAllText("JceStruct.json", JsonSerializer.Serialize(data, new JsonSerializerOptions { WriteIndented = true }));
using Lagrange.Core.Test.Tests;

// BenchmarkRunner.Run<ProtoBufTest>(new DebugBuildConfig());
await new NTLoginTest().LoginByPassword();
// await new WtLoginTest().FetchQrCode();

Expand Down
40 changes: 40 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotGroupMsg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Common.Entity;
using Lagrange.OneBot.Core.Message;

namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotGroupMsg : OneBotEntityBase
{
[JsonPropertyName("message_type")] public string MessageType { get; set; }

[JsonPropertyName("sub_type")] public string SubType { get; set; }

[JsonPropertyName("message_id")] public int MessageId { get; set; }

[JsonPropertyName("group_id")] public uint GroupId { get; set; }

[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("anonymous")] public object? Anonymous { get; set; }

[JsonPropertyName("message")] public List<ISegment> Message { get; set; }

[JsonPropertyName("raw_message")] public string RawMessage { get; set; }

[JsonPropertyName("font")] public int Font { get; set; }

[JsonPropertyName("sender")] public OneBotGroupSender GroupSender { get; set; }

public OneBotGroupMsg(uint selfId, List<ISegment> message, BotGroupMember member) : base(selfId, "message")
{
MessageType = "group";
SubType = "normal";
Anonymous = null;
Message = message;
RawMessage = string.Empty;
Font = 0;
GroupSender = new OneBotGroupSender(member.Uin, member.MemberName, member.MemberCard, (int)member.GroupLevel, member.Permission);
}
}
45 changes: 45 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotGroupSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Common.Entity;

namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotGroupSender
{
[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("nickname")] public string Nickname { get; set; }

[JsonPropertyName("card")] public string Card { get; set; }

[JsonPropertyName("sex")] public string Sex { get; set; }

[JsonPropertyName("age")] public uint Age { get; set; }

[JsonPropertyName("area")] public string Area { get; set; }

[JsonPropertyName("level")] public string Level { get; set; }

[JsonPropertyName("role")] public string Role { get; set; }

[JsonPropertyName("title")] public string Title { get; set; }

public OneBotGroupSender(uint userId, string nickname, string card, int level, GroupMemberPermission permission)
{
UserId = userId;
Nickname = nickname;
Card = card;
Sex = "unknown";
Age = 0;
Area = string.Empty;
Level = level.ToString();
Role = permission switch
{
GroupMemberPermission.Owner => "owner",
GroupMemberPermission.Admin => "admin",
GroupMemberPermission.Member => "member",
_ => "unknown"
};
Title = string.Empty;
}
}
34 changes: 34 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotPrivateMsg.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text.Json.Serialization;
using Lagrange.OneBot.Core.Message;

namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotPrivateMsg : OneBotEntityBase
{
[JsonPropertyName("message_type")] public string MessageType { get; set; }

[JsonPropertyName("sub_type")] public string SubType { get; set; }

[JsonPropertyName("message_id")] public int MessageId { get; set; }

[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("message")] public List<ISegment> Message { get; set; }

[JsonPropertyName("raw_message")] public string RawMessage { get; set; }

[JsonPropertyName("font")] public int Font { get; set; }

[JsonPropertyName("sender")] public OneBotSender GroupSender { get; set; }

public OneBotPrivateMsg(uint selfId) : base(selfId, "message")
{
MessageType = "private";
SubType = "friend";
Message = new List<ISegment>();
RawMessage = string.Empty;
Font = 0;
GroupSender = new OneBotSender();
}
}
7 changes: 7 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Lagrange.OneBot.Core.Entity.Message;

[Serializable]
public class OneBotSender
{

}
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Entity/OneBotEntityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class OneBotEntityBase

protected OneBotEntityBase(uint selfId, string postType)
{
Time = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Time = DateTimeOffset.Now.ToUnixTimeSeconds();
SelfId = selfId;
PostType = postType;
}
Expand Down
14 changes: 10 additions & 4 deletions Lagrange.OneBot/Core/Message/Entity/AtSegment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

namespace Lagrange.OneBot.Core.Message.Entity;
Expand All @@ -11,11 +12,16 @@ public partial class AtSegment
[JsonPropertyName("qq")] public string At { get; set; }
}

public partial class AtSegment : IOneBotSegment<MentionEntity>
public partial class AtSegment : ISegment
{
string IOneBotSegment<MentionEntity>.Type => "at";
string ISegment.Type => "at";

public MentionEntity ToEntity() => new("", uint.Parse(At));
public IMessageEntity ToEntity() => new MentionEntity("", uint.Parse(At));

public ISegment FromMessageEntity(MentionEntity entity) => new AtSegment(entity.Uin);
public ISegment FromMessageEntity(IMessageEntity entity)
{
if (entity is not MentionEntity mentionEntity) throw new ArgumentException("Invalid entity type.");

return new AtSegment(mentionEntity.Uin);
}
}
14 changes: 10 additions & 4 deletions Lagrange.OneBot/Core/Message/Entity/FaceSegment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

namespace Lagrange.OneBot.Core.Message.Entity;
Expand All @@ -11,11 +12,16 @@ public partial class FaceSegment
[JsonPropertyName("id")] public string Id { get; set; }
}

public partial class FaceSegment : IOneBotSegment<FaceEntity>
public partial class FaceSegment : ISegment
{
string IOneBotSegment<FaceEntity>.Type => "face";
string ISegment.Type => "face";

public FaceEntity ToEntity() => new(ushort.Parse(Id), false);
public IMessageEntity ToEntity() => new FaceEntity(ushort.Parse(Id), false);

public ISegment FromMessageEntity(FaceEntity entity) => new FaceSegment(entity.FaceId);
public ISegment FromMessageEntity(IMessageEntity entity)
{
if (entity is not FaceEntity faceEntity) throw new ArgumentException("Invalid entity type.");

return new FaceSegment(faceEntity.FaceId);
}
}
12 changes: 0 additions & 12 deletions Lagrange.OneBot/Core/Message/IOneBotSegment.cs

This file was deleted.

6 changes: 6 additions & 0 deletions Lagrange.OneBot/Core/Message/ISegment.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using Lagrange.Core.Message;

namespace Lagrange.OneBot.Core.Message;

public interface ISegment
{
internal string Type { get; }

public IMessageEntity ToEntity();

public ISegment FromMessageEntity(IMessageEntity entity);
}
38 changes: 37 additions & 1 deletion Lagrange.OneBot/Core/Message/MessageConverter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
using System.Reflection;
using Lagrange.Core;
using Lagrange.Core.Core.Event.EventArg;

namespace Lagrange.OneBot.Core.Message;

/// <summary>
/// The class that converts the OneBot message to/from MessageEntity of Lagrange.Core
/// </summary>
public class MessageConverter
public sealed class MessageConverter
{
public Dictionary<Type, Type> EntityToSegment { get; set; }

public MessageConverter(BotContext bot, ILagrangeWebService service)
{
var invoker = bot.Invoker;

invoker.OnFriendMessageReceived += OnFriendMessageReceived;
invoker.OnGroupMessageReceived += OnGroupMessageReceived;
invoker.OnTempMessageReceived += OnTempMessageReceived;

EntityToSegment = new Dictionary<Type, Type>();
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
if (type.IsSubclassOf(typeof(ISegment)))
{
}
}
}

private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e)
{

}

private void OnGroupMessageReceived(BotContext bot, GroupMessageEvent e)
{

}

private void OnTempMessageReceived(BotContext bot, TempMessageEvent e)
{

}
}
6 changes: 0 additions & 6 deletions Lagrange.OneBot/Core/MessageConverter.cs

This file was deleted.

49 changes: 49 additions & 0 deletions Lagrange.OneBot/Core/Service/HttpPostService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Text;
using System.Text.Json;
using Lagrange.OneBot.Core.Entity.Meta;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Lagrange.OneBot.Core.Service;

public sealed class HttpPostService : ILagrangeWebService
{
private readonly HttpClient _client;

private readonly IConfiguration _config;

private readonly ILogger _logger;

private readonly string _url;

public HttpPostService(IConfiguration config, ILogger<LagrangeApp> logger)
{
_config = config;
_logger = logger;
_client = new HttpClient();

_client.DefaultRequestHeaders.Add("User-Agent", "Lagrange");
_client.DefaultRequestHeaders.Add("X-Self-ID", _config.GetValue<uint>("Account:Uin").ToString());

var conf = _config.GetSection("Implementation").GetSection("HttpPost");
_url = $"http://{conf["Host"]}:{conf["Port"]}{conf["Suffix"]}";
}

public async Task StartAsync(CancellationToken cancellationToken)
{
var lifecycle = new OneBotLifecycle(_config.GetValue<uint>("Account:Uin"), "enable");
await SendJsonAsync(lifecycle, cancellationToken);
}

public async Task StopAsync(CancellationToken cancellationToken)
{
var lifecycle = new OneBotLifecycle(_config.GetValue<uint>("Account:Uin"), "disable");
await SendJsonAsync(lifecycle, cancellationToken);
}

public async ValueTask SendJsonAsync<T>(T json, CancellationToken cancellationToken = default)
{
string payload = JsonSerializer.Serialize(json);
await _client.PostAsync(_url, new StringContent(payload, Encoding.UTF8), cancellationToken);
}
}
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Service/ReverseWSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Lagrange.OneBot.Core.Service;

public class ReverseWSService : ILagrangeWebService
public sealed class ReverseWSService : ILagrangeWebService
{
private readonly ClientWebSocket _socket;

Expand Down
Loading

0 comments on commit 2d71b5c

Please sign in to comment.