Skip to content

Commit

Permalink
[All] add support for custom BotAppInfo (#687)
Browse files Browse the repository at this point in the history
* [All] add support for custom `BotAppInfo`

* [OneBot] rewrite app info fetching logic to style of that of `deviceInfo`
  • Loading branch information
Wesley-Young authored Nov 17, 2024
1 parent 53d82d5 commit ad18e88
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Lagrange.Core/BotContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class BotContext : IDisposable

private readonly BotKeystore _keystore;

internal BotContext(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore)
internal BotContext(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore, BotAppInfo appInfo)
{
Invoker = new EventInvoker(this);
Scheduler = new Utility.TaskScheduler();

Config = config;
AppInfo = BotAppInfo.ProtocolToAppInfo[config.Protocol];
AppInfo = appInfo;
_deviceInfo = deviceInfo;
_keystore = keystore;

Expand Down
32 changes: 16 additions & 16 deletions Lagrange.Core/Common/BotAppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ namespace Lagrange.Core.Common;

public class BotAppInfo
{
public string Os { get; private set; }
public string Os { get; set; }

public string VendorOs { get; private set; }
public string VendorOs { get; set; }

public string Kernel { get; private set; }
public string Kernel { get; set; }

public string CurrentVersion { get; private set; }
public string CurrentVersion { get; set; }

public int MiscBitmap { get; private set; }
public int MiscBitmap { get; set; }

public string PtVersion { get; private set; }
public string PtVersion { get; set; }

public int SsoVersion { get; private set; }
public int SsoVersion { get; set; }

public string PackageName { get; private set; }
public string PackageName { get; set; }

public string WtLoginSdk { get; private set; }
public string WtLoginSdk { get; set; }

public int AppId { get; private set; }
public int AppId { get; set; }

/// <summary>Or known as pubId in tencent log</summary>
public int SubAppId { get; private set; }
public int SubAppId { get; set; }

public int AppIdQrCode { get; private set; }
public int AppIdQrCode { get; set; }

public ushort AppClientVersion { get; private set; }
public ushort AppClientVersion { get; set; }

public uint MainSigMap { get; private set; }
public uint MainSigMap { get; set; }

public ushort SubSigMap { get; private set; }
public ushort SubSigMap { get; set; }

public ushort NTLoginType { get; private set; }
public ushort NTLoginType { get; set; }

private static readonly BotAppInfo Linux = new()
{
Expand Down
27 changes: 25 additions & 2 deletions Lagrange.Core/Common/Interface/BotFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ public static class BotFactory
/// <param name="keystore">Existing Keystore from deserialization</param>
/// <returns>Created BotContext Instance</returns>
public static BotContext Create(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore) =>
new(config, deviceInfo, keystore);
new(config, deviceInfo, keystore, BotAppInfo.ProtocolToAppInfo[config.Protocol]);

/// <summary>
/// Create new Bot from existing <see cref="BotKeystore"/> and <see cref="BotDeviceInfo"/> with custom <see cref="BotAppInfo"/>
/// </summary>
/// <param name="config">The config for Bot</param>
/// <param name="deviceInfo">Existing DeviceInfo from deserialization</param>
/// <param name="keystore">Existing Keystore from deserialization</param>
/// <param name="appInfo">Custom BotAppInfo, including client version, app ID, etc</param>
/// <returns></returns>
public static BotContext Create(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore, BotAppInfo appInfo) =>
new(config, deviceInfo, keystore, appInfo);

/// <summary>
/// Create new Bot from Password and uin
Expand All @@ -21,5 +32,17 @@ public static BotContext Create(BotConfig config, BotDeviceInfo deviceInfo, BotK
/// <param name="device">Created device, should be serialized to files for next use</param>
/// <returns>Created BotContext Instance</returns>
public static BotContext Create(BotConfig config, uint uin, string password, out BotDeviceInfo device) =>
new(config, device = BotDeviceInfo.GenerateInfo(), new BotKeystore(uin, password));
new(config, device = BotDeviceInfo.GenerateInfo(), new BotKeystore(uin, password), BotAppInfo.ProtocolToAppInfo[config.Protocol]);

/// <summary>
/// Create new Bot from Password and uin with custom <see cref="BotAppInfo"/>
/// </summary>
/// <param name="config">The config for Bot</param>
/// <param name="uin">Uin, if QrCode login is used, ensure the account that scans QrCode is consistent with this Uin</param>
/// <param name="password">The password of account, for Password Login</param>
/// <param name="appInfo">Custom BotAppInfo, including client version, app ID, etc</param>
/// <param name="device">Created device, should be serialized to files for next use</param>
/// <returns></returns>
public static BotContext Create(BotConfig config, uint uin, string password, BotAppInfo appInfo, out BotDeviceInfo device) =>
new(config, device = BotDeviceInfo.GenerateInfo(), new BotKeystore(uin, password), appInfo);
}
20 changes: 19 additions & 1 deletion Lagrange.OneBot/LagrangeAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public LagrangeAppBuilder ConfigureBots()
{
string keystorePath = Configuration["ConfigPath:Keystore"] ?? "keystore.json";
string deviceInfoPath = Configuration["ConfigPath:DeviceInfo"] ?? "device.json";
string appInfoPath = Configuration["ConfigPath:AppInfo"] ?? "appinfo.json";

bool isSuccess = Enum.TryParse<Protocols>(Configuration["Account:Protocol"], out var protocol);
var config = new BotConfig
Expand Down Expand Up @@ -86,7 +87,24 @@ public LagrangeAppBuilder ConfigureBots()
deviceInfo = JsonSerializer.Deserialize<BotDeviceInfo>(File.ReadAllText(deviceInfoPath)) ?? BotDeviceInfo.GenerateInfo();
}

Services.AddSingleton(BotFactory.Create(config, deviceInfo, keystore));
BotAppInfo appInfo;
if (!File.Exists(appInfoPath))
{
appInfo = BotAppInfo.ProtocolToAppInfo[config.Protocol];
string json = JsonSerializer.Serialize(appInfo);
string? directoryPath = Path.GetDirectoryName(appInfoPath);
if (!string.IsNullOrEmpty(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.WriteAllText(appInfoPath, json);
}
else
{
appInfo = JsonSerializer.Deserialize<BotAppInfo>(File.ReadAllText(appInfoPath)) ?? BotAppInfo.ProtocolToAppInfo[config.Protocol];
}

Services.AddSingleton(BotFactory.Create(config, deviceInfo, keystore, appInfo));

return this;
}
Expand Down

0 comments on commit ad18e88

Please sign in to comment.