@ -27,6 +27,7 @@ class ConsoleWriter extends AcediaObject
// or not
// or not
var private string NEWLINE_PREFIX;
var private string NEWLINE_PREFIX;
var private string BROKENLINE_PREFIX;
var private string BROKENLINE_PREFIX;
var private string INDENTATION;
/**
/**
* Describes current output target of the `ConsoleWriter`.
* Describes current output target of the `ConsoleWriter`.
@ -41,10 +42,9 @@ enum ConsoleWriterTarget
CWTARGET_All
CWTARGET_All
};
};
var private ConsoleWriterTarget targetType;
var private ConsoleWriterTarget targetType;
// Controller of the player that will receive output passed
// Player that will receive output passed to this `ConsoleWriter`.
// to this `ConsoleWriter`.
// Only used when `targetType == CWTARGET_Player`
// Only used when `targetType == CWTARGET_Player`
var private PlayerController outputTarget;
var private APlayer outputTarget;
var private ConsoleBuffer outputBuffer;
var private ConsoleBuffer outputBuffer;
var private ConsoleAPI.ConsoleDisplaySettings displaySettings;
var private ConsoleAPI.ConsoleDisplaySettings displaySettings;
@ -190,25 +190,22 @@ public final function ConsoleWriter ForAll()
}
}
/**
/**
* Configures caller `ConsoleWriter` to output only to a player,
* Configures caller `ConsoleWriter` to output only to the given player.
* given by a passed `PlayerController`.
* `Flush()` will be automatically called between target change.
* `Flush()` will be automatically called between target change.
*
*
* @param targetControll er Player, to whom console we want to write.
* @param targetPlay er Player, to whom console we want to write.
* If `none` - caller `ConsoleWriter` would be configured to
* If `none` - caller `ConsoleWriter` would be configured to
* throw messages away.
* throw messages away.
* @return ConsoleWriter Returns caller `ConsoleWriter` to allow for
* @return ConsoleWriter Returns caller `ConsoleWriter` to allow for
* method chaining.
* method chaining.
*/
*/
public final function ConsoleWriter ForController(
public final function ConsoleWriter ForPlayer(APlayer targetPlayer)
PlayerController targetController
)
{
{
Flush();
Flush();
if (targetControll er != none)
if (targetPlay er != none)
{
{
targetType = CWTARGET_Player;
targetType = CWTARGET_Player;
outputTarget = targetControll er;
outputTarget = targetPlay er;
}
}
else {
else {
targetType = CWTARGET_None;
targetType = CWTARGET_None;
@ -231,14 +228,14 @@ public final function ConsoleWriterTarget CurrentTarget()
}
}
/**
/**
* Returns `PlayerControll er` of the player to whom console caller
* Returns `A Player` to whom console caller `ConsoleWriter` is
* `ConsoleWriter` is outputting messages.
* outputting messages.
*
*
* @return `PlayerController` of the player to whom console caller
* @return Player (`APlayer` class) to whom console caller `ConsoleWriter` is
* `ConsoleWriter` is outputting messages.
* outputting messages. Returns `none` iff it currently outputs to
* Returns `none` iff it currently outputs to every player or to no one.
* every player or to no one.
*/
*/
public final function PlayerControll er GetTargetPlayerControll er()
public final function A Player GetTargetPlayer()
{
{
if (targetType == CWTARGET_All) return none;
if (targetType == CWTARGET_All) return none;
return outputTarget;
return outputTarget;
@ -282,12 +279,54 @@ public final function ConsoleWriter WriteLine(Text message)
return Write(message).Flush();
return Write(message).Flush();
}
}
// Send all completed lines from an `outputBuffer`
/**
private final function SendBuffer()
* Writes text's indented contents into console.
*
* Acts like a `Flush().WriteLine()` chain of calls, except all output contents
* will be additionally indented by four whitespace symbols
* (including lines after line breaks).
*
* Result will be output immediately, starts a new line.
*
* @param message `Text` to output.
* @return Returns caller `ConsoleWriter` to allow for method chaining.
*/
public final function ConsoleWriter WriteBlock(Text message)
{
Flush();
outputBuffer.Insert(message).Flush();
SendBuffer(true);
return self;
}
/**
* Writes text's contents into console as a player's chat message, causing them
* to appear on screen in vanilla UI.
*
* All the buffer stored in caller `ConsoleWriter` so far will be flushed.
* Result will be output immediately. Starts a new line.
*
* @param message `Text` to output.
* @return Returns caller `ConsoleWriter` to allow for method chaining.
*/
public final function ConsoleWriter Say(Text message)
{
Flush();
outputBuffer.Insert(message).Flush();
SendBuffer(, true);
return self;
}
// Send all completed lines from an `outputBuffer`.
// Setting `indented` to `true` will cause additional four whitespaces to
// be added to the output.
private final function SendBuffer(optional bool asIndented, optional bool asSay)
{
{
local string prefix;
local string prefix;
local ConnectionService service;
local ConsoleBuffer.LineRecord nextLineRecord;
local ConsoleBuffer.LineRecord nextLineRecord;
local array<PlayerController> recipients;
recipients = GetRecipientsControllers();
while (outputBuffer.HasCompletedLines())
while (outputBuffer.HasCompletedLines())
{
{
nextLineRecord = outputBuffer.PopNextLine();
nextLineRecord = outputBuffer.PopNextLine();
@ -297,33 +336,80 @@ private final function SendBuffer()
else {
else {
prefix = BROKENLINE_PREFIX;
prefix = BROKENLINE_PREFIX;
}
}
service = ConnectionService(class'ConnectionService'.static.Require());
if (asIndented) {
SendConsoleMessage(service, prefix $ nextLineRecord.contents);
prefix $= INDENTATION;
}
SendConsoleMessage(recipients, prefix $ nextLineRecord.contents, asSay);
}
}
}
}
// Assumes `service != none`, caller function must ensure that.
// Assumes `playerService != none` and `connectionService != none`,
// caller function must ensure that.
private final function SendConsoleMessage(
private final function SendConsoleMessage(
ConnectionService service,
array<PlayerController> recipients,
string message)
string message,
bool asSay)
{
{
local int i;
local int i;
for (i = 0; i < recipients.length; i += 1)
{
if (recipients[i] != none)
{
if (asSay) {
recipients[i].ClientMessage(message);
}
else {
recipients[i].TeamMessage(none, message, 'AcediaConsole');
}
}
}
}
// Method for retrieving `PlayerController`s of recipients at the moment
// of the call
private final function array<PlayerController> GetRecipientsControllers()
{
local int i;
local PlayerController nextRecipient;
local PlayerService playerService;
local ConnectionService connectionService;
local array<PlayerController> recipients;
local array<ConnectionService.Connection> connections;
local array<ConnectionService.Connection> connections;
// No targets
if (targetType == CWTARGET_None) {
return recipients;
}
// Single target case
if (targetType != CWTARGET_All)
if (targetType != CWTARGET_All)
{
{
if (outputTarget != none) {
playerService = PlayerService(class'PlayerService'.static.Require());
outputTarget.ClientMessage(message);
if (playerService != none && outputTarget != none) {
nextRecipient = playerService.GetController(outputTarget);
}
if (nextRecipient != none) {
recipients[0] = nextRecipient;
}
return recipients;
}
}
return;
// All players target case
connectionService =
ConnectionService(class'ConnectionService'.static.Require());
if (connectionService == none) {
return recipients;
}
connections = connectionService.GetActiveConnections();
for (i = 0; i < connections.length; i += 1)
{
if (connections[i].controllerReference != none) {
recipients[recipients.length] = connections[i].controllerReference;
}
}
connections = service.GetActiveConnections();
for (i = 0; i < connections.length; i += 1) {
connections[i].controllerReference.ClientMessage(message);
}
}
return recipients;
}
}
defaultproperties
defaultproperties
{
{
NEWLINE_PREFIX = "| "
NEWLINE_PREFIX = "| "
BROKENLINE_PREFIX = " "
BROKENLINE_PREFIX = " "
INDENTATION = " "
}
}