Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/CaiBotLite/CaiBotLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CaiBotLite;
// ReSharper disable once ClassNeverInstantiated.Global
public class CaiBotLite(Main game) : TerrariaPlugin(game)
{
public static readonly Version VersionNum = new (2026, 02, 14, 0); //日期+版本号(0,1,2...)
public static readonly Version VersionNum = new (2026, 02, 17, 0); //日期+版本号(0,1,2...)
internal static int InitCode = -1;
internal static bool DebugMode = Program.LaunchParameters.ContainsKey("-caidebug");
private const string CharacterInfoKey = "CaiBotLite.CharacterInfo";
Expand All @@ -39,7 +39,7 @@ public override void Initialize()
GeneralHooks.ReloadEvent += GeneralHooksOnReloadEvent;
PlayerHooks.PlayerPostLogin += PlayerHooksOnPlayerPostLogin;
GetDataHandlers.KillMe.Register(KillMe, HandlerPriority.Highest);
MapGenerator.Init();
MapGeneratorSupport.Init();
EconomicSupport.Init();
BossLockSupport.Init();
ProgressControlSupport.Init();
Expand All @@ -63,7 +63,6 @@ protected override void Dispose(bool disposing)
GeneralHooks.ReloadEvent -= GeneralHooksOnReloadEvent;
PlayerHooks.PlayerPostLogin -= PlayerHooksOnPlayerPostLogin;
GetDataHandlers.KillMe.UnRegister(KillMe);
MapGenerator.Dispose();
WebsocketManager.StopWebsocket();
}

Expand Down
6 changes: 1 addition & 5 deletions src/CaiBotLite/CaiBotLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

<ItemGroup>
<PackageReference Include="linq2db" Version="5.4.1"/>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12"/>
</ItemGroup>

<PropertyGroup>
<EmbedNuGetDlls>SixLabors.ImageSharp.dll</EmbedNuGetDlls>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BossLock\BossLock.csproj"/>
<ProjectReference Include="..\Economics.RPG\Economics.RPG.csproj"/>
<ProjectReference Include="..\Economics.Skill\Economics.Skill.csproj"/>
<ProjectReference Include="..\Economics.Core\Economics.Core.csproj"/>
<ProjectReference Include="..\GenerateMap\GenerateMap.csproj" />
<ProjectReference Include="..\ProgressControls\ProgressControls.csproj"/>
</ItemGroup>

Expand Down
24 changes: 9 additions & 15 deletions src/CaiBotLite/Common/CaiBotApi.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CaiBotLite.Enums;
using CaiBotLite.Models;
using Microsoft.Xna.Framework;
using SixLabors.ImageSharp.Formats.Png;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
Expand All @@ -11,7 +10,7 @@ namespace CaiBotLite.Common;

internal static class CaiBotApi
{
internal static async Task HandleMessageAsync(string receivedData)
internal static void HandleMessage(string receivedData)
{
var package = Package.Parse(receivedData);
var packetWriter = new PackageWriter(package.Type, package.IsRequest, package.RequestId);
Expand Down Expand Up @@ -161,23 +160,18 @@ internal static async Task HandleMessageAsync(string receivedData)

break;
case PackageType.MapImage:
var bitmap = MapGenerator.CreateMapImg();
using (MemoryStream ms = new ())
{
await bitmap.SaveAsync(ms, new PngEncoder());
var imageBytes = ms.ToArray();
var base64 = Convert.ToBase64String(imageBytes);
packetWriter
.Write("base64", Utils.CompressBase64(base64))
.Send();
}
var imageBytes = MapGeneratorSupport.CreatMapImgBytes();
packetWriter
.Write("base64", Utils.CompressBase64(Convert.ToBase64String(imageBytes)))
.Send();


break;
case PackageType.MapFile:
var mapFile = MapGenerator.CreateMapFile();
var mapFile = MapGeneratorSupport.CreateMapFile();
packetWriter
.Write("name", mapFile.Item2)
.Write("base64", Utils.CompressBase64(mapFile.Item1))
.Write("base64", Utils.CompressBase64(Convert.ToBase64String(mapFile.Item1)))
.Send();

break;
Expand Down Expand Up @@ -339,7 +333,7 @@ internal static async Task HandleMessageAsync(string receivedData)
$"源数据包: {receivedData}");

packetWriter.Package.Type = PackageType.Error;
packetWriter.Write("error", ex)
packetWriter.Write("error", ex.ToString())
.Send();
}
}
Expand Down
80 changes: 0 additions & 80 deletions src/CaiBotLite/Common/MapGenerator.cs

This file was deleted.

38 changes: 38 additions & 0 deletions src/CaiBotLite/Common/MapGeneratorSupport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using TerrariaApi.Server;

namespace CaiBotLite.Common;

internal static class MapGeneratorSupport
{
private static bool Support { get; set; }
internal static void Init()
{
var pluginContainer = ServerApi.Plugins.FirstOrDefault(x => x.Plugin.Name == "GenerateMap");
if (pluginContainer is not null)
{
Support = true;
}
}

private static void ThrowIfNotSupported()
{
if (!Support)
{
throw new NotSupportedException("需要安装GenerateMap插件");
}
}


internal static byte[] CreatMapImgBytes()
{
ThrowIfNotSupported();
return GenerateMap.MapGenerator.CreatMapImgBytes();
}

internal static (byte[], string) CreateMapFile()
{
ThrowIfNotSupported();
var mapFile = GenerateMap.MapGenerator.CreatMapFile();
return (mapFile.File, mapFile.Name);
}
}
19 changes: 16 additions & 3 deletions src/CaiBotLite/Common/WebsocketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public static class WebsocketManager
public static ClientWebSocket? WebSocket;

private const string BotServerUrl = "api.terraria.ink:22338";

//private const string BotServerUrl = "127.0.0.1:8080";

internal static bool IsWebsocketConnected => WebSocket?.State == WebSocketState.Open;
private static bool _isStopWebsocket;

Expand Down Expand Up @@ -137,7 +136,21 @@ public static void StopWebsocket()
TShock.Log.ConsoleInfo($"[CaiBotLite]收到BOT数据包: {receivedData}");
}

await CaiBotApi.HandleMessageAsync(receivedData);
_ = Task.Run(() =>
{
try
{
CaiBotApi.HandleMessage(receivedData);
}
catch (Exception e)
{
TShock.Log.ConsoleError("[CaiBotLite]处理消息时发生错误: \n" +
$"{e}");
}

});


}
}
catch (Exception ex)
Expand Down
1 change: 1 addition & 0 deletions src/CaiBotLite/Models/BossKillInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class BossKillInfo
{
[PrimaryKey]
[Identity]
[NotNull]
[Column("id")]
public int Id;

Expand Down
1 change: 1 addition & 0 deletions src/CaiBotLite/Models/CaiCharacterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace CaiBotLite.Models;
public class CaiCharacterInfo
{
[PrimaryKey]
[NotNull]
[Column("account_name")]
public string AccountName = null!;

Expand Down
1 change: 1 addition & 0 deletions src/CaiBotLite/Models/Mail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Mail
{
[PrimaryKey]
[Identity]
[NotNull]
[Column("id")]
public int Id;

Expand Down
4 changes: 4 additions & 0 deletions src/CaiBotLite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ https://docs.terraria.ink/zh/caibot/CaiBotLite.html

## 更新日志

### v2026.02.17.0

- 改用GenerateMap生成地图,异步处理数据包

### v2026.02.15.0

- 适配Terraria 1.4.5
Expand Down
Loading
Loading