diff --git a/Torch.API/ITorchBase.cs b/Torch.API/ITorchBase.cs
index b7e15d0..6c851ab 100644
--- a/Torch.API/ITorchBase.cs
+++ b/Torch.API/ITorchBase.cs
@@ -44,10 +44,6 @@ namespace Torch.API
///
ITorchConfig Config { get; }
- ///
- [Obsolete]
- IMultiplayerManager Multiplayer { get; }
-
///
[Obsolete]
IPluginManager Plugins { get; }
@@ -55,6 +51,12 @@ namespace Torch.API
///
IDependencyManager Managers { get; }
+ [Obsolete("Prefer using Managers.GetManager for global managers")]
+ T GetManager() where T : class, IManager;
+
+ [Obsolete("Prefer using Managers.AddManager for global managers")]
+ bool AddManager(T manager) where T : class, IManager;
+
///
/// The binary version of the current instance.
///
diff --git a/Torch.API/Managers/IChatManagerClient.cs b/Torch.API/Managers/IChatManagerClient.cs
new file mode 100644
index 0000000..0c8ca18
--- /dev/null
+++ b/Torch.API/Managers/IChatManagerClient.cs
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using VRage.Network;
+
+namespace Torch.API.Managers
+{
+ ///
+ /// Represents a scripted or user chat message.
+ ///
+ public struct TorchChatMessage
+ {
+ ///
+ /// The author's steam ID, if available. Else, null.
+ ///
+ public ulong? AuthorSteamId;
+ ///
+ /// The author's name, if available. Else, null.
+ ///
+ public string Author;
+ ///
+ /// The message contents.
+ ///
+ public string Message;
+ ///
+ /// The font, or null if default.
+ ///
+ public string Font;
+ }
+
+ ///
+ /// Callback used to indicate that a messaage has been recieved.
+ ///
+ ///
+ /// If true, this event has been consumed and should be ignored
+ public delegate void DelMessageRecieved(TorchChatMessage msg, ref bool consumed);
+
+ ///
+ /// Callback used to indicate the user is attempting to send a message locally.
+ ///
+ /// Message the user is attempting to send
+ /// If true, this event has been consumed and should be ignored
+ public delegate void DelMessageSending(string msg, ref bool consumed);
+
+ public interface IChatManagerClient : IManager
+ {
+ ///
+ /// Event that is raised when a message addressed to us is recieved.
+ ///
+ event DelMessageRecieved MessageRecieved;
+
+ ///
+ /// Event that is raised when we are attempting to send a message.
+ ///
+ event DelMessageSending MessageSending;
+
+ ///
+ /// Triggers the event,
+ /// typically raised by the user entering text into the chat window.
+ ///
+ /// The message to send
+ void SendMessageAsSelf(string message);
+
+ ///
+ /// Displays a message on the UI given an author name and a message.
+ ///
+ /// Author name
+ /// Message content
+ /// font to use
+ void DisplayMessageOnSelf(string author, string message, string font = "Blue" );
+ }
+}
diff --git a/Torch.API/Managers/IChatManagerServer.cs b/Torch.API/Managers/IChatManagerServer.cs
new file mode 100644
index 0000000..3a402c8
--- /dev/null
+++ b/Torch.API/Managers/IChatManagerServer.cs
@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using VRage.Network;
+
+namespace Torch.API.Managers
+{
+
+ ///
+ /// Callback used to indicate the server has recieved a message to process.
+ ///
+ /// Steam ID of the user sending a message
+ /// Message the user is attempting to send
+ /// If true, this event has been consumed and should be ignored
+ public delegate void DelMessageProcessing(TorchChatMessage msg, ref bool consumed);
+
+ public interface IChatManagerServer : IChatManagerClient
+ {
+ ///
+ /// Event triggered when the server has recieved a message and should process it.
+ ///
+ event DelMessageProcessing MessageProcessing;
+
+
+ ///
+ /// Sends a message with the given author and message to the given player, or all players by default.
+ ///
+ /// Author's steam ID
+ /// The message to send
+ /// Player to send the message to, or everyone by default
+ void SendMessageAsOther(ulong authorId, string message, ulong targetSteamId = 0);
+
+
+ ///
+ /// Sends a scripted message with the given author and message to the given player, or all players by default.
+ ///
+ /// Author name
+ /// The message to send
+ /// Font to use
+ /// Player to send the message to, or everyone by default
+ void SendMessageAsOther(string author, string message, string font, ulong targetSteamId = 0);
+ }
+}
diff --git a/Torch.API/Managers/IMultiplayerManager.cs b/Torch.API/Managers/IMultiplayerManagerBase.cs
similarity index 65%
rename from Torch.API/Managers/IMultiplayerManager.cs
rename to Torch.API/Managers/IMultiplayerManagerBase.cs
index 509360d..8b552e7 100644
--- a/Torch.API/Managers/IMultiplayerManager.cs
+++ b/Torch.API/Managers/IMultiplayerManagerBase.cs
@@ -16,7 +16,7 @@ namespace Torch.API.Managers
///
/// API for multiplayer related functions.
///
- public interface IMultiplayerManager : IManager
+ public interface IMultiplayerManagerBase : IManager
{
///
/// Fired when a player joins.
@@ -27,27 +27,7 @@ namespace Torch.API.Managers
/// Fired when a player disconnects.
///
event Action PlayerLeft;
-
- ///
- /// Fired when a chat message is received.
- ///
- event MessageReceivedDel MessageReceived;
-
- ///
- /// Send a chat message to all or one specific player.
- ///
- void SendMessage(string message, string author = "Server", long playerId = 0, string font = MyFontEnum.Blue);
-
- ///
- /// Kicks the player from the game.
- ///
- void KickPlayer(ulong steamId);
-
- ///
- /// Bans or unbans a player from the game.
- ///
- void BanPlayer(ulong steamId, bool banned = true);
-
+
///
/// Gets a player by their Steam64 ID or returns null if the player isn't found.
///
@@ -57,5 +37,12 @@ namespace Torch.API.Managers
/// Gets a player by their display name or returns null if the player isn't found.
///
IMyPlayer GetPlayerByName(string name);
+
+ ///
+ /// Gets the steam username of a member's steam ID
+ ///
+ /// steam ID
+ /// steam username
+ string GetSteamUsername(ulong steamId);
}
}
\ No newline at end of file
diff --git a/Torch.API/Managers/IMultiplayerManagerClient.cs b/Torch.API/Managers/IMultiplayerManagerClient.cs
new file mode 100644
index 0000000..f4e6a32
--- /dev/null
+++ b/Torch.API/Managers/IMultiplayerManagerClient.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Torch.API.Managers
+{
+ public interface IMultiplayerManagerClient : IMultiplayerManagerBase
+ {
+ }
+}
diff --git a/Torch.API/Managers/IMultiplayerManagerServer.cs b/Torch.API/Managers/IMultiplayerManagerServer.cs
new file mode 100644
index 0000000..36dddf8
--- /dev/null
+++ b/Torch.API/Managers/IMultiplayerManagerServer.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Torch.API.Managers
+{
+ public interface IMultiplayerManagerServer : IMultiplayerManagerBase
+ {
+ ///
+ /// Kicks the player from the game.
+ ///
+ void KickPlayer(ulong steamId);
+
+ ///
+ /// Bans or unbans a player from the game.
+ ///
+ void BanPlayer(ulong steamId, bool banned = true);
+ }
+}
diff --git a/Torch.API/Managers/INetworkManager.cs b/Torch.API/Managers/INetworkManager.cs
index 548ec10..acbea3b 100644
--- a/Torch.API/Managers/INetworkManager.cs
+++ b/Torch.API/Managers/INetworkManager.cs
@@ -18,6 +18,12 @@ namespace Torch.API.Managers
/// Register a network handler.
///
void RegisterNetworkHandler(INetworkHandler handler);
+
+ ///
+ /// Unregister a network handler.
+ ///
+ /// true if the handler was unregistered, false if it wasn't registered to begin with
+ bool UnregisterNetworkHandler(INetworkHandler handler);
}
///
@@ -33,6 +39,7 @@ namespace Torch.API.Managers
///
/// Processes a network message.
///
+ /// true if the message should be discarded
bool Handle(ulong remoteUserId, CallSite site, BitStream stream, object obj, MyPacket packet);
}
}
diff --git a/Torch.API/Torch.API.csproj b/Torch.API/Torch.API.csproj
index 99b4836..6ffcc86 100644
--- a/Torch.API/Torch.API.csproj
+++ b/Torch.API/Torch.API.csproj
@@ -164,11 +164,15 @@
+
+
-
+
+
+
diff --git a/Torch.Client.Tests/TorchClientReflectionTest.cs b/Torch.Client.Tests/TorchClientReflectionTest.cs
index 756b144..12a1948 100644
--- a/Torch.Client.Tests/TorchClientReflectionTest.cs
+++ b/Torch.Client.Tests/TorchClientReflectionTest.cs
@@ -29,6 +29,10 @@ namespace Torch.Client.Tests
public static IEnumerable Invokers => Manager().Invokers;
+ public static IEnumerable MemberInfo => Manager().MemberInfo;
+
+ public static IEnumerable Events => Manager().Events;
+
#region Binding
[Theory]
[MemberData(nameof(Getters))]
@@ -62,6 +66,28 @@ namespace Torch.Client.Tests
if (field.Field.IsStatic)
Assert.NotNull(field.Field.GetValue(null));
}
+
+ [Theory]
+ [MemberData(nameof(MemberInfo))]
+ public void TestBindingMemberInfo(ReflectionTestManager.FieldRef field)
+ {
+ if (field.Field == null)
+ return;
+ Assert.True(ReflectedManager.Process(field.Field));
+ if (field.Field.IsStatic)
+ Assert.NotNull(field.Field.GetValue(null));
+ }
+
+ [Theory]
+ [MemberData(nameof(Events))]
+ public void TestBindingEvents(ReflectionTestManager.FieldRef field)
+ {
+ if (field.Field == null)
+ return;
+ Assert.True(ReflectedManager.Process(field.Field));
+ if (field.Field.IsStatic)
+ Assert.NotNull(field.Field.GetValue(null));
+ }
#endregion
}
}
diff --git a/Torch.Client/Manager/MultiplayerManagerClient.cs b/Torch.Client/Manager/MultiplayerManagerClient.cs
new file mode 100644
index 0000000..3cfc519
--- /dev/null
+++ b/Torch.Client/Manager/MultiplayerManagerClient.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Sandbox.Engine.Multiplayer;
+using Torch.API;
+using Torch.API.Managers;
+using Torch.Managers;
+
+namespace Torch.Client.Manager
+{
+ public class MultiplayerManagerClient : MultiplayerManagerBase, IMultiplayerManagerClient
+ {
+ ///
+ public MultiplayerManagerClient(ITorchBase torch) : base(torch) { }
+
+ ///
+ public override void Attach()
+ {
+ base.Attach();
+ MyMultiplayer.Static.ClientJoined += RaiseClientJoined;
+ }
+
+ ///
+ public override void Detach()
+ {
+ MyMultiplayer.Static.ClientJoined -= RaiseClientJoined;
+ base.Detach();
+ }
+ }
+}
diff --git a/Torch.Client/Manager/MultiplayerManagerLobby.cs b/Torch.Client/Manager/MultiplayerManagerLobby.cs
new file mode 100644
index 0000000..5ffd7e9
--- /dev/null
+++ b/Torch.Client/Manager/MultiplayerManagerLobby.cs
@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Sandbox.Engine.Multiplayer;
+using Torch.API;
+using Torch.API.Managers;
+using Torch.Managers;
+
+namespace Torch.Client.Manager
+{
+ public class MultiplayerManagerLobby : MultiplayerManagerBase, IMultiplayerManagerServer
+ {
+ ///
+ public MultiplayerManagerLobby(ITorchBase torch) : base(torch) { }
+
+ ///
+ public void KickPlayer(ulong steamId) => Torch.Invoke(() => MyMultiplayer.Static.KickClient(steamId));
+
+ ///
+ public void BanPlayer(ulong steamId, bool banned = true) => Torch.Invoke(() => MyMultiplayer.Static.BanClient(steamId, banned));
+
+ ///
+ public override void Attach()
+ {
+ base.Attach();
+ MyMultiplayer.Static.ClientJoined += RaiseClientJoined;
+ }
+
+ ///
+ public override void Detach()
+ {
+ MyMultiplayer.Static.ClientJoined -= RaiseClientJoined;
+ base.Detach();
+ }
+ }
+}
diff --git a/Torch.Client/Torch.Client.csproj b/Torch.Client/Torch.Client.csproj
index abe6a5f..9ed8a34 100644
--- a/Torch.Client/Torch.Client.csproj
+++ b/Torch.Client/Torch.Client.csproj
@@ -121,6 +121,8 @@
Properties\AssemblyVersion.cs
+
+
@@ -167,6 +169,7 @@
+
diff --git a/Torch.Client/TorchClient.cs b/Torch.Client/TorchClient.cs
index 384ad3b..839c40a 100644
--- a/Torch.Client/TorchClient.cs
+++ b/Torch.Client/TorchClient.cs
@@ -4,12 +4,17 @@ using System.IO;
using System.Reflection;
using System.Windows;
using Sandbox;
+using Sandbox.Engine.Multiplayer;
using Sandbox.Engine.Networking;
using Sandbox.Engine.Platform;
using Sandbox.Game;
using SpaceEngineers.Game;
using VRage.Steam;
using Torch.API;
+using Torch.API.Managers;
+using Torch.API.Session;
+using Torch.Client.Manager;
+using Torch.Session;
using VRage;
using VRage.FileSystem;
using VRage.GameServices;
@@ -27,6 +32,13 @@ namespace Torch.Client
public TorchClient()
{
Config = new TorchClientConfig();
+ var sessionManager = Managers.GetManager();
+ sessionManager.AddFactory((x) => MyMultiplayer.Static is MyMultiplayerLobby
+ ? new MultiplayerManagerLobby(this)
+ : null);
+ sessionManager.AddFactory((x) => MyMultiplayer.Static is MyMultiplayerClientBase
+ ? new MultiplayerManagerClient(this)
+ : null);
}
public override void Init()
diff --git a/Torch.Client/TorchMainMenuScreen.cs b/Torch.Client/TorchMainMenuScreen.cs
index c0078e4..0bd9e5c 100644
--- a/Torch.Client/TorchMainMenuScreen.cs
+++ b/Torch.Client/TorchMainMenuScreen.cs
@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using Sandbox.Game.Gui;
using Sandbox.Graphics;
using Sandbox.Graphics.GUI;
using Sandbox.Gui;
@@ -16,6 +17,15 @@ namespace Torch.Client
{
public class TorchMainMenuScreen : MyGuiScreenMainMenu
{
+ public TorchMainMenuScreen()
+ : this(false)
+ {
+ }
+
+ public TorchMainMenuScreen(bool pauseGame)
+ : base(pauseGame)
+ {
+ }
///
public override void RecreateControls(bool constructor)
{
diff --git a/Torch.Server.Tests/TorchServerReflectionTest.cs b/Torch.Server.Tests/TorchServerReflectionTest.cs
index e4ec2de..8897c8d 100644
--- a/Torch.Server.Tests/TorchServerReflectionTest.cs
+++ b/Torch.Server.Tests/TorchServerReflectionTest.cs
@@ -28,6 +28,10 @@ namespace Torch.Server.Tests
public static IEnumerable Invokers => Manager().Invokers;
+ public static IEnumerable MemberInfo => Manager().MemberInfo;
+
+ public static IEnumerable Events => Manager().Events;
+
#region Binding
[Theory]
[MemberData(nameof(Getters))]
@@ -61,6 +65,17 @@ namespace Torch.Server.Tests
if (field.Field.IsStatic)
Assert.NotNull(field.Field.GetValue(null));
}
+
+ [Theory]
+ [MemberData(nameof(Events))]
+ public void TestBindingEvents(ReflectionTestManager.FieldRef field)
+ {
+ if (field.Field == null)
+ return;
+ Assert.True(ReflectedManager.Process(field.Field));
+ if (field.Field.IsStatic)
+ Assert.NotNull(field.Field.GetValue(null));
+ }
#endregion
}
}
diff --git a/Torch.Server/Managers/MultiplayerManagerDedicated.cs b/Torch.Server/Managers/MultiplayerManagerDedicated.cs
new file mode 100644
index 0000000..85399c0
--- /dev/null
+++ b/Torch.Server/Managers/MultiplayerManagerDedicated.cs
@@ -0,0 +1,166 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using NLog;
+using NLog.Fluent;
+using Sandbox;
+using Sandbox.Engine.Multiplayer;
+using Sandbox.Engine.Networking;
+using Torch.API;
+using Torch.API.Managers;
+using Torch.Managers;
+using Torch.Utils;
+using Torch.ViewModels;
+using VRage.GameServices;
+using VRage.Network;
+
+namespace Torch.Server.Managers
+{
+ public class MultiplayerManagerDedicated : MultiplayerManagerBase, IMultiplayerManagerServer
+ {
+ private static readonly Logger _log = LogManager.GetCurrentClassLogger();
+
+#pragma warning disable 649
+ [ReflectedGetter(Name = "m_members")]
+ private static Func> _members;
+ [ReflectedGetter(Name = "m_waitingForGroup")]
+ private static Func> _waitingForGroup;
+#pragma warning restore 649
+
+ private Dictionary _gameOwnerIds = new Dictionary();
+
+ ///
+ public MultiplayerManagerDedicated(ITorchBase torch) : base(torch) { }
+
+ ///
+ public void KickPlayer(ulong steamId) => Torch.Invoke(() => MyMultiplayer.Static.KickClient(steamId));
+
+ ///
+ public void BanPlayer(ulong steamId, bool banned = true)
+ {
+ Torch.Invoke(() =>
+ {
+ MyMultiplayer.Static.BanClient(steamId, banned);
+ if (_gameOwnerIds.ContainsKey(steamId))
+ MyMultiplayer.Static.BanClient(_gameOwnerIds[steamId], banned);
+ });
+ }
+
+ ///
+ public override void Attach()
+ {
+ base.Attach();
+ _gameServerValidateAuthTicketReplacer = _gameServerValidateAuthTicketFactory.Invoke();
+ _gameServerUserGroupStatusReplacer = _gameServerUserGroupStatusFactory.Invoke();
+ _gameServerValidateAuthTicketReplacer.Replace(new Action(ValidateAuthTicketResponse), MyGameService.GameServer);
+ _gameServerUserGroupStatusReplacer.Replace(new Action(UserGroupStatusResponse), MyGameService.GameServer);
+ _log.Info("Inserted steam authentication intercept");
+ }
+
+ ///
+ public override void Detach()
+ {
+ if (_gameServerValidateAuthTicketReplacer != null && _gameServerValidateAuthTicketReplacer.Replaced)
+ _gameServerValidateAuthTicketReplacer.Restore(MyGameService.GameServer);
+ if (_gameServerUserGroupStatusReplacer != null && _gameServerUserGroupStatusReplacer.Replaced)
+ _gameServerUserGroupStatusReplacer.Restore(MyGameService.GameServer);
+ _log.Info("Removed steam authentication intercept");
+ base.Detach();
+ }
+
+
+#pragma warning disable 649
+ [ReflectedEventReplace(typeof(IMyGameServer), nameof(IMyGameServer.ValidateAuthTicketResponse), typeof(MyDedicatedServerBase), "GameServer_ValidateAuthTicketResponse")]
+ private static Func _gameServerValidateAuthTicketFactory;
+ [ReflectedEventReplace(typeof(IMyGameServer), nameof(IMyGameServer.UserGroupStatusResponse), typeof(MyDedicatedServerBase), "GameServer_UserGroupStatus")]
+ private static Func _gameServerUserGroupStatusFactory;
+ private ReflectedEventReplacer _gameServerValidateAuthTicketReplacer;
+ private ReflectedEventReplacer _gameServerUserGroupStatusReplacer;
+#pragma warning restore 649
+
+ #region CustomAuth
+#pragma warning disable 649
+ [ReflectedStaticMethod(Type = typeof(MyDedicatedServerBase), Name = "ConvertSteamIDFrom64")]
+ private static Func _convertSteamIDFrom64;
+
+ [ReflectedStaticMethod(Type = typeof(MyGameService), Name = "GetServerAccountType")]
+ private static Func _getServerAccountType;
+
+ [ReflectedMethod(Name = "UserAccepted")]
+ private static Action _userAcceptedImpl;
+
+ [ReflectedMethod(Name = "UserRejected")]
+ private static Action _userRejected;
+ [ReflectedMethod(Name = "IsClientBanned")]
+ private static Func _isClientBanned;
+ [ReflectedMethod(Name = "IsClientKicked")]
+ private static Func _isClientKicked;
+ [ReflectedMethod(Name = "RaiseClientKicked")]
+ private static Action _raiseClientKicked;
+#pragma warning restore 649
+
+ //Largely copied from SE
+ private void ValidateAuthTicketResponse(ulong steamID, JoinResult response, ulong steamOwner)
+ {
+ _log.Debug($"ValidateAuthTicketResponse(user={steamID}, response={response}, owner={steamOwner}");
+ if (_isClientBanned.Invoke(MyMultiplayer.Static, steamOwner) || MySandboxGame.ConfigDedicated.Banned.Contains(steamOwner))
+ {
+ _userRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.BannedByAdmins);
+ _raiseClientKicked.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID);
+ }
+ else if (_isClientKicked.Invoke(MyMultiplayer.Static, steamOwner))
+ {
+ _userRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.KickedRecently);
+ _raiseClientKicked.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID);
+ }
+ if (response != JoinResult.OK)
+ {
+ _userRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, response);
+ return;
+ }
+ if (MyMultiplayer.Static.MemberLimit > 0 && _members.Invoke((MyDedicatedServerBase)MyMultiplayer.Static).Count - 1 >= MyMultiplayer.Static.MemberLimit)
+ {
+ _userRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.ServerFull);
+ return;
+ }
+ if (MySandboxGame.ConfigDedicated.GroupID == 0uL ||
+ MySandboxGame.ConfigDedicated.Administrators.Contains(steamID.ToString()) ||
+ MySandboxGame.ConfigDedicated.Administrators.Contains(_convertSteamIDFrom64(steamID)))
+ {
+ this.UserAccepted(steamID);
+ return;
+ }
+ if (_getServerAccountType(MySandboxGame.ConfigDedicated.GroupID) != MyGameServiceAccountType.Clan)
+ {
+ _userRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.GroupIdInvalid);
+ return;
+ }
+ if (MyGameService.GameServer.RequestGroupStatus(steamID, MySandboxGame.ConfigDedicated.GroupID))
+ {
+ _waitingForGroup.Invoke((MyDedicatedServerBase)MyMultiplayer.Static).Add(steamID);
+ return;
+ }
+ _userRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.SteamServersOffline);
+ }
+
+ private void UserGroupStatusResponse(ulong userId, ulong groupId, bool member, bool officer)
+ {
+ if (groupId == MySandboxGame.ConfigDedicated.GroupID && _waitingForGroup.Invoke((MyDedicatedServerBase)MyMultiplayer.Static).Remove(userId))
+ {
+ if (member || officer)
+ UserAccepted(userId);
+ else
+ _userRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, userId, JoinResult.NotInGroup);
+ }
+ }
+ private void UserAccepted(ulong steamId)
+ {
+ _userAcceptedImpl.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamId);
+ base.RaiseClientJoined(steamId);
+ }
+ #endregion
+ }
+}
diff --git a/Torch.Server/Torch.Server.csproj b/Torch.Server/Torch.Server.csproj
index fbfab75..5d9bee4 100644
--- a/Torch.Server/Torch.Server.csproj
+++ b/Torch.Server/Torch.Server.csproj
@@ -191,6 +191,7 @@
Properties\AssemblyVersion.cs
+
diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs
index 987aad9..b5dfef4 100644
--- a/Torch.Server/TorchServer.cs
+++ b/Torch.Server/TorchServer.cs
@@ -17,6 +17,8 @@ using Sandbox.Game.Multiplayer;
using Sandbox.ModAPI;
using SteamSDK;
using Torch.API;
+using Torch.API.Managers;
+using Torch.API.Session;
using Torch.Managers;
using Torch.Server.Managers;
using Torch.Utils;
@@ -63,6 +65,9 @@ namespace Torch.Server
AddManager(DedicatedInstance);
Config = config ?? new TorchConfig();
MyFakes.ENABLE_INFINARIO = false;
+
+ var sessionManager = Managers.GetManager();
+ sessionManager.AddFactory((x) => new MultiplayerManagerDedicated(this));
}
///
@@ -253,19 +258,20 @@ namespace Torch.Server
{
case SaveGameStatus.Success:
Log.Info("Save completed.");
- Multiplayer.SendMessage("Saved game.", playerId: callerId);
+ // TODO
+// Multiplayer.SendMessage("Saved game.", playerId: callerId);
break;
case SaveGameStatus.SaveInProgress:
Log.Error("Save failed, a save is already in progress.");
- Multiplayer.SendMessage("Save failed, a save is already in progress.", playerId: callerId, font: MyFontEnum.Red);
+// Multiplayer.SendMessage("Save failed, a save is already in progress.", playerId: callerId, font: MyFontEnum.Red);
break;
case SaveGameStatus.GameNotReady:
Log.Error("Save failed, game was not ready.");
- Multiplayer.SendMessage("Save failed, game was not ready.", playerId: callerId, font: MyFontEnum.Red);
+// Multiplayer.SendMessage("Save failed, game was not ready.", playerId: callerId, font: MyFontEnum.Red);
break;
case SaveGameStatus.TimedOut:
Log.Error("Save failed, save timed out.");
- Multiplayer.SendMessage("Save failed, save timed out.", playerId: callerId, font: MyFontEnum.Red);
+// Multiplayer.SendMessage("Save failed, save timed out.", playerId: callerId, font: MyFontEnum.Red);
break;
default:
break;
diff --git a/Torch.Server/Views/ChatControl.xaml.cs b/Torch.Server/Views/ChatControl.xaml.cs
index 03bab57..2b8e9de 100644
--- a/Torch.Server/Views/ChatControl.xaml.cs
+++ b/Torch.Server/Views/ChatControl.xaml.cs
@@ -20,7 +20,9 @@ using Sandbox.Engine.Multiplayer;
using Sandbox.Game.World;
using SteamSDK;
using Torch.API;
+using Torch.API.Managers;
using Torch.Managers;
+using Torch.Server.Managers;
namespace Torch.Server
{
@@ -30,7 +32,6 @@ namespace Torch.Server
public partial class ChatControl : UserControl
{
private TorchBase _server;
- private MultiplayerManager _multiplayer;
public ChatControl()
{
@@ -40,11 +41,15 @@ namespace Torch.Server
public void BindServer(ITorchServer server)
{
_server = (TorchBase)server;
- _multiplayer = (MultiplayerManager)server.Multiplayer;
ChatItems.Items.Clear();
- DataContext = _multiplayer;
- if (_multiplayer.ChatHistory is INotifyCollectionChanged ncc)
- ncc.CollectionChanged += ChatHistory_CollectionChanged;
+ server.SessionLoaded += () =>
+ {
+ var multiplayer = server.CurrentSession.Managers.GetManager();
+ DataContext = multiplayer;
+ // TODO
+ // if (multiplayer.ChatHistory is INotifyCollectionChanged ncc)
+ // ncc.CollectionChanged += ChatHistory_CollectionChanged;
+ };
}
private void ChatHistory_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
@@ -78,10 +83,10 @@ namespace Torch.Server
if (string.IsNullOrEmpty(text))
return;
- var commands = _server.Commands;
- if (commands.IsCommand(text))
+ var commands = _server.CurrentSession?.Managers.GetManager();
+ if (commands != null && commands.IsCommand(text))
{
- _multiplayer.ChatHistory.Add(new ChatMessage(DateTime.Now, 0, "Server", text));
+ // TODO _multiplayer.ChatHistory.Add(new ChatMessage(DateTime.Now, 0, "Server", text));
_server.Invoke(() =>
{
var response = commands.HandleCommandFromServer(text);
@@ -90,15 +95,15 @@ namespace Torch.Server
}
else
{
- _server.Multiplayer.SendMessage(text);
+ _server.CurrentSession?.Managers.GetManager().SendMessageAsSelf(text);
}
Message.Text = "";
}
private void OnMessageEntered_Callback(string response)
{
- if (!string.IsNullOrEmpty(response))
- _multiplayer.ChatHistory.Add(new ChatMessage(DateTime.Now, 0, "Server", response));
+ //if (!string.IsNullOrEmpty(response))
+ // TODO _multiplayer.ChatHistory.Add(new ChatMessage(DateTime.Now, 0, "Server", response));
}
}
}
diff --git a/Torch.Server/Views/PlayerListControl.xaml.cs b/Torch.Server/Views/PlayerListControl.xaml.cs
index a54a306..70fbbc3 100644
--- a/Torch.Server/Views/PlayerListControl.xaml.cs
+++ b/Torch.Server/Views/PlayerListControl.xaml.cs
@@ -20,7 +20,9 @@ using Sandbox.Game.World;
using Sandbox.ModAPI;
using SteamSDK;
using Torch.API;
+using Torch.API.Managers;
using Torch.Managers;
+using Torch.Server.Managers;
using Torch.ViewModels;
using VRage.Game.ModAPI;
@@ -41,19 +43,23 @@ namespace Torch.Server
public void BindServer(ITorchServer server)
{
_server = server;
- DataContext = (MultiplayerManager)_server.Multiplayer;
+ server.SessionLoaded += () =>
+ {
+ var multiplayer = server.CurrentSession?.Managers.GetManager();
+ DataContext = multiplayer;
+ };
}
private void KickButton_Click(object sender, RoutedEventArgs e)
{
var player = (KeyValuePair)PlayerList.SelectedItem;
- _server.Multiplayer.KickPlayer(player.Key);
+ _server.CurrentSession?.Managers.GetManager()?.KickPlayer(player.Key);
}
private void BanButton_Click(object sender, RoutedEventArgs e)
{
var player = (KeyValuePair) PlayerList.SelectedItem;
- _server.Multiplayer.BanPlayer(player.Key);
+ _server.CurrentSession?.Managers.GetManager()?.BanPlayer(player.Key);
}
}
}
diff --git a/Torch.Tests/ReflectionSystemTest.cs b/Torch.Tests/ReflectionSystemTest.cs
index 0e6fc92..4161fe7 100644
--- a/Torch.Tests/ReflectionSystemTest.cs
+++ b/Torch.Tests/ReflectionSystemTest.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Reflection;
using Torch.Utils;
using Xunit;
@@ -11,7 +12,7 @@ namespace Torch.Tests
{
TestUtils.Init();
}
-
+
private static ReflectionTestManager _manager = new ReflectionTestManager().Init(typeof(ReflectionTestBinding));
public static IEnumerable Getters => _manager.Getters;
@@ -19,6 +20,10 @@ namespace Torch.Tests
public static IEnumerable Invokers => _manager.Invokers;
+ public static IEnumerable MemberInfo => _manager.MemberInfo;
+
+ public static IEnumerable Events => _manager.Events;
+
#region Binding
[Theory]
[MemberData(nameof(Getters))]
@@ -52,6 +57,28 @@ namespace Torch.Tests
if (field.Field.IsStatic)
Assert.NotNull(field.Field.GetValue(null));
}
+
+ [Theory]
+ [MemberData(nameof(MemberInfo))]
+ public void TestBindingMemberInfo(ReflectionTestManager.FieldRef field)
+ {
+ if (field.Field == null)
+ return;
+ Assert.True(ReflectedManager.Process(field.Field));
+ if (field.Field.IsStatic)
+ Assert.NotNull(field.Field.GetValue(null));
+ }
+
+ [Theory]
+ [MemberData(nameof(Events))]
+ public void TestBindingEvents(ReflectionTestManager.FieldRef field)
+ {
+ if (field.Field == null)
+ return;
+ Assert.True(ReflectedManager.Process(field.Field));
+ if (field.Field.IsStatic)
+ Assert.NotNull(field.Field.GetValue(null));
+ }
#endregion
#region Results
@@ -79,10 +106,51 @@ namespace Torch.Tests
{
return k >= 0;
}
+
+ public event Action Event1;
+
+ public ReflectionTestTarget()
+ {
+ Event1 += Callback1;
+ }
+
+ public bool Callback1Flag = false;
+ public void Callback1()
+ {
+ Callback1Flag = true;
+ }
+ public bool Callback2Flag = false;
+ public void Callback2()
+ {
+ Callback2Flag = true;
+ }
+
+ public void RaiseEvent()
+ {
+ Event1?.Invoke();
+ }
}
private class ReflectionTestBinding
{
+ #region Instance
+ #region MemberInfo
+ [ReflectedFieldInfo(typeof(ReflectionTestTarget), "TestField")]
+ public static FieldInfo TestFieldInfo;
+
+ [ReflectedPropertyInfo(typeof(ReflectionTestTarget), "TestProperty")]
+ public static PropertyInfo TestPropertyInfo;
+
+ [ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCall")]
+ public static MethodInfo TestMethodInfoGeneral;
+
+ [ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCall", Parameters = new[] { typeof(int) })]
+ public static MethodInfo TestMethodInfoExplicitArgs;
+
+ [ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCall", ReturnType = typeof(bool))]
+ public static MethodInfo TestMethodInfoExplicitReturn;
+ #endregion
+
[ReflectedGetter(Name = "TestField")]
public static Func TestFieldGetter;
[ReflectedSetter(Name = "TestField")]
@@ -96,7 +164,27 @@ namespace Torch.Tests
[ReflectedMethod]
public static Func TestCall;
+ [ReflectedEventReplace(typeof(ReflectionTestTarget), "Event1", typeof(ReflectionTestTarget), "Callback1")]
+ public static Func TestEventReplacer;
+ #endregion
+ #region Static
+ #region MemberInfo
+ [ReflectedFieldInfo(typeof(ReflectionTestTarget), "TestFieldStatic")]
+ public static FieldInfo TestStaticFieldInfo;
+
+ [ReflectedPropertyInfo(typeof(ReflectionTestTarget), "TestPropertyStatic")]
+ public static PropertyInfo TestStaticPropertyInfo;
+
+ [ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCallStatic")]
+ public static MethodInfo TestStaticMethodInfoGeneral;
+
+ [ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCallStatic", Parameters = new[] { typeof(int) })]
+ public static MethodInfo TestStaticMethodInfoExplicitArgs;
+
+ [ReflectedMethodInfo(typeof(ReflectionTestTarget), "TestCallStatic", ReturnType = typeof(bool))]
+ public static MethodInfo TestStaticMethodInfoExplicitReturn;
+ #endregion
[ReflectedGetter(Name = "TestFieldStatic", Type = typeof(ReflectionTestTarget))]
public static Func TestStaticFieldGetter;
[ReflectedSetter(Name = "TestFieldStatic", Type = typeof(ReflectionTestTarget))]
@@ -109,6 +197,7 @@ namespace Torch.Tests
[ReflectedStaticMethod(Type = typeof(ReflectionTestTarget))]
public static Func TestCallStatic;
+ #endregion
}
#endregion
@@ -215,7 +304,33 @@ namespace Torch.Tests
Assert.True(ReflectionTestBinding.TestCallStatic.Invoke(1));
Assert.False(ReflectionTestBinding.TestCallStatic.Invoke(-1));
}
+
+ [Fact]
+ public void TestInstanceEventReplace()
+ {
+ var target = new ReflectionTestTarget();
+ target.Callback1Flag = false;
+ target.RaiseEvent();
+ Assert.True(target.Callback1Flag, "Control test failed");
+
+ target.Callback1Flag = false;
+ target.Callback2Flag = false;
+ ReflectedEventReplacer binder = ReflectionTestBinding.TestEventReplacer.Invoke();
+ Assert.True(binder.Test(target), "Binder was unable to find the requested method");
+
+ binder.Replace(new Action(() => target.Callback2()), target);
+ target.RaiseEvent();
+ Assert.True(target.Callback2Flag, "Substitute callback wasn't called");
+ Assert.False(target.Callback1Flag, "Original callback wasn't removed");
+
+ target.Callback1Flag = false;
+ target.Callback2Flag = false;
+ binder.Restore(target);
+ target.RaiseEvent();
+ Assert.False(target.Callback2Flag, "Substitute callback wasn't removed");
+ Assert.True(target.Callback1Flag, "Original callback wasn't restored");
+ }
#endregion
#endregion
- }
+ }
}
diff --git a/Torch.Tests/ReflectionTestManager.cs b/Torch.Tests/ReflectionTestManager.cs
index 425024e..87c3757 100644
--- a/Torch.Tests/ReflectionTestManager.cs
+++ b/Torch.Tests/ReflectionTestManager.cs
@@ -28,12 +28,16 @@ namespace Torch.Tests
private readonly HashSet _getters = new HashSet();
private readonly HashSet _setters = new HashSet();
private readonly HashSet _invokers = new HashSet();
+ private readonly HashSet _memberInfo = new HashSet();
+ private readonly HashSet _events = new HashSet();
public ReflectionTestManager()
{
_getters.Add(new object[] { new FieldRef(null) });
_setters.Add(new object[] { new FieldRef(null) });
_invokers.Add(new object[] { new FieldRef(null) });
+ _memberInfo.Add(new object[] {new FieldRef(null)});
+ _events.Add(new object[] {new FieldRef(null)});
}
public ReflectionTestManager Init(Assembly asm)
@@ -50,12 +54,36 @@ namespace Torch.Tests
BindingFlags.Public |
BindingFlags.NonPublic))
{
- if (field.GetCustomAttribute() != null)
- _invokers.Add(new object[] { new FieldRef(field) });
- if (field.GetCustomAttribute() != null)
- _getters.Add(new object[] { new FieldRef(field) });
- if (field.GetCustomAttribute() != null)
- _setters.Add(new object[] { new FieldRef(field) });
+ var args = new object[] { new FieldRef(field) };
+ foreach (ReflectedMemberAttribute attr in field.GetCustomAttributes())
+ {
+ if (!field.IsStatic)
+ throw new ArgumentException("Field must be static to be reflected");
+ switch (attr)
+ {
+ case ReflectedMethodAttribute rma:
+ _invokers.Add(args);
+ break;
+ case ReflectedGetterAttribute rga:
+ _getters.Add(args);
+ break;
+ case ReflectedSetterAttribute rsa:
+ _setters.Add(args);
+ break;
+ case ReflectedFieldInfoAttribute rfia:
+ case ReflectedPropertyInfoAttribute rpia:
+ case ReflectedMethodInfoAttribute rmia:
+ _memberInfo.Add(args);
+ break;
+ }
+ }
+ var reflectedEventReplacer = field.GetCustomAttribute();
+ if (reflectedEventReplacer != null)
+ {
+ if (!field.IsStatic)
+ throw new ArgumentException("Field must be static to be reflected");
+ _events.Add(args);
+ }
}
return this;
}
@@ -66,6 +94,10 @@ namespace Torch.Tests
public IEnumerable Invokers => _invokers;
+ public IEnumerable MemberInfo => _memberInfo;
+
+ public IEnumerable Events => _events;
+
#endregion
}
diff --git a/Torch.Tests/TorchReflectionTest.cs b/Torch.Tests/TorchReflectionTest.cs
index bb495d8..26014b5 100644
--- a/Torch.Tests/TorchReflectionTest.cs
+++ b/Torch.Tests/TorchReflectionTest.cs
@@ -27,6 +27,10 @@ namespace Torch.Tests
public static IEnumerable Invokers => Manager().Invokers;
+ public static IEnumerable MemberInfo => Manager().MemberInfo;
+
+ public static IEnumerable Events => Manager().Events;
+
#region Binding
[Theory]
[MemberData(nameof(Getters))]
@@ -60,6 +64,28 @@ namespace Torch.Tests
if (field.Field.IsStatic)
Assert.NotNull(field.Field.GetValue(null));
}
+
+ [Theory]
+ [MemberData(nameof(MemberInfo))]
+ public void TestBindingMemberInfo(ReflectionTestManager.FieldRef field)
+ {
+ if (field.Field == null)
+ return;
+ Assert.True(ReflectedManager.Process(field.Field));
+ if (field.Field.IsStatic)
+ Assert.NotNull(field.Field.GetValue(null));
+ }
+
+ [Theory]
+ [MemberData(nameof(Events))]
+ public void TestBindingEvents(ReflectionTestManager.FieldRef field)
+ {
+ if (field.Field == null)
+ return;
+ Assert.True(ReflectedManager.Process(field.Field));
+ if (field.Field.IsStatic)
+ Assert.NotNull(field.Field.GetValue(null));
+ }
#endregion
}
}
diff --git a/Torch/Commands/CommandContext.cs b/Torch/Commands/CommandContext.cs
index dd54918..30700a7 100644
--- a/Torch/Commands/CommandContext.cs
+++ b/Torch/Commands/CommandContext.cs
@@ -2,6 +2,7 @@
using System.Linq;
using System.Text.RegularExpressions;
using Torch.API;
+using Torch.API.Managers;
using Torch.API.Plugins;
using VRage.Game;
using VRage.Game.ModAPI;
@@ -47,7 +48,7 @@ namespace Torch.Commands
Response = message;
if (Player != null)
- Torch.Multiplayer.SendMessage(message, sender, Player.IdentityId, font);
+ Torch.CurrentSession.Managers.GetManager()?.SendMessageAsOther(sender, message, font, Player.SteamUserId);
}
}
}
\ No newline at end of file
diff --git a/Torch/Commands/CommandManager.cs b/Torch/Commands/CommandManager.cs
index 20b2101..e1083cc 100644
--- a/Torch/Commands/CommandManager.cs
+++ b/Torch/Commands/CommandManager.cs
@@ -9,6 +9,7 @@ using Torch.API;
using Torch.API.Managers;
using Torch.API.Plugins;
using Torch.Managers;
+using VRage.Game;
using VRage.Game.ModAPI;
using VRage.Network;
@@ -21,7 +22,7 @@ namespace Torch.Commands
public CommandTree Commands { get; set; } = new CommandTree();
private Logger _log = LogManager.GetLogger(nameof(CommandManager));
[Dependency]
- private ChatManager _chatManager;
+ private IChatManagerServer _chatManager;
public CommandManager(ITorchBase torch, char prefix = '!') : base(torch)
{
@@ -31,7 +32,7 @@ namespace Torch.Commands
public override void Attach()
{
RegisterCommandModule(typeof(TorchCommands));
- _chatManager.MessageRecieved += HandleCommand;
+ _chatManager.MessageProcessing += HandleCommand;
}
public bool HasPermission(ulong steamId, Command command)
@@ -93,20 +94,21 @@ namespace Torch.Commands
return context.Response;
}
- public void HandleCommand(ChatMsg msg, ref bool sendToOthers)
+ public void HandleCommand(TorchChatMessage msg, ref bool consumed)
{
- HandleCommand(msg.Text, msg.Author, ref sendToOthers);
+ if (msg.AuthorSteamId.HasValue)
+ HandleCommand(msg.Message, msg.AuthorSteamId.Value, ref consumed);
}
- public void HandleCommand(string message, ulong steamId, ref bool sendToOthers, bool serverConsole = false)
+ public void HandleCommand(string message, ulong steamId, ref bool consumed, bool serverConsole = false)
{
if (message.Length < 1 || message[0] != Prefix)
return;
- sendToOthers = false;
+ consumed = true;
- var player = Torch.Multiplayer.GetPlayerBySteamId(steamId);
+ var player = Torch.GetManager().GetPlayerBySteamId(steamId);
if (player == null)
{
_log.Error($"Command {message} invoked by nonexistant player");
@@ -123,7 +125,7 @@ namespace Torch.Commands
if (!HasPermission(steamId, command))
{
_log.Info($"{player.DisplayName} tried to use command {cmdPath} without permission");
- Torch.Multiplayer.SendMessage($"You need to be a {command.MinimumPromoteLevel} or higher to use that command.", playerId: player.IdentityId);
+ _chatManager.SendMessageAsOther("Server", $"You need to be a {command.MinimumPromoteLevel} or higher to use that command.", MyFontEnum.Red, steamId);
return;
}
diff --git a/Torch/Commands/TorchCommands.cs b/Torch/Commands/TorchCommands.cs
index 0f604f0..5e7dbb6 100644
--- a/Torch/Commands/TorchCommands.cs
+++ b/Torch/Commands/TorchCommands.cs
@@ -21,7 +21,7 @@ namespace Torch.Commands
[Permission(MyPromoteLevel.None)]
public void Help()
{
- var commandManager = ((TorchBase)Context.Torch).Commands;
+ var commandManager = ((TorchBase)Context.Torch).CurrentSession.Managers.GetManager();
commandManager.Commands.GetNode(Context.Args, out CommandTree.CommandNode node);
if (node != null)
@@ -128,13 +128,15 @@ namespace Torch.Commands
{
if (i >= 60 && i % 60 == 0)
{
- Context.Torch.Multiplayer.SendMessage($"Restarting server in {i / 60} minute{Pluralize(i / 60)}.");
- yield return null;
+ // TODO
+// Context.Torch.Multiplayer.SendMessage($"Restarting server in {i / 60} minute{Pluralize(i / 60)}.");
+// yield return null;
}
else if (i > 0)
{
- if (i < 11)
- Context.Torch.Multiplayer.SendMessage($"Restarting server in {i} second{Pluralize(i)}.");
+ // TODO
+// if (i < 11)
+// Context.Torch.Multiplayer.SendMessage($"Restarting server in {i} second{Pluralize(i)}.");
yield return null;
}
else
diff --git a/Torch/Managers/ChatManager.cs b/Torch/Managers/ChatManager.cs
deleted file mode 100644
index 2d9216a..0000000
--- a/Torch/Managers/ChatManager.cs
+++ /dev/null
@@ -1,104 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using NLog;
-using Sandbox.Engine.Multiplayer;
-using Torch.API;
-using Torch.API.Managers;
-using VRage;
-using VRage.Library.Collections;
-using VRage.Network;
-using VRage.Serialization;
-using VRage.Utils;
-
-namespace Torch.Managers
-{
- [Manager]
- public class ChatManager : Manager
- {
- private static Logger _log = LogManager.GetLogger(nameof(ChatManager));
-
- public delegate void MessageRecievedDel(ChatMsg msg, ref bool sendToOthers);
-
- public event MessageRecievedDel MessageRecieved;
-
- internal void RaiseMessageRecieved(ChatMsg msg, ref bool sendToOthers) =>
- MessageRecieved?.Invoke(msg, ref sendToOthers);
-
- [Dependency]
- private INetworkManager _networkManager;
-
- public ChatManager(ITorchBase torchInstance) : base(torchInstance)
- {
-
- }
-
- public override void Attach()
- {
- try
- {
- _networkManager.RegisterNetworkHandler(new ChatIntercept(this));
- }
- catch
- {
- _log.Error("Failed to initialize network intercept, command hiding will not work! Falling back to another method.");
- MyMultiplayer.Static.ChatMessageReceived += Static_ChatMessageReceived;
- }
- }
-
- private void Static_ChatMessageReceived(ulong arg1, string arg2)
- {
- var msg = new ChatMsg {Author = arg1, Text = arg2};
- var sendToOthers = true;
-
- RaiseMessageRecieved(msg, ref sendToOthers);
- }
-
- internal class ChatIntercept : NetworkHandlerBase, INetworkHandler
- {
- private ChatManager _chatManager;
- private bool? _unitTestResult;
-
- public ChatIntercept(ChatManager chatManager)
- {
- _chatManager = chatManager;
- }
-
- public override bool CanHandle(CallSite site)
- {
- if (site.MethodInfo.Name != "OnChatMessageRecieved")
- return false;
-
- if (_unitTestResult.HasValue)
- return _unitTestResult.Value;
-
- var parameters = site.MethodInfo.GetParameters();
- if (parameters.Length != 1)
- {
- _unitTestResult = false;
- return false;
- }
-
- if (parameters[0].ParameterType != typeof(ChatMsg))
- _unitTestResult = false;
-
- _unitTestResult = true;
-
- return _unitTestResult.Value;
- }
-
- public override bool Handle(ulong remoteUserId, CallSite site, BitStream stream, object obj, MyPacket packet)
- {
- var msg = new ChatMsg();
- Serialize(site.MethodInfo, stream, ref msg);
-
- bool sendToOthers = true;
- _chatManager.RaiseMessageRecieved(msg, ref sendToOthers);
-
- return !sendToOthers;
- }
- }
- }
-}
diff --git a/Torch/Managers/ChatManager/ChatManagerClient.cs b/Torch/Managers/ChatManager/ChatManagerClient.cs
new file mode 100644
index 0000000..796b8de
--- /dev/null
+++ b/Torch/Managers/ChatManager/ChatManagerClient.cs
@@ -0,0 +1,144 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using NLog;
+using Sandbox.Engine.Multiplayer;
+using Sandbox.Engine.Networking;
+using Sandbox.Game.Entities.Character;
+using Sandbox.Game.Gui;
+using Sandbox.Game.World;
+using Torch.API;
+using Torch.API.Managers;
+using Torch.Utils;
+using VRage.Game;
+using Game = Sandbox.Engine.Platform.Game;
+
+namespace Torch.Managers.ChatManager
+{
+ public class ChatManagerClient : Manager, IChatManagerClient
+ {
+ private static readonly Logger _log = LogManager.GetCurrentClassLogger();
+
+ ///
+ public ChatManagerClient(ITorchBase torchInstance) : base(torchInstance) { }
+
+ ///
+ // TODO doesn't work in Offline worlds. Method injection or network injection.
+ public event DelMessageRecieved MessageRecieved;
+
+ ///
+ // TODO doesn't work at all. Method injection or network injection.
+ public event DelMessageSending MessageSending;
+
+ ///
+ public void SendMessageAsSelf(string message)
+ {
+ if (MyMultiplayer.Static != null)
+ {
+ if (Game.IsDedicated)
+ {
+ var scripted = new ScriptedChatMsg()
+ {
+ Author = "Server",
+ Font = MyFontEnum.Red,
+ Text = message,
+ Target = 0
+ };
+ MyMultiplayerBase.SendScriptedChatMessage(ref scripted);
+ }
+ else
+ MyMultiplayer.Static.SendChatMessage(message);
+ }
+ else if (MyHud.Chat != null)
+ MyHud.Chat.ShowMessage(MySession.Static.LocalHumanPlayer?.DisplayName ?? "Player", message);
+ }
+
+ ///
+ public void DisplayMessageOnSelf(string author, string message, string font)
+ {
+ MyHud.Chat.ShowMessage(author, message, font);
+ MySession.Static.GlobalChatHistory.GlobalChatHistory.Chat.Enqueue(new MyGlobalChatItem()
+ {
+ Author = author,
+ AuthorFont = font,
+ Text = message
+ });
+ }
+
+ ///
+ public override void Attach()
+ {
+ base.Attach();
+ if (MyMultiplayer.Static == null)
+ _log.Warn("Currently ChatManagerClient doesn't support handling on an offline client");
+ else
+ {
+ _chatMessageRecievedReplacer = _chatMessageReceivedFactory.Invoke();
+ _scriptedChatMessageRecievedReplacer = _scriptedChatMessageReceivedFactory.Invoke();
+ _chatMessageRecievedReplacer.Replace(new Action(Multiplayer_ChatMessageReceived),
+ MyMultiplayer.Static);
+ _scriptedChatMessageRecievedReplacer.Replace(
+ new Action(Multiplayer_ScriptedChatMessageReceived), MyMultiplayer.Static);
+ }
+ }
+
+ ///
+ public override void Detach()
+ {
+ if (_chatMessageRecievedReplacer != null && _chatMessageRecievedReplacer.Replaced)
+ _chatMessageRecievedReplacer.Restore(MyHud.Chat);
+ if (_scriptedChatMessageRecievedReplacer != null && _scriptedChatMessageRecievedReplacer.Replaced)
+ _scriptedChatMessageRecievedReplacer.Restore(MyHud.Chat);
+ base.Detach();
+ }
+
+ private void Multiplayer_ChatMessageReceived(ulong steamUserId, string message)
+ {
+ var torchMsg = new TorchChatMessage()
+ {
+ AuthorSteamId = steamUserId,
+ Author = Torch.CurrentSession.Managers.GetManager()
+ ?.GetSteamUsername(steamUserId),
+ Font = (steamUserId == MyGameService.UserId) ? "DarkBlue" : "Blue",
+ Message = message
+ };
+ var consumed = false;
+ MessageRecieved?.Invoke(torchMsg, ref consumed);
+ if (!consumed)
+ _hudChatMessageReceived.Invoke(MyHud.Chat, steamUserId, message);
+ }
+
+ private void Multiplayer_ScriptedChatMessageReceived(string author, string message, string font)
+ {
+ var torchMsg = new TorchChatMessage()
+ {
+ AuthorSteamId = null,
+ Author = author,
+ Font = font,
+ Message = message
+ };
+ var consumed = false;
+ MessageRecieved?.Invoke(torchMsg, ref consumed);
+ if (!consumed)
+ _hudChatScriptedMessageReceived.Invoke(MyHud.Chat, author, message, font);
+ }
+
+ private const string _hudChatMessageReceivedName = "Multiplayer_ChatMessageReceived";
+ private const string _hudChatScriptedMessageReceivedName = "multiplayer_ScriptedChatMessageReceived";
+
+ [ReflectedMethod(Name = _hudChatMessageReceivedName)]
+ private static Action _hudChatMessageReceived;
+ [ReflectedMethod(Name = _hudChatScriptedMessageReceivedName)]
+ private static Action _hudChatScriptedMessageReceived;
+
+ [ReflectedEventReplace(typeof(MyMultiplayerBase), nameof(MyMultiplayerBase.ChatMessageReceived), typeof(MyHudChat), _hudChatMessageReceivedName)]
+ private static Func _chatMessageReceivedFactory;
+ [ReflectedEventReplace(typeof(MyMultiplayerBase), nameof(MyMultiplayerBase.ScriptedChatMessageReceived), typeof(MyHudChat), _hudChatScriptedMessageReceivedName)]
+ private static Func _scriptedChatMessageReceivedFactory;
+
+ private ReflectedEventReplacer _chatMessageRecievedReplacer;
+ private ReflectedEventReplacer _scriptedChatMessageRecievedReplacer;
+ }
+}
diff --git a/Torch/Managers/ChatManager/ChatManagerServer.cs b/Torch/Managers/ChatManager/ChatManagerServer.cs
new file mode 100644
index 0000000..8f1a507
--- /dev/null
+++ b/Torch/Managers/ChatManager/ChatManagerServer.cs
@@ -0,0 +1,193 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using NLog;
+using Sandbox.Engine.Multiplayer;
+using Sandbox.Engine.Networking;
+using Sandbox.Game.Gui;
+using Sandbox.Game.Multiplayer;
+using Sandbox.Game.World;
+using Torch.API;
+using Torch.API.Managers;
+using Torch.Utils;
+using VRage;
+using VRage.Library.Collections;
+using VRage.Network;
+
+namespace Torch.Managers.ChatManager
+{
+ public class ChatManagerServer : ChatManagerClient, IChatManagerServer
+ {
+ [Dependency(Optional = true)]
+ private INetworkManager _networkManager;
+
+ private static readonly Logger _log = LogManager.GetCurrentClassLogger();
+
+ private readonly ChatIntercept _chatIntercept;
+
+ ///
+ public ChatManagerServer(ITorchBase torchInstance) : base(torchInstance)
+ {
+ _chatIntercept = new ChatIntercept(this);
+ }
+
+ ///
+ public event DelMessageProcessing MessageProcessing;
+
+ ///
+ public void SendMessageAsOther(ulong authorId, string message, ulong targetSteamId = 0)
+ {
+ if (MyMultiplayer.Static == null)
+ {
+ if (targetSteamId == MyGameService.UserId || targetSteamId == 0)
+ MyHud.Chat?.ShowMessage(authorId == MyGameService.UserId ?
+ (MySession.Static.LocalHumanPlayer?.DisplayName ?? "Player") : $"user_{authorId}", message);
+ return;
+ }
+ if (MyMultiplayer.Static is MyDedicatedServerBase dedicated)
+ {
+ var msg = new ChatMsg() { Author = authorId, Text = message };
+ _dedicatedServerBaseSendChatMessage.Invoke(ref msg);
+ _dedicatedServerBaseOnChatMessage.Invoke(dedicated, new object[] { msg });
+
+ }
+ }
+
+
+#pragma warning disable 649
+ private delegate void DelMultiplayerBaseSendChatMessage(ref ChatMsg arg);
+ [ReflectedStaticMethod(Name = "SendChatMessage", Type = typeof(MyMultiplayerBase))]
+ private static DelMultiplayerBaseSendChatMessage _dedicatedServerBaseSendChatMessage;
+
+ // [ReflectedMethod] doesn't play well with instance methods and refs.
+ [ReflectedMethodInfo(typeof(MyDedicatedServerBase), "OnChatMessage")]
+ private static MethodInfo _dedicatedServerBaseOnChatMessage;
+#pragma warning restore 649
+
+ ///
+ public void SendMessageAsOther(string author, string message, string font, ulong targetSteamId = 0)
+ {
+ if (MyMultiplayer.Static == null)
+ {
+ if (targetSteamId == MyGameService.UserId || targetSteamId == 0)
+ MyHud.Chat?.ShowMessage(author, message, font);
+ return;
+ }
+ var scripted = new ScriptedChatMsg()
+ {
+ Author = author,
+ Text = message,
+ Font = font,
+ Target = (long)targetSteamId
+ };
+ MyMultiplayerBase.SendScriptedChatMessage(ref scripted);
+ }
+
+
+ ///
+ public override void Attach()
+ {
+ base.Attach();
+ if (_networkManager != null)
+ try
+ {
+ _networkManager.RegisterNetworkHandler(_chatIntercept);
+ _log.Debug("Initialized network intercept for chat messages");
+ return;
+ }
+ catch
+ {
+ // Discard exception and use second method
+ }
+
+ if (MyMultiplayer.Static != null)
+ {
+ MyMultiplayer.Static.ChatMessageReceived += MpStaticChatMessageReceived;
+ _log.Warn("Failed to initialize network intercept, we can't discard chat messages! Falling back to another method.");
+ }
+ else
+ _log.Error("Failed to initialize network intercept, we can't discard chat messages!");
+ }
+
+ private void MpStaticChatMessageReceived(ulong a, string b)
+ {
+ var tmp = false;
+ RaiseMessageRecieved(new ChatMsg()
+ {
+ Author = a,
+ Text = b
+ }, ref tmp);
+ }
+
+ ///
+ public override void Detach()
+ {
+ if (MyMultiplayer.Static != null)
+ MyMultiplayer.Static.ChatMessageReceived -= MpStaticChatMessageReceived;
+ _networkManager?.UnregisterNetworkHandler(_chatIntercept);
+ base.Detach();
+ }
+
+ internal void RaiseMessageRecieved(ChatMsg message, ref bool consumed)
+ {
+ var torchMsg = new TorchChatMessage()
+ {
+ AuthorSteamId = message.Author,
+ Author = MyMultiplayer.Static?.GetMemberName(message.Author) ?? $"user_{message.Author}",
+ Message = message.Text
+ };
+ MessageProcessing?.Invoke(torchMsg, ref consumed);
+ }
+
+ internal class ChatIntercept : NetworkHandlerBase, INetworkHandler
+ {
+ private readonly ChatManagerServer _chatManager;
+ private bool? _unitTestResult;
+
+ public ChatIntercept(ChatManagerServer chatManager)
+ {
+ _chatManager = chatManager;
+ }
+
+ ///
+ public override bool CanHandle(CallSite site)
+ {
+ if (site.MethodInfo.Name != "OnChatMessageRecieved")
+ return false;
+
+ if (_unitTestResult.HasValue)
+ return _unitTestResult.Value;
+
+ ParameterInfo[] parameters = site.MethodInfo.GetParameters();
+ if (parameters.Length != 1)
+ {
+ _unitTestResult = false;
+ return false;
+ }
+
+ if (parameters[0].ParameterType != typeof(ChatMsg))
+ _unitTestResult = false;
+
+ _unitTestResult = true;
+
+ return _unitTestResult.Value;
+ }
+
+ ///
+ public override bool Handle(ulong remoteUserId, CallSite site, BitStream stream, object obj, MyPacket packet)
+ {
+ var msg = new ChatMsg();
+ Serialize(site.MethodInfo, stream, ref msg);
+
+ var consumed = false;
+ _chatManager.RaiseMessageRecieved(msg, ref consumed);
+
+ return consumed;
+ }
+ }
+ }
+}
diff --git a/Torch/Managers/MultiplayerManager.cs b/Torch/Managers/MultiplayerManager.cs
deleted file mode 100644
index ac1f09f..0000000
--- a/Torch/Managers/MultiplayerManager.cs
+++ /dev/null
@@ -1,338 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.Linq;
-using System.Reflection;
-using System.Runtime.CompilerServices;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
-using System.Windows;
-using System.Windows.Data;
-using System.Windows.Threading;
-using NLog;
-using Torch;
-using Sandbox;
-using Sandbox.Engine.Multiplayer;
-using Sandbox.Engine.Networking;
-using Sandbox.Game.Entities.Character;
-using Sandbox.Game.Multiplayer;
-using Sandbox.Game.World;
-using Sandbox.ModAPI;
-using SteamSDK;
-using Torch.API;
-using Torch.API.Managers;
-using Torch.Collections;
-using Torch.Commands;
-using Torch.Utils;
-using Torch.ViewModels;
-using VRage.Game;
-using VRage.Game.ModAPI;
-using VRage.GameServices;
-using VRage.Library.Collections;
-using VRage.Network;
-using VRage.Steam;
-using VRage.Utils;
-
-namespace Torch.Managers
-{
- ///
- public class MultiplayerManager : Manager, IMultiplayerManager
- {
- ///
- public event Action PlayerJoined;
- ///
- public event Action PlayerLeft;
- ///
- public event MessageReceivedDel MessageReceived;
-
- public IList ChatHistory { get; } = new ObservableList();
- public ObservableDictionary Players { get; } = new ObservableDictionary();
- public IMyPlayer LocalPlayer => MySession.Static.LocalHumanPlayer;
- private static readonly Logger Log = LogManager.GetLogger(nameof(MultiplayerManager));
- private static readonly Logger ChatLog = LogManager.GetLogger("Chat");
-
- [ReflectedGetter(Name = "m_players")]
- private static Func> _onlinePlayers;
-
- [Dependency]
- private ChatManager _chatManager;
- [Dependency]
- private CommandManager _commandManager;
- [Dependency]
- private NetworkManager _networkManager;
-
- internal MultiplayerManager(ITorchBase torch) : base(torch)
- {
-
- }
-
- ///
- public override void Attach()
- {
- Torch.SessionLoaded += OnSessionLoaded;
- _chatManager.MessageRecieved += Instance_MessageRecieved;
- }
-
- private void Instance_MessageRecieved(ChatMsg msg, ref bool sendToOthers)
- {
- var message = ChatMessage.FromChatMsg(msg);
- ChatHistory.Add(message);
- ChatLog.Info($"{message.Name}: {message.Message}");
- MessageReceived?.Invoke(message, ref sendToOthers);
- }
-
- ///
- public void KickPlayer(ulong steamId) => Torch.Invoke(() => MyMultiplayer.Static.KickClient(steamId));
-
- ///
- public void BanPlayer(ulong steamId, bool banned = true)
- {
- Torch.Invoke(() =>
- {
- MyMultiplayer.Static.BanClient(steamId, banned);
- if (_gameOwnerIds.ContainsKey(steamId))
- MyMultiplayer.Static.BanClient(_gameOwnerIds[steamId], banned);
- });
- }
-
- ///
- public IMyPlayer GetPlayerByName(string name)
- {
- return _onlinePlayers.Invoke(MySession.Static.Players).FirstOrDefault(x => x.Value.DisplayName == name).Value;
- }
-
- ///
- public IMyPlayer GetPlayerBySteamId(ulong steamId)
- {
- _onlinePlayers.Invoke(MySession.Static.Players).TryGetValue(new MyPlayer.PlayerId(steamId), out MyPlayer p);
- return p;
- }
-
- public ulong GetSteamId(long identityId)
- {
- foreach (var kv in _onlinePlayers.Invoke(MySession.Static.Players))
- {
- if (kv.Value.Identity.IdentityId == identityId)
- return kv.Key.SteamId;
- }
-
- return 0;
- }
-
- ///
- public string GetSteamUsername(ulong steamId)
- {
- return MyMultiplayer.Static.GetMemberName(steamId);
- }
-
- ///
- public void SendMessage(string message, string author = "Server", long playerId = 0, string font = MyFontEnum.Red)
- {
- if (string.IsNullOrEmpty(message))
- return;
-
- ChatHistory.Add(new ChatMessage(DateTime.Now, 0, author, message));
- if (_commandManager.IsCommand(message))
- {
- var response = _commandManager.HandleCommandFromServer(message);
- ChatHistory.Add(new ChatMessage(DateTime.Now, 0, author, response));
- }
- else
- {
- var msg = new ScriptedChatMsg { Author = author, Font = font, Target = playerId, Text = message };
- MyMultiplayerBase.SendScriptedChatMessage(ref msg);
- var character = MySession.Static.Players.TryGetIdentity(playerId)?.Character;
- var steamId = GetSteamId(playerId);
- if (character == null)
- return;
-
- var addToGlobalHistoryMethod = typeof(MyCharacter).GetMethod("OnGlobalMessageSuccess", BindingFlags.Instance | BindingFlags.NonPublic);
- _networkManager.RaiseEvent(addToGlobalHistoryMethod, character, steamId, steamId, message);
- }
- }
-
- private void OnSessionLoaded()
- {
- Log.Info("Initializing Steam auth");
- MyMultiplayer.Static.ClientKicked += OnClientKicked;
- MyMultiplayer.Static.ClientLeft += OnClientLeft;
-
- //TODO: Move these with the methods?
- if (!RemoveHandlers())
- {
- Log.Error("Steam auth failed to initialize");
- return;
- }
- MyGameService.GameServer.ValidateAuthTicketResponse += ValidateAuthTicketResponse;
- MyGameService.GameServer.UserGroupStatusResponse += UserGroupStatusResponse;
- Log.Info("Steam auth initialized");
- }
-
- private void OnClientKicked(ulong steamId)
- {
- OnClientLeft(steamId, MyChatMemberStateChangeEnum.Kicked);
- }
-
- private void OnClientLeft(ulong steamId, MyChatMemberStateChangeEnum stateChange)
- {
- Players.TryGetValue(steamId, out PlayerViewModel vm);
- if (vm == null)
- vm = new PlayerViewModel(steamId);
- Log.Info($"{vm.Name} ({vm.SteamId}) {(ConnectionState)stateChange}.");
- PlayerLeft?.Invoke(vm);
- Players.Remove(steamId);
- }
-
- //TODO: Split the following into a new file?
- //These methods override some Keen code to allow us full control over client authentication.
- //This lets us have a server set to private (admins only) or friends (friends of all listed admins)
- [ReflectedGetter(Name = "m_members")]
- private static Func> _members;
- [ReflectedGetter(Name = "m_waitingForGroup")]
- private static Func> _waitingForGroup;
- [ReflectedGetter(Name = "m_kickedClients")]
- private static Func> _kickedClients;
- //private HashSet _waitingForFriends;
- private Dictionary _gameOwnerIds = new Dictionary();
- //private IMultiplayer _multiplayerImplementation;
-
- ///
- /// Removes Keen's hooks into some Steam events so we have full control over client authentication
- ///
- private static bool RemoveHandlers()
- {
- MethodInfo methodValidateAuthTicket = typeof(MyDedicatedServerBase).GetMethod("GameServer_ValidateAuthTicketResponse",
- BindingFlags.NonPublic | BindingFlags.Instance);
- if (methodValidateAuthTicket == null)
- {
- Log.Error("Unable to find the GameServer_ValidateAuthTicketResponse method to unhook");
- return false;
- }
- var eventValidateAuthTicket = Reflection.GetInstanceEvent(MyGameService.GameServer, nameof(MyGameService.GameServer.ValidateAuthTicketResponse))
- .FirstOrDefault(x => x.Method == methodValidateAuthTicket) as Action;
- if (eventValidateAuthTicket == null)
- {
- Log.Error(
- "Unable to unhook the GameServer_ValidateAuthTicketResponse method from GameServer.ValidateAuthTicketResponse");
- Log.Debug(" Want to unhook {0}", methodValidateAuthTicket);
- Log.Debug(" Registered handlers: ");
- foreach (Delegate method in Reflection.GetInstanceEvent(MyGameService.GameServer,
- nameof(MyGameService.GameServer.ValidateAuthTicketResponse)))
- Log.Debug(" - " + method.Method);
- return false;
- }
-
- MethodInfo methodUserGroupStatus = typeof(MyDedicatedServerBase).GetMethod("GameServer_UserGroupStatus",
- BindingFlags.NonPublic | BindingFlags.Instance);
- if (methodUserGroupStatus == null)
- {
- Log.Error("Unable to find the GameServer_UserGroupStatus method to unhook");
- return false;
- }
- var eventUserGroupStatus = Reflection.GetInstanceEvent(MyGameService.GameServer, nameof(MyGameService.GameServer.UserGroupStatusResponse))
- .FirstOrDefault(x => x.Method == methodUserGroupStatus)
- as Action;
- if (eventUserGroupStatus == null)
- {
- Log.Error("Unable to unhook the GameServer_UserGroupStatus method from GameServer.UserGroupStatus");
- Log.Debug(" Want to unhook {0}", methodUserGroupStatus);
- Log.Debug(" Registered handlers: ");
- foreach (Delegate method in Reflection.GetInstanceEvent(MyGameService.GameServer, nameof(MyGameService.GameServer.UserGroupStatusResponse)))
- Log.Debug(" - " + method.Method);
- return false;
- }
-
- MyGameService.GameServer.ValidateAuthTicketResponse -=
- eventValidateAuthTicket;
- MyGameService.GameServer.UserGroupStatusResponse -=
- eventUserGroupStatus;
- return true;
- }
-
- //Largely copied from SE
- private void ValidateAuthTicketResponse(ulong steamID, JoinResult response, ulong steamOwner)
- {
- Log.Debug($"ValidateAuthTicketResponse(user={steamID}, response={response}, owner={steamOwner}");
- if (IsClientBanned.Invoke(MyMultiplayer.Static, steamOwner) || MySandboxGame.ConfigDedicated.Banned.Contains(steamOwner))
- {
- UserRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.BannedByAdmins);
- RaiseClientKicked.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID);
- }
- else if (IsClientKicked.Invoke(MyMultiplayer.Static, steamOwner))
- {
- UserRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.KickedRecently);
- RaiseClientKicked.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID);
- }
- if (response != JoinResult.OK)
- {
- UserRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, response);
- return;
- }
- if (MyMultiplayer.Static.MemberLimit > 0 && _members.Invoke((MyDedicatedServerBase)MyMultiplayer.Static).Count - 1 >= MyMultiplayer.Static.MemberLimit)
- {
- UserRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.ServerFull);
- return;
- }
- if (MySandboxGame.ConfigDedicated.GroupID == 0uL ||
- MySandboxGame.ConfigDedicated.Administrators.Contains(steamID.ToString()) ||
- MySandboxGame.ConfigDedicated.Administrators.Contains(ConvertSteamIDFrom64(steamID)))
- {
- this.UserAccepted(steamID);
- return;
- }
- if (GetServerAccountType(MySandboxGame.ConfigDedicated.GroupID) != MyGameServiceAccountType.Clan)
- {
- UserRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.GroupIdInvalid);
- return;
- }
- if (MyGameService.GameServer.RequestGroupStatus(steamID, MySandboxGame.ConfigDedicated.GroupID))
- {
- _waitingForGroup.Invoke((MyDedicatedServerBase)MyMultiplayer.Static).Add(steamID);
- return;
- }
- UserRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamID, JoinResult.SteamServersOffline);
- }
-
- private void UserGroupStatusResponse(ulong userId, ulong groupId, bool member, bool officer)
- {
- if (groupId == MySandboxGame.ConfigDedicated.GroupID && _waitingForGroup.Invoke((MyDedicatedServerBase)MyMultiplayer.Static).Remove(userId))
- {
- if (member || officer)
- UserAccepted(userId);
- else
- UserRejected.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, userId, JoinResult.NotInGroup);
- }
- }
-
- private void UserAccepted(ulong steamId)
- {
- UserAcceptedImpl.Invoke((MyDedicatedServerBase)MyMultiplayer.Static, steamId);
-
- var vm = new PlayerViewModel(steamId) { State = ConnectionState.Connected };
- Log.Info($"Player {vm.Name} joined ({vm.SteamId})");
- Players.Add(steamId, vm);
- PlayerJoined?.Invoke(vm);
- }
-
- [ReflectedStaticMethod(Type = typeof(MyDedicatedServerBase))]
- private static Func ConvertSteamIDFrom64;
-
- [ReflectedStaticMethod(Type = typeof(MyGameService))]
- private static Func GetServerAccountType;
-
- [ReflectedMethod(Name = "UserAccepted")]
- private static Action UserAcceptedImpl;
-
- [ReflectedMethod]
- private static Action UserRejected;
- [ReflectedMethod]
- private static Func IsClientBanned;
- [ReflectedMethod]
- private static Func IsClientKicked;
- [ReflectedMethod]
- private static Action RaiseClientKicked;
- }
-}
diff --git a/Torch/Managers/MultiplayerManagerBase.cs b/Torch/Managers/MultiplayerManagerBase.cs
new file mode 100644
index 0000000..eaad039
--- /dev/null
+++ b/Torch/Managers/MultiplayerManagerBase.cs
@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Diagnostics;
+using System.Linq;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Data;
+using System.Windows.Threading;
+using NLog;
+using Torch;
+using Sandbox;
+using Sandbox.Engine.Multiplayer;
+using Sandbox.Engine.Networking;
+using Sandbox.Game.Entities.Character;
+using Sandbox.Game.Multiplayer;
+using Sandbox.Game.World;
+using Sandbox.ModAPI;
+using SteamSDK;
+using Torch.API;
+using Torch.API.Managers;
+using Torch.Collections;
+using Torch.Commands;
+using Torch.Utils;
+using Torch.ViewModels;
+using VRage.Game;
+using VRage.Game.ModAPI;
+using VRage.GameServices;
+using VRage.Library.Collections;
+using VRage.Network;
+using VRage.Steam;
+using VRage.Utils;
+
+namespace Torch.Managers
+{
+ ///
+ public abstract class MultiplayerManagerBase : Manager, IMultiplayerManagerBase
+ {
+ private static readonly Logger _log = LogManager.GetCurrentClassLogger();
+
+ ///
+ public event Action PlayerJoined;
+ ///
+ public event Action PlayerLeft;
+
+ public ObservableDictionary Players { get; } = new ObservableDictionary();
+
+#pragma warning disable 649
+ [ReflectedGetter(Name = "m_players")]
+ private static Func> _onlinePlayers;
+#pragma warning restore 649
+
+ protected MultiplayerManagerBase(ITorchBase torch) : base(torch)
+ {
+
+ }
+
+ ///
+ public override void Attach()
+ {
+ MyMultiplayer.Static.ClientLeft += OnClientLeft;
+ }
+
+ ///
+ public override void Detach()
+ {
+ MyMultiplayer.Static.ClientLeft -= OnClientLeft;
+ }
+
+ ///
+ public IMyPlayer GetPlayerByName(string name)
+ {
+ return _onlinePlayers.Invoke(MySession.Static.Players).FirstOrDefault(x => x.Value.DisplayName == name).Value;
+ }
+
+ ///
+ public IMyPlayer GetPlayerBySteamId(ulong steamId)
+ {
+ _onlinePlayers.Invoke(MySession.Static.Players).TryGetValue(new MyPlayer.PlayerId(steamId), out MyPlayer p);
+ return p;
+ }
+
+ public ulong GetSteamId(long identityId)
+ {
+ foreach (KeyValuePair kv in _onlinePlayers.Invoke(MySession.Static.Players))
+ {
+ if (kv.Value.Identity.IdentityId == identityId)
+ return kv.Key.SteamId;
+ }
+
+ return 0;
+ }
+
+ ///
+ public string GetSteamUsername(ulong steamId)
+ {
+ return MyMultiplayer.Static.GetMemberName(steamId);
+ }
+
+ private void OnClientLeft(ulong steamId, MyChatMemberStateChangeEnum stateChange)
+ {
+ Players.TryGetValue(steamId, out PlayerViewModel vm);
+ if (vm == null)
+ vm = new PlayerViewModel(steamId);
+ _log.Info($"{vm.Name} ({vm.SteamId}) {(ConnectionState)stateChange}.");
+ PlayerLeft?.Invoke(vm);
+ Players.Remove(steamId);
+ }
+
+ protected void RaiseClientJoined(ulong steamId)
+ {
+ var vm = new PlayerViewModel(steamId){State=ConnectionState.Connected};
+ _log.Info($"Plat {vm.Name} joined ({vm.SteamId}");
+ Players.Add(steamId, vm);
+ PlayerJoined?.Invoke(vm);
+ }
+ }
+}
diff --git a/Torch/Managers/NetworkManager/NetworkManager.cs b/Torch/Managers/NetworkManager/NetworkManager.cs
index 91dff87..743f319 100644
--- a/Torch/Managers/NetworkManager/NetworkManager.cs
+++ b/Torch/Managers/NetworkManager/NetworkManager.cs
@@ -20,9 +20,9 @@ namespace Torch.Managers
{
private static Logger _log = LogManager.GetLogger(nameof(NetworkManager));
- private const string MyTransportLayerField = "TransportLayer";
- private const string TransportHandlersField = "m_handlers";
- private HashSet _networkHandlers = new HashSet();
+ private const string _myTransportLayerField = "TransportLayer";
+ private const string _transportHandlersField = "m_handlers";
+ private readonly HashSet _networkHandlers = new HashSet();
private bool _init;
[ReflectedGetter(Name = "m_typeTable")]
@@ -40,14 +40,14 @@ namespace Torch.Managers
try
{
var syncLayerType = typeof(MySyncLayer);
- var transportLayerField = syncLayerType.GetField(MyTransportLayerField, BindingFlags.NonPublic | BindingFlags.Instance);
+ var transportLayerField = syncLayerType.GetField(_myTransportLayerField, BindingFlags.NonPublic | BindingFlags.Instance);
if (transportLayerField == null)
throw new TypeLoadException("Could not find internal type for TransportLayer");
var transportLayerType = transportLayerField.FieldType;
- if (!Reflection.HasField(transportLayerType, TransportHandlersField))
+ if (!Reflection.HasField(transportLayerType, _transportHandlersField))
throw new TypeLoadException("Could not find Handlers field");
return true;
@@ -79,9 +79,9 @@ namespace Torch.Managers
throw new InvalidOperationException("Reflection unit test failed.");
//don't bother with nullchecks here, it was all handled in ReflectionUnitTest
- var transportType = typeof(MySyncLayer).GetField(MyTransportLayerField, BindingFlags.NonPublic | BindingFlags.Instance).FieldType;
- var transportInstance = typeof(MySyncLayer).GetField(MyTransportLayerField, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(MyMultiplayer.Static.SyncLayer);
- var handlers = (IDictionary)transportType.GetField(TransportHandlersField, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(transportInstance);
+ var transportType = typeof(MySyncLayer).GetField(_myTransportLayerField, BindingFlags.NonPublic | BindingFlags.Instance).FieldType;
+ var transportInstance = typeof(MySyncLayer).GetField(_myTransportLayerField, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(MyMultiplayer.Static.SyncLayer);
+ var handlers = (IDictionary)transportType.GetField(_transportHandlersField, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(transportInstance);
var handlerTypeField = handlers.GetType().GenericTypeArguments[0].GetField("messageId"); //Should be MyTransportLayer.HandlerId
object id = null;
foreach (var key in handlers.Keys)
@@ -205,6 +205,8 @@ namespace Torch.Managers
}
}
+
+ ///
public void RegisterNetworkHandler(INetworkHandler handler)
{
var handlerType = handler.GetType().FullName;
@@ -225,6 +227,12 @@ namespace Torch.Managers
_networkHandlers.Add(handler);
}
+ ///
+ public bool UnregisterNetworkHandler(INetworkHandler handler)
+ {
+ return _networkHandlers.Remove(handler);
+ }
+
public void RegisterNetworkHandlers(params INetworkHandler[] handlers)
{
foreach (var handler in handlers)
diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj
index 77002b8..4b7be00 100644
--- a/Torch/Torch.csproj
+++ b/Torch/Torch.csproj
@@ -155,6 +155,8 @@
+
+
@@ -172,13 +174,12 @@
-
-
+
diff --git a/Torch/TorchBase.cs b/Torch/TorchBase.cs
index 3a40a23..cc88c84 100644
--- a/Torch/TorchBase.cs
+++ b/Torch/TorchBase.cs
@@ -10,6 +10,7 @@ using System.Threading;
using System.Threading.Tasks;
using NLog;
using Sandbox;
+using Sandbox.Engine.Multiplayer;
using Sandbox.Game;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.Screens.Helpers;
@@ -22,10 +23,12 @@ using Torch.API.ModAPI;
using Torch.API.Session;
using Torch.Commands;
using Torch.Managers;
+using Torch.Managers.ChatManager;
using Torch.Utils;
using Torch.Session;
using VRage.Collections;
using VRage.FileSystem;
+using VRage.Game;
using VRage.Game.ObjectBuilder;
using VRage.ObjectBuilders;
using VRage.Plugins;
@@ -67,18 +70,6 @@ namespace Torch
///
[Obsolete("Use GetManager() or the [Dependency] attribute.")]
public IPluginManager Plugins { get; protected set; }
- ///
- [Obsolete("Use GetManager() or the [Dependency] attribute.")]
- public IMultiplayerManager Multiplayer { get; protected set; }
- ///
- [Obsolete("Use GetManager() or the [Dependency] attribute.")]
- public EntityManager Entities { get; protected set; }
- ///
- [Obsolete("Use GetManager() or the [Dependency] attribute.")]
- public INetworkManager Network { get; protected set; }
- ///
- [Obsolete("Use GetManager() or the [Dependency] attribute.")]
- public CommandManager Commands { get; protected set; }
///
public ITorchSession CurrentSession => Managers?.GetManager()?.CurrentSession;
@@ -120,31 +111,28 @@ namespace Torch
Managers = new DependencyManager();
Plugins = new PluginManager(this);
- Multiplayer = new MultiplayerManager(this);
- Entities = new EntityManager(this);
- Network = new NetworkManager(this);
- Commands = new CommandManager(this);
- Managers.AddManager(new TorchSessionManager(this));
+ var sessionManager = new TorchSessionManager(this);
+ sessionManager.AddFactory((x) => MyMultiplayer.Static?.SyncLayer != null ? new NetworkManager(this) : null);
+ sessionManager.AddFactory((x) => Sync.IsServer ? new ChatManagerServer(this) : new ChatManagerClient(this));
+ sessionManager.AddFactory((x) => Sync.IsServer ? new CommandManager(this) : null);
+ sessionManager.AddFactory((x) => new EntityManager(this));
+
+ Managers.AddManager(sessionManager);
Managers.AddManager(new FilesystemManager(this));
Managers.AddManager(new UpdateManager(this));
- Managers.AddManager(Network);
- Managers.AddManager(Commands);
Managers.AddManager(Plugins);
- Managers.AddManager(Multiplayer);
- Managers.AddManager(Entities);
- Managers.AddManager(new ChatManager(this));
TorchAPI.Instance = this;
}
- ///
+ [Obsolete("Prefer using Managers.GetManager for global managers")]
public T GetManager() where T : class, IManager
{
return Managers.GetManager();
}
- ///
+ [Obsolete("Prefer using Managers.AddManager for global managers")]
public bool AddManager(T manager) where T : class, IManager
{
return Managers.AddManager(manager);
@@ -254,10 +242,13 @@ namespace Torch
Debug.Assert(MyPerGameSettings.BasicGameInfo.GameVersion != null, "MyPerGameSettings.BasicGameInfo.GameVersion != null");
GameVersion = new Version(new MyVersion(MyPerGameSettings.BasicGameInfo.GameVersion.Value).FormattedText.ToString().Replace("_", "."));
- try { Console.Title = $"{Config.InstanceName} - Torch {TorchVersion}, SE {GameVersion}"; }
+ try
+ {
+ Console.Title = $"{Config.InstanceName} - Torch {TorchVersion}, SE {GameVersion}";
+ }
catch
{
- ///Running as service
+ // Running as service
}
#if DEBUG
diff --git a/Torch/Utils/ReflectedManager.cs b/Torch/Utils/ReflectedManager.cs
index ce27f93..2f52507 100644
--- a/Torch/Utils/ReflectedManager.cs
+++ b/Torch/Utils/ReflectedManager.cs
@@ -25,6 +25,71 @@ namespace Torch.Utils
public Type Type { get; set; } = null;
}
+ #region MemberInfoAttributes
+ ///
+ /// Indicates that this field should contain the instance for the given field.
+ ///
+ [AttributeUsage(AttributeTargets.Field)]
+ public class ReflectedFieldInfoAttribute : ReflectedMemberAttribute
+ {
+ ///
+ /// Creates a reflected field info attribute using the given type and name.
+ ///
+ /// Type that contains the member
+ /// Name of the member
+ public ReflectedFieldInfoAttribute(Type type, string name)
+ {
+ Type = type;
+ Name = name;
+ }
+ }
+
+ ///
+ /// Indicates that this field should contain the instance for the given method.
+ ///
+ [AttributeUsage(AttributeTargets.Field)]
+ public class ReflectedMethodInfoAttribute : ReflectedMemberAttribute
+ {
+ ///
+ /// Creates a reflected method info attribute using the given type and name.
+ ///
+ /// Type that contains the member
+ /// Name of the member
+ public ReflectedMethodInfoAttribute(Type type, string name)
+ {
+ Type = type;
+ Name = name;
+ }
+ ///
+ /// Expected parameters of this method, or null if any parameters are accepted.
+ ///
+ public Type[] Parameters { get; set; } = null;
+ ///
+ /// Expected return type of this method, or null if any return type is accepted.
+ ///
+ public Type ReturnType { get; set; } = null;
+ }
+
+ ///
+ /// Indicates that this field should contain the instance for the given property.
+ ///
+ [AttributeUsage(AttributeTargets.Field)]
+ public class ReflectedPropertyInfoAttribute : ReflectedMemberAttribute
+ {
+ ///
+ /// Creates a reflected property info attribute using the given type and name.
+ ///
+ /// Type that contains the member
+ /// Name of the member
+ public ReflectedPropertyInfoAttribute(Type type, string name)
+ {
+ Type = type;
+ Name = name;
+ }
+ }
+ #endregion
+
+ #region FieldPropGetSet
///
/// Indicates that this field should contain a delegate capable of retrieving the value of a field.
///
@@ -72,7 +137,9 @@ namespace Torch.Utils
public class ReflectedSetterAttribute : ReflectedMemberAttribute
{
}
+ #endregion
+ #region Invoker
///
/// Indicates that this field should contain a delegate capable of invoking an instance method.
///
@@ -116,6 +183,184 @@ namespace Torch.Utils
public class ReflectedStaticMethodAttribute : ReflectedMethodAttribute
{
}
+ #endregion
+
+ #region EventReplacer
+ ///
+ /// Instance of statefully replacing and restoring the callbacks of an event.
+ ///
+ public class ReflectedEventReplacer
+ {
+ private const BindingFlags BindFlagAll = BindingFlags.Static |
+ BindingFlags.Instance |
+ BindingFlags.Public |
+ BindingFlags.NonPublic;
+
+ private object _instance;
+ private Func> _backingStoreReader;
+ private Action _callbackAdder;
+ private Action _callbackRemover;
+ private readonly ReflectedEventReplaceAttribute _attributes;
+ private readonly HashSet _registeredCallbacks = new HashSet();
+ private readonly MethodInfo _targetMethodInfo;
+
+ internal ReflectedEventReplacer(ReflectedEventReplaceAttribute attr)
+ {
+ _attributes = attr;
+ FieldInfo backingStore = GetEventBackingField(attr.EventName, attr.EventDeclaringType);
+ if (backingStore == null)
+ throw new ArgumentException($"Unable to find backing field for event {attr.EventDeclaringType.FullName}#{attr.EventName}");
+ EventInfo evtInfo = ReflectedManager.GetFieldPropRecursive(attr.EventDeclaringType, attr.EventName, BindFlagAll, (a, b, c) => a.GetEvent(b, c));
+ if (evtInfo == null)
+ throw new ArgumentException($"Unable to find event info for event {attr.EventDeclaringType.FullName}#{attr.EventName}");
+ _backingStoreReader = () => GetEventsInternal(_instance, backingStore);
+ _callbackAdder = (x) => evtInfo.AddEventHandler(_instance, x);
+ _callbackRemover = (x) => evtInfo.RemoveEventHandler(_instance, x);
+ if (attr.TargetParameters == null)
+ {
+ _targetMethodInfo = attr.TargetDeclaringType.GetMethod(attr.TargetName, BindFlagAll);
+ if (_targetMethodInfo == null)
+ throw new ArgumentException($"Unable to find method {attr.TargetDeclaringType.FullName}#{attr.TargetName} to replace");
+ }
+ else
+ {
+ _targetMethodInfo =
+ attr.TargetDeclaringType.GetMethod(attr.TargetName, BindFlagAll, null, attr.TargetParameters, null);
+ if (_targetMethodInfo == null)
+ throw new ArgumentException($"Unable to find method {attr.TargetDeclaringType.FullName}#{attr.TargetName}){string.Join(", ", attr.TargetParameters.Select(x => x.FullName))}) to replace");
+ }
+ }
+
+ ///
+ /// Test that this replacement can be performed.
+ ///
+ /// The instance to operate on, or null if static
+ /// true if possible, false if unsuccessful
+ public bool Test(object instance)
+ {
+ _instance = instance;
+ _registeredCallbacks.Clear();
+ foreach (Delegate callback in _backingStoreReader.Invoke())
+ if (callback.Method == _targetMethodInfo)
+ _registeredCallbacks.Add(callback);
+
+ return _registeredCallbacks.Count > 0;
+ }
+
+ private Delegate _newCallback;
+
+ ///
+ /// Removes the target callback defined in the attribute and replaces it with the provided callback.
+ ///
+ /// The new event callback
+ /// The instance to operate on, or null if static
+ public void Replace(Delegate newCallback, object instance)
+ {
+ _instance = instance;
+ if (_newCallback != null)
+ throw new Exception("Reflected event replacer is in invalid state: Replace when already replaced");
+ _newCallback = newCallback;
+ Test(instance);
+ if (_registeredCallbacks.Count == 0)
+ throw new Exception("Reflected event replacer is in invalid state: Nothing to replace");
+ foreach (Delegate callback in _registeredCallbacks)
+ _callbackRemover.Invoke(callback);
+ _callbackAdder.Invoke(_newCallback);
+ }
+
+ ///
+ /// Checks if the callback is currently replaced
+ ///
+ public bool Replaced => _newCallback != null;
+
+ ///
+ /// Removes the callback added by and puts the original callback back.
+ ///
+ /// The instance to operate on, or null if static
+ public void Restore(object instance)
+ {
+ _instance = instance;
+ if (_newCallback == null)
+ throw new Exception("Reflected event replacer is in invalid state: Restore when not replaced");
+ _callbackRemover.Invoke(_newCallback);
+ foreach (Delegate callback in _registeredCallbacks)
+ _callbackAdder.Invoke(callback);
+ _newCallback = null;
+ }
+
+
+ private static readonly string[] _backingFieldForEvent = { "{0}", "{0}" };
+
+ private static FieldInfo GetEventBackingField(string eventName, Type baseType)
+ {
+ FieldInfo eventField = null;
+ Type type = baseType;
+ while (type != null && eventField == null)
+ {
+ for (var i = 0; i < _backingFieldForEvent.Length && eventField == null; i++)
+ eventField = type.GetField(string.Format(_backingFieldForEvent[i], eventName), BindFlagAll);
+ type = type.BaseType;
+ }
+ return eventField;
+ }
+
+ private static IEnumerable GetEventsInternal(object instance, FieldInfo eventField)
+ {
+ if (eventField.GetValue(instance) is MulticastDelegate eventDel)
+ {
+ foreach (Delegate handle in eventDel.GetInvocationList())
+ yield return handle;
+ }
+ }
+ }
+
+ ///
+ /// Attribute used to indicate that the the given field, of type ]]>, should be filled with
+ /// a function used to create a new event replacer.
+ ///
+ [AttributeUsage(AttributeTargets.Field)]
+ public class ReflectedEventReplaceAttribute : Attribute
+ {
+ ///
+ /// Type that the event is declared in
+ ///
+ public Type EventDeclaringType { get; set; }
+ ///
+ /// Name of the event
+ ///
+ public string EventName { get; set; }
+
+ ///
+ /// Type that the method to replace is declared in
+ ///
+ public Type TargetDeclaringType { get; set; }
+ ///
+ /// Name of the method to replace
+ ///
+ public string TargetName { get; set; }
+ ///
+ /// Optional parameters of the method to replace. Null to ignore.
+ ///
+ public Type[] TargetParameters { get; set; } = null;
+
+ ///
+ /// Creates a reflected event replacer attribute to, for the event defined as eventName in eventDeclaringType,
+ /// replace the method defined as targetName in targetDeclaringType with a custom callback.
+ ///
+ /// Type the event is declared in
+ /// Name of the event
+ /// Type the method to remove is declared in
+ /// Name of the method to remove
+ public ReflectedEventReplaceAttribute(Type eventDeclaringType, string eventName, Type targetDeclaringType,
+ string targetName)
+ {
+ EventDeclaringType = eventDeclaringType;
+ EventName = eventName;
+ TargetDeclaringType = targetDeclaringType;
+ TargetName = targetName;
+ }
+ }
+ #endregion
///
/// Automatically calls for every assembly already loaded, and every assembly that is loaded in the future.
@@ -125,7 +370,7 @@ namespace Torch.Utils
private static readonly string[] _namespaceBlacklist = new[] {
"System", "VRage", "Sandbox", "SpaceEngineers"
};
-
+
///
/// Registers the assembly load event and loads every already existing assembly.
///
@@ -185,37 +430,94 @@ namespace Torch.Utils
/// If the field failed to process
public static bool Process(FieldInfo field)
{
- var attr = field.GetCustomAttribute();
- if (attr != null)
+ foreach (ReflectedMemberAttribute attr in field.GetCustomAttributes())
{
if (!field.IsStatic)
throw new ArgumentException("Field must be static to be reflected");
- ProcessReflectedMethod(field, attr);
- return true;
- }
- var attr2 = field.GetCustomAttribute();
- if (attr2 != null)
- {
- if (!field.IsStatic)
- throw new ArgumentException("Field must be static to be reflected");
- ProcessReflectedField(field, attr2);
- return true;
- }
- var attr3 = field.GetCustomAttribute();
- if (attr3 != null)
- {
- if (!field.IsStatic)
+ switch (attr)
{
- throw new ArgumentException("Field must be static to be reflected");
+ case ReflectedMethodAttribute rma:
+ ProcessReflectedMethod(field, rma);
+ return true;
+ case ReflectedGetterAttribute rga:
+ ProcessReflectedField(field, rga);
+ return true;
+ case ReflectedSetterAttribute rsa:
+ ProcessReflectedField(field, rsa);
+ return true;
+ case ReflectedFieldInfoAttribute rfia:
+ ProcessReflectedMemberInfo(field, rfia);
+ return true;
+ case ReflectedPropertyInfoAttribute rpia:
+ ProcessReflectedMemberInfo(field, rpia);
+ return true;
+ case ReflectedMethodInfoAttribute rmia:
+ ProcessReflectedMemberInfo(field, rmia);
+ return true;
}
-
- ProcessReflectedField(field, attr3);
- return true;
}
+ var reflectedEventReplacer = field.GetCustomAttribute();
+ if (reflectedEventReplacer != null)
+ {
+ if (!field.IsStatic)
+ throw new ArgumentException("Field must be static to be reflected");
+ field.SetValue(null, new Func(() => new ReflectedEventReplacer(reflectedEventReplacer)));
+ return true;
+ }
return false;
}
+ private static void ProcessReflectedMemberInfo(FieldInfo field, ReflectedMemberAttribute attr)
+ {
+ MemberInfo info = null;
+ if (attr.Type == null)
+ throw new ArgumentException("Reflected member info attributes require Type to be defined");
+ if (attr.Name == null)
+ throw new ArgumentException("Reflected member info attributes require Name to be defined");
+ switch (attr)
+ {
+ case ReflectedFieldInfoAttribute rfia:
+ info = GetFieldPropRecursive(rfia.Type, rfia.Name,
+ BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
+ (a, b, c) => a.GetField(b));
+ if (info == null)
+ throw new ArgumentException($"Unable to find field {rfia.Type.FullName}#{rfia.Name}");
+ break;
+ case ReflectedPropertyInfoAttribute rpia:
+ info = GetFieldPropRecursive(rpia.Type, rpia.Name,
+ BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
+ (a, b, c) => a.GetProperty(b));
+ if (info == null)
+ throw new ArgumentException($"Unable to find property {rpia.Type.FullName}#{rpia.Name}");
+ break;
+ case ReflectedMethodInfoAttribute rmia:
+ if (rmia.Parameters != null)
+ {
+ info = rmia.Type.GetMethod(rmia.Name,
+ BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
+ null, CallingConventions.Any, rmia.Parameters, null);
+ if (info == null)
+ throw new ArgumentException(
+ $"Unable to find method {rmia.Type.FullName}#{rmia.Name}({string.Join(", ", rmia.Parameters.Select(x => x.FullName))})");
+ }
+ else
+ {
+ info = rmia.Type.GetMethod(rmia.Name,
+ BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
+ if (info == null)
+ throw new ArgumentException(
+ $"Unable to find method {rmia.Type.FullName}#{rmia.Name}");
+ }
+ if (rmia.ReturnType != null && !rmia.ReturnType.IsAssignableFrom(((MethodInfo)info).ReturnType))
+ throw new ArgumentException($"Method {rmia.Type.FullName}#{rmia.Name} has return type {((MethodInfo)info).ReturnType.FullName}, expected {rmia.ReturnType.FullName}");
+ break;
+ }
+ if (info == null)
+ throw new ArgumentException($"Unable to find member info for {attr.GetType().Name}[{attr.Type.FullName}#{attr.Name}");
+ field.SetValue(null, info);
+ }
+
private static void ProcessReflectedMethod(FieldInfo field, ReflectedMethodAttribute attr)
{
MethodInfo delegateMethod = field.FieldType.GetMethod("Invoke");
@@ -249,14 +551,14 @@ namespace Torch.Utils
field.SetValue(null, Delegate.CreateDelegate(field.FieldType, methodInstance));
else
{
- ParameterExpression[] paramExp = parameters.Select(x => Expression.Parameter(x.ParameterType)).ToArray();
+ ParameterExpression[] paramExp = parameters.Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToArray();
field.SetValue(null,
Expression.Lambda(Expression.Call(paramExp[0], methodInstance, paramExp.Skip(1)), paramExp)
.Compile());
}
}
- private static T GetFieldPropRecursive(Type baseType, string name, BindingFlags flags, Func getter) where T : class
+ internal static T GetFieldPropRecursive(Type baseType, string name, BindingFlags flags, Func getter) where T : class
{
while (baseType != null)
{