MCPcopy Create free account
hub / github.com/alliedmodders/sourcemod / AllocPooledString

Method AllocPooledString

core/HalfLife2.cpp:1479–1539  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1477#endif
1478
1479string_t CHalfLife2::AllocPooledString(const char *pszValue)
1480{
1481 // This is admittedly a giant hack, but it's a relatively safe method for
1482 // inserting a string into the game's string pool that isn't likely to break.
1483 //
1484 // We find the first valid ent (should always be worldspawn), save off it's
1485 // current targetname string_t, set it to our string to insert via SetKeyValue,
1486 // read back the new targetname value, restore the old value, and return the new one.
1487
1488#if SOURCE_ENGINE < SE_ORANGEBOX
1489 CBaseEntity* pEntity = nullptr;
1490 for (int i = 0; i < gpGlobals->maxEntities; ++i)
1491 {
1492 pEntity = ReferenceToEntity(i);
1493 if (pEntity)
1494 {
1495 break;
1496 }
1497 }
1498
1499 if (!pEntity)
1500 {
1501 logger->LogError("Failed to locate a valid entity for AllocPooledString.");
1502 return NULL_STRING;
1503 }
1504
1505#else
1506 CBaseEntity *pEntity = ((IServerUnknown *) servertools->FirstEntity())->GetBaseEntity();
1507#endif
1508 auto *pDataMap = GetDataMap(pEntity);
1509 assert(pDataMap);
1510
1511 static int iNameOffset = -1;
1512 if (iNameOffset == -1)
1513 {
1514 sm_datatable_info_t info;
1515 bool found = FindDataMapInfo(pDataMap, "m_iName", &info);
1516 assert(found);
1517 iNameOffset = info.actual_offset;
1518 }
1519
1520 string_t* pProp = (string_t*)((intp)pEntity + iNameOffset);
1521 string_t backup = *pProp;
1522
1523#if SOURCE_ENGINE < SE_ORANGEBOX
1524 static int iFuncOffset;
1525 if (!g_pGameConf->GetOffset("DispatchKeyValue", &iFuncOffset) || !iFuncOffset)
1526 {
1527 logger->LogError("Failed to locate DispatchKeyValue in core gamedata. AllocPooledString unsupported.");
1528 return NULL_STRING;
1529 }
1530 VKeyValuesSS(pEntity, "targetname", pszValue, iFuncOffset);
1531#else
1532 servertools->SetKeyValue(pEntity, "targetname", pszValue);
1533#endif
1534
1535 string_t newString = *pProp;
1536 *pProp = backup;

Callers 1

SetEntPropStringFunction · 0.80

Calls 4

FindDataMapInfoFunction · 0.85
VKeyValuesSSFunction · 0.85
LogErrorMethod · 0.45
GetOffsetMethod · 0.45

Tested by

no test coverage detected