Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
通过继续传递connet包从而适配crossplay等需要读取玩家连接消息的插件
Browse files Browse the repository at this point in the history
  • Loading branch information
Megghy committed Aug 30, 2021
1 parent de2c5f9 commit c4afd5c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions MultiSCore/Core/HostServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OTAPI;
using System;
using System.Linq;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
Expand Down
32 changes: 24 additions & 8 deletions MultiSCore/Core/ServerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -61,18 +61,20 @@ public void OnConnectRequest(MSCHooks.PlayerJoinEventArgs args)
if (text == "A1" && Utils.GetConfigValue<bool>("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)
{
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions MultiSCore/MSCHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions MultiSCore/MSCPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 9 additions & 3 deletions MultiSCore/MSCPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -69,6 +69,7 @@ protected override void Dispose(bool disposing)
/// 使用反代进入此服务器的玩家
/// </summary>
public Utils.HostInfo[] ForwordInfo = new Utils.HostInfo[256];
public string[] VersionInfo = new string[256];
void OnReload(ReloadEventArgs args)
{
Config.Load();
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions MultiSCore/MultiSCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<Reference Include="BCrypt.Net, Version=0.1.4141.31969, Culture=neutral, PublicKeyToken=f3bc8f8c31beeb49, processorArchitecture=MSIL">
<HintPath>..\packages\BCrypt.Net.0.1.0\lib\net35\BCrypt.Net.dll</HintPath>
</Reference>
<Reference Include="Crossplay" />
<Reference Include="Crossplay">
<HintPath>..\..\..\TShock4.5.0_Terraria1.4.2.1\ServerPlugins\Crossplay.dll</HintPath>
</Reference>
<Reference Include="HttpServer, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5684c2c737cd4271, processorArchitecture=MSIL">
<HintPath>..\packages\TerrariaServer.TShock.4.5.2\lib\net452\HttpServer.dll</HintPath>
</Reference>
Expand Down
4 changes: 2 additions & 2 deletions MultiSCore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
1 change: 1 addition & 0 deletions MultiSCore/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down

0 comments on commit c4afd5c

Please sign in to comment.