From c4afd5c6c12fe55aad5e4eaf722fb536c3ca6ea8 Mon Sep 17 00:00:00 2001 From: Megghy Date: Mon, 30 Aug 2021 10:12:00 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E7=BB=A7=E7=BB=AD=E4=BC=A0?= =?UTF-8?q?=E9=80=92connet=E5=8C=85=E4=BB=8E=E8=80=8C=E9=80=82=E9=85=8Dcro?= =?UTF-8?q?ssplay=E7=AD=89=E9=9C=80=E8=A6=81=E8=AF=BB=E5=8F=96=E7=8E=A9?= =?UTF-8?q?=E5=AE=B6=E8=BF=9E=E6=8E=A5=E6=B6=88=E6=81=AF=E7=9A=84=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MultiSCore/Core/HostServer.cs | 1 + MultiSCore/Core/ServerAdapter.cs | 32 ++++++++++++++++++++------- MultiSCore/MSCHooks.cs | 8 ++++--- MultiSCore/MSCPlayer.cs | 11 ++++++--- MultiSCore/MSCPlugin.cs | 12 +++++++--- MultiSCore/MultiSCore.csproj | 4 ++++ MultiSCore/Properties/AssemblyInfo.cs | 4 ++-- MultiSCore/Utils.cs | 1 + 8 files changed, 54 insertions(+), 19 deletions(-) diff --git a/MultiSCore/Core/HostServer.cs b/MultiSCore/Core/HostServer.cs index 997d8b4..561a7d2 100644 --- a/MultiSCore/Core/HostServer.cs +++ b/MultiSCore/Core/HostServer.cs @@ -1,5 +1,6 @@ using OTAPI; using System; +using System.Linq; using Terraria; using TerrariaApi.Server; using TShockAPI; diff --git a/MultiSCore/Core/ServerAdapter.cs b/MultiSCore/Core/ServerAdapter.cs index 43c2ea8..f112acd 100644 --- a/MultiSCore/Core/ServerAdapter.cs +++ b/MultiSCore/Core/ServerAdapter.cs @@ -21,7 +21,7 @@ public ServerAdapter(string name, string key) } public string Name { get; set; } public string Key { get; set; } - public void OnConnectRequest(MSCHooks.PlayerJoinEventArgs args) + public bool OnConnectRequest(MSCHooks.PlayerJoinEventArgs args) { var index = args.Index; if (!MSCPlugin.Instance.ServerConfig.AllowOthorServerJoin) @@ -61,18 +61,20 @@ public void OnConnectRequest(MSCHooks.PlayerJoinEventArgs args) if (text == "A1" && Utils.GetConfigValue("KickProxyUsers")) { tsplayer.Disconnect("Proxy connections are not allowed."); - return; + return false; } } else TShock.Players[index] = tsplayer; - } - Netplay.Clients[index].State = 1; - MSCPlugin.Instance.ForwordInfo[index] = new() { Version = args.Version, Key = args.Key }; - NetMessage.TrySendData(3, index); + } + MSCPlugin.Instance.ForwordInfo[index] = new() { Version = args.Version, Key = args.Key, TRVersion = args.TRVersion }; TShock.Log.ConsoleInfo(Utils.GetText("Log_FromAnothorMultiSCore")); + /*NetMessage.TrySendData(3, index); + Netplay.Clients[index].State = 1;*/ + return true; } } + return false; } public HookResult OnReceiveData(MessageBuffer buffer, ref byte packetid, ref int readoffset, ref int start, ref int length) { @@ -96,12 +98,26 @@ public HookResult OnReceiveData(MessageBuffer buffer, ref byte packetid, ref int if (key.StartsWith("Terraria")) { if (MSCPlugin.Instance.ServerConfig.AllowDirectJoin) - return MSCPlugin.Instance.OldGetDataHandler.Invoke(buffer, ref packetid, ref readoffset, ref start, ref length); //让tr自己处理加入事件 + { + MSCPlugin.Instance.VersionInfo[index] = key.Remove(0, 8); + return MSCPlugin.Instance.OldGetDataHandler(buffer, ref packetid, ref readoffset, ref start, ref length); + } else NetMessage.TrySendData(2, index, -1, NetworkText.FromLiteral(Utils.GetText("Log_DontAllowDirectJoin"))); return HookResult.Cancel; } - if (!MSCHooks.OnPlayerJoin(index, buffer.reader.ReadString(), key, buffer.reader.ReadString(), buffer.reader.ReadString(), out var joinArgs)) OnConnectRequest(joinArgs); + else if (!MSCHooks.OnPlayerJoin(index, buffer.reader.ReadString(), key, buffer.reader.ReadString(), buffer.reader.ReadString(), buffer.reader.ReadString(), out var joinArgs) && OnConnectRequest(joinArgs)) + { + var tempBuffer = buffer.readBuffer.ToList(); + tempBuffer.RemoveRange(start - 2, length + 2); + var data = new RawDataBuilder(1).PackString($"Terraria{joinArgs.TRVersion}").GetByteData(); + tempBuffer.InsertRange(start - 2, data); + length = data.Length - 2; + tempBuffer.CopyTo(buffer.readBuffer, 0); + buffer.reader = new(new System.IO.MemoryStream(buffer.readBuffer)); + buffer.readerStream = (System.IO.MemoryStream)buffer.reader.BaseStream; + return MSCPlugin.Instance.OldGetDataHandler(buffer, ref packetid, ref readoffset, ref start, ref length); + } return HookResult.Cancel; } buffer.reader.BaseStream.Position = position; diff --git a/MultiSCore/MSCHooks.cs b/MultiSCore/MSCHooks.cs index 8087913..979efb6 100644 --- a/MultiSCore/MSCHooks.cs +++ b/MultiSCore/MSCHooks.cs @@ -12,7 +12,7 @@ public class MSCHooks { public struct PlayerJoinEventArgs { - public PlayerJoinEventArgs(int index,string name, string key, string ip, string version) + public PlayerJoinEventArgs(int index,string name, string key, string ip, string version, string trVersion) { Handled = false; Index = index; @@ -21,12 +21,14 @@ public PlayerJoinEventArgs(int index,string name, string key, string ip, string IP = ip; Version.TryParse(version, out var _v); Version = _v; + TRVersion = trVersion; } public int Index { get; internal set; } public string Name { get; internal set; } public string Key { get; internal set; } public string IP { get; internal set; } public Version Version { get; set; } + public string TRVersion { get; set; } public bool Handled { get; set; } } public struct RecieveCustomDataEventArgs @@ -75,9 +77,9 @@ public PlayerFinishSwitchEventArgs(int index) public static event PlayerReadyToSwitchEvent PlayerReadyToSwitch; public delegate void PlayerFinishSwitchEvent(PlayerFinishSwitchEventArgs args); public static event PlayerFinishSwitchEvent PlayerFinishJoin; - internal static bool OnPlayerJoin(int index, string name, string key, string ip, string version, out PlayerJoinEventArgs args) + internal static bool OnPlayerJoin(int index, string name, string key, string ip, string version, string trVersion, out PlayerJoinEventArgs args) { - args = new(index, name, key, ip, version); + args = new(index, name, key, ip, version, trVersion); PlayerJoin?.Invoke(args); return args.Handled; } diff --git a/MultiSCore/MSCPlayer.cs b/MultiSCore/MSCPlayer.cs index b9f88b4..733b853 100644 --- a/MultiSCore/MSCPlayer.cs +++ b/MultiSCore/MSCPlayer.cs @@ -98,12 +98,17 @@ public void SwitchServer(Config.ForwordServer server) DataBackup.CopyCharacter(Player); PlayerDifficulty = Player.TPlayer.difficulty; - SendDataToForword(new RawDataBuilder(1).PackString(Key).PackString(server.Name).PackString(Player.IP).PackString(MSCPlugin.Instance.Version.ToString())); //发起连接请求 + SendDataToForword(new RawDataBuilder(1) + .PackString(Key) + .PackString(server.Name) + .PackString(Player.IP) + .PackString(MSCPlugin.Instance.Version.ToString()) + .PackString(Player.IsForwordPlayer() ? "0" : MSCPlugin.Instance.VersionInfo[Index])); //发起连接请求 } - catch + catch(Exception ex) { Player.RemoveData("MultiSCore_Switching"); - TShock.Log.ConsoleError(string.Format(Utils.GetText("Log_CannotConnect"), server.IP, server.Port)); + TShock.Log.ConsoleError(string.Format($"{Utils.GetText("Log_CannotConnect")}\r\n{ex}", server.IP, server.Port)); Player?.SendErrorMsg(string.Format(Utils.GetText("Prompt_CannotConnect"), server.Name)); Dispose(); } diff --git a/MultiSCore/MSCPlugin.cs b/MultiSCore/MSCPlugin.cs index 7808a49..ddcc4fb 100644 --- a/MultiSCore/MSCPlugin.cs +++ b/MultiSCore/MSCPlugin.cs @@ -3,8 +3,8 @@ using System; using System.Linq; using System.Reflection; +using System.Text; using Terraria; -using Terraria.Chat.Commands; using TerrariaApi.Server; using TShockAPI; using TShockAPI.Hooks; @@ -44,7 +44,7 @@ protected override void Dispose(bool disposing) if (disposing) { ServerApi.Hooks.GamePostInitialize.Deregister(this, OnPostInit); - if(OldGetDataHandler is not null) + if (OldGetDataHandler is not null) Hooks.Net.ReceiveData = OldGetDataHandler; if (OldSendDataHandler is not null) Hooks.Net.SendBytes = OldSendDataHandler; @@ -69,6 +69,7 @@ protected override void Dispose(bool disposing) /// 使用反代进入此服务器的玩家 /// public Utils.HostInfo[] ForwordInfo = new Utils.HostInfo[256]; + public string[] VersionInfo = new string[256]; void OnReload(ReloadEventArgs args) { Config.Load(); @@ -154,11 +155,16 @@ void OnCommand(CommandArgs args) cmd.RemoveAt(0); Commands.HandleCommand(plr, string.Join(" ", cmd)); } -else + else plr.SendErrorMsg($"{Utils.GetText("Prompt_InvalidFormat")}\r\n{Utils.GetText("Help_Command")}"); } else plr.SendInfoMsg(Utils.GetText("Command_NotJoined")); break; + case "online": + case "playing": + case "o": + var sb = new StringBuilder(); //todo + break; default: sendHelpText(); break; diff --git a/MultiSCore/MultiSCore.csproj b/MultiSCore/MultiSCore.csproj index 43a28da..c7336c0 100644 --- a/MultiSCore/MultiSCore.csproj +++ b/MultiSCore/MultiSCore.csproj @@ -37,6 +37,10 @@ ..\packages\BCrypt.Net.0.1.0\lib\net35\BCrypt.Net.dll + + + ..\..\..\TShock4.5.0_Terraria1.4.2.1\ServerPlugins\Crossplay.dll + ..\packages\TerrariaServer.TShock.4.5.2\lib\net452\HttpServer.dll diff --git a/MultiSCore/Properties/AssemblyInfo.cs b/MultiSCore/Properties/AssemblyInfo.cs index 523975c..0a4d6d1 100644 --- a/MultiSCore/Properties/AssemblyInfo.cs +++ b/MultiSCore/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.5.3.0")] -[assembly: AssemblyFileVersion("1.5.3.0")] +[assembly: AssemblyVersion("1.5.4.0")] +[assembly: AssemblyFileVersion("1.5.4.0")] diff --git a/MultiSCore/Utils.cs b/MultiSCore/Utils.cs index 158d4d7..a8d1788 100644 --- a/MultiSCore/Utils.cs +++ b/MultiSCore/Utils.cs @@ -17,6 +17,7 @@ public enum CustomPacket } public class HostInfo { + public string TRVersion { get; set; } public Version Version { get; set; } public string Key { get; set; } }