Skip to content

Commit

Permalink
Move IGameConfigPtr to a separate header
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Aug 19, 2023
1 parent 9758334 commit 3596f4d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_sources(
sdk/amxx_gameconfigs.h
sdk/amxxmodule.cpp
sdk/amxxmodule.h
sdk/HLTypeConversion.h
Expand Down
65 changes: 65 additions & 0 deletions src/sdk/amxx_gameconfigs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Half-Life Weapon Mod
* Copyright (c) 2012 - 2023 AGHL.RU Dev Team
*
* http://aghl.ru/forum/ - Russian Half-Life and Adrenaline Gamer Community
*
*
* 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 2 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, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*
*/

#pragma once
#include <memory>
#include "sdk/amxxmodule.h"
#include "sdk/IGameConfigs.h"
#include "wpnmod_log.h"

//! Deleter for IGameConfig pointers.
struct AmxxGameConfigDeleter
{
void operator()(IGameConfig* pCfg)
{
MF_GetConfigManager()->CloseGameConfigFile(pCfg);
}
};

//! Smart pointer for IGameConfig.
using IGameConfigPtr = std::unique_ptr<IGameConfig, AmxxGameConfigDeleter>;

//! Loads a game config file. Crashes if fails.
static IGameConfigPtr WpnMod_LoadGameConfigFile(const char* file)
{
IGameConfig* pCfg = nullptr;
char error[256] = "";

if (!MF_GetConfigManager()->LoadGameConfigFile(file, &pCfg, error, sizeof(error)))
{
WPNMOD_LOG("Failed to load game config '%s': %s\n", file, error);
std::abort();
}

return IGameConfigPtr(pCfg, AmxxGameConfigDeleter());
}
41 changes: 10 additions & 31 deletions src/wpnmod_pvdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,30 @@
*
*/

#include <memory>
#include "sdk/IGameConfigs.h"
#include "sdk/amxx_gameconfigs.h"
#include "wpnmod_pvdata.h"
#include "wpnmod_memory.h"
#include "wpnmod_config.h"
#include "wpnmod_utils.h"

namespace
{

//! Class and field for which an offset must be loaded.
struct OffsetInitializer
{
const char* szClassName;
const char* szFieldName;
};

//! List of offsets.
TypeDescription GamePvDatasOffsets[pvData_End];

//! List class and field name for each offset.
static OffsetInitializer g_OffsetInitializers[pvData_End];

//! Deleter for IGameConfig pointers.
struct GameConfigDeleter
{
void operator()(IGameConfig* pCfg)
{
MF_GetConfigManager()->CloseGameConfigFile(pCfg);
}
};
OffsetInitializer g_OffsetInitializers[pvData_End];

//! Smart pointer for IGameConfig.
using IGameConfigPtr = std::unique_ptr<IGameConfig, GameConfigDeleter>;
} // namespace

//! Loads a game config file. Crashes if fails.
static IGameConfigPtr LoadGameConfigFile(const char* file)
{
IGameConfig* pCfg = nullptr;
char error[256] = "";

if (!MF_GetConfigManager()->LoadGameConfigFile(file, &pCfg, error, sizeof(error)))
{
WPNMOD_LOG("Failed to load game config '%s': %s\n", file, error);
std::abort();
}
//! List of offsets.
TypeDescription GamePvDatasOffsets[pvData_End];

return IGameConfigPtr(pCfg, GameConfigDeleter());
}

void pvData_Init(void)
{
Expand Down Expand Up @@ -130,7 +109,7 @@ void pvData_Init(void)
g_OffsetInitializers[pvData_szAnimExtention] = { "CBasePlayer", "m_szAnimExtention" };

// Load offsets
IGameConfigPtr pCfg = LoadGameConfigFile("common.games");
IGameConfigPtr pCfg = WpnMod_LoadGameConfigFile("common.games");
bool anyNotFound = false;

for (int i = 0; i < std::size(g_OffsetInitializers); i++)
Expand Down

0 comments on commit 3596f4d

Please sign in to comment.