Вопрос Keyvalues crash when select team

Начинающий
Статус
Оффлайн
Регистрация
21 Янв 2020
Сообщения
9
Реакции[?]
1
Поинты[?]
0
Пожалуйста, авторизуйтесь для просмотра ссылки.

Mutiny.pw
keyvalues:
bool KeyValues::LoadFromBuffer(char const * resourceName, const char * pBuffer, void * pFileSystem, const char * pPathID, GetSymbolProc_t pfnEvaluateSymbolProc)
{
    ASSIGNVARANDIFNZERODO(_LoadFromBuffer, reinterpret_cast<bool(__thiscall*)(KeyValues*, const char*, const char*, void*, const char*, GetSymbolProc_t, int unk)>(StaticOffsets.GetOffsetValue(_KeyValues_LoadFromBuffer)))
        return _LoadFromBuffer(this, resourceName, pBuffer, pFileSystem, pPathID, pfnEvaluateSymbolProc, 0);
    else
        return false;
}

void KeyValues::SetName(const char * setName)
{
    m_iKeyName = KeyValuesSystem()->GetSymbolForString(setName);
}

//-----------------------------------------------------------------------------
// Purpose: Set the integer value of a keyName.
//-----------------------------------------------------------------------------
void KeyValues::SetInt(const char *keyName, int value)
{
    KeyValues *dat = FindKey(keyName, true);

    if (dat)
    {
        dat->m_iValue = value;
        dat->m_iDataType = TYPE_INT;
    }
}

//-----------------------------------------------------------------------------
// Purpose: Set the string value of a keyName.
//-----------------------------------------------------------------------------
void KeyValues::SetString(const char *keyName, const char *value)
{
    KeyValues *dat = FindKey(keyName, true);

    if (dat)
    {
        // delete the old value
        delete[] dat->m_sValue;
        // make sure we're not storing the WSTRING  - as we're converting over to STRING
        delete[] dat->m_wsValue;
        dat->m_wsValue = NULL;

        if (!value)
        {
            // ensure a valid value
            value = "";
        }

        // allocate memory for the new value and copy it in
        int len = Q_strlen(value);
        dat->m_sValue = new char[len + 1];
        Q_memcpy(dat->m_sValue, value, len + 1);

        dat->m_iDataType = TYPE_STRING;
    }
}

void KeyValues::SetWString(const char *keyName, const wchar_t *value)
{
    KeyValues *dat = FindKey(keyName, true);
    if (dat)
    {
        // delete the old value
        delete[] dat->m_wsValue;
        // make sure we're not storing the STRING  - as we're converting over to WSTRING
        delete[] dat->m_sValue;
        dat->m_sValue = NULL;

        if (!value)
        {
            // ensure a valid value
            value = L"";
        }

        // allocate memory for the new value and copy it in
        int len = wcslen(value);
        dat->m_wsValue = new wchar_t[len + 1];
        Q_memcpy(dat->m_wsValue, value, (len + 1) * sizeof(wchar_t));

        dat->m_iDataType = TYPE_WSTRING;
    }
}



#define TRACK_KV_ADD( ptr, name )
#define TRACK_KV_REMOVE( ptr )  

//-----------------------------------------------------------------------------
// Purpose: Return the first subkey in the list
//-----------------------------------------------------------------------------
KeyValues *KeyValues::GetFirstSubKey()
{
    return m_pSub;
}

//-----------------------------------------------------------------------------
// Purpose: Return the next subkey
//-----------------------------------------------------------------------------
KeyValues *KeyValues::GetNextKey()
{
    return m_pPeer;
}


//-----------------------------------------------------------------------------
// Purpose: Sets this key's peer to the KeyValues passed in
//-----------------------------------------------------------------------------
void KeyValues::SetNextKey(KeyValues *pDat)
{
    m_pPeer = pDat;
}


KeyValues* KeyValues::GetFirstTrueSubKey()
{
    KeyValues *pRet = m_pSub;
    while (pRet && pRet->m_iDataType != TYPE_NONE)
        pRet = pRet->m_pPeer;

    return pRet;
}

KeyValues* KeyValues::GetNextTrueSubKey()
{
    KeyValues *pRet = m_pPeer;
    while (pRet && pRet->m_iDataType != TYPE_NONE)
        pRet = pRet->m_pPeer;

    return pRet;
}

KeyValues* KeyValues::GetFirstValue()
{
    KeyValues *pRet = m_pSub;
    while (pRet && pRet->m_iDataType == TYPE_NONE)
        pRet = pRet->m_pPeer;

    return pRet;
}

KeyValues* KeyValues::GetNextValue()
{
    KeyValues *pRet = m_pPeer;
    while (pRet && pRet->m_iDataType == TYPE_NONE)
        pRet = pRet->m_pPeer;

    return pRet;
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
KeyValues::KeyValues(const char *setName)
{
    CallConstructor(setName);
}

void KeyValues::CallConstructor(const char *setName)
{
    TRACK_KV_ADD(this, setName);

    Init();
    SetName(setName);
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
KeyValues::KeyValues(const char *setName, const char *firstKey, const char *firstValue)
{
    TRACK_KV_ADD(this, setName);

    Init();
    SetName(setName);
    SetString(firstKey, firstValue);
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
KeyValues::KeyValues(const char *setName, const char *firstKey, const wchar_t *firstValue)
{
    TRACK_KV_ADD(this, setName);

    Init();
    SetName(setName);
    SetWString(firstKey, firstValue);
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
KeyValues::KeyValues(const char *setName, const char *firstKey, int firstValue)
{
    TRACK_KV_ADD(this, setName);

    Init();
    SetName(setName);
    SetInt(firstKey, firstValue);
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
KeyValues::KeyValues(const char *setName, const char *firstKey, const char *firstValue, const char *secondKey, const char *secondValue)
{
    TRACK_KV_ADD(this, setName);

    Init();
    SetName(setName);
    SetString(firstKey, firstValue);
    SetString(secondKey, secondValue);
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
KeyValues::KeyValues(const char *setName, const char *firstKey, int firstValue, const char *secondKey, int secondValue)
{
    TRACK_KV_ADD(this, setName);

    Init();
    SetName(setName);
    SetInt(firstKey, firstValue);
    SetInt(secondKey, secondValue);
}

//-----------------------------------------------------------------------------
// Purpose: Initialize member variables
//-----------------------------------------------------------------------------
void KeyValues::Init()
{
    m_iKeyName = 0;
    m_iKeyNameCaseSensitive1 = 0;
    m_iKeyNameCaseSensitive2 = 0;
    m_iDataType = TYPE_NONE;

    m_pSub = NULL;
    m_pPeer = NULL;
    m_pChain = NULL;

    m_sValue = NULL;
    m_wsValue = NULL;
    m_pValue = NULL;

    m_bHasEscapeSequences = false;
}

//-----------------------------------------------------------------------------
// Purpose: memory allocator
//-----------------------------------------------------------------------------
void *KeyValues::operator new(size_t iAllocSize)
{
    //MEM_ALLOC_CREDIT();
    return KeyValuesSystem()->AllocKeyValuesMemory(iAllocSize);
}

void *KeyValues::operator new(size_t iAllocSize, int nBlockUse, const char *pFileName, int nLine)
{
    //MemAlloc_PushAllocDbgInfo(pFileName, nLine);
    void *p = KeyValuesSystem()->AllocKeyValuesMemory(iAllocSize);
    //MemAlloc_PopAllocDbgInfo();
    return p;
}

//-----------------------------------------------------------------------------
// Purpose: deallocator
//-----------------------------------------------------------------------------
void KeyValues::operator delete(void *pMem)
{
    KeyValuesSystem()->FreeKeyValuesMemory(pMem);
}


void KeyValues::operator delete(void *pMem, int nBlockUse, const char *pFileName, int nLine)
{
    KeyValuesSystem()->FreeKeyValuesMemory(pMem);
}

//-----------------------------------------------------------------------------
// Purpose: remove everything
//-----------------------------------------------------------------------------
void KeyValues::RemoveEverything()
{
    KeyValues *dat;
    KeyValues *datNext = NULL;
    for (dat = m_pSub; dat != NULL; dat = datNext)
    {
        datNext = dat->m_pPeer;
        dat->m_pPeer = NULL;
#if defined NO_MALLOC_OVERRIDE || defined NO_MEMOVERRIDE_NEW_DELETE
        dat->CallDestructor();
        FREE(dat);
#else
        delete dat;
#endif
    }

    for (dat = m_pPeer; dat && dat != this; dat = datNext)
    {
        datNext = dat->m_pPeer;
        dat->m_pPeer = NULL;
#if defined NO_MALLOC_OVERRIDE || defined NO_MEMOVERRIDE_NEW_DELETE
        dat->CallDestructor();
        FREE(dat);
#else
        delete dat;
#endif
    }

#if defined NO_MALLOC_OVERRIDE || defined NO_MEMOVERRIDE_NEW_DELETE
    FREE(m_sValue);
#else
    delete[] m_sValue;
#endif
    m_sValue = NULL;
#if defined NO_MALLOC_OVERRIDE || defined NO_MEMOVERRIDE_NEW_DELETE
    FREE(m_wsValue);
#else
    delete[] m_wsValue;
#endif
    m_wsValue = NULL;
}

void KeyValues::CallDestructor()
{
    TRACK_KV_REMOVE(this);

    RemoveEverything();
}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
KeyValues::~KeyValues()
{
    CallDestructor();
}

void KeyValues::deleteThis()
{
#if defined NO_MALLOC_OVERRIDE || defined NO_MEMOVERRIDE_NEW_DELETE
    CallDestructor();
    FREE(this);
#else
    delete this;
#endif
}

//-----------------------------------------------------------------------------
// Purpose: Find a keyValue, create it if it is not found.
//            Set bCreate to true to create the key if it doesn't already exist
//            (which ensures a valid pointer will be returned)
//-----------------------------------------------------------------------------
#if defined NO_MALLOC_OVERRIDE || defined NO_MEMOVERRIDE_NEW_DELETE
KeyValues *KeyValues::FindKey(const char *keyName, bool bCreate)
{
    return StaticOffsets.GetOffsetValueByType<KeyValues* (__thiscall*)(KeyValues*, const char*, bool)>(_FindKey)(this, keyName, bCreate);
}
#else
KeyValues *KeyValues::FindKey(const char *keyName, bool bCreate)
{
    // return the current key if a NULL subkey is asked for
    if (!keyName || !keyName[0])
        return this;

    // look for '/' characters deliminating sub fields
    char szBuf[256];
    const char *subStr = strchr(keyName, '/');
    const char *searchStr = keyName;

    // pull out the substring if it exists
    if (subStr)
    {
        int size = subStr - keyName;
        Q_memcpy(szBuf, keyName, size);
        szBuf[size] = 0;
        searchStr = szBuf;
    }

    // lookup the symbol for the search string
    HKeySymbol iSearchStr = KeyValuesSystem()->GetSymbolForString(searchStr, bCreate);
    if (iSearchStr == INVALID_KEY_SYMBOL)
    {
        // not found, couldn't possibly be in key value list
        return NULL;
    }

    KeyValues *lastItem = NULL;
    KeyValues *dat;
    // find the searchStr in the current peer list
    for (dat = m_pSub; dat != NULL; dat = dat->m_pPeer)
    {
        lastItem = dat;    // record the last item looked at (for if we need to append to the end of the list)

        // symbol compare
        if (dat->m_iKeyName == iSearchStr)
        {
            break;
        }
    }

    if (!dat && m_pChain)
    {
        dat = m_pChain->FindKey(keyName, false);
    }

    // make sure a key was found
    if (!dat)
    {
        if (bCreate)
        {
            // we need to create a new key
            dat = new KeyValues(searchStr);
            //            Assert(dat != NULL);

                        // insert new key at end of list
            if (lastItem)
            {
                lastItem->m_pPeer = dat;
            }
            else
            {
                m_pSub = dat;
            }
            dat->m_pPeer = NULL;

            // a key graduates to be a submsg as soon as it's m_pSub is set
            // this should be the only place m_pSub is set
            m_iDataType = TYPE_NONE;
        }
        else
        {
            return NULL;
        }
    }

    // if we've still got a subStr we need to keep looking deeper in the tree
    if (subStr)
    {
        // recursively chain down through the paths in the string
        return dat->FindKey(subStr + 1, bCreate);
    }

    return dat;
}
#endif


#if defined NO_MALLOC_OVERRIDE || defined NO_MEMOVERRIDE_NEW_DELETE
int KeyValues::GetInt(const char *keyName, int defaultValue)
{
    return StaticOffsets.GetOffsetValueByType<int (__thiscall*)(KeyValues*, const char*, int)>(_GetInt)(this, keyName, defaultValue);
}
#else
int KeyValues::GetInt(const char *keyName, int defaultValue)
{
    KeyValues *dat = FindKey(keyName, false);
    if (that)
    {
        switch (dat->m_iDataType)
        {
        case TYPE_STRING:
            return atoi(dat->m_sValue);
        case TYPE_WSTRING:
#ifdef _WIN32
            return _wtoi(dat->m_wsValue);
#else
            DevMsg("TODO: implement _wtoi\n");
            return 0;
#endif
        case TYPE_FLOAT:
            return (int)dat->m_flValue;
        case TYPE_UINT64:
            // can't convert, since it would lose data
            Assert(0);
            return 0;
        case TYPE_INT:
        case TYPE_PTR:
        default:
            return dat->m_iValue;
        };
    }
    return defaultValue;
}
#endif
 
Похожие темы
Сверху Снизу