Modify game config parser to support more offset datas (type, size, etc.)

This commit is contained in:
Arkshine 2015-08-09 14:53:15 +02:00
parent 10eb6b370a
commit 7423ac6824
3 changed files with 161 additions and 30 deletions

View File

@ -50,6 +50,8 @@ struct TempSigInfo
} TempSig; } TempSig;
TypeDescription TempType;
static char ParseEngine[32]; static char ParseEngine[32];
static bool DoesGameMatch(const char *value) static bool DoesGameMatch(const char *value)
@ -62,13 +64,10 @@ static bool DoesEngineMatch(const char* value)
return strcmp(ParseEngine, value) == 0; return strcmp(ParseEngine, value) == 0;
} }
CGameConfig::CGameConfig(const char *path) CGameConfig::CGameConfig(const char *path) : m_FoundOffset(false), m_CustomLevel(0), m_CustomHandler(nullptr)
{ {
strncopy(m_File, path, sizeof(m_File)); strncopy(m_File, path, sizeof(m_File));
strncopy(ParseEngine, IS_DEDICATED_SERVER() ? "engine_ds" : "engine_ls", sizeof(ParseEngine)); strncopy(ParseEngine, IS_DEDICATED_SERVER() ? "engine_ds" : "engine_ls", sizeof(ParseEngine));
m_CustomLevel = 0;
m_CustomHandler = nullptr;
} }
CGameConfig::~CGameConfig() CGameConfig::~CGameConfig()
@ -180,9 +179,11 @@ SMCResult CGameConfig::ReadSMC_NewSection(const SMCStates *states, const char *n
case PSTATE_GAMEDEFS_OFFSETS: case PSTATE_GAMEDEFS_OFFSETS:
{ {
strncopy(m_Offset, name, sizeof(m_Offset)); strncopy(m_Offset, name, sizeof(m_Offset));
TempType.reset();
m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET; m_ParseState = PSTATE_GAMEDEFS_OFFSETS_OFFSET;
m_MatchedPlatform = false; m_MatchedPlatform = false;
m_FoundOffset = false;
break; break;
} }
case PSTATE_GAMEDEFS_SIGNATURES: case PSTATE_GAMEDEFS_SIGNATURES:
@ -250,26 +251,89 @@ SMCResult CGameConfig::ReadSMC_KeyValue(const SMCStates *states, const char *key
{ {
case PSTATE_GAMEDEFS_OFFSETS_OFFSET: case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
{ {
if (g_LibSys.IsPlatformCompatible(key, &m_MatchedPlatform)) if (!strcmp(key, "type"))
{ {
if (m_Class[0]) auto type = FieldType::FIELD_NONE;
{
auto ic = m_OffsetsByClass.findForAdd(m_Class);
if (ic.found()) if (!strcmp(value, "stringint"))
{ {
ic->value->list.replace(m_Offset, atoi(value)); type = FieldType::FIELD_STRINGINT;
} }
else if (m_OffsetsByClass.add(ic, m_Class)) else if (!strcmp(value, "stringptr"))
{ {
ic->value = new OffsetClass; type = FieldType::FIELD_STRINGPTR;
ic->value->list.insert(m_Offset, atoi(value));
} }
} else if (!strcmp(value, "string"))
else
{ {
m_Offsets.replace(m_Offset, atoi(value)); type = FieldType::FIELD_STRING;
} }
else if (!strcmp(value, "classptr"))
{
type = FieldType::FIELD_CLASSPTR;
}
else if (!strcmp(value, "class"))
{
type = FieldType::FIELD_CLASS;
}
else if (!strcmp(value, "ehandle"))
{
type = FieldType::FIELD_EHANDLE;
}
else if (!strcmp(value, "edict"))
{
type = FieldType::FIELD_EDICT;
}
else if (!strcmp(value, "entvars"))
{
type = FieldType::FIELD_ENTVARS;
}
else if (!strcmp(value, "vector"))
{
type = FieldType::FIELD_VECTOR;
}
else if (!strcmp(value, "pointer"))
{
type = FieldType::FIELD_POINTER;
}
else if (!strcmp(value, "integer"))
{
type = FieldType::FIELD_INTEGER;
}
else if (!strcmp(value, "function"))
{
type = FieldType::FIELD_FUNCTION;
}
else if (!strcmp(value, "boolean"))
{
type = FieldType::FIELD_BOOLEAN;
}
else if (!strcmp(value, "short"))
{
type = FieldType::FIELD_SHORT;
}
else if (!strcmp(value, "character"))
{
type = FieldType::FIELD_CHARACTER;
}
else if (!strcmp(value, "float"))
{
type = FieldType::FIELD_FLOAT;
}
TempType.fieldType = type;
}
else if (!strcmp(key, "size"))
{
TempType.fieldSize = ke::Min(0, atoi(value));
}
else if (!strcmp(key, "unsigned"))
{
TempType.fieldUnsigned = !!atoi(value);
}
else if (g_LibSys.IsPlatformCompatible(key, &m_MatchedPlatform))
{
m_FoundOffset = true;
TempType.fieldOffset = atoi(value);
} }
break; break;
} }
@ -413,6 +477,28 @@ SMCResult CGameConfig::ReadSMC_LeavingSection(const SMCStates *states)
} }
case PSTATE_GAMEDEFS_OFFSETS_OFFSET: case PSTATE_GAMEDEFS_OFFSETS_OFFSET:
{ {
if (m_FoundOffset)
{
if (m_Class[0])
{
auto ic = m_OffsetsByClass.findForAdd(m_Class);
if (ic.found())
{
ic->value->list.replace(m_Offset, TempType);
}
else if (m_OffsetsByClass.add(ic, m_Class))
{
ic->value = new OffsetClass;
ic->value->list.insert(m_Offset, TempType);
}
}
else
{
m_Offsets.replace(m_Offset, TempType);
}
}
m_ParseState = PSTATE_GAMEDEFS_OFFSETS; m_ParseState = PSTATE_GAMEDEFS_OFFSETS;
break; break;
} }
@ -671,12 +757,12 @@ bool CGameConfig::EnterFile(const char *file, char *error, size_t maxlength)
return true; return true;
} }
bool CGameConfig::GetOffset(const char *key, int *value) bool CGameConfig::GetOffset(const char *key, TypeDescription *value)
{ {
return m_Offsets.retrieve(key, value); return m_Offsets.retrieve(key, value);
} }
bool CGameConfig::GetOffsetByClass(const char *classname, const char *key, int *value) bool CGameConfig::GetOffsetByClass(const char *classname, const char *key, TypeDescription *value)
{ {
auto r = m_OffsetsByClass.find(classname); auto r = m_OffsetsByClass.find(classname);

View File

@ -45,8 +45,8 @@ class CGameConfig
public: // IGameConfig public: // IGameConfig
const char* GetKeyValue(const char *key); const char* GetKeyValue(const char *key);
bool GetOffset(const char *key, int *value); bool GetOffset(const char *key, TypeDescription *value);
bool GetOffsetByClass(const char *classname, const char *key, int *value); bool GetOffsetByClass(const char *classname, const char *key, TypeDescription *value);
bool GetMemSig(const char *key, void **addr); bool GetMemSig(const char *key, void **addr);
bool GetAddress(const char *key, void **addr); bool GetAddress(const char *key, void **addr);
@ -61,15 +61,16 @@ class CGameConfig
struct OffsetClass struct OffsetClass
{ {
StringHashMap<int> list; StringHashMap<TypeDescription> list;
}; };
typedef StringHashMap<ke::AutoPtr<OffsetClass>> OffsetClassMap; typedef StringHashMap<ke::AutoPtr<OffsetClass>> OffsetClassMap;
typedef StringHashMap<TypeDescription> OffsetMap;
char m_File[PLATFORM_MAX_PATH]; char m_File[PLATFORM_MAX_PATH];
char m_CurrentPath[PLATFORM_MAX_PATH]; char m_CurrentPath[PLATFORM_MAX_PATH];
StringHashMap<int> m_Offsets; OffsetMap m_Offsets;
OffsetClassMap m_OffsetsByClass; OffsetClassMap m_OffsetsByClass;
StringHashMap<ke::AString> m_Keys; StringHashMap<ke::AString> m_Keys;
StringHashMap<void*> m_Sigs; StringHashMap<void*> m_Sigs;
@ -81,6 +82,7 @@ class CGameConfig
char m_Offset[64]; char m_Offset[64];
char m_Game[256]; char m_Game[256];
bool m_FoundOffset;
bool m_MatchedClasses; bool m_MatchedClasses;
bool m_ShouldBeReadingDefault; bool m_ShouldBeReadingDefault;
bool m_HadGame; bool m_HadGame;

View File

@ -34,6 +34,49 @@
#include <ITextParsers.h> #include <ITextParsers.h>
enum class FieldType
{
FIELD_NONE,
FIELD_FLOAT, // Floating point value
FIELD_STRINGINT, // String ID (return from ALLOC_STRING)
FIELD_STRINGPTR, // String, pointer-to-char
FIELD_STRING, // String, fixed size
FIELD_CLASSPTR, // Classes pointer derived of CBaseEntity
FIELD_CLASS, // Arbitrary classes, direct
FIELD_STRUCTURE, // Arbitrary structures, direct
FIELD_EHANDLE, // Entity handle
FIELD_ENTVARS, // entvars_t*
FIELD_EDICT, // edict_t*
FIELD_VECTOR, // Vector
FIELD_POINTER, // Arbitrary data pointer
FIELD_INTEGER, // Integer or enum
FIELD_FUNCTION, // Class function pointer (Think, Use, etc)
FIELD_BOOLEAN, // Boolean
FIELD_SHORT, // 2 bytes integer
FIELD_CHARACTER, // 1 byte
};
struct TypeDescription
{
TypeDescription()
{
reset();
}
void reset()
{
fieldType = FieldType::FIELD_NONE;
fieldOffset = 0;
fieldSize = 0;
fieldUnsigned = false;
}
FieldType fieldType;
int fieldOffset;
int fieldSize;
bool fieldUnsigned;
};
/** /**
* @brief Describes a game private data config file * @brief Describes a game private data config file
*/ */
@ -44,20 +87,20 @@ public:
* @brief Returns an offset value. * @brief Returns an offset value.
* *
* @param key Key to retrieve from the offset section. * @param key Key to retrieve from the offset section.
* @param value Pointer to store the offset value in. * @param value Pointer to store the TypeDescription reference in.
* @return True if found, false otherwise. * @return True if found, false otherwise.
*/ */
virtual bool GetOffset(const char *key, int *value) = 0; virtual bool GetOffset(const char *key, TypeDescription *value) = 0;
/** /**
* @brief Returns an offset value from given class. * @brief Returns an offset value from given class.
* *
* @param classname class name to match from the offset section. * @param classname class name to match from the offset section.
* @param key Key to retrieve from the offset section. * @param key Key to retrieve from the offset section.
* @param value Pointer to store the offset value in. * @param value Pointer to store the TypeDescription reference in.
* @return True if found, false otherwise. * @return True if found, false otherwise.
*/ */
virtual bool GetOffsetByClass(const char *classname, const char *key, int *value) = 0; virtual bool GetOffsetByClass(const char *classname, const char *key, TypeDescription *value) = 0;
/** /**
* @brief Returns the value of a key from the "Keys" section. * @brief Returns the value of a key from the "Keys" section.