//----------------------------------------------------------------------------- // CUserMessage. //----------------------------------------------------------------------------- CUserMessage::CUserMessage(IRecipientFilter& recipients, const char* message_name): m_recipients(recipients) { m_message_name = message_name; m_index = ::GetMessageIndex(message_name); // Initialize buffer #ifdef USE_PROTOBUF const google::protobuf::Message* message = g_Cstrike15UsermessageHelpers.GetPrototype(message_name); if (!message) { BOOST_RAISE_EXCEPTION(PyExc_NameError, "Invalid message name: '%s'.", message_name); } m_buffer = message->New(); #else if (m_index == -1) { BOOST_RAISE_EXCEPTION(PyExc_NameError, "Invalid message name: '%s'.", message_name); } #if defined(ENGINE_LEFT4DEAD2) || defined(ENGINE_BLADE) m_buffer = engine->UserMessageBegin(&recipients, m_index, message_name); #else m_buffer = engine->UserMessageBegin(&recipients, m_index); #endif #endif }
BoostDataTableProxyFn SendPropSharedExt::get_data_table_proxy_function(SendProp *pSendProp) { if (pSendProp->IsExcludeProp()) BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is excluded.", pSendProp->GetName()); if (pSendProp->GetType() != DPT_DataTable) BOOST_RAISE_EXCEPTION(PyExc_TypeError, "%s is not a DataTable.", pSendProp->GetName()); return BoostDataTableProxyFn(pSendProp->GetDataTableProxyFn()); }
static const char* GetString(INetworkStringTable& table, int index) { if ((index < 0) || (index >= table.GetNumStrings())) BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.") return table.GetString(index); }
CPointer* CBinaryFile::FindSignatureRaw(object oSignature) { unsigned char* sigstr = (unsigned char *) PyBytes_AsString(oSignature.ptr()); if (!sigstr) BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to read the given signature."); int iLength = len(oSignature); unsigned char* base = (unsigned char *) m_ulAddr; unsigned char* end = (unsigned char *) (base + m_ulSize - iLength); while(base < end) { int i = 0; for(; i < iLength; i++) { if (sigstr[i] == '\x2A') continue; if (sigstr[i] != base[i]) break; } if (i == iLength) { return new CPointer((unsigned long) base); } base++; } return new CPointer(); }
object CListenerManager::__getitem__(unsigned int index) { if (index >= (unsigned int) m_vecCallables.Count()) BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.") return m_vecCallables[index]; }
CGameEventDescriptor& CGameEventDescriptorIter::__next__() { if (current_index >= game_events->Count()) BOOST_RAISE_EXCEPTION(PyExc_StopIteration, "No more descriptors.") return game_events->Element(current_index++); }
Vector* SendPropVariantExt::get_vector(DVariant *pVariant) { if (pVariant->m_Type != DPT_Vector) { BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type."); } return new Vector(pVariant->m_Vector[0], pVariant->m_Vector[1], pVariant->m_Vector[2]); }
// ============================================================================ // >> SendPropVariantExt // ============================================================================ const char* SendPropVariantExt::get_string(DVariant *pVariant) { if (pVariant->m_Type != DPT_String) { BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type."); } return pVariant->m_pString; }
// ============================================================================ // >> SendTableSharedExt // ============================================================================ SendProp* SendTableSharedExt::__getitem__(SendTable *pSendTable, int iIndex) { if (iIndex < 0 || iIndex > (pSendTable->m_nProps - 1)) { BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Invalid index."); } return pSendTable->GetProp(iIndex); }
//--------------------------------------------------------------------------------- // Returns the user data of the given string. //--------------------------------------------------------------------------------- int GetStringUserDataLength( INetworkStringTable *pTable, const char *string ) { int index = pTable->FindStringIndex(string); if (index == INVALID_STRING_INDEX) { BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Given string not found.") } return GetStringIndexUserDataLength(pTable, index); }
void SendPropVariantExt::set_int(DVariant* pVariant, int iValue) { if (pVariant->m_Type != DPT_Int) { BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type."); } pVariant->m_Int = iValue; }
int SendPropVariantExt::get_int(DVariant* pVariant) { if (pVariant->m_Type != DPT_Int) { BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Unable to cast to the specified type."); } return pVariant->m_Int; }
//--------------------------------------------------------------------------------- // Sets the user data of the given string. //--------------------------------------------------------------------------------- void SetStringUserData( INetworkStringTable *pTable, const char *string, const char *user_data, int length ) { int string_index = pTable->FindStringIndex(string); if (string_index == INVALID_STRING_INDEX) { BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Given string not found.") } SetStringIndexUserData(pTable, string_index, user_data, length); }
object execute_server_command(tuple args, dict kwargs) { std::string szCommand; ConCommand* pCommand; prepare_command(args, kwargs, &pCommand, &szCommand); CCommand c; if (!c.Tokenize(szCommand.c_str())) BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to tokenize '%s'.", szCommand.c_str()) pCommand->Dispatch(c); return object(); }
//----------------------------------------------------------------------------- // Removes all instances of a callable from the CListenerManager vector. //----------------------------------------------------------------------------- void CListenerManager::UnregisterListener(PyObject* pCallable) { // Get the object instance of the callable object oCallable = object(handle<>(borrowed(pCallable))); int index = FindCallback(oCallable); //m_vecCallables.Find(oCallable); if (index == -1) { BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Callback not registered.") } else { m_vecCallables.Remove(index); } }
//----------------------------------------------------------------------------- // Commands to execute server commands //----------------------------------------------------------------------------- void prepare_command(tuple args, dict kwargs, ConCommand** pCommandOut, std::string* szCommandOut) { if (kwargs) BOOST_RAISE_EXCEPTION(PyExc_TypeError, "Function does not accept keywords.") const char* command = extract<const char*>(args[0]); ConCommand* pCommand = cvar->FindCommand(command); if (!pCommand) BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to find command '%s'.", command); *pCommandOut = pCommand; *szCommandOut = ""; for(int i=0; i < len(args); ++i) { const char* temp = extract<const char*>(str(args[i])); *szCommandOut += temp; *szCommandOut += " "; } }
CPointer* CBinaryFile::FindSignature(object oSignature) { unsigned char* sigstr = (unsigned char *) PyBytes_AsString(oSignature.ptr()); if (!sigstr) BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to read the given signature."); // Search for a cached signature PythonLog(4, "Searching for a cached signature..."); for (std::list<Signature_t>::iterator iter=m_Signatures.begin(); iter != m_Signatures.end(); iter++) { Signature_t sig = *iter; if (strcmp((const char *) sig.m_szSignature, (const char *) sigstr) == 0) { PythonLog(4, "Found a cached signature!"); return new CPointer(sig.m_ulAddr); } } PythonLog(4, "Could not find a cached signature. Searching in the binary..."); // Search for a signature in the binary int iLength = len(oSignature); CPointer* pPtr = FindSignatureRaw(oSignature); // Found the signature? if (pPtr->IsValid()) { PythonLog(4, "Found a signature in the binary!"); // Add the signature to the cache Signature_t sig_t = {new unsigned char[iLength+1], pPtr->m_ulAddr}; strcpy((char*) sig_t.m_szSignature, (char*) sigstr); m_Signatures.push_back(sig_t); // Return the result return pPtr; } delete pPtr; // Haven't found a match? Then seach for a hooked signature PythonLog(4, "Could not find the signature in the binary. Searching for a hooked signature..."); // Check length of signature if (iLength <= 6) BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Signature is too short to search for a hooked signature."); // Create the hooked signature oSignature = import("binascii").attr("unhexlify")("E92A2A2A2A") + oSignature.slice(5, _); // Try to find the hooked signature pPtr = FindSignatureRaw(oSignature); // Couldn't find it? if (!pPtr->IsValid()) { PythonLog(4, "Could not find a hooked signature."); delete pPtr; } // Yay, we found a match! Now, check if the signature is unique else { PythonLog(4, "Found a hooked signature! Checking if it's unique..."); // Add iLength, so we start searching after the match CPointer new_ptr = CPointer(pPtr->m_ulAddr + iLength); // Got another match after the first one? CPointer* pNext = new_ptr.SearchBytes(oSignature, (m_ulAddr + m_ulSize) - new_ptr.m_ulAddr); bool bIsValid = pNext->IsValid(); delete pNext; if (bIsValid) BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Found more than one hooked signatures. Please pass more bytes."); PythonLog(4, "Signature is unique!"); // It's unique! So, add the original signature to the cache Signature_t sig_t = {new unsigned char[iLength+1], pPtr->m_ulAddr}; strcpy((char*) sig_t.m_szSignature, (char*) sigstr); m_Signatures.push_back(sig_t); // Now, return the result return pPtr; } BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Could not find signature."); return new CPointer(); // To fix a warning. This will never get called. }