Browse Source

Add additional `ConsoleAPI` output methods

Adds additional methods fo rconsole output, also changing how the old
one worked: now most methods do not output player's name in front of the
message, but `Say()` method was added if such behavior is needed.

Additionally fixed issue with `PlayerController` actor being stored
inside `ConsoleWriter` object before.
pull/8/head
Anton Tarasenko 4 years ago
parent
commit
295639bd5d
  1. 10
      sources/Console/ConsoleAPI.uc
  2. 148
      sources/Console/ConsoleWriter.uc
  3. 5
      sources/Players/APlayer.uc

10
sources/Console/ConsoleAPI.uc

@ -70,7 +70,7 @@ class ConsoleAPI extends AcediaObject
* that we had to break a long line. * that we had to break a long line.
* *
* Described measures are not perfect: * Described measures are not perfect:
* 1. Since Killing Floor's console doe not use monospaced font, * 1. Since Killing Floor's console does not use monospaced font,
* the same amount of characters on the line does not mean lines of * the same amount of characters on the line does not mean lines of
* visually the same length; * visually the same length;
* 2. Heavily enough colored lines are still going to be shorter; * 2. Heavily enough colored lines are still going to be shorter;
@ -211,24 +211,24 @@ public final function ConsoleWriter ForAll()
/** /**
* Returns new `ConsoleWriter` instance that will write into * Returns new `ConsoleWriter` instance that will write into
* console of the player with a given controller. * console of the given player.
* Should be freed after use. * Should be freed after use.
* *
* @param targetController Player, to whom console we want to write. * @param targetPlayer Player, to whom console we want to write.
* If `none` - returned `ConsoleWriter` would be configured to * If `none` - returned `ConsoleWriter` would be configured to
* throw messages away. * throw messages away.
* @return New `ConsoleWriter` instance, configured to * @return New `ConsoleWriter` instance, configured to
* write into consoles of all players. * write into consoles of all players.
* Guaranteed to not be `none`. * Guaranteed to not be `none`.
*/ */
public final function ConsoleWriter For(PlayerController targetController) public final function ConsoleWriter For(APlayer targetPlayer)
{ {
local ConsoleDisplaySettings globalSettings; local ConsoleDisplaySettings globalSettings;
globalSettings.defaultColor = defaultColor; globalSettings.defaultColor = defaultColor;
globalSettings.maxTotalLineWidth = maxTotalLineWidth; globalSettings.maxTotalLineWidth = maxTotalLineWidth;
globalSettings.maxVisibleLineWidth = maxVisibleLineWidth; globalSettings.maxVisibleLineWidth = maxVisibleLineWidth;
return ConsoleWriter(_.memory.Allocate(class'ConsoleWriter')) return ConsoleWriter(_.memory.Allocate(class'ConsoleWriter'))
.Initialize(globalSettings).ForController(targetController); .Initialize(globalSettings).ForPlayer(targetPlayer);
} }
defaultproperties defaultproperties

148
sources/Console/ConsoleWriter.uc

@ -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 targetController Player, to whom console we want to write. * @param targetPlayer 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 (targetController != none) if (targetPlayer != none)
{ {
targetType = CWTARGET_Player; targetType = CWTARGET_Player;
outputTarget = targetController; outputTarget = targetPlayer;
} }
else { else {
targetType = CWTARGET_None; targetType = CWTARGET_None;
@ -231,14 +228,14 @@ public final function ConsoleWriterTarget CurrentTarget()
} }
/** /**
* Returns `PlayerController` of the player to whom console caller * Returns `APlayer` 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 PlayerController GetTargetPlayerController() public final function APlayer 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 = " "
} }

5
sources/Players/APlayer.uc

@ -304,10 +304,11 @@ public final function ConsoleWriter Console()
if ( consoleInstance == none if ( consoleInstance == none
|| consoleInstance.GetLifeVersion() != consoleLifeVersion) || consoleInstance.GetLifeVersion() != consoleLifeVersion)
{ {
consoleInstance = _.console.For(GetController()); consoleInstance = _.console.For(self);
consoleLifeVersion = consoleInstance.GetLifeVersion(); consoleLifeVersion = consoleInstance.GetLifeVersion();
} }
return consoleInstance; // Set us as target in case someone messed with this setting
return consoleInstance.ForPlayer(self);
} }
defaultproperties defaultproperties

Loading…
Cancel
Save