Add functions for working with entity lumps (#1673)
This commit is contained in:
@@ -84,6 +84,8 @@ for cxx in builder.targets:
|
||||
'smn_halflife.cpp',
|
||||
'FrameIterator.cpp',
|
||||
'DatabaseConfBuilder.cpp',
|
||||
'LumpManager.cpp',
|
||||
'smn_entitylump.cpp',
|
||||
]
|
||||
|
||||
if binary.compiler.target.arch == 'x86_64':
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* =============================================================================
|
||||
* Entity Lump Manager
|
||||
* Copyright (C) 2021-2022 AlliedModders LLC. All rights reserved.
|
||||
* =============================================================================
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||
* Free Software Foundation.
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||
* this exception to all derivative works. AlliedModders LLC defines further
|
||||
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||
* or <http://www.sourcemod.net/license.php>.
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#include "LumpManager.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
EntityLumpParseResult::operator bool() const {
|
||||
return m_Status == Status_OK;
|
||||
}
|
||||
|
||||
EntityLumpParseResult EntityLumpManager::Parse(const char* pMapEntities) {
|
||||
m_Entities.clear();
|
||||
|
||||
std::istringstream mapEntities(pMapEntities);
|
||||
|
||||
for (;;) {
|
||||
std::string token;
|
||||
mapEntities >> std::ws >> token >> std::ws;
|
||||
|
||||
// Assert that we're at the start of a new block, otherwise we're done parsing
|
||||
if (token != "{") {
|
||||
if (token == "\0") {
|
||||
break;
|
||||
} else {
|
||||
return EntityLumpParseResult {
|
||||
Status_UnexpectedChar, mapEntities.tellg()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse key / value pairs until we reach a closing brace. We currently assume there
|
||||
* are only quoted keys / values up to the next closing brace.
|
||||
*
|
||||
* The SDK suggests that there are cases that could use non-quoted symbols and nested
|
||||
* braces (`shared/mapentities_shared.cpp::MapEntity_ParseToken`), but I haven't seen
|
||||
* those in practice.
|
||||
*/
|
||||
EntityLumpEntry entry;
|
||||
while (mapEntities.peek() != '}') {
|
||||
std::string key, value;
|
||||
|
||||
if (mapEntities.peek() != '"') {
|
||||
return EntityLumpParseResult {
|
||||
Status_UnexpectedChar, mapEntities.tellg()
|
||||
};
|
||||
}
|
||||
mapEntities >> quoted(key) >> std::ws;
|
||||
|
||||
if (mapEntities.peek() != '"') {
|
||||
return EntityLumpParseResult {
|
||||
Status_UnexpectedChar, mapEntities.tellg()
|
||||
};
|
||||
}
|
||||
mapEntities >> quoted(value) >> std::ws;
|
||||
|
||||
entry.emplace_back(key, value);
|
||||
}
|
||||
mapEntities.get();
|
||||
m_Entities.push_back(std::make_shared<EntityLumpEntry>(entry));
|
||||
}
|
||||
|
||||
return EntityLumpParseResult{};
|
||||
}
|
||||
|
||||
std::string EntityLumpManager::Dump() {
|
||||
std::ostringstream stream;
|
||||
for (const auto& entry : m_Entities) {
|
||||
// ignore empty entries
|
||||
if (entry->empty()) {
|
||||
continue;
|
||||
}
|
||||
stream << "{\n";
|
||||
for (const auto& pair : *entry) {
|
||||
stream << '"' << pair.first << "\" \"" << pair.second << '"' << '\n';
|
||||
}
|
||||
stream << "}\n";
|
||||
}
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
std::weak_ptr<EntityLumpEntry> EntityLumpManager::Get(size_t index) {
|
||||
return m_Entities[index];
|
||||
}
|
||||
|
||||
void EntityLumpManager::Erase(size_t index) {
|
||||
m_Entities.erase(m_Entities.begin() + index);
|
||||
}
|
||||
|
||||
void EntityLumpManager::Insert(size_t index) {
|
||||
m_Entities.emplace(m_Entities.begin() + index, std::make_shared<EntityLumpEntry>());
|
||||
}
|
||||
|
||||
size_t EntityLumpManager::Append() {
|
||||
return std::distance(
|
||||
m_Entities.begin(),
|
||||
m_Entities.emplace(m_Entities.end(), std::make_shared<EntityLumpEntry>())
|
||||
);
|
||||
}
|
||||
|
||||
size_t EntityLumpManager::Length() {
|
||||
return m_Entities.size();
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* =============================================================================
|
||||
* Entity Lump Manager
|
||||
* Copyright (C) 2021-2022 AlliedModders LLC. All rights reserved.
|
||||
* =============================================================================
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||
* Free Software Foundation.
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||
* this exception to all derivative works. AlliedModders LLC defines further
|
||||
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||
* or <http://www.sourcemod.net/license.php>.
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#ifndef _INCLUDE_LUMPMANAGER_H_
|
||||
#define _INCLUDE_LUMPMANAGER_H_
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Entity lump manager. Provides a list that stores a list of key / value pairs and the
|
||||
* functionality to (de)serialize it from / to an entity string.
|
||||
* This file and its corresponding .cpp should be compilable independently of SourceMod;
|
||||
* the SourceMod interop is located within smn_entitylump.
|
||||
*
|
||||
* @file lumpmanager.h
|
||||
* @brief Class definition for object that parses lumps.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief A container of key / value pairs.
|
||||
*/
|
||||
using EntityLumpEntry = std::vector<std::pair<std::string, std::string>>;
|
||||
|
||||
enum EntityLumpParseStatus {
|
||||
Status_OK,
|
||||
Status_UnexpectedChar,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Result of parsing an entity lump. On a parse error, m_Status is not Status_OK and
|
||||
* m_Position indicates the offset within the string that caused the parse error.
|
||||
*/
|
||||
struct EntityLumpParseResult {
|
||||
EntityLumpParseStatus m_Status;
|
||||
std::streamoff m_Position;
|
||||
|
||||
operator bool() const;
|
||||
const char* Description() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Manages entity lump entries.
|
||||
*/
|
||||
class EntityLumpManager
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Parses the map entities string into an internal representation.
|
||||
*/
|
||||
EntityLumpParseResult Parse(const char* pMapEntities);
|
||||
|
||||
/**
|
||||
* @brief Dumps the current internal representation out to an std::string.
|
||||
*/
|
||||
std::string Dump();
|
||||
|
||||
/**
|
||||
* @brief Returns a weak reference to an EntityLumpEntry. Used for handles on the scripting side.
|
||||
*/
|
||||
std::weak_ptr<EntityLumpEntry> Get(size_t index);
|
||||
|
||||
/**
|
||||
* @brief Removes an EntityLumpEntry at the given index, shifting down all entries after it by one.
|
||||
*/
|
||||
void Erase(size_t index);
|
||||
|
||||
/**
|
||||
* @brief Inserts a new EntityLumpEntry at the given index, shifting up the entries previously at the index and after it up by one.
|
||||
*/
|
||||
void Insert(size_t index);
|
||||
|
||||
/**
|
||||
* @brief Adds a new EntityLumpEntry to the end. Returns the index of the entry.
|
||||
*/
|
||||
size_t Append();
|
||||
|
||||
/**
|
||||
* @brief Returns the number of EntityLumpEntry items in the list.
|
||||
*/
|
||||
size_t Length();
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<EntityLumpEntry>> m_Entities;
|
||||
};
|
||||
|
||||
#endif // _INCLUDE_LUMPMANAGER_H_
|
||||
@@ -56,6 +56,7 @@
|
||||
#include "LibrarySys.h"
|
||||
#include "RootConsoleMenu.h"
|
||||
#include "CellArray.h"
|
||||
#include "smn_entitylump.h"
|
||||
#include <bridge/include/BridgeAPI.h>
|
||||
#include <bridge/include/IProviderCallbacks.h>
|
||||
|
||||
@@ -89,6 +90,8 @@ CNativeOwner g_CoreNatives;
|
||||
PseudoAddressManager pseudoAddr;
|
||||
#endif
|
||||
|
||||
EntityLumpParseResult lastParseResult;
|
||||
|
||||
static void AddCorePhraseFile(const char *filename)
|
||||
{
|
||||
g_pCorePhrases->AddPhraseFile(filename);
|
||||
@@ -135,6 +138,35 @@ static uint32_t ToPseudoAddress(void *addr)
|
||||
#endif
|
||||
}
|
||||
|
||||
static void SetEntityLumpWritable(bool writable)
|
||||
{
|
||||
g_bLumpAvailableForWriting = writable;
|
||||
|
||||
// write-lock causes the map entities to be serialized out to string
|
||||
if (!writable)
|
||||
{
|
||||
g_strMapEntities = lumpmanager->Dump();
|
||||
}
|
||||
}
|
||||
|
||||
static bool ParseEntityLumpString(const char *pMapEntities, int &status, size_t &position)
|
||||
{
|
||||
lastParseResult = lumpmanager->Parse(pMapEntities);
|
||||
status = static_cast<int>(lastParseResult.m_Status);
|
||||
position = static_cast<size_t>(lastParseResult.m_Position);
|
||||
return lastParseResult;
|
||||
}
|
||||
|
||||
// returns nullptr if the original lump failed to parse
|
||||
static const char* GetEntityLumpString()
|
||||
{
|
||||
if (!lastParseResult)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return g_strMapEntities.c_str();
|
||||
}
|
||||
|
||||
// Defined in smn_filesystem.cpp.
|
||||
extern bool OnLogPrint(const char *msg);
|
||||
|
||||
@@ -170,6 +202,9 @@ static sm_logic_t logic =
|
||||
CellArray::Free,
|
||||
FromPseudoAddress,
|
||||
ToPseudoAddress,
|
||||
SetEntityLumpWritable,
|
||||
ParseEntityLumpString,
|
||||
GetEntityLumpString,
|
||||
&g_PluginSys,
|
||||
&g_ShareSys,
|
||||
&g_Extensions,
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* =============================================================================
|
||||
* Entity Lump Manager
|
||||
* Copyright (C) 2021-2022 AlliedModders LLC. All rights reserved.
|
||||
* =============================================================================
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||
* Free Software Foundation.
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||
* this exception to all derivative works. AlliedModders LLC defines further
|
||||
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||
* or <http://www.sourcemod.net/license.php>.
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#include "HandleSys.h"
|
||||
#include "common_logic.h"
|
||||
|
||||
#include "LumpManager.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
HandleType_t g_EntityLumpEntryType;
|
||||
|
||||
std::string g_strMapEntities;
|
||||
bool g_bLumpAvailableForWriting = false;
|
||||
|
||||
static EntityLumpManager s_LumpManager;
|
||||
EntityLumpManager *lumpmanager = &s_LumpManager;
|
||||
|
||||
class LumpManagerNatives :
|
||||
public IHandleTypeDispatch,
|
||||
public SMGlobalClass
|
||||
{
|
||||
public: //SMGlobalClass
|
||||
void OnSourceModAllInitialized()
|
||||
{
|
||||
g_EntityLumpEntryType = handlesys->CreateType("EntityLumpEntry", this, 0, NULL, NULL, g_pCoreIdent, NULL);
|
||||
}
|
||||
void OnSourceModShutdown()
|
||||
{
|
||||
handlesys->RemoveType(g_EntityLumpEntryType, g_pCoreIdent);
|
||||
}
|
||||
public: //IHandleTypeDispatch
|
||||
void OnHandleDestroy(HandleType_t type, void* object)
|
||||
{
|
||||
if (type == g_EntityLumpEntryType)
|
||||
{
|
||||
delete reinterpret_cast<std::weak_ptr<EntityLumpEntry>*>(object);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static LumpManagerNatives s_LumpManagerNatives;
|
||||
|
||||
cell_t sm_LumpManagerGet(IPluginContext *pContext, const cell_t *params) {
|
||||
int index = params[1];
|
||||
if (index < 0 || index >= static_cast<int>(lumpmanager->Length())) {
|
||||
return pContext->ThrowNativeError("Invalid index %d", index);
|
||||
}
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* pReference = new std::weak_ptr<EntityLumpEntry>;
|
||||
*pReference = lumpmanager->Get(index);
|
||||
|
||||
return handlesys->CreateHandle(g_EntityLumpEntryType, pReference,
|
||||
pContext->GetIdentity(), g_pCoreIdent, NULL);
|
||||
}
|
||||
|
||||
cell_t sm_LumpManagerErase(IPluginContext *pContext, const cell_t *params) {
|
||||
if (!g_bLumpAvailableForWriting) {
|
||||
return pContext->ThrowNativeError("Cannot use EntityLump.Erase() outside of OnMapInit");
|
||||
}
|
||||
|
||||
int index = params[1];
|
||||
if (index < 0 || index >= static_cast<int>(lumpmanager->Length())) {
|
||||
return pContext->ThrowNativeError("Invalid index %d", index);
|
||||
}
|
||||
|
||||
lumpmanager->Erase(index);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cell_t sm_LumpManagerInsert(IPluginContext *pContext, const cell_t *params) {
|
||||
if (!g_bLumpAvailableForWriting) {
|
||||
return pContext->ThrowNativeError("Cannot use EntityLump.Insert() outside of OnMapInit");
|
||||
}
|
||||
|
||||
int index = params[1];
|
||||
if (index < 0 || index > static_cast<int>(lumpmanager->Length())) {
|
||||
return pContext->ThrowNativeError("Invalid index %d", index);
|
||||
}
|
||||
|
||||
lumpmanager->Insert(index);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cell_t sm_LumpManagerAppend(IPluginContext *pContext, const cell_t *params) {
|
||||
if (!g_bLumpAvailableForWriting) {
|
||||
return pContext->ThrowNativeError("Cannot use EntityLump.Append() outside of OnMapInit");
|
||||
}
|
||||
return lumpmanager->Append();
|
||||
}
|
||||
|
||||
cell_t sm_LumpManagerLength(IPluginContext *pContext, const cell_t *params) {
|
||||
return lumpmanager->Length();
|
||||
}
|
||||
|
||||
cell_t sm_LumpEntryGet(IPluginContext *pContext, const cell_t *params) {
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
HandleError err;
|
||||
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* entryref = nullptr;
|
||||
if ((err = handlesys->ReadHandle(hndl, g_EntityLumpEntryType, &sec, (void**) &entryref)) != HandleError_None) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (error: %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (entryref->expired()) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (reference expired)", hndl);
|
||||
}
|
||||
|
||||
auto entry = entryref->lock();
|
||||
|
||||
int index = params[2];
|
||||
if (index < 0 || index >= static_cast<int>(entry->size())) {
|
||||
return pContext->ThrowNativeError("Invalid index %d", index);
|
||||
}
|
||||
|
||||
const auto& pair = (*entry)[index];
|
||||
|
||||
size_t nBytes;
|
||||
pContext->StringToLocalUTF8(params[3], params[4], pair.first.c_str(), &nBytes);
|
||||
pContext->StringToLocalUTF8(params[5], params[6], pair.second.c_str(), &nBytes);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cell_t sm_LumpEntryUpdate(IPluginContext *pContext, const cell_t *params) {
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
HandleError err;
|
||||
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* entryref = nullptr;
|
||||
if ((err = handlesys->ReadHandle(hndl, g_EntityLumpEntryType, &sec, (void**) &entryref)) != HandleError_None) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (error: %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (entryref->expired()) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (reference expired)", hndl);
|
||||
}
|
||||
|
||||
if (!g_bLumpAvailableForWriting) {
|
||||
return pContext->ThrowNativeError("Cannot use EntityLumpEntry.Update() outside of OnMapInit");
|
||||
}
|
||||
|
||||
auto entry = entryref->lock();
|
||||
|
||||
int index = params[2];
|
||||
if (index < 0 || index >= static_cast<int>(entry->size())) {
|
||||
return pContext->ThrowNativeError("Invalid index %d", index);
|
||||
}
|
||||
|
||||
char *key, *value;
|
||||
pContext->LocalToStringNULL(params[3], &key);
|
||||
pContext->LocalToStringNULL(params[4], &value);
|
||||
|
||||
auto& pair = (*entry)[index];
|
||||
if (key != nullptr) {
|
||||
pair.first = key;
|
||||
}
|
||||
if (value != nullptr) {
|
||||
pair.second = value;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cell_t sm_LumpEntryInsert(IPluginContext *pContext, const cell_t *params) {
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
HandleError err;
|
||||
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* entryref = nullptr;
|
||||
if ((err = handlesys->ReadHandle(hndl, g_EntityLumpEntryType, &sec, (void**) &entryref)) != HandleError_None) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (error: %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (entryref->expired()) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (reference expired)", hndl);
|
||||
}
|
||||
|
||||
if (!g_bLumpAvailableForWriting) {
|
||||
return pContext->ThrowNativeError("Cannot use EntityLumpEntry.Insert() outside of OnMapInit");
|
||||
}
|
||||
|
||||
auto entry = entryref->lock();
|
||||
|
||||
int index = params[2];
|
||||
if (index < 0 || index > static_cast<int>(entry->size())) {
|
||||
return pContext->ThrowNativeError("Invalid index %d", index);
|
||||
}
|
||||
|
||||
char *key, *value;
|
||||
pContext->LocalToString(params[3], &key);
|
||||
pContext->LocalToString(params[4], &value);
|
||||
|
||||
entry->emplace(entry->begin() + index, key, value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cell_t sm_LumpEntryErase(IPluginContext *pContext, const cell_t *params) {
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
HandleError err;
|
||||
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* entryref = nullptr;
|
||||
if ((err = handlesys->ReadHandle(hndl, g_EntityLumpEntryType, &sec, (void**) &entryref)) != HandleError_None) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (error: %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (entryref->expired()) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (reference expired)", hndl);
|
||||
}
|
||||
|
||||
if (!g_bLumpAvailableForWriting) {
|
||||
return pContext->ThrowNativeError("Cannot use EntityLumpEntry.Erase() outside of OnMapInit");
|
||||
}
|
||||
|
||||
auto entry = entryref->lock();
|
||||
|
||||
int index = params[2];
|
||||
if (index < 0 || index >= static_cast<int>(entry->size())) {
|
||||
return pContext->ThrowNativeError("Invalid index %d", index);
|
||||
}
|
||||
|
||||
entry->erase(entry->begin() + index);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cell_t sm_LumpEntryAppend(IPluginContext *pContext, const cell_t *params) {
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
HandleError err;
|
||||
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* entryref = nullptr;
|
||||
if ((err = handlesys->ReadHandle(hndl, g_EntityLumpEntryType, &sec, (void**) &entryref)) != HandleError_None) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (error: %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (entryref->expired()) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (reference expired)", hndl);
|
||||
}
|
||||
|
||||
if (!g_bLumpAvailableForWriting) {
|
||||
return pContext->ThrowNativeError("Cannot use EntityLumpEntry.Append() outside of OnMapInit");
|
||||
}
|
||||
|
||||
auto entry = entryref->lock();
|
||||
|
||||
char *key, *value;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
pContext->LocalToString(params[3], &value);
|
||||
|
||||
entry->emplace_back(key, value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
cell_t sm_LumpEntryFindKey(IPluginContext *pContext, const cell_t *params) {
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
HandleError err;
|
||||
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* entryref = nullptr;
|
||||
if ((err = handlesys->ReadHandle(hndl, g_EntityLumpEntryType, &sec, (void**) &entryref)) != HandleError_None) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (error: %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (entryref->expired()) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (reference expired)", hndl);
|
||||
}
|
||||
|
||||
// start from the index after the current one
|
||||
int start = params[3] + 1;
|
||||
|
||||
auto entry = entryref->lock();
|
||||
|
||||
if (start < 0 || start >= static_cast<int>(entry->size())) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *key;
|
||||
pContext->LocalToString(params[2], &key);
|
||||
|
||||
auto matches_key = [&key](std::pair<std::string,std::string> pair) {
|
||||
return pair.first == key;
|
||||
};
|
||||
|
||||
auto result = std::find_if(entry->begin() + start, entry->end(), matches_key);
|
||||
|
||||
if (result == entry->end()) {
|
||||
return -1;
|
||||
}
|
||||
return std::distance(entry->begin(), result);
|
||||
}
|
||||
|
||||
cell_t sm_LumpEntryLength(IPluginContext *pContext, const cell_t *params) {
|
||||
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
||||
HandleError err;
|
||||
|
||||
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
||||
|
||||
std::weak_ptr<EntityLumpEntry>* entryref = nullptr;
|
||||
if ((err = handlesys->ReadHandle(hndl, g_EntityLumpEntryType, &sec, (void**) &entryref)) != HandleError_None) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (error: %d)", hndl, err);
|
||||
}
|
||||
|
||||
if (entryref->expired()) {
|
||||
return pContext->ThrowNativeError("Invalid EntityLumpEntry handle %x (reference expired)", hndl);
|
||||
}
|
||||
|
||||
auto entry = entryref->lock();
|
||||
return entry->size();
|
||||
}
|
||||
|
||||
REGISTER_NATIVES(entityLumpNatives)
|
||||
{
|
||||
{ "EntityLump.Get", sm_LumpManagerGet },
|
||||
{ "EntityLump.Erase", sm_LumpManagerErase },
|
||||
{ "EntityLump.Insert", sm_LumpManagerInsert },
|
||||
{ "EntityLump.Append", sm_LumpManagerAppend },
|
||||
{ "EntityLump.Length", sm_LumpManagerLength },
|
||||
|
||||
{ "EntityLumpEntry.Get", sm_LumpEntryGet },
|
||||
{ "EntityLumpEntry.Update", sm_LumpEntryUpdate },
|
||||
{ "EntityLumpEntry.Insert", sm_LumpEntryInsert },
|
||||
{ "EntityLumpEntry.Erase", sm_LumpEntryErase },
|
||||
{ "EntityLumpEntry.Append", sm_LumpEntryAppend },
|
||||
{ "EntityLumpEntry.FindKey", sm_LumpEntryFindKey },
|
||||
{ "EntityLumpEntry.Length.get", sm_LumpEntryLength },
|
||||
|
||||
{NULL, NULL}
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* =============================================================================
|
||||
* SourceMod
|
||||
* Copyright (C) 2022-2022 AlliedModders LLC. All rights reserved.
|
||||
* =============================================================================
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, version 3.0, as published by the
|
||||
* Free Software Foundation.
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* As a special exception, AlliedModders LLC gives you permission to link the
|
||||
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
||||
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
||||
* by the Valve Corporation. You must obey the GNU General Public License in
|
||||
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
||||
* this exception to all derivative works. AlliedModders LLC defines further
|
||||
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
||||
* or <http://www.sourcemod.net/license.php>.
|
||||
*
|
||||
* Version: $Id$
|
||||
*/
|
||||
|
||||
#ifndef _INCLUDE_SOURCEMOD_ENTITYLUMP_H_
|
||||
#define _INCLUDE_SOURCEMOD_ENTITYLUMP_H_
|
||||
|
||||
#include <IHandleSys.h>
|
||||
#include "LumpManager.h"
|
||||
|
||||
using namespace SourceMod;
|
||||
|
||||
extern std::string g_strMapEntities;
|
||||
extern bool g_bLumpAvailableForWriting;
|
||||
extern EntityLumpManager *lumpmanager;
|
||||
|
||||
#endif // _INCLUDE_SOURCEMOD_ENTITYLUMP_H_
|
||||
+26
-1
@@ -54,6 +54,7 @@ SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
|
||||
SH_DECL_HOOK1_void(IServerGameDLL, GameFrame, SH_NOATTRIB, false, bool);
|
||||
SH_DECL_HOOK1_void(IServerGameDLL, Think, SH_NOATTRIB, false, bool);
|
||||
SH_DECL_HOOK1_void(IVEngineServer, ServerCommand, SH_NOATTRIB, false, const char *);
|
||||
SH_DECL_HOOK0(IVEngineServer, GetMapEntitiesString, SH_NOATTRIB, 0, const char *);
|
||||
|
||||
SourceModBase g_SourceMod;
|
||||
|
||||
@@ -278,6 +279,7 @@ bool SourceModBase::InitializeSourceMod(char *error, size_t maxlength, bool late
|
||||
|
||||
/* Hook this now so we can detect startup without calling StartSourceMod() */
|
||||
SH_ADD_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &SourceModBase::LevelInit), false);
|
||||
SH_ADD_HOOK(IVEngineServer, GetMapEntitiesString, engine, SH_MEMBER(this, &SourceModBase::GetMapEntitiesString), false);
|
||||
|
||||
/* Only load if we're not late */
|
||||
if (!late)
|
||||
@@ -416,10 +418,32 @@ bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, ch
|
||||
|
||||
g_LevelEndBarrier = true;
|
||||
|
||||
int parseError;
|
||||
size_t position;
|
||||
bool success = logicore.ParseEntityLumpString(pMapEntities, parseError, position);
|
||||
|
||||
logicore.SetEntityLumpWritable(true);
|
||||
g_pOnMapInit->PushString(pMapName);
|
||||
g_pOnMapInit->Execute();
|
||||
logicore.SetEntityLumpWritable(false);
|
||||
|
||||
RETURN_META_VALUE(MRES_IGNORED, true);
|
||||
if (!success)
|
||||
{
|
||||
logger->LogError("Map entity lump parsing for %s failed with error code %d on position %d", pMapName, parseError, position);
|
||||
RETURN_META_VALUE(MRES_IGNORED, true);
|
||||
}
|
||||
|
||||
RETURN_META_VALUE_NEWPARAMS(MRES_HANDLED, true, &IServerGameDLL::LevelInit, (pMapName, logicore.GetEntityLumpString(), pOldLevel, pLandmarkName, loadGame, background));
|
||||
}
|
||||
|
||||
const char *SourceModBase::GetMapEntitiesString()
|
||||
{
|
||||
const char *pNewMapEntities = logicore.GetEntityLumpString();
|
||||
if (pNewMapEntities != nullptr)
|
||||
{
|
||||
RETURN_META_VALUE(MRES_SUPERCEDE, pNewMapEntities);
|
||||
}
|
||||
RETURN_META_VALUE(MRES_IGNORED, NULL);
|
||||
}
|
||||
|
||||
void SourceModBase::LevelShutdown()
|
||||
@@ -534,6 +558,7 @@ void SourceModBase::CloseSourceMod()
|
||||
return;
|
||||
|
||||
SH_REMOVE_HOOK(IServerGameDLL, LevelInit, gamedll, SH_MEMBER(this, &SourceModBase::LevelInit), false);
|
||||
SH_REMOVE_HOOK(IVEngineServer, GetMapEntitiesString, engine, SH_MEMBER(this, &SourceModBase::GetMapEntitiesString), false);
|
||||
|
||||
if (g_Loaded)
|
||||
{
|
||||
|
||||
@@ -140,6 +140,7 @@ public: // ISourceMod
|
||||
private:
|
||||
void ShutdownServices();
|
||||
private:
|
||||
const char* GetMapEntitiesString();
|
||||
char m_SMBaseDir[PLATFORM_MAX_PATH];
|
||||
char m_SMRelDir[PLATFORM_MAX_PATH];
|
||||
char m_ModDir[32];
|
||||
|
||||
Reference in New Issue
Block a user