|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
/** |
|
|
|
|
* API that provides functions for working with chat. |
|
|
|
|
* Copyright 2022 Anton Tarasenko |
|
|
|
|
* Author: dkanus |
|
|
|
|
* Home repo: https://www.insultplayers.ru/git/AcediaFramework/AcediaCore |
|
|
|
|
* License: GPL |
|
|
|
|
* Copyright 2022-2023 Anton Tarasenko |
|
|
|
|
*------------------------------------------------------------------------------ |
|
|
|
|
* This file is part of Acedia. |
|
|
|
|
* |
|
|
|
@ -17,34 +19,30 @@
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
|
|
|
|
* along with Acedia. If not, see <https://www.gnu.org/licenses/>. |
|
|
|
|
*/ |
|
|
|
|
class ChatAPI extends AcediaObject; |
|
|
|
|
class ChatApi extends AcediaObject; |
|
|
|
|
|
|
|
|
|
var protected bool connectedToBroadcastAPI; |
|
|
|
|
|
|
|
|
|
var protected ChatAPI_OnMessage_Signal onMessageSignal; |
|
|
|
|
var protected ChatAPI_OnMessageFor_Signal onMessageForSignal; |
|
|
|
|
var protected ChatAPI_OnMessage_Signal onMessageSignal; |
|
|
|
|
var protected ChatAPI_OnMessageFor_Signal onMessageForSignal; |
|
|
|
|
|
|
|
|
|
protected function Constructor() |
|
|
|
|
{ |
|
|
|
|
onMessageSignal = ChatAPI_OnMessage_Signal( |
|
|
|
|
_.memory.Allocate(class'ChatAPI_OnMessage_Signal')); |
|
|
|
|
onMessageForSignal = ChatAPI_OnMessageFor_Signal( |
|
|
|
|
_.memory.Allocate(class'ChatAPI_OnMessageFor_Signal')); |
|
|
|
|
protected function Constructor() { |
|
|
|
|
onMessageSignal = ChatAPI_OnMessage_Signal(_.memory.Allocate(class'ChatAPI_OnMessage_Signal')); |
|
|
|
|
onMessageForSignal = |
|
|
|
|
ChatAPI_OnMessageFor_Signal(_.memory.Allocate(class'ChatAPI_OnMessageFor_Signal')); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected function Finalizer() |
|
|
|
|
{ |
|
|
|
|
protected function Finalizer() { |
|
|
|
|
_.memory.Free(onMessageSignal); |
|
|
|
|
_.memory.Free(onMessageForSignal); |
|
|
|
|
onMessageSignal = none; |
|
|
|
|
onMessageForSignal = none; |
|
|
|
|
onMessageSignal = none; |
|
|
|
|
onMessageForSignal = none; |
|
|
|
|
_server.unreal.broadcasts.OnHandleText(self).Disconnect(); |
|
|
|
|
_server.unreal.broadcasts.OnHandleTextFor(self).Disconnect(); |
|
|
|
|
connectedToBroadcastAPI = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private final function TryConnectingBroadcastSignals() |
|
|
|
|
{ |
|
|
|
|
private final function TryConnectingBroadcastSignals() { |
|
|
|
|
if (connectedToBroadcastAPI) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
@ -53,76 +51,73 @@ private final function TryConnectingBroadcastSignals()
|
|
|
|
|
_server.unreal.broadcasts.OnHandleTextFor(self).connect = HandleTextFor; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Signal that will be emitted when a player sends a message into the chat. |
|
|
|
|
* Allows to modify message before sending it, as well as prevent it from |
|
|
|
|
* being sent at all. |
|
|
|
|
* |
|
|
|
|
* Return `false` to prevent message from being sent. |
|
|
|
|
* If `false` is returned, signal propagation to the remaining handlers will |
|
|
|
|
* also be interrupted. |
|
|
|
|
* |
|
|
|
|
* [Signature] |
|
|
|
|
* bool <slot>(EPlayer sender, MutableText message, bool teamMessage) |
|
|
|
|
* |
|
|
|
|
* @param sender `EPlayer` that has sent the message. |
|
|
|
|
* @param message Message that `sender` has sent. This is a mutable |
|
|
|
|
* variable and can be modified from message will be sent. |
|
|
|
|
* @param teamMessage Is this a team message |
|
|
|
|
* (to be sent only to players on the same team)? |
|
|
|
|
* @return Return `false` to prevent this message from being sent at all |
|
|
|
|
* and `true` otherwise. Message will be sent only if all handlers will |
|
|
|
|
* return `true`. |
|
|
|
|
*/ |
|
|
|
|
/* SIGNAL */ |
|
|
|
|
public function ChatAPI_OnMessage_Slot OnMessage( |
|
|
|
|
AcediaObject receiver) |
|
|
|
|
{ |
|
|
|
|
/// Signal that will be emitted when a player sends a message into the chat. |
|
|
|
|
/// |
|
|
|
|
/// Allows to modify message before sending it, as well as prevent it from being sent at all. |
|
|
|
|
/// |
|
|
|
|
/// Return `false` to prevent message from being sent. |
|
|
|
|
/// If `false` is returned, signal propagation to the remaining handlers will also be interrupted. |
|
|
|
|
/// |
|
|
|
|
/// # Slot description |
|
|
|
|
/// |
|
|
|
|
/// bool <slot>(EPlayer sender, MutableText message, bool teamMessage) |
|
|
|
|
/// |
|
|
|
|
/// ## Parameters |
|
|
|
|
/// |
|
|
|
|
/// * [`sender`]: `EPlayer` that has sent the message. |
|
|
|
|
/// * [`message`]: Message that `sender` has sent. |
|
|
|
|
/// This is a mutable variable and can be modified from message will be sent. |
|
|
|
|
/// * [`teamMessage`]: Is this a team message (to be sent only to players on the same team)? |
|
|
|
|
/// |
|
|
|
|
/// ## Returns |
|
|
|
|
/// |
|
|
|
|
/// Return `false` to prevent this message from being sent at all and `true` otherwise. |
|
|
|
|
/// Message will be sent only if all handlers will return `true`. |
|
|
|
|
public /*signal*/ function ChatAPI_OnMessage_Slot OnMessage(AcediaObject receiver) { |
|
|
|
|
TryConnectingBroadcastSignals(); |
|
|
|
|
return ChatAPI_OnMessage_Slot(onMessageSignal.NewSlot(receiver)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Signal that will be emitted when a player sends a message into the chat. |
|
|
|
|
* Allows to modify message before sending it, as well as prevent it from |
|
|
|
|
* being sent at all. |
|
|
|
|
* |
|
|
|
|
* Return `false` to prevent message from being sent to a specific player. |
|
|
|
|
* If `false` is returned, signal propagation to the remaining handlers will |
|
|
|
|
* also be interrupted. |
|
|
|
|
* |
|
|
|
|
* [Signature] |
|
|
|
|
* bool <slot>(EPlayer receiver, EPlayer sender, BaseText message) |
|
|
|
|
* |
|
|
|
|
* @param receiver `EPlayer` that will receive the message. |
|
|
|
|
* @param sender `EPlayer` that has sent the message. |
|
|
|
|
* @param message Message that `sender` has sent. This is an immutable |
|
|
|
|
* variable and cannot be changed at this point. Use `OnMessage()` |
|
|
|
|
* signal function for that. |
|
|
|
|
* @return Return `false` to prevent this message from being sent to |
|
|
|
|
* a particular player and `true` otherwise. Message will be sent only if |
|
|
|
|
* all handlers will return `true`. |
|
|
|
|
* However decision whether to send message or not is made for |
|
|
|
|
* every player separately. |
|
|
|
|
*/ |
|
|
|
|
/* SIGNAL */ |
|
|
|
|
public function ChatAPI_OnMessageFor_Slot OnMessageFor( |
|
|
|
|
AcediaObject receiver) |
|
|
|
|
{ |
|
|
|
|
/// Signal that will be emitted when a player sends a message into the chat. |
|
|
|
|
/// |
|
|
|
|
/// Allows to modify message before sending it, as well as prevent it from being sent at all. |
|
|
|
|
/// |
|
|
|
|
/// Return `false` to prevent message from being sent to a specific player. |
|
|
|
|
/// If `false` is returned, signal propagation to the remaining handlers will also be interrupted. |
|
|
|
|
/// |
|
|
|
|
/// # Slot description |
|
|
|
|
/// |
|
|
|
|
/// bool <slot>(EPlayer receiver, EPlayer sender, BaseText message) |
|
|
|
|
/// |
|
|
|
|
/// ## Parameters |
|
|
|
|
/// |
|
|
|
|
/// * [`receiver`]: `EPlayer` that will receive the message. |
|
|
|
|
/// * [`sender`]: `EPlayer` that has sent the message. |
|
|
|
|
/// * [`message`]: Message that `sender` has sent. This is an immutable variable and cannot |
|
|
|
|
/// be changed at this point. Use `OnMessage()` signal function for that. |
|
|
|
|
/// |
|
|
|
|
/// ## Returns |
|
|
|
|
/// |
|
|
|
|
/// Return `false` to prevent this message from being sent to a particular player and |
|
|
|
|
/// `true` otherwise. |
|
|
|
|
/// Message will be sent only if all handlers will return `true`. |
|
|
|
|
/// However decision whether to send message or not is made for every player separately. |
|
|
|
|
public /*signal*/ function ChatAPI_OnMessageFor_Slot OnMessageFor(AcediaObject receiver) { |
|
|
|
|
TryConnectingBroadcastSignals(); |
|
|
|
|
return ChatAPI_OnMessageFor_Slot(onMessageForSignal.NewSlot(receiver)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function bool HandleText( |
|
|
|
|
Actor sender, |
|
|
|
|
out string message, |
|
|
|
|
name messageType, |
|
|
|
|
bool teamMessage) |
|
|
|
|
{ |
|
|
|
|
local bool result; |
|
|
|
|
local MutableText messageAsText; |
|
|
|
|
local EPlayer senderPlayer; |
|
|
|
|
// We only want to catch chat messages from a player |
|
|
|
|
Actor sender, |
|
|
|
|
out string message, |
|
|
|
|
name messageType, |
|
|
|
|
bool teamMessage |
|
|
|
|
) { |
|
|
|
|
local bool result; |
|
|
|
|
local MutableText messageAsText; |
|
|
|
|
local EPlayer senderPlayer; |
|
|
|
|
|
|
|
|
|
// We only want to catch chat messages from a player |
|
|
|
|
if (messageType != 'Say' && messageType != 'TeamSay') return true; |
|
|
|
|
senderPlayer = _.players.FromController(PlayerController(sender)); |
|
|
|
|
if (senderPlayer == none) return true; |
|
|
|
@ -145,34 +140,32 @@ private function bool HandleText(
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function bool HandleTextFor( |
|
|
|
|
PlayerController receiver, |
|
|
|
|
Actor sender, |
|
|
|
|
out string message, |
|
|
|
|
name messageType) |
|
|
|
|
{ |
|
|
|
|
local bool result; |
|
|
|
|
local Text messageAsText; |
|
|
|
|
local EPlayer senderPlayer, receiverPlayer; |
|
|
|
|
// We only want to catch chat messages from another player |
|
|
|
|
PlayerController receiver, |
|
|
|
|
Actor sender, |
|
|
|
|
out string message, |
|
|
|
|
name messageType |
|
|
|
|
) { |
|
|
|
|
local bool result; |
|
|
|
|
local Text messageAsText; |
|
|
|
|
local EPlayer senderPlayer, receiverPlayer; |
|
|
|
|
|
|
|
|
|
// We only want to catch chat messages from another player |
|
|
|
|
if (messageType != 'Say' && messageType != 'TeamSay') return true; |
|
|
|
|
senderPlayer = _.players.FromController(PlayerController(sender)); |
|
|
|
|
if (senderPlayer == none) return true; |
|
|
|
|
|
|
|
|
|
receiverPlayer = _.players.FromController(receiver); |
|
|
|
|
if (receiverPlayer == none) |
|
|
|
|
{ |
|
|
|
|
if (receiverPlayer == none) { |
|
|
|
|
_.memory.Free(senderPlayer); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
messageAsText = __().text.FromColoredString(message); |
|
|
|
|
result = onMessageForSignal.Emit( receiverPlayer, senderPlayer, |
|
|
|
|
messageAsText); |
|
|
|
|
result = onMessageForSignal.Emit(receiverPlayer, senderPlayer, messageAsText); |
|
|
|
|
_.memory.Free(messageAsText); |
|
|
|
|
_.memory.Free(senderPlayer); |
|
|
|
|
_.memory.Free(receiverPlayer); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
defaultproperties |
|
|
|
|
{ |
|
|
|
|
defaultproperties { |
|
|
|
|
} |