1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Implement message prefixing and other style customization.

This commit is contained in:
Sandu Liviu Catalin 2015-11-09 04:32:32 +02:00
parent 8b9d46d9a9
commit 3c3e67bdaa
2 changed files with 163 additions and 9 deletions

View File

@ -19,7 +19,14 @@ Color3 CPlayer::s_Color3;
Vector3 CPlayer::s_Vector3;
// ------------------------------------------------------------------------------------------------
SQChar CPlayer::s_Buffer[128];
SQChar CPlayer::s_Buffer[MAX_PLAYER_TEMPORARY_BUFFER];
// ------------------------------------------------------------------------------------------------
CPlayer::Prefixes CPlayer::s_MsgPrefixes{{_SC("")}};
// ------------------------------------------------------------------------------------------------
SQUint32 CPlayer::s_MessageColor = 0x6599FFFF;
SQInt32 CPlayer::s_AnnounceStyle = 1;
// ------------------------------------------------------------------------------------------------
CPlayer::CPlayer(const Reference< CPlayer > & o)
@ -194,7 +201,7 @@ bool CPlayer::IsSpawned() const
}
// ------------------------------------------------------------------------------------------------
SQUnsignedInteger CPlayer::GetKey() const
SQUint32 CPlayer::GetKey() const
{
if (VALID_ENTITY(m_ID))
{
@ -1713,7 +1720,7 @@ void CPlayer::Disembark() const
}
// ------------------------------------------------------------------------------------------------
bool CPlayer::Redirect(const SQChar * ip, SQUnsignedInteger port, const SQChar * nick, \
bool CPlayer::Redirect(const SQChar * ip, SQUint32 port, const SQChar * nick, \
const SQChar * pass, const SQChar * user)
{
if (VALID_ENTITY(m_ID))
@ -1728,6 +1735,34 @@ bool CPlayer::Redirect(const SQChar * ip, SQUnsignedInteger port, const SQChar *
return false;
}
// ------------------------------------------------------------------------------------------------
const SQChar * CPlayer::GetMessagePrefix(SQUint32 index)
{
if (index < MAX_PLAYER_MESSAGE_PREFIXES)
{
return s_MsgPrefixes[index].c_str();
}
else
{
LogWrn(_SC("Attempting to <get player message prefix> using an out of bounds index: %u"), index);
}
return _SC("");
}
// ------------------------------------------------------------------------------------------------
void CPlayer::SetMessagePrefix(SQUint32 index, const SQChar * prefix)
{
if (index < MAX_PLAYER_MESSAGE_PREFIXES)
{
s_MsgPrefixes[index].assign(prefix);
}
else
{
LogWrn(_SC("Attempting to <set player message prefix> using an out of bounds index: %u"), index);
}
}
// ------------------------------------------------------------------------------------------------
SQInteger CPlayer::Msg(HSQUIRRELVM vm)
{
@ -1799,6 +1834,85 @@ SQInteger CPlayer::Msg(HSQUIRRELVM vm)
return 0;
}
// ------------------------------------------------------------------------------------------------
SQInteger CPlayer::MsgP(HSQUIRRELVM vm)
{
const SQInteger top = sq_gettop(vm);
// Are there any arguments on the stack?
if (top <= 1)
{
LogErr("Attempting to <send player message> without specifying a prefix index");
// Failed to send the message
return 0;
}
// Is there a valid prefix index on the stack?
else if (top == 2)
{
LogErr("Attempting to <send player message> without specifying a value");
// Failed to send the message
return 0;
}
// Attempt to retrieve the player instance
Var< CPlayer & > inst(vm, 1);
// Attempt to retrieve the prefix index
Var< SQUint32 > index(vm, 2);
// Validate the player instance
if (!inst.value)
{
LogErr("Attempting to <send player message> using an invalid reference: %d", _SCI32(inst.value));
// Failed to send the message
return 0;
}
else if (index.value > MAX_PLAYER_MESSAGE_PREFIXES)
{
LogErr("Attempting to <send player message> using an out of range prefix: %u", index.value);
// Failed to send the message
return 0;
}
// Is there a single string or at least something that can convert to a string on the stack?
else if (top == 3 && ((sq_gettype(vm, -1) == OT_STRING) || !SQ_FAILED(sq_tostring(vm, -1))))
{
// Variable where the resulted string will be retrieved
const SQChar * msg = 0;
// Attempt to retrieve the specified message from the stack
if (SQ_FAILED(sq_getstring(vm, -1, &msg)))
{
LogErr("Unable to <retrieve the player message> from the stack");
// Pop any pushed values pushed to the stack
sq_settop(vm, top);
// Failed to send the value
return 0;
}
// Pop any pushed values pushed to the stack
sq_settop(vm, top);
// Send the specified string
_Func->SendClientMessage(_SCI32(inst.value), s_MessageColor, "%s%s",
s_MsgPrefixes[index.value].c_str(), msg);
}
else if (top > 3)
{
// Variables containing the resulted string
SQChar * msg = NULL;
SQInteger len = 0;
// Attempt to call the format function with the passed arguments
if (SQ_FAILED(sqstd_format(vm, 3, &len, &msg)))
{
LogErr("Unable to <generate the player message> because : %s", Error::Message(vm).c_str());
// Failed to send the value
return 0;
}
// Send the resulted string
_Func->SendClientMessage(_SCI32(inst.value), s_MessageColor, "%s%s",
s_MsgPrefixes[index.value].c_str(), msg);
}
else
{
LogErr("Unable to <extract the player message> from the specified value");
}
// At this point everything went correctly
return 0;
}
// ------------------------------------------------------------------------------------------------
SQInteger CPlayer::MsgEx(HSQUIRRELVM vm)
{
@ -1909,7 +2023,7 @@ SQInteger CPlayer::Message(HSQUIRRELVM vm)
// Pop any pushed values pushed to the stack
sq_settop(vm, top);
// Send the specified string
_Func->SendClientMessage(_SCI32(inst.value), 0x6599FFFF, "%s", msg);
_Func->SendClientMessage(_SCI32(inst.value), s_MessageColor, "%s", msg);
}
else if (top > 2)
{
@ -1924,7 +2038,7 @@ SQInteger CPlayer::Message(HSQUIRRELVM vm)
return 0;
}
// Send the resulted string
_Func->SendClientMessage(_SCI32(inst.value), 0x6599FFFF, "%s", msg);
_Func->SendClientMessage(_SCI32(inst.value), s_MessageColor, "%s", msg);
}
else
{
@ -2170,6 +2284,7 @@ bool Register_CPlayer(HSQUIRRELVM vm)
.Func(_SC("redirect"), &CPlayer::Redirect)
/* Raw Functions */
.SquirrelFunc(_SC("msg"), &CPlayer::Msg)
.SquirrelFunc(_SC("msgp"), &CPlayer::MsgP)
.SquirrelFunc(_SC("emsg"), &CPlayer::MsgEx)
.SquirrelFunc(_SC("message"), &CPlayer::Message)
.SquirrelFunc(_SC("announce"), &CPlayer::Announce)
@ -2192,6 +2307,14 @@ bool Register_CPlayer(HSQUIRRELVM vm)
);
// Output debugging information
LogDbg("Registration of <CPlayer> type was successful");
// Output debugging information
LogDbg("Beginning registration of <Player functions> type");
// Several global functions
Sqrat::RootTable(vm).Func(_SC("GetPlayerMsgPrefix"), &CPlayer::GetMessagePrefix);
Sqrat::RootTable(vm).Func(_SC("SetPlayerMsgPrefix"), &CPlayer::SetMessagePrefix);
// Output debugging information
LogDbg("Registration of <Player functions> type was successful");
// Registration succeeded
return true;
}

View File

@ -4,14 +4,24 @@
// ------------------------------------------------------------------------------------------------
#include "Entity.hpp"
// ------------------------------------------------------------------------------------------------
#include <array>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
#define MAX_PLAYER_MESSAGE_PREFIXES 128
#define MAX_PLAYER_TEMPORARY_BUFFER 128
/* ------------------------------------------------------------------------------------------------
* Class responsible for managing the referenced player instance.
*/
class CPlayer : public Reference< CPlayer >
{
// --------------------------------------------------------------------------------------------
typedef std::array< String, MAX_PLAYER_MESSAGE_PREFIXES > Prefixes;
// --------------------------------------------------------------------------------------------
static CSkin s_Skin;
static CWeapon s_Weapon;
@ -21,7 +31,14 @@ class CPlayer : public Reference< CPlayer >
static Vector3 s_Vector3;
// --------------------------------------------------------------------------------------------
static SQChar s_Buffer[128];
static SQChar s_Buffer[MAX_PLAYER_TEMPORARY_BUFFER];
// --------------------------------------------------------------------------------------------
static Prefixes s_MsgPrefixes;
// --------------------------------------------------------------------------------------------
static SQUint32 s_MessageColor;
static SQInt32 s_AnnounceStyle;
public:
@ -93,7 +110,7 @@ public:
/* --------------------------------------------------------------------------------------------
* Retrieve the key of the referenced player instance.
*/
SQUnsignedInteger GetKey() const;
SQUint32 GetKey() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the world in which the referenced player instance exists.
@ -608,14 +625,29 @@ public:
/* --------------------------------------------------------------------------------------------
* Redirect the referenced player instance to the specified server.
*/
bool Redirect(const SQChar * ip, SQUnsignedInteger port, const SQChar * nick, \
bool Redirect(const SQChar * ip, SQUint32 port, const SQChar * nick, \
const SQChar * pass, const SQChar * user);
/* --------------------------------------------------------------------------------------------
* Retrive the current value of a predefined message prefix.
*/
static const SQChar * GetMessagePrefix(SQUint32 index);
/* --------------------------------------------------------------------------------------------
* Change the current value of a predefined message prefix.
*/
static void SetMessagePrefix(SQUint32 index, const SQChar * prefix);
/* --------------------------------------------------------------------------------------------
* Send a chat message to the referenced player instance.
*/
static SQInteger Msg(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Send a chat message to the referenced player instance.
*/
static SQInteger MsgP(HSQUIRRELVM vm);
/* --------------------------------------------------------------------------------------------
* Send a chat message to the referenced player instance.
*/
@ -635,7 +667,6 @@ public:
* Send an announcement message to the referenced player instance.
*/
static SQInteger AnnounceEx(HSQUIRRELVM vm);
};
} // Namespace:: SqMod