Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sisi0318 committed Nov 1, 2024
2 parents 362c42c + c301e31 commit b3d82a0
Show file tree
Hide file tree
Showing 22 changed files with 504 additions and 22 deletions.
49 changes: 49 additions & 0 deletions Lagrange.Core/Common/Entity/ImageOcrResultEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Text.Json.Serialization;

namespace Lagrange.Core.Common.Entity
{
[Serializable]
public class ImageOcrResult
{
[JsonPropertyName("texts")] public List<TextDetection> Texts { get; set; }

[JsonPropertyName("language")] public string Language { get; set; }

public ImageOcrResult(List<TextDetection> texts, string language)
{
Texts = texts;
Language = language;
}
}

[Serializable]
public class TextDetection
{
[JsonPropertyName("text")] public string Text { get; set; }

[JsonPropertyName("confidence")] public int Confidence { get; set; }

[JsonPropertyName("coordinates")] public List<Coordinate> Coordinates { get; set; }

public TextDetection(string text, int confidence, List<Coordinate> coordinates)
{
Text = text;
Confidence = confidence;
Coordinates = coordinates;
}
}

[Serializable]
public class Coordinate
{
[JsonPropertyName("x")] public int X { get; set; }

[JsonPropertyName("y")] public int Y { get; set; }

public Coordinate(int x, int y)
{
X = x;
Y = y;
}
}
}
9 changes: 9 additions & 0 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,14 @@ public static Task<bool> GroupJoinEmojiChain(this BotContext bot, uint groupUin,

public static Task<bool> FriendJoinEmojiChain(this BotContext bot, uint friendUin, uint emojiId, uint targetMessageSeq)
=> bot.ContextCollection.Business.OperationLogic.FriendJoinEmojiChain(friendUin, emojiId, targetMessageSeq);

public static Task<string> UploadImage(this BotContext bot, ImageEntity entity)
=> bot.ContextCollection.Business.OperationLogic.UploadImage(entity);

public static Task<ImageOcrResult?> OcrImage(this BotContext bot, string url)
=> bot.ContextCollection.Business.OperationLogic.ImageOcr(url);

public static Task<ImageOcrResult?> OcrImage(this BotContext bot, ImageEntity entity)
=> bot.ContextCollection.Business.OperationLogic.ImageOcr(entity);
}

19 changes: 19 additions & 0 deletions Lagrange.Core/Event/EventArg/GroupMemberEnterEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Lagrange.Core.Event.EventArg;

public class GroupMemberEnterEvent : EventBase
{
public uint GroupUin { get; }

public uint GroupMemberUin { get; }

public uint StyleId { get; }

internal GroupMemberEnterEvent(uint groupUin, uint groupMemberUin, uint styleId)
{
GroupUin = groupUin;
GroupMemberUin = groupMemberUin;
StyleId = styleId;

EventMessage = $"[{nameof(GroupMemberEnterEvent)}]: {GroupMemberUin} Enter {GroupUin} | {StyleId}";
}
}
2 changes: 2 additions & 0 deletions Lagrange.Core/Event/EventInvoker.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ public partial class EventInvoker
public event LagrangeEvent<GroupNameChangeEvent>? OnGroupNameChangeEvent;

public event LagrangeEvent<GroupTodoEvent>? OnGroupTodoEvent;

public event LagrangeEvent<GroupMemberEnterEvent>? OnGroupMemberEnterEvent;
}
1 change: 1 addition & 0 deletions Lagrange.Core/Event/EventInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ internal EventInvoker(BotContext context)
RegisterEvent((GroupReactionEvent e) => OnGroupReactionEvent?.Invoke(context, e));
RegisterEvent((GroupNameChangeEvent e) => OnGroupNameChangeEvent?.Invoke(context, e));
RegisterEvent((GroupTodoEvent e) => OnGroupTodoEvent?.Invoke(context, e));
RegisterEvent((GroupMemberEnterEvent e) => OnGroupMemberEnterEvent?.Invoke(context, e));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Lagrange.Core.Internal.Context.Logic.Implementation;
[EventSubscribe(typeof(GroupSysNameChangeEvent))]
[EventSubscribe(typeof(FriendSysRecallEvent))]
[EventSubscribe(typeof(FriendSysRequestEvent))]
[EventSubscribe(typeof(GroupSysMemberEnterEvent))]
[EventSubscribe(typeof(FriendSysPokeEvent))]
[EventSubscribe(typeof(LoginNotifyEvent))]
[EventSubscribe(typeof(MultiMsgDownloadEvent))]
Expand Down Expand Up @@ -159,6 +160,12 @@ public override async Task Incoming(ProtocolEvent e)
Collection.Invoker.PostEvent(requestArgs);
break;
}
case GroupSysMemberEnterEvent info:
{
var @event = new GroupMemberEnterEvent(info.GroupUin, info.GroupMemberUin, info.StyleId);
Collection.Invoker.PostEvent(@event);
break;
}
case GroupSysMuteEvent groupMute:
{
uint? operatorUin = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,28 @@ public async Task<bool> FriendJoinEmojiChain(uint friendUin, uint emojiId, uint
var results = await Collection.Business.SendEvent(friendJoinEmojiChainEvent);
return results.Count != 0 && results[0].ResultCode == 0;
}

public async Task<string> UploadImage(ImageEntity image)
{
await Collection.Highway.ManualUploadEntity(image);
var msgInfo = image.MsgInfo;
if (msgInfo is null) throw new Exception();
var downloadEvent = ImageDownloadEvent.Create(Collection.Keystore.Uid ?? "", msgInfo);
var result = await Collection.Business.SendEvent(downloadEvent);
var ret = (ImageDownloadEvent)result[0];
return ret.ImageUrl;
}

public async Task<ImageOcrResult?> ImageOcr(string imageUrl)
{
var imageOcrEvent = ImageOcrEvent.Create(imageUrl);
var results = await Collection.Business.SendEvent(imageOcrEvent);
return results.Count != 0 ? ((ImageOcrEvent)results[0]).ImageOcrResult : null;
}

public async Task<ImageOcrResult?> ImageOcr(ImageEntity image)
{
var imageUrl = await UploadImage(image);
return await ImageOcr(imageUrl);
}
}
25 changes: 25 additions & 0 deletions Lagrange.Core/Internal/Event/Action/ImageOcrEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Lagrange.Core.Common.Entity;

namespace Lagrange.Core.Internal.Event.Action;

internal class ImageOcrEvent : ProtocolEvent
{
public string Url { get; } = string.Empty;

public ImageOcrResult ImageOcrResult { get; }

private ImageOcrEvent(string url) : base(true)
{
Url = url;
ImageOcrResult = new ImageOcrResult(new List<TextDetection>(), "");
}

private ImageOcrEvent(int resultCode, ImageOcrResult result) : base(resultCode)
{
ImageOcrResult = result;
}

public static ImageOcrEvent Create(string url) => new(url);

public static ImageOcrEvent Result(int resultCode, ImageOcrResult result) => new(resultCode, result);
}
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Event/Notify/GroupSysMemberEnterEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Lagrange.Core.Internal.Event.Notify;

internal class GroupSysMemberEnterEvent : ProtocolEvent
{
public uint GroupUin { get; }

public uint GroupMemberUin { get; }

public uint StyleId { get; }

private GroupSysMemberEnterEvent(uint groupUin, uint groupMemberUin, uint styleId) : base(0)
{
GroupUin = groupUin;
GroupMemberUin = groupMemberUin;
StyleId = styleId;
}

public static GroupSysMemberEnterEvent Result(uint groupUin, uint groupMemberUin, uint styleId) =>
new(groupUin, groupMemberUin, styleId);
}
62 changes: 62 additions & 0 deletions Lagrange.Core/Internal/Packets/Message/Notify/GroupMemberEnter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using ProtoBuf;

#pragma warning disable CS8618

namespace Lagrange.Core.Internal.Packets.Message.Notify;

[ProtoContract]
internal class GroupMemberEnter
{
[ProtoMember(1)] public bool Field1 { get; set; }

[ProtoMember(2)] public GroupMemberEnterContentBody Body { get; set; }
}

[ProtoContract]
internal class GroupMemberEnterContentBody
{
[ProtoMember(1)] public GroupMemberEnterContentBodyField1 Field1 { get; set; }

[ProtoMember(2)] public uint GroupId { get; set; }

[ProtoMember(3)] public uint Field3 { get; set; }

[ProtoMember(4)] public GroupMemberEnterInfo Info { get; set; }

[ProtoMember(5)] public uint Field5 { get; set; }
}

[ProtoContract]
internal class GroupMemberEnterContentBodyField1
{
[ProtoMember(1)] public uint Field1 { get; set; }
}

[ProtoContract]
internal class GroupMemberEnterInfo
{
[ProtoMember(2)] public uint Field2 { get; set; }

[ProtoMember(3)] public GroupMemberEnterDetail Detail { get; set; }

[ProtoMember(4)] public uint Field4 { get; set; }

[ProtoMember(5)] public uint Field5 { get; set; }
}

[ProtoContract]
internal class GroupMemberEnterDetail
{
[ProtoMember(3)] public uint GroupMemberUin { get; set; }
[ProtoMember(4)] public uint GroupId { get; set; }
[ProtoMember(5)] public uint Field5 { get; set; }
[ProtoMember(6)] public uint Field6 { get; set; }
[ProtoMember(20)] public GroupMemberEnterStyle Style { get; set; }
[ProtoMember(21)] public uint Field21 { get; set; }
}

[ProtoContract]
internal class GroupMemberEnterStyle
{
[ProtoMember(1)] public uint StyleId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using ProtoBuf;

#pragma warning disable CS8618
// ReSharper disable InconsistentNaming

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

[ProtoContract]
[OidbSvcTrpcTcp(0xE07, 0)]
internal class OidbSvcTrpcTcp0xE07_0
{
[ProtoMember(1)] public uint Version { get; set; }

[ProtoMember(2)] public uint Client { get; set; }

[ProtoMember(3)] public uint Entrance { get; set; }

[ProtoMember(10)] public OcrReqBody OcrReqBody { get; set; }
}

[ProtoContract]
internal class OcrReqBody
{
[ProtoMember(1)] public string ImageUrl { get; set; }

[ProtoMember(2)] public uint LanguageType { get; set; }

[ProtoMember(3)] public uint Scene { get; set; }

[ProtoMember(10)] public string OriginMd5 { get; set; }

[ProtoMember(11)] public string AfterCompressMd5 { get; set; }

[ProtoMember(12)] public string AfterCompressFileSize { get; set; }

[ProtoMember(13)] public string AfterCompressWeight { get; set; }

[ProtoMember(14)] public string AfterCompressHeight { get; set; }

[ProtoMember(15)] public bool IsCut { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using ProtoBuf;

#pragma warning disable CS8618
// ReSharper disable InconsistentNaming

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Response;

[ProtoContract]
internal class OidbSvcTrpcTcp0xE07_0_Response
{
[ProtoMember(1)] public int RetCode { get; set; }

[ProtoMember(2)] public string ErrMsg { get; set; }

[ProtoMember(3)] public string Wording { get; set; }

[ProtoMember(10)] public OcrRspBody OcrRspBody { get; set; }
}

[ProtoContract]
internal class OcrRspBody
{
[ProtoMember(1)] public List<TextDetection> TextDetections { get; set; }

[ProtoMember(2)] public string Language { get; set; }

[ProtoMember(3)] public string RequestId { get; set; }

[ProtoMember(101)] public List<string> OcrLanguageList { get; set; }

[ProtoMember(102)] public List<string> DstTranslateLanguageList { get; set; }

[ProtoMember(103)] public List<Language> LanguageList { get; set; }

[ProtoMember(111)] public uint AfterCompressWeight { get; set; }

[ProtoMember(112)] public uint AfterCompressHeight { get; set; }
}

[ProtoContract]
internal class TextDetection
{
[ProtoMember(1)] public string DetectedText { get; set; }

[ProtoMember(2)] public uint Confidence { get; set; }

[ProtoMember(3)] public Polygon Polygon { get; set; }

[ProtoMember(4)] public string AdvancedInfo { get; set; }
}

[ProtoContract]
internal class Polygon
{
[ProtoMember(1)] public List<Coordinate> Coordinates { get; set; }
}

[ProtoContract]
internal class Coordinate
{
[ProtoMember(1)] public int X { get; set; }

[ProtoMember(2)] public int Y { get; set; }
}

[ProtoContract]
internal class Language
{
[ProtoMember(1)] public string LanguageCode { get; set; }

[ProtoMember(2)] public string LanguageDesc { get; set; }
}
Loading

0 comments on commit b3d82a0

Please sign in to comment.