Skip to content

Commit

Permalink
initial messages support
Browse files Browse the repository at this point in the history
copy from amxmodx
  • Loading branch information
Mistrick committed Jun 13, 2018
1 parent 2c09354 commit ed4bc66
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 0 deletions.
67 changes: 67 additions & 0 deletions scripts/include/messages.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (C) 2018 SPMod Development Team
*
* This file is part of SPMod.
*
* SPMod is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#if defined _messages_included
#endinput
#endif
#define _messages_included

/**
* Destination types for message_begin()
*/
#define MSG_BROADCAST 0 // Unreliable to all
#define MSG_ONE 1 // Reliable to one (msg_entity)
#define MSG_ALL 2 // Reliable to all
#define MSG_INIT 3 // Write to the init string
#define MSG_PVS 4 // Ents in PVS of org
#define MSG_PAS 5 // Ents in PAS of org
#define MSG_PVS_R 6 // Reliable to PVS
#define MSG_PAS_R 7 // Reliable to PAS
#define MSG_ONE_UNRELIABLE 8 // Send to one client, but don't put in reliable stream, put in unreliable datagram (could be dropped)
#define MSG_SPEC 9 // Sends to all spectator proxies

native int GetUserMsgId(const char[] msg_name);

native void GetUserMsgName(int msgid, char[] str, int len);

native void MessageBegin(int dest, int msgid, const int origin[3] = {0,0,0}, int player = 0);

native void MessageBegin(int dest, int msgid, const float origin[3] = {0,0,0}, int player = 0);

native void MessageEnd();

native void WriteByte(int value);

native void WriteChar(char value);

native void WriteShort(int value);

native void WriteLong(int value);

native void WriteEntity(int value);

native void WriteAngle(int value);

native void WriteAngleF(float value);

native void WriteCoord(int value);

native void WriteCoordF(float value);

native void WriteString(const char[] value);
1 change: 1 addition & 0 deletions scripts/include/spmod.inc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum PluginReturn
#include <natives>
#include <forwards>
#include <string>
#include <messages>

/*
* @brief Provides info about plugin.
Expand Down
222 changes: 222 additions & 0 deletions src/MessageNatives.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/* SPMod - SourcePawn Scripting Engine for Half-Life
* Copyright (C) 2018 SPMod Development Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "spmod.hpp"

// int GetUserMsgId(const char[] msgname);
static cell_t GetUserMsgId(SourcePawn::IPluginContext *ctx, const cell_t *params) /* 1 param */
{
enum { arg_msgname = 1 };

char *string;
ctx->LocalToString(params[arg_msgname], &string);

return GET_USER_MSG_ID(PLID, string, NULL);
}

// void GetUserMsgName(int msgid, char[] str, int len) = 3 params
static cell_t GetUserMsgName(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_msg = 1, arg_str, arg_len };

const char* string = GET_USER_MSG_NAME(PLID, params[arg_msg], NULL);
if (string)
{
ctx->StringToLocal(params[arg_str], params[arg_len], string);
return strlen(string);
}

return 0;
}

static cell_t MessageBegin_(SourcePawn::IPluginContext *ctx, const cell_t *params, bool useFloat) /* 4 param */
{
enum { arg_dest = 1, arg_msg_type, arg_origin, arg_player };

int numparam = *params / sizeof(cell_t);
float vecOrigin[3];
cell_t *cpOrigin;

if (params[arg_msg_type] < 1 || ((params[arg_msg_type] > 63) // maximal number of engine messages
&& !GET_USER_MSG_NAME(PLID, params[2], NULL)))
{
// LogError(amx, AMX_ERR_NATIVE, "Plugin called message_begin with an invalid message id (%d).", params[2]);
return 0;
}

switch (params[arg_dest])
{
case MSG_BROADCAST:
case MSG_ALL:
case MSG_SPEC:
case MSG_INIT:
MESSAGE_BEGIN(params[arg_dest], params[arg_msg_type], NULL);
break;
case MSG_PVS: case MSG_PAS:
case MSG_PVS_R: case MSG_PAS_R:
if (numparam < 3)
{
// LogError(amx, AMX_ERR_NATIVE, "Invalid number of parameters passed");
return 0;
}

ctx->LocalToPhysAddr(params[arg_origin], &cpOrigin);

if (!useFloat)
{
vecOrigin[0] = static_cast<float>(*cpOrigin);
vecOrigin[1] = static_cast<float>(*(cpOrigin + 1));
vecOrigin[2] = static_cast<float>(*(cpOrigin + 2));
}
else
{
vecOrigin[0] = sp_ctof(*cpOrigin);
vecOrigin[1] = sp_ctof(*(cpOrigin + 1));
vecOrigin[2] = sp_ctof(*(cpOrigin + 2));
}

MESSAGE_BEGIN(params[arg_dest], params[arg_msg_type], vecOrigin);

break;
case MSG_ONE_UNRELIABLE:
case MSG_ONE:
if (numparam < 4)
{
// LogError(amx, AMX_ERR_NATIVE, "Invalid number of parameters passed");
return 0;
}

MESSAGE_BEGIN(params[arg_dest], params[arg_msg_type], NULL, INDEXENT(params[arg_player]));
break;
}

return 1;
}

// native void MessageBegin(int dest, int msgid, const int origin[3] = {0,0,0}, int player = 0);
static cell_t MessageBegin(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
MessageBegin_(ctx, params, false);
return 1;
}

// native void MessageBeginF(int dest, int msgid, const float origin[3] = {0.0,0.0,0.0}, int player = 0);
static cell_t MessageBeginF(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
MessageBegin_(ctx, params, true);
return 1;
}

// native void MessageEnd();
static cell_t MessageEnd(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
MESSAGE_END();
return 1;
}

// native void WriteByte(int value);
static cell_t WriteByte(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_BYTE(params[arg_value]);
return 1;
}
// native void WriteChar(char value);
static cell_t WriteChar(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_CHAR(params[arg_value]);
return 1;
}
// native void WriteShort(int value);
static cell_t WriteShort(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_SHORT(params[arg_value]);
return 1;
}
// native void WriteLong(int value);
static cell_t WriteLong(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_LONG(params[arg_value]);
return 1;
}
// native void WriteEntity(int value);
static cell_t WriteEntity(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_ENTITY(params[arg_value]);
return 1;
}
// native void WriteAngle(int value);
static cell_t WriteAngle(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_ANGLE(static_cast<float>(params[arg_value]));
return 1;
}
// native void WriteAngleF(float value);
static cell_t WriteAngleF(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_ANGLE(sp_ctof(params[arg_value]));
return 1;
}
// native void WriteCoord(int value);
static cell_t WriteCoord(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_COORD(static_cast<float>(params[arg_value]));
return 1;
}
// native void WriteCoordF(float value);
static cell_t WriteCoordF(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
WRITE_COORD(sp_ctof(params[arg_value]));
return 1;
}
// native void WriteString(char[] value);
static cell_t WriteString(SourcePawn::IPluginContext *ctx, const cell_t *params)
{
enum { arg_value = 1 };
char *string;
ctx->LocalToString(params[arg_value], &string);
WRITE_STRING(string);
return 1;
}

sp_nativeinfo_t gMessageNatives[] =
{
{ "GetUserMsgId", GetUserMsgId },
{ "GetUserMsgName", GetUserMsgName },
{ "MessageBegin", MessageBegin },
{ "MessageBeginF", MessageBeginF },
{ "MessageEnd", MessageEnd },
{ "WriteByte", WriteByte },
{ "WriteChar", WriteChar },
{ "WriteShort", WriteShort },
{ "WriteLong", WriteLong },
{ "WriteEntity", WriteEntity },
{ "WriteAngle", WriteAngle },
{ "WriteAngleF", WriteAngleF },
{ "WriteCoord", WriteCoord },
{ "WriteCoordF", WriteCoordF },
{ "WriteString", WriteString },
{ nullptr, nullptr }
};
1 change: 1 addition & 0 deletions src/SPGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ SPGlobal::SPGlobal(fs::path &&dllDir) : m_SPModDir(dllDir.parent_path().parent_p
m_nativeManager->addNatives(gSPModModuleDef.get(), gCvarsNatives);
m_nativeManager->addNatives(gSPModModuleDef.get(), gForwardsNatives);
m_nativeManager->addNatives(gSPModModuleDef.get(), gStringNatives);
m_nativeManager->addNatives(gSPModModuleDef.get(), gMessageNatives);

// Sets up listener for debbugging
getSPEnvironment()->APIv2()->SetDebugListener(m_loggingSystem.get());
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sourceFiles = files('coreNatives.cpp',
'CvarsNatives.cpp',
'ForwardsNatives.cpp',
'StringNatives.cpp',
'MessageNatives.cpp',
'h_export.cpp',
'engine_api.cpp',
'dllapi.cpp',
Expand Down
1 change: 1 addition & 0 deletions src/spmod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ extern sp_nativeinfo_t gCoreNatives[];
extern sp_nativeinfo_t gCvarsNatives[];
extern sp_nativeinfo_t gForwardsNatives[];
extern sp_nativeinfo_t gStringNatives[];
extern sp_nativeinfo_t gMessageNatives[];

// Server command function (SrvCommand.cpp)
void SPModInfoCommand();

0 comments on commit ed4bc66

Please sign in to comment.