Offline mode fallbacks for the chat manager.

This commit is contained in:
Westin Miller
2017-08-22 20:33:53 -07:00
parent 2c7b522378
commit 4b2fee7614
2 changed files with 57 additions and 7 deletions

View File

@@ -8,7 +8,9 @@ using Sandbox.Engine.Multiplayer;
using Sandbox.Engine.Networking; using Sandbox.Engine.Networking;
using Sandbox.Game.Entities.Character; using Sandbox.Game.Entities.Character;
using Sandbox.Game.Gui; using Sandbox.Game.Gui;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.World; using Sandbox.Game.World;
using Sandbox.ModAPI;
using Torch.API; using Torch.API;
using Torch.API.Managers; using Torch.API.Managers;
using Torch.Utils; using Torch.Utils;
@@ -25,11 +27,9 @@ namespace Torch.Managers.ChatManager
public ChatManagerClient(ITorchBase torchInstance) : base(torchInstance) { } public ChatManagerClient(ITorchBase torchInstance) : base(torchInstance) { }
/// <inheritdoc /> /// <inheritdoc />
// TODO doesn't work in Offline worlds. Method injection or network injection.
public event MessageRecievedDel MessageRecieved; public event MessageRecievedDel MessageRecieved;
/// <inheritdoc /> /// <inheritdoc />
// TODO doesn't work at all. Method injection or network injection.
public event MessageSendingDel MessageSending; public event MessageSendingDel MessageSending;
/// <inheritdoc /> /// <inheritdoc />
@@ -71,9 +71,8 @@ namespace Torch.Managers.ChatManager
public override void Attach() public override void Attach()
{ {
base.Attach(); base.Attach();
if (MyMultiplayer.Static == null) MyAPIUtilities.Static.MessageEntered += OnMessageEntered;
_log.Warn("Currently ChatManagerClient doesn't support handling on an offline client"); if (MyMultiplayer.Static != null)
else
{ {
_chatMessageRecievedReplacer = _chatMessageReceivedFactory.Invoke(); _chatMessageRecievedReplacer = _chatMessageReceivedFactory.Invoke();
_scriptedChatMessageRecievedReplacer = _scriptedChatMessageReceivedFactory.Invoke(); _scriptedChatMessageRecievedReplacer = _scriptedChatMessageReceivedFactory.Invoke();
@@ -82,18 +81,61 @@ namespace Torch.Managers.ChatManager
_scriptedChatMessageRecievedReplacer.Replace( _scriptedChatMessageRecievedReplacer.Replace(
new Action<string, string, string>(Multiplayer_ScriptedChatMessageReceived), MyMultiplayer.Static); new Action<string, string, string>(Multiplayer_ScriptedChatMessageReceived), MyMultiplayer.Static);
} }
else
{
MyAPIUtilities.Static.MessageEntered += OfflineMessageReciever;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
public override void Detach() public override void Detach()
{ {
MyAPIUtilities.Static.MessageEntered -= OnMessageEntered;
if (_chatMessageRecievedReplacer != null && _chatMessageRecievedReplacer.Replaced) if (_chatMessageRecievedReplacer != null && _chatMessageRecievedReplacer.Replaced)
_chatMessageRecievedReplacer.Restore(MyHud.Chat); _chatMessageRecievedReplacer.Restore(MyHud.Chat);
if (_scriptedChatMessageRecievedReplacer != null && _scriptedChatMessageRecievedReplacer.Replaced) if (_scriptedChatMessageRecievedReplacer != null && _scriptedChatMessageRecievedReplacer.Replaced)
_scriptedChatMessageRecievedReplacer.Restore(MyHud.Chat); _scriptedChatMessageRecievedReplacer.Restore(MyHud.Chat);
MyAPIUtilities.Static.MessageEntered -= OfflineMessageReciever;
base.Detach(); base.Detach();
} }
/// <summary>
/// Callback used to process offline messages.
/// </summary>
/// <param name="msg"></param>
/// <returns>true if the message was consumed</returns>
protected virtual bool OfflineMessageProcessor(TorchChatMessage msg)
{
return false;
}
private void OfflineMessageReciever(string messageText, ref bool sendToOthers)
{
if (!sendToOthers)
return;
var torchMsg = new TorchChatMessage()
{
AuthorSteamId = Sync.MyId,
Author = MySession.Static.LocalHumanPlayer?.DisplayName ?? "Player",
Message = messageText
};
var consumed = false;
MessageRecieved?.Invoke(torchMsg, ref consumed);
if (!consumed)
consumed = OfflineMessageProcessor(torchMsg);
sendToOthers = !consumed;
}
private void OnMessageEntered(string messageText, ref bool sendToOthers)
{
if (!sendToOthers)
return;
var consumed = false;
MessageSending?.Invoke(messageText, ref consumed);
sendToOthers = !consumed;
}
private void Multiplayer_ChatMessageReceived(ulong steamUserId, string message) private void Multiplayer_ChatMessageReceived(ulong steamUserId, string message)
{ {
var torchMsg = new TorchChatMessage() var torchMsg = new TorchChatMessage()

View File

@@ -109,8 +109,16 @@ namespace Torch.Managers.ChatManager
MyMultiplayer.Static.ChatMessageReceived += MpStaticChatMessageReceived; MyMultiplayer.Static.ChatMessageReceived += MpStaticChatMessageReceived;
_log.Warn("Failed to initialize network intercept, we can't discard chat messages! Falling back to another method."); _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!");
/// <inheritdoc />
protected override bool OfflineMessageProcessor(TorchChatMessage msg)
{
if (MyMultiplayer.Static != null)
return false;
var consumed = false;
MessageProcessing?.Invoke(msg, ref consumed);
return consumed;
} }
private void MpStaticChatMessageReceived(ulong a, string b) private void MpStaticChatMessageReceived(ulong a, string b)