Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial messages support #3

Merged
merged 2 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ SPMod is licensed under the General Public License version 3 - see the [LICENSE]

## Acknowledgments

The SPMod uses code from the following libraries:
The SPMod uses code from the following libraries, projects:
- [Printf Implementation](https://github.com/mpaland/printf), The MIT License (MIT)
- [AMX Mod X](https://github.com/alliedmodders/amxmodx), GNU GPLv3
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 MessageBegin()
*/
#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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here must be a return type int

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, I overlooked it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in master.


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

native void MessageBeginF(int dest, int msgid, const float origin[3] = {0.0,0.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
236 changes: 236 additions & 0 deletions src/MessageNatives.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/* 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)
{
enum { arg_msgname = 1 };

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

return GET_USER_MSG_ID(PLID, string, nullptr);
}

// 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], nullptr);
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)
{
enum { arg_dest = 1, arg_msg_type, arg_origin, arg_player };

int numparam = *params;
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], nullptr)))
{
ctx->ReportError("Plugin called message_begin with an invalid message id (%d).", params[arg_msg_type]);
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], nullptr);
break;
case MSG_PVS: case MSG_PAS:
case MSG_PVS_R: case MSG_PAS_R:
if (numparam < 3)
{
ctx->ReportError("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)
{
ctx->ReportError("Invalid number of parameters passed");
return 0;
}

MESSAGE_BEGIN(params[arg_dest], params[arg_msg_type], nullptr, 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)
{
return MessageBegin_(ctx, params, false);
}

// 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)
{
return MessageBegin_(ctx, params, true);
}

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

// native void WriteByte(int value);
static cell_t WriteByte(SourcePawn::IPluginContext *ctx [[maybe_unused]],
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 [[maybe_unused]],
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 [[maybe_unused]],
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 [[maybe_unused]],
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 [[maybe_unused]],
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 [[maybe_unused]],
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 [[maybe_unused]],
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 [[maybe_unused]],
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 [[maybe_unused]],
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();