diff --git a/cathook/.cproject b/cathook/.cproject index 016952f9..475ee116 100644 --- a/cathook/.cproject +++ b/cathook/.cproject @@ -50,6 +50,7 @@ + diff --git a/cathook/TODO b/cathook/TODO index e75cfdb5..7aaa3e5f 100644 --- a/cathook/TODO +++ b/cathook/TODO @@ -13,6 +13,9 @@ alias clearcond "host_timescale 300;wait 500;host_timescale 1" alias clearcond_taunt "taunt;wait 50;host_timescale 300;wait 500;host_timescale 1" bind l clearcond_taunt +Тебе нужно отследить, что MatSystemTopPanel отрисовался, поставить какой то флаг в 1, далее ждать FocusOverlayPanel и проверять что твой флаг был вяставлен в 1, потом рисовать всё что надо и ставить флаг в 0 + + fullbright toggle instant taunt TTS diff --git a/cathook/src/common.h b/cathook/src/common.h index 29893594..b4ac4ad9 100644 --- a/cathook/src/common.h +++ b/cathook/src/common.h @@ -44,9 +44,10 @@ #define SQR(x) x * x -#define CATHOOK_VERSION_MAJOR "0" -#define CATHOOK_VERSION_MINOR "5" -#define CATHOOK_VERSION_PATCH "2" +#ifndef CATHOOK_BUILD_NUMBER +#define CATHOOK_BUILD_NUMBER "1" +#endif +#define CATHOOK_BUILD_NAME "Sweet Roll" #define CON_NAME "cat" #define CON_PREFIX CON_NAME "_" diff --git a/cathook/src/drm.h b/cathook/src/drm.h index c194e00e..be07c93f 100644 --- a/cathook/src/drm.h +++ b/cathook/src/drm.h @@ -13,16 +13,10 @@ #ifndef __DRM_ENABLED #define __DRM_ENABLED true #endif -#define __DRM_NOTES "Unstable build, for testing only!" +#define __DRM_NOTES "Semi-stable build" #define __QUIT_SEGV (*((int*)0) = 0) -#if __DRM_ENABLED == false -#define _DEVELOPER true -#else -#define _DEVELOPER false -#endif - #ifndef __DRM_HWID_0 #define __DRM_HWID_0 0x7fa8d247bb8389e7 #define __DRM_HWID_1 0x08ebdb1cdb642f0e diff --git a/cathook/src/globals.cpp b/cathook/src/globals.cpp index 5491168e..567bf266 100644 --- a/cathook/src/globals.cpp +++ b/cathook/src/globals.cpp @@ -39,6 +39,7 @@ void GlobalSettings::Init() { this->bThirdperson = CREATE_CV(CV_SWITCH, "thirdperson", "0", "Thirdperson"); this->bNoVisuals = CREATE_CV(CV_SWITCH, "novisuals", "0", "Disable visuals"); this->bCleanScreenshots = CREATE_CV(CV_SWITCH, "clean_screenshot", "1", "Clean screenshots"); + this->bDebugLog = CREATE_CV(CV_SWITCH, "log", "1", "Debug Log"); this->bThirdperson->m_pConVar->InstallChangeCallback(ThirdpersonCallback); bInvalid = true; } diff --git a/cathook/src/globals.h b/cathook/src/globals.h index 9bc892fe..759b2fe9 100644 --- a/cathook/src/globals.h +++ b/cathook/src/globals.h @@ -36,6 +36,7 @@ public: CatVar* bThirdperson; CatVar* bNoVisuals; CatVar* bCleanScreenshots; + CatVar* bDebugLog; Vector last_angles; bool bInvalid; }; diff --git a/cathook/src/gui/CBaseWidget.h b/cathook/src/gui/CBaseWidget.h index 1c2e7aa1..e227ba72 100644 --- a/cathook/src/gui/CBaseWidget.h +++ b/cathook/src/gui/CBaseWidget.h @@ -10,13 +10,21 @@ #include "IWidget.h" +#include + class CBaseWidget : public virtual IWidget { public: inline ~CBaseWidget() { delete [] m_pszName; } - inline CBaseWidget(IWidget* parent, const char* name) { + KeyValues::AutoDelete m_pKeyValues; + + inline KeyValues* GetKeyValues() { + return m_pKeyValues; + } + + inline CBaseWidget(IWidget* parent, const char* name) : m_pKeyValues("widgetkv") { m_pParentWidget = parent; m_bMouseInside = false; m_bMousePressed = false; @@ -32,9 +40,12 @@ public: m_bVisible = true; m_nMaxX = 0; m_nMaxY = 0; + m_bFocused = false; } + inline virtual bool ConsumesKey(ButtonCode_t key) { return false; } + inline virtual void SetMaxSize(int w, int h) { if (w >= 0) m_nMaxX = w; @@ -106,6 +117,7 @@ public: inline virtual void Hide() { m_bVisible = false; + OnFocusLose(); } inline virtual bool IsVisible() { @@ -125,7 +137,7 @@ public: } virtual IWidget* GetChildByPoint(int x, int y) { - for (int i = 0; i < m_nChildCount; i++) { + for (int i = m_nChildCount - 1; i >= 0; i--) { IWidget* child = m_pChildrenList[i]; int ox, oy; child->GetOffset(ox, oy); diff --git a/cathook/src/gui/CBaseWindow.cpp b/cathook/src/gui/CBaseWindow.cpp index 03249acf..7477d38a 100644 --- a/cathook/src/gui/CBaseWindow.cpp +++ b/cathook/src/gui/CBaseWindow.cpp @@ -94,6 +94,11 @@ void CBaseWindow::Update() { hovered = nhov; } +void CBaseWindow::OnFocusLose() { + m_bFocused = false; + if (focused) focused->OnFocusLose(); +} + void CBaseWindow::OnMouseLeave() { if (hovered) hovered->OnMouseLeave(); hovered = 0; @@ -102,13 +107,11 @@ void CBaseWindow::OnMouseLeave() { void CBaseWindow::OnMousePress() { int ax, ay; this->GetAbsolutePosition(ax, ay); - logging::Info("%s MousePress! %i %i", GetName(), g_pGUI->m_iMouseX - ax, g_pGUI->m_iMouseY - ay); IWidget* newfocus = GetChildByPoint(g_pGUI->m_iMouseX - ax, g_pGUI->m_iMouseY - ay); if (newfocus) { if (focused) focused->OnFocusLose(); focused = newfocus; newfocus->OnFocusGain(); - logging::Info("%s Child MousePress! %s", GetName(), focused->GetName()); newfocus->OnMousePress(); } else { if (focused) focused->OnFocusLose(); @@ -140,8 +143,15 @@ void CBaseWindow::Draw() { GetSize(sx, sy); draw::DrawRect(px, py, sx, sy, colors::Transparent(colors::black)); draw::OutlineRect(px, py, sx, sy, colors::pink); - for (int i = 0; i < m_nChildCount; i++) { + for (int i = m_nChildCount - 1; i >= 0; i--) { if (m_pChildrenList[i]->IsVisible()) m_pChildrenList[i]->Draw(); } } + +bool CBaseWindow::ConsumesKey(ButtonCode_t key) { + if (focused && focused->IsVisible()) { + return focused->ConsumesKey(key); + } + return false; +} diff --git a/cathook/src/gui/CBaseWindow.h b/cathook/src/gui/CBaseWindow.h index 5fc1118b..43f391c4 100644 --- a/cathook/src/gui/CBaseWindow.h +++ b/cathook/src/gui/CBaseWindow.h @@ -29,7 +29,10 @@ public: virtual void OnMousePress(); virtual void OnMouseRelease(); virtual void OnKeyPress(ButtonCode_t key); - virtual void OnKeyRelease(ButtonCode_t key);/* + virtual void OnKeyRelease(ButtonCode_t key); + virtual bool ConsumesKey(ButtonCode_t key); + virtual void OnFocusLose(); + /* virtual void SetOffset(int x, int y); virtual void GetOffset(int& x, int& y);*/ diff --git a/cathook/src/gui/CTextInput.cpp b/cathook/src/gui/CTextInput.cpp index 56b84c6c..ce428560 100644 --- a/cathook/src/gui/CTextInput.cpp +++ b/cathook/src/gui/CTextInput.cpp @@ -17,6 +17,10 @@ CTextInput::CTextInput(IWidget* parent, const char* name) : CBaseWidget(parent, m_nLength = 0; } +bool CTextInput::ConsumesKey(ButtonCode_t key) { + return key >= ButtonCode_t::KEY_FIRST && key <= ButtonCode_t::KEY_LAST && key != KEY_INSERT; +} + void CTextInput::SetMaxWidth(int width) { int length, height; draw::GetStringLength(fonts::MENU, "W", length, height); @@ -30,7 +34,7 @@ void CTextInput::Draw() { int height, length; draw::GetStringLength(fonts::MENU, "W", length, height); int color = colors::Create(0, 0, 0, 80); - if (m_bFocused) color = colors::Create(255, 255, 255, 80); + if (m_bFocused) color = colors::Transparent(colors::pink, 0.25); draw::DrawRect(ax, ay, m_nSizeX, height + 4, color); draw::OutlineRect(ax, ay, m_nSizeX, height + 4, colors::pink); int ml = 0; @@ -40,8 +44,8 @@ void CTextInput::Draw() { for (int i = 0; i < strlen(m_pszContents); i++) { int w, h; draw::GetStringLength(fonts::MENU, m_pszContents + i, w, h); - if (w + 4 + tx > m_nSizeX) md = i; - if (w + 4 > m_nSizeX) ml = i; + if (w + 10 + tx >= m_nSizeX) md = i; + if (w + 8 > m_nSizeX) ml = i; } if (ml) { draw::FString(fonts::MENU, ax + 2, ay + 2, colors::white, 1, "...%s", (m_pszContents + md)); @@ -87,12 +91,14 @@ void CTextInput::OnKeyPress(ButtonCode_t key) { PutChar(' '); return; } else { - if (strlen(interfaces::input->ButtonCodeToString(key)) == 1) { - if (g_pGUI->m_bPressedState[ButtonCode_t::KEY_LSHIFT] || g_pGUI->m_bPressedState[ButtonCode_t::KEY_RSHIFT]) { - PutChar(GetUpperChar(key)); - } else { - PutChar(GetChar(key)); - } + char ch = 0; + if (g_pGUI->m_bPressedState[ButtonCode_t::KEY_LSHIFT] || g_pGUI->m_bPressedState[ButtonCode_t::KEY_RSHIFT]) { + ch = GetUpperChar(key); + } else { + ch = GetChar(key); + } + if (ch) { + PutChar(ch); } } } diff --git a/cathook/src/gui/CTextInput.h b/cathook/src/gui/CTextInput.h index e1a99f8a..32157411 100644 --- a/cathook/src/gui/CTextInput.h +++ b/cathook/src/gui/CTextInput.h @@ -18,6 +18,7 @@ public: virtual void OnKeyPress(ButtonCode_t key); virtual void Draw(); + virtual bool ConsumesKey(ButtonCode_t key); void PutChar(char ch); void SetLength(int newlength); diff --git a/cathook/src/gui/CTooltip.cpp b/cathook/src/gui/CTooltip.cpp index c55eb7aa..1a765564 100644 --- a/cathook/src/gui/CTooltip.cpp +++ b/cathook/src/gui/CTooltip.cpp @@ -10,7 +10,7 @@ #include "../sdk.h" CTooltip::CTooltip(IWidget* parent, const char* name) : CBaseWidget(parent, name) { - + m_pszText = 0; } void CTooltip::SetText(const char* text) { diff --git a/cathook/src/gui/GUI.cpp b/cathook/src/gui/GUI.cpp index fcfade82..052b56ba 100644 --- a/cathook/src/gui/GUI.cpp +++ b/cathook/src/gui/GUI.cpp @@ -30,8 +30,6 @@ CatGUI::~CatGUI() { void CatGUI::Setup() { m_pRootWindow = new RootWindow(); - m_pTooltip = new CTooltip(m_pRootWindow, "tooltip"); - m_pRootWindow->AddChild(m_pTooltip); v_bGUIVisible->m_pConVar->InstallChangeCallback(GUIVisibleCallback); } @@ -39,10 +37,11 @@ void CatGUI::ShowTooltip(const char* text) { m_pTooltip->SetText(text); m_pTooltip->SetOffset(m_iMouseX + 5, m_iMouseY + 5); m_pTooltip->Show(); + m_bShowTooltip = true; } void CatGUI::Update() { - m_pTooltip->Hide(); + m_bShowTooltip = false; for (int i = 0; i < ButtonCode_t::MOUSE_LAST; i++) { bool down = interfaces::input->IsButtonDown((ButtonCode_t)(KEY_FIRST + i)); bool changed = m_bPressedState[i] != down; @@ -58,27 +57,53 @@ void CatGUI::Update() { if (down) m_pRootWindow->OnKeyPress((ButtonCode_t)i); else m_pRootWindow->OnKeyRelease((ButtonCode_t)i); } + } else { + if (down && i >= ButtonCode_t::KEY_FIRST && i <= ButtonCode_t::KEY_LAST) { + int frame = interfaces::gvars->framecount - m_iPressedFrame[i]; + bool shouldrepeat = false; + if (frame) { + if (frame > 150) { + if (frame > 400) { + if (frame % 30 == 0) shouldrepeat = true; + } else { + if (frame % 80 == 0) shouldrepeat = true; + } + } + } + if (shouldrepeat) m_pRootWindow->OnKeyPress((ButtonCode_t)i); + } } } } + m_iMouseX = interfaces::input->GetAnalogValue(AnalogCode_t::MOUSE_X); m_iMouseY = interfaces::input->GetAnalogValue(AnalogCode_t::MOUSE_Y); if (!m_bKeysInit) m_bKeysInit = 1; if (v_bGUIVisible->GetBool()) { + m_pRootWindow->Show(); m_pRootWindow->Update(); + if (!m_bShowTooltip) m_pTooltip->Hide(); m_pRootWindow->Draw(); draw::DrawRect(m_iMouseX - 5, m_iMouseY - 5, 10, 10, colors::Transparent(colors::white)); draw::OutlineRect(m_iMouseX - 5, m_iMouseY - 5, 10, 10, colors::pink); if (v_bDrawBounds->GetBool()) { m_pRootWindow->DrawBounds(); } + } else { + m_pRootWindow->Hide(); } } +bool CatGUI::ConsumesKey(ButtonCode_t key) { + if (m_pRootWindow->IsVisible()) + return m_pRootWindow->ConsumesKey(key); + else return false; +} + IWidget* CatGUI::GetRootWindow() { return m_pRootWindow; } diff --git a/cathook/src/gui/GUI.h b/cathook/src/gui/GUI.h index 309854a1..b8416036 100644 --- a/cathook/src/gui/GUI.h +++ b/cathook/src/gui/GUI.h @@ -26,14 +26,17 @@ public: void Update(); void Setup(); IWidget* GetRootWindow(); + bool ConsumesKey(ButtonCode_t key); void ShowTooltip(const char* text); - CTooltip* m_pTooltip; + CTooltip* m_pTooltip; IWidget* m_pRootWindow; CatVar* v_bGUIVisible; CatVar* v_bDrawBounds; + bool m_bShowTooltip; + bool m_bConsumeKeys; bool m_bKeysInit; bool m_bPressedState[ButtonCode_t::MOUSE_LAST]; int m_iPressedFrame[ButtonCode_t::MOUSE_LAST]; diff --git a/cathook/src/gui/IWidget.h b/cathook/src/gui/IWidget.h index 64e3d856..1e88cb03 100644 --- a/cathook/src/gui/IWidget.h +++ b/cathook/src/gui/IWidget.h @@ -18,18 +18,23 @@ enum PositionMode { FLOATING }; +class KeyValues; + class IWidget { public: virtual ~IWidget(); virtual void Update() = 0; + virtual KeyValues* GetKeyValues() = 0; + virtual void OnMouseEnter() = 0; virtual void OnMouseLeave() = 0; virtual void OnMousePress() = 0; virtual void OnMouseRelease() = 0; virtual void OnKeyPress(ButtonCode_t key) = 0; virtual void OnKeyRelease(ButtonCode_t key) = 0; + virtual bool ConsumesKey(ButtonCode_t key) = 0; virtual void OnFocusGain() = 0; virtual void OnFocusLose() = 0; diff --git a/cathook/src/gui/RootWindow.cpp b/cathook/src/gui/RootWindow.cpp index c49e602d..33420e88 100644 --- a/cathook/src/gui/RootWindow.cpp +++ b/cathook/src/gui/RootWindow.cpp @@ -13,16 +13,15 @@ #include "CTextInput.h" #include "CSplitContainer.h" #include "CSlider.h" +#include "CTooltip.h" #include "../common.h" void B1Callback(IWidget* thisptr) { IWidget* label = thisptr->GetParent()->GetChildByName("td"); - logging::Info("0x%08x", label); CTextLabel* tl = dynamic_cast(label); - logging::Info("0x%08x", tl); if (tl) { - char* text = strfmt("wow! curtime: %.2f", interfaces::gvars->curtime); + char* text = strfmt("wow! this[\"%s\"] = %i", "test_value", thisptr->GetKeyValues()->GetInt("test_value")); tl->SetText(text); delete [] text; } @@ -30,9 +29,7 @@ void B1Callback(IWidget* thisptr) { void TICallback(IWidget* thisptr, const char* old, const char* newc) { IWidget* label = thisptr->GetParent()->GetChildByName("td"); - logging::Info("0x%08x", label); CTextLabel* tl = dynamic_cast(label); - logging::Info("0x%08x", tl); if (tl) { char* text = strfmt("wow! text: %s", newc); tl->SetText(text); @@ -41,6 +38,8 @@ void TICallback(IWidget* thisptr, const char* old, const char* newc) { } RootWindow::RootWindow() : CBaseWindow(0, "root") { + g_pGUI->m_pTooltip = new CTooltip(this, "tooltip"); + this->AddChild(g_pGUI->m_pTooltip); CBaseWindow* ws = new CBaseWindow(this, "splitwindow"); ws->SetPositionMode(ABSOLUTE); TitleBar* wst = new TitleBar(ws, "Window Layout Test"); @@ -55,11 +54,15 @@ RootWindow::RootWindow() : CBaseWindow(0, "root") { CBaseButton* ccb1 = new CBaseButton(sc1, "b1"); ccb1->SetText("Ayy Lmao"); CSlider* sl = new CSlider(ws, "sl"); - sl->Setup(0.0f, 100.0f); + sl->GetKeyValues()->SetString("cvar", "cat_fov"); + sl->Setup(10.0f, 150.0f); + sl->SetCallback([](CSlider* slider, float newv, float oldv) { + interfaces::cvar->FindVar(slider->GetKeyValues()->GetString("cvar"))->SetValue(newv); + }); sc1->AddChild(ccb1); //sc1->AddChild(new CTextLabel(sc1, "tl3", "wow")); ws->AddChild(sc1); - CSplitContainer* sc2 = new CSplitContainer(ws, "sc1"); + CSplitContainer* sc2 = new CSplitContainer(ws, "sc2"); sc2->m_nSizeX = 480; sc2->m_nMaxX = 480; sc2->AddChild(new CTextLabel(sc2, "tl1", "1")); @@ -67,7 +70,7 @@ RootWindow::RootWindow() : CBaseWindow(0, "root") { sc2->AddChild(new CTextLabel(sc2, "tl3", "3")); sc2->AddChild(new CTextLabel(sc2, "tl4", "4")); ws->AddChild(sc2); - CSplitContainer* sc3 = new CSplitContainer(ws, "sc1"); + CSplitContainer* sc3 = new CSplitContainer(ws, "sc3"); sc3->m_nSizeX = 480; sc3->m_nMaxX = 480; sc3->AddChild(new CTextLabel(sc3, "tl1", "ayy")); @@ -105,6 +108,7 @@ RootWindow::RootWindow() : CBaseWindow(0, "root") { b1->SetText("Press me!"); b1->SetCallback(B1Callback); b1->SetPositionMode(INLINE_BLOCK); + b1->GetKeyValues()->SetInt("test_value", 1337); wgt->AddChild(b1); wgt->SetOffset(200, 200); CTextLabel* td = new CTextLabel(wgt, "td", ""); diff --git a/cathook/src/hack.cpp b/cathook/src/hack.cpp index 6c257fe7..93dde989 100644 --- a/cathook/src/hack.cpp +++ b/cathook/src/hack.cpp @@ -49,15 +49,14 @@ #include "CDumper.h" #include "ipc/ipcctl.h" +#include + /* * Credits to josh33901 aka F1ssi0N for butifel F1Public and Darkstorm 2015 Linux */ bool hack::shutdown = false; -ICvar* g_pCVar = 0; -std::string ggggppppvvvv; - void hack::InitHacks() { ADD_HACK(AutoStrafe); ADD_HACK(AntiAim); @@ -83,22 +82,25 @@ void hack::InitHacks() { ConCommand* hack::c_Cat = 0; void hack::CC_Cat(const CCommand& args) { - Color x = *reinterpret_cast(&colors::blu); - logging::Info("x: %i", x.GetRawColor()); - logging::Info("r %i g %i b %i", x.r(), x.g(), x.b()); - interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::blu), "CatHook"); + interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::blu), "cathook"); interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::white), " by "); interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::blu), "d4rkc4t\n"); - interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::white), "Build: " __DATE__ " " __TIME__"\n"); + interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::white), "build: " CATHOOK_BUILD_NUMBER " \"" CATHOOK_BUILD_NAME "\"\n"); #if _DEVELOPER interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::red), "[DEVELOPER BUILD]\n"); -#endif +#else interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::red), "Build for user " __DRM_NAME " (Early Access)\n"); +#endif #ifdef __DRM_NOTES interfaces::cvar->ConsoleColorPrintf(*reinterpret_cast(&colors::red), "Build notes: " __DRM_NOTES "\n"); #endif } +typedef bool(handlevent_t)(IMatSystemSurface* thisptr, const InputEvent_t& event); +bool test_handleevent(IMatSystemSurface* thisptr, const InputEvent_t& event) { + +} + void hack::Initialize() { logging::Initialize(); srand(time(0)); @@ -107,22 +109,13 @@ void hack::Initialize() { hwid::read_hwid_fstab(); hwid::read_hwid_machineid(); hwid::compute_result(); - logging::Info("Build: " __DATE__ " " __TIME__); - logging::Info("Loading shared objects..."); sharedobj::LoadAllSharedObjects(); g_pszTFPath = tf_path_from_maps(); - logging::Info("TF folder: %s", g_pszTFPath); - logging::Info("Creating interfaces..."); interfaces::CreateInterfaces(); - logging::Info("User: %llu", interfaces::user->GetSteamID().ConvertToUint64()); DRM_ENFORCE; - logging::Info("Interfaces created!"); - logging::Info("Dumping NetVars..."); CDumper dumper; dumper.SaveDump(); - logging::Info("Initializing surface..."); draw::Initialize(); - logging::Info("Colorizing..."); colors::Init(); uintptr_t mmmf = (gSignatures.GetClientSignature("C7 44 24 04 09 00 00 00 BB ? ? ? ? C7 04 24 00 00 00 00 E8 ? ? ? ? BA ? ? ? ? 85 C0 B8 ? ? ? ? 0F 44 DA") + 37); if (mmmf) { @@ -130,13 +123,9 @@ void hack::Initialize() { unsigned char patch2[] = { 0x89, 0xC2, 0x90 }; Patch((void*)mmmf, (void*)patch1, 3); Patch((void*)(mmmf + 8), (void*)patch2, 3); - } else logging::Info("You are already filled with luck."); - logging::Info("Adding hacks..."); - - BeginConVars(); + }BeginConVars(); hack::c_Cat = CreateConCommand(CON_NAME, &hack::CC_Cat, "Info"); hack::InitHacks(); - logging::Info("Init global settings"); g_Settings.Init(); #if ENTITY_CACHE_PROFILER == true if (!g_vEntityCacheProfiling) { @@ -146,15 +135,12 @@ void hack::Initialize() { g_pGUI = new CatGUI(); g_pGUI->Setup(); EndConVars(); - logging::Info("Initializing NetVar tree..."); gNetvars.init(); - logging::Info("Initializing entity offsets..."); InitNetVars(); g_pLocalPlayer = new LocalPlayer(); g_pPlayerResource = new TFPlayerResource(); - logging::Info("Hooking methods..."); hooks::hkPanel = new hooks::VMTHook(); hooks::hkPanel->Init(interfaces::panel, 0); //hooks::hkPanel->HookMethod((void*)&hack::Hk_PaintTraverse, hooks::offPaintTraverse); @@ -165,6 +151,9 @@ void hack::Initialize() { while(!(clientMode = **(uintptr_t***)((uintptr_t)((*(void***)interfaces::baseClient)[10]) + 1))) { sleep(1); } + //hooks::hkMatSurface = new hooks::VMTHook(); + //hooks::hkMatSurface->Init((void*)interfaces::matsurface, 0); + //hooks::hkMatSurface->HookMethod((void*)test_handleevent, 1); hooks::hkClientMode->Init((void*)clientMode, 0); //hooks::hkClientMode->HookMethod((void*)&hack::Hk_CreateMove, hooks::offCreateMove); hooks::hkClientMode->HookMethod((void*)CreateMove_hook, hooks::offCreateMove); @@ -178,18 +167,9 @@ void hack::Initialize() { hooks::hkClient->HookMethod((void*)DispatchUserMessage_hook, hooks::offFrameStageNotify + 1); hooks::hkClient->HookMethod((void*)IN_KeyEvent_hook, hooks::offKeyEvent); hooks::hkClient->Apply(); - /*hooks::hkMatSurface = new hooks::VMTHook(); - hooks::hkMatSurface->Init((void*)interfaces::matsurface, 0); - hooks::hkMatSurface->HookMethod((void*)hk_HandleInputEvent, hooks::offHandleInputEvent); - hooks::hkMatSurface->Apply(); - logging::Info("MatSurface Hooked? %f", interfaces::matsurface->DrawGetAlphaMultiplier());*/ - logging::Info("Hooked!"); - logging::Info("Finding GlowObjectManager..."); g_GlowObjectManager = *reinterpret_cast(gSignatures.GetClientSignature("C1 E0 05 03 05") + 5); - logging::Info("GlowObjectManager: 0x%08x", g_GlowObjectManager); InitStrings(); g_pChatStack = new ChatStack(); - logging::Info("Init done!"); } void hack::Think() { @@ -199,7 +179,6 @@ void hack::Think() { void hack::Shutdown() { if (hack::shutdown) return; hack::shutdown = true; - logging::Info("Shutting down..."); logging::Shutdown(); if (hooks::hkPanel) hooks::hkPanel->Kill(); if (hooks::hkClientMode) hooks::hkClientMode->Kill(); diff --git a/cathook/src/hack.h b/cathook/src/hack.h index fe83c67d..a9e39365 100644 --- a/cathook/src/hack.h +++ b/cathook/src/hack.h @@ -26,11 +26,6 @@ #define DELETE_HACK(x) \ delete g_ph##x -#include "beforecheaders.h" -#include -#include "aftercheaders.h" -extern std::string ggggppppvvvv; - class IHack; class CUserCmd; class CViewSetup; diff --git a/cathook/src/helpers.cpp b/cathook/src/helpers.cpp index 4e5bffd3..442c7947 100644 --- a/cathook/src/helpers.cpp +++ b/cathook/src/helpers.cpp @@ -266,6 +266,7 @@ char GetUpperChar(ButtonCode_t button) { case KEY_EQUAL: return '+'; default: + if (strlen(interfaces::input->ButtonCodeToString(button)) != 1) return 0; return toupper(*interfaces::input->ButtonCodeToString(button)); } } @@ -286,6 +287,7 @@ char GetChar(ButtonCode_t button) { if (button >= KEY_PAD_0 && button <= KEY_PAD_9) { return button - KEY_PAD_0 + '0'; } + if (strlen(interfaces::input->ButtonCodeToString(button)) != 1) return 0; return *interfaces::input->ButtonCodeToString(button); } } @@ -544,6 +546,9 @@ bool GetProjectileData(CachedEntity* weapon, float& speed, float& gravity) { } bool Developer(CachedEntity* ent) { +#if _DEVELOPER == 1 + return (ent == LOCAL_E); +#endif return (ent->m_pPlayerInfo && ent->m_pPlayerInfo->friendsID == 347272825UL); } diff --git a/cathook/src/hooks.cpp b/cathook/src/hooks.cpp index e23bebe4..d85eff9e 100644 --- a/cathook/src/hooks.cpp +++ b/cathook/src/hooks.cpp @@ -40,11 +40,9 @@ void**& hooks::GetVMT(void* inst, unsigned int offset) { } void hooks::VMTHook::Init(void* inst, unsigned int offset) { - logging::Info("Initializing VMTHook at 0x%08x", inst); vmt = &GetVMT(inst, offset); oldvmt = *vmt; unsigned int cnt = CountMethods(oldvmt); - logging::Info("found %i methods...", cnt); void **arr = array = (void**)malloc((cnt + 4) * sizeof(void*)); arr[0] = this; arr[1] = (void* )GUARD; diff --git a/cathook/src/hooks/PaintTraverse.cpp b/cathook/src/hooks/PaintTraverse.cpp index 0ebe8326..c6c98664 100644 --- a/cathook/src/hooks/PaintTraverse.cpp +++ b/cathook/src/hooks/PaintTraverse.cpp @@ -17,12 +17,13 @@ void PaintTraverse_hook(void* p, unsigned int vp, bool fr, bool ar) { if (!segvcatch::handler_fpe || !segvcatch::handler_segv) { segvcatch::init_segv(); segvcatch::init_fpe(); - logging::Info("segvcatch init!"); } #endif SEGV_BEGIN; + static unsigned long panel_focus = 0; static unsigned long panel_scope = 0; static unsigned long panel_top = 0; + static bool draw_flag = false; bool call_default = true; if (g_Settings.bHackEnabled->GetBool() && panel_scope && g_Settings.bNoZoom->GetBool() && vp == panel_scope) call_default = false; if (g_Settings.bHackEnabled->GetBool()) { @@ -30,15 +31,10 @@ void PaintTraverse_hook(void* p, unsigned int vp, bool fr, bool ar) { interfaces::surface->SetCursorAlwaysVisible(vis); } + if (call_default) SAFE_CALL(((PaintTraverse_t*)hooks::hkPanel->GetMethod(hooks::offPaintTraverse))(p, vp, fr, ar)); + if (vp == panel_top) draw_flag = true; if (!g_Settings.bHackEnabled->GetBool()) return; -#if GUI_ENABLED == true - /*g_pGUI->UpdateKeys(); - g_pGUI->UpdateMouse(); - g_pGUI->Draw();*/ - if (vp == panel_top) - g_pGUI->Update(); -#endif // Because of single-multi thread shit I'm gonna put this thing riiiight here. static bool autoexec_done = false; if (!autoexec_done) { @@ -72,10 +68,6 @@ void PaintTraverse_hook(void* p, unsigned int vp, bool fr, bool ar) { } #endif - if (g_Settings.bNoVisuals->GetBool()) { - return; - } - if (g_Settings.bCleanScreenshots->GetBool() && interfaces::engineClient->IsTakingScreenshot()) return; if (!draw::width || !draw::height) { interfaces::engineClient->GetScreenSize(draw::width, draw::height); @@ -85,7 +77,9 @@ void PaintTraverse_hook(void* p, unsigned int vp, bool fr, bool ar) { if (strlen(name) > 4) { if (name[0] == 'M' && name[3] == 'S') { panel_top = vp; - logging::Info("Got top panel: %i", vp); + } + if (name[0] == 'F' && name[5] == 'O') { + panel_focus = vp; } } } @@ -97,24 +91,36 @@ void PaintTraverse_hook(void* p, unsigned int vp, bool fr, bool ar) { if (!interfaces::engineClient->IsInGame()) { g_Settings.bInvalid = true; } - if (g_Settings.bInvalid) { - return; - } - if (CE_BAD(g_pLocalPlayer->entity)) { - return; - } - if (panel_top == vp) { - ResetStrings(); - if (g_Settings.bShowLogo->GetBool()) { - AddSideString(colors::green, "cathook by d4rkc4t"); -#if _DEVELOPER - AddSideString(colors::red, "DEVELOPER BUILD"); -#else - AddSideString(colors::green, "Early Access: " __DRM_NAME); -#endif - AddSideString(colors::green, "Version: " CATHOOK_VERSION_MAJOR "." CATHOOK_VERSION_MINOR "." CATHOOK_VERSION_PATCH); - } + if (g_Settings.bNoVisuals->GetBool()) { + return; + } + if (g_Settings.bCleanScreenshots->GetBool() && interfaces::engineClient->IsTakingScreenshot()) return; + + ResetStrings(); + + if (vp != panel_focus) return; + if (!draw_flag) return; + draw_flag = false; + +#if GUI_ENABLED == true + /*g_pGUI->UpdateKeys(); + g_pGUI->UpdateMouse(); + g_pGUI->Draw();*/ + g_pGUI->Update(); +#endif + + + if (g_Settings.bShowLogo->GetBool()) { + AddSideString(colors::green, "cathook by d4rkc4t"); +#if _DEVELOPER + AddSideString(colors::red, "[developer build]"); +#else + AddSideString(colors::green, "built for " __DRM_NAME); +#endif + AddSideString(colors::green, "alpha build " CATHOOK_BUILD_NUMBER " \"" CATHOOK_BUILD_NAME "\""); + } + if (CE_GOOD(g_pLocalPlayer->entity) && !g_Settings.bInvalid) { //SAFE_CALL(PAINT_TRAVERSE(AutoStrafe)); //SAFE_CALL(PAINT_TRAVERSE(AntiAim)); SAFE_CALL(PAINT_TRAVERSE(AntiDisguise)); @@ -154,8 +160,9 @@ void PaintTraverse_hook(void* p, unsigned int vp, bool fr, bool ar) { RemoveCondition(g_pLocalPlayer->entity, condition::TFCond_Zoomed); } } - DrawStrings(); } + + DrawStrings(); SEGV_END; } diff --git a/cathook/src/hooks/others.cpp b/cathook/src/hooks/others.cpp index e4f491f3..180f1aef 100644 --- a/cathook/src/hooks/others.cpp +++ b/cathook/src/hooks/others.cpp @@ -20,9 +20,9 @@ bool CanPacket_hook(void* thisptr) { int IN_KeyEvent_hook(void* thisptr, int eventcode, int keynum, const char* pszCurrentBinding) { SEGV_BEGIN; - /*if (eventcode == 1) { - if (g_pGUI->KeyEvent((ButtonCode_t)keynum)) return 1; - }*/ + if (g_pGUI->ConsumesKey((ButtonCode_t)keynum)) { + return 0; + } return ((IN_KeyEvent_t*)hooks::hkClient->GetMethod(hooks::offKeyEvent))(thisptr, eventcode, keynum, pszCurrentBinding); SEGV_END; return 0; diff --git a/cathook/src/logging.cpp b/cathook/src/logging.cpp index 2189db8c..ef520035 100644 --- a/cathook/src/logging.cpp +++ b/cathook/src/logging.cpp @@ -5,14 +5,11 @@ * Author: nullifiedcat */ -#include "logging.h" -#include "helpers.h" - #include #include #include -#include "interfaces.h" +#include "common.h" #include "sdk.h" FILE* logging::handle = 0; @@ -36,6 +33,7 @@ void logging::Info(const char* fmt, ...) { fprintf(logging::handle, "%s", result); fflush(logging::handle); if (interfaces::cvar) { + if (g_Settings.bDebugLog && g_Settings.bDebugLog->GetBool()) interfaces::cvar->ConsolePrintf("%s", result); } delete [] buffer; diff --git a/cathook/src/sdk/KeyValues.cpp b/cathook/src/sdk/KeyValues.cpp new file mode 100644 index 00000000..ad4af096 --- /dev/null +++ b/cathook/src/sdk/KeyValues.cpp @@ -0,0 +1,3169 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +// +//=============================================================================// + +#if defined( _WIN32 ) && !defined( _X360 ) +#include // for WideCharToMultiByte and MultiByteToWideChar +#elif defined(POSIX) +#include // wcslen() +#define _alloca alloca +#define _wtoi(arg) wcstol(arg, NULL, 10) +#define _wtoi64(arg) wcstoll(arg, NULL, 10) +#endif + +#include +#include "filesystem.h" +#include +#include "tier0/icommandline.h" + +#include +#include +#include "tier0/dbg.h" +#include "tier0/mem.h" +#include "utlbuffer.h" +#include "utlhash.h" +#include "utlvector.h" +#include "utlqueue.h" +#include "UtlSortVector.h" +#include "convar.h" + +// memdbgon must be the last include file in a .cpp file!!! +#include + +static const char * s_LastFileLoadingFrom = "unknown"; // just needed for error messages + +// Statics for the growable string table +int (*KeyValues::s_pfGetSymbolForString)( const char *name, bool bCreate ) = &KeyValues::GetSymbolForStringClassic; +const char *(*KeyValues::s_pfGetStringForSymbol)( int symbol ) = &KeyValues::GetStringForSymbolClassic; +CKeyValuesGrowableStringTable *KeyValues::s_pGrowableStringTable = NULL; + +#define KEYVALUES_TOKEN_SIZE 4096 +static char s_pTokenBuf[KEYVALUES_TOKEN_SIZE]; + + +#define INTERNALWRITE( pData, len ) InternalWrite( filesystem, f, pBuf, pData, len ) + + +// a simple class to keep track of a stack of valid parsed symbols +const int MAX_ERROR_STACK = 64; +class CKeyValuesErrorStack +{ +public: + CKeyValuesErrorStack() : m_pFilename("NULL"), m_errorIndex(0), m_maxErrorIndex(0) {} + + void SetFilename( const char *pFilename ) + { + m_pFilename = pFilename; + m_maxErrorIndex = 0; + } + + // entering a new keyvalues block, save state for errors + // Not save symbols instead of pointers because the pointers can move! + int Push( int symName ) + { + if ( m_errorIndex < MAX_ERROR_STACK ) + { + m_errorStack[m_errorIndex] = symName; + } + m_errorIndex++; + m_maxErrorIndex = max( m_maxErrorIndex, (m_errorIndex-1) ); + return m_errorIndex-1; + } + + // exiting block, error isn't in this block, remove. + void Pop() + { + m_errorIndex--; + Assert(m_errorIndex>=0); + } + + // Allows you to keep the same stack level, but change the name as you parse peers + void Reset( int stackLevel, int symName ) + { + Assert( stackLevel >= 0 ); + Assert( stackLevel < m_errorIndex ); + if ( stackLevel < MAX_ERROR_STACK ) + m_errorStack[stackLevel] = symName; + } + + // Hit an error, report it and the parsing stack for context + void ReportError( const char *pError ) + { + bool bSpewCR = false; + + Warning( "KeyValues Error: %s in file %s\n", pError, m_pFilename ); + for ( int i = 0; i < m_maxErrorIndex; i++ ) + { + if ( i < MAX_ERROR_STACK && m_errorStack[i] != INVALID_KEY_SYMBOL ) + { + if ( i < m_errorIndex ) + { + Warning( "%s, ", KeyValues::CallGetStringForSymbol(m_errorStack[i]) ); + } + else + { + Warning( "(*%s*), ", KeyValues::CallGetStringForSymbol(m_errorStack[i]) ); + } + + bSpewCR = true; + } + } + + if ( bSpewCR ) + Warning( "\n" ); + } + +private: + int m_errorStack[MAX_ERROR_STACK]; + const char *m_pFilename; + int m_errorIndex; + int m_maxErrorIndex; +} g_KeyValuesErrorStack; + + +// a simple helper that creates stack entries as it goes in & out of scope +class CKeyErrorContext +{ +public: + CKeyErrorContext( KeyValues *pKv ) + { + Init( pKv->GetNameSymbol() ); + } + + ~CKeyErrorContext() + { + g_KeyValuesErrorStack.Pop(); + } + CKeyErrorContext( int symName ) + { + Init( symName ); + } + void Reset( int symName ) + { + g_KeyValuesErrorStack.Reset( m_stackLevel, symName ); + } + int GetStackLevel() const + { + return m_stackLevel; + } +private: + void Init( int symName ) + { + m_stackLevel = g_KeyValuesErrorStack.Push( symName ); + } + + int m_stackLevel; +}; + +// Uncomment this line to hit the ~CLeakTrack assert to see what's looking like it's leaking +// #define LEAKTRACK + +#ifdef LEAKTRACK + +class CLeakTrack +{ +public: + CLeakTrack() + { + } + ~CLeakTrack() + { + if ( keys.Count() != 0 ) + { + Assert( 0 ); + } + } + + struct kve + { + KeyValues *kv; + char name[ 256 ]; + }; + + void AddKv( KeyValues *kv, char const *name ) + { + kve k; + Q_strncpy( k.name, name ? name : "NULL", sizeof( k.name ) ); + k.kv = kv; + + keys.AddToTail( k ); + } + + void RemoveKv( KeyValues *kv ) + { + int c = keys.Count(); + for ( int i = 0; i < c; i++ ) + { + if ( keys[i].kv == kv ) + { + keys.Remove( i ); + break; + } + } + } + + CUtlVector< kve > keys; +}; + +static CLeakTrack track; + +#define TRACK_KV_ADD( ptr, name ) track.AddKv( ptr, name ) +#define TRACK_KV_REMOVE( ptr ) track.RemoveKv( ptr ) + +#else + +#define TRACK_KV_ADD( ptr, name ) +#define TRACK_KV_REMOVE( ptr ) + +#endif + + +//----------------------------------------------------------------------------- +// Purpose: An arbitrarily growable string table for KeyValues key names. +// See the comment in the header for more info. +//----------------------------------------------------------------------------- +class CKeyValuesGrowableStringTable +{ +public: + // Constructor + CKeyValuesGrowableStringTable() : + #ifdef PLATFORM_64BITS + m_vecStrings( 0, 4 * 512 * 1024 ) + #else + m_vecStrings( 0, 512 * 1024 ) + #endif + , m_hashLookup( 2048, 0, 0, m_Functor, m_Functor ) + { + m_vecStrings.AddToTail( '\0' ); + } + + // Translates a string to an index + int GetSymbolForString( const char *name, bool bCreate = true ) + { + AUTO_LOCK( m_mutex ); + + // Put the current details into our hash functor + m_Functor.SetCurString( name ); + m_Functor.SetCurStringBase( (const char *)m_vecStrings.Base() ); + + if ( bCreate ) + { + bool bInserted = false; + UtlHashHandle_t hElement = m_hashLookup.Insert( -1, &bInserted ); + if ( bInserted ) + { + int iIndex = m_vecStrings.AddMultipleToTail( V_strlen( name ) + 1, name ); + m_hashLookup[ hElement ] = iIndex; + } + + return m_hashLookup[ hElement ]; + } + else + { + UtlHashHandle_t hElement = m_hashLookup.Find( -1 ); + if ( m_hashLookup.IsValidHandle( hElement ) ) + return m_hashLookup[ hElement ]; + else + return -1; + } + } + + // Translates an index back to a string + const char *GetStringForSymbol( int symbol ) + { + return (const char *)m_vecStrings.Base() + symbol; + } + +private: + + // A class plugged into CUtlHash that allows us to change the behavior of the table + // and store only the index in the table. + class CLookupFunctor + { + public: + CLookupFunctor() : m_pchCurString( NULL ), m_pchCurBase( NULL ) {} + + // Sets what we are currently inserting or looking for. + void SetCurString( const char *pchCurString ) { m_pchCurString = pchCurString; } + void SetCurStringBase( const char *pchCurBase ) { m_pchCurBase = pchCurBase; } + + // The compare function. + bool operator()( int nLhs, int nRhs ) const + { + const char *pchLhs = nLhs > 0 ? m_pchCurBase + nLhs : m_pchCurString; + const char *pchRhs = nRhs > 0 ? m_pchCurBase + nRhs : m_pchCurString; + + return ( 0 == V_stricmp( pchLhs, pchRhs ) ); + } + + // The hash function. + unsigned int operator()( int nItem ) const + { + return HashStringCaseless( m_pchCurString ); + } + + private: + const char *m_pchCurString; + const char *m_pchCurBase; + }; + + CThreadFastMutex m_mutex; + CLookupFunctor m_Functor; + CUtlHash m_hashLookup; + CUtlVector m_vecStrings; +}; + + +//----------------------------------------------------------------------------- +// Purpose: Sets whether the KeyValues system should use an arbitrarily growable +// string table. See the comment in the header for more info. +//----------------------------------------------------------------------------- +void KeyValues::SetUseGrowableStringTable( bool bUseGrowableTable ) +{ + if ( bUseGrowableTable ) + { + s_pfGetStringForSymbol = &(KeyValues::GetStringForSymbolGrowable); + s_pfGetSymbolForString = &(KeyValues::GetSymbolForStringGrowable); + + if ( NULL == s_pGrowableStringTable ) + { + s_pGrowableStringTable = new CKeyValuesGrowableStringTable; + } + } + else + { + s_pfGetStringForSymbol = &(KeyValues::GetStringForSymbolClassic); + s_pfGetSymbolForString = &(KeyValues::GetSymbolForStringClassic); + + delete s_pGrowableStringTable; + s_pGrowableStringTable = NULL; + } +} + +//----------------------------------------------------------------------------- +// Purpose: Bodys of the function pointers used for interacting with the key +// name string table +//----------------------------------------------------------------------------- +int KeyValues::GetSymbolForStringClassic( const char *name, bool bCreate ) +{ + return KeyValuesSystem()->GetSymbolForString( name, bCreate ); +} + +const char *KeyValues::GetStringForSymbolClassic( int symbol ) +{ + return KeyValuesSystem()->GetStringForSymbol( symbol ); +} + +int KeyValues::GetSymbolForStringGrowable( const char *name, bool bCreate ) +{ + return s_pGrowableStringTable->GetSymbolForString( name, bCreate ); +} + +const char *KeyValues::GetStringForSymbolGrowable( int symbol ) +{ + return s_pGrowableStringTable->GetStringForSymbol( symbol ); +} + + + +//----------------------------------------------------------------------------- +// Purpose: Constructor +//----------------------------------------------------------------------------- +KeyValues::KeyValues( 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 = INVALID_KEY_SYMBOL; + 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; + m_bEvaluateConditionals = true; + + // for future proof + memset( unused, 0, sizeof(unused) ); +} + +//----------------------------------------------------------------------------- +// Purpose: Destructor +//----------------------------------------------------------------------------- +KeyValues::~KeyValues() +{ + TRACK_KV_REMOVE( this ); + + RemoveEverything(); +} + +//----------------------------------------------------------------------------- +// 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; + delete dat; + } + + for ( dat = m_pPeer; dat && dat != this; dat = datNext ) + { + datNext = dat->m_pPeer; + dat->m_pPeer = NULL; + delete dat; + } + + delete [] m_sValue; + m_sValue = NULL; + delete [] m_wsValue; + m_wsValue = NULL; +} + +//----------------------------------------------------------------------------- +// Purpose: +// Input : *f - +//----------------------------------------------------------------------------- + +void KeyValues::RecursiveSaveToFile( CUtlBuffer& buf, int indentLevel, bool sortKeys /*= false*/, bool bAllowEmptyString /*= false*/ ) +{ + RecursiveSaveToFile( NULL, FILESYSTEM_INVALID_HANDLE, &buf, indentLevel, sortKeys, bAllowEmptyString ); +} + +//----------------------------------------------------------------------------- +// Adds a chain... if we don't find stuff in this keyvalue, we'll look +// in the one we're chained to. +//----------------------------------------------------------------------------- + +void KeyValues::ChainKeyValue( KeyValues* pChain ) +{ + m_pChain = pChain; +} + +//----------------------------------------------------------------------------- +// Purpose: Get the name of the current key section +//----------------------------------------------------------------------------- +const char *KeyValues::GetName( void ) const +{ + return s_pfGetStringForSymbol( m_iKeyName ); +} + +//----------------------------------------------------------------------------- +// Purpose: Read a single token from buffer (0 terminated) +//----------------------------------------------------------------------------- +#pragma warning (disable:4706) +const char *KeyValues::ReadToken( CUtlBuffer &buf, bool &wasQuoted, bool &wasConditional ) +{ + wasQuoted = false; + wasConditional = false; + + if ( !buf.IsValid() ) + return NULL; + + // eating white spaces and remarks loop + while ( true ) + { + buf.EatWhiteSpace(); + if ( !buf.IsValid() ) + return NULL; // file ends after reading whitespaces + + // stop if it's not a comment; a new token starts here + if ( !buf.EatCPPComment() ) + break; + } + + const char *c = (const char*)buf.PeekGet( sizeof(char), 0 ); + if ( !c ) + return NULL; + + // read quoted strings specially + if ( *c == '\"' ) + { + wasQuoted = true; + buf.GetDelimitedString( m_bHasEscapeSequences ? GetCStringCharConversion() : GetNoEscCharConversion(), + s_pTokenBuf, KEYVALUES_TOKEN_SIZE ); + return s_pTokenBuf; + } + + if ( *c == '{' || *c == '}' ) + { + // it's a control char, just add this one char and stop reading + s_pTokenBuf[0] = *c; + s_pTokenBuf[1] = 0; + buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 1 ); + return s_pTokenBuf; + } + + // read in the token until we hit a whitespace or a control character + bool bReportedError = false; + bool bConditionalStart = false; + int nCount = 0; + while ( ( c = (const char*)buf.PeekGet( sizeof(char), 0 ) ) ) + { + // end of file + if ( *c == 0 ) + break; + + // break if any control character appears in non quoted tokens + if ( *c == '"' || *c == '{' || *c == '}' ) + break; + + if ( *c == '[' ) + bConditionalStart = true; + + if ( *c == ']' && bConditionalStart ) + { + wasConditional = true; + } + + // break on whitespace + if ( isspace(*c) ) + break; + + if (nCount < (KEYVALUES_TOKEN_SIZE-1) ) + { + s_pTokenBuf[nCount++] = *c; // add char to buffer + } + else if ( !bReportedError ) + { + bReportedError = true; + g_KeyValuesErrorStack.ReportError(" ReadToken overflow" ); + } + + buf.SeekGet( CUtlBuffer::SEEK_CURRENT, 1 ); + } + s_pTokenBuf[ nCount ] = 0; + return s_pTokenBuf; +} +#pragma warning (default:4706) + + + +//----------------------------------------------------------------------------- +// Purpose: if parser should translate escape sequences ( /n, /t etc), set to true +//----------------------------------------------------------------------------- +void KeyValues::UsesEscapeSequences(bool state) +{ + m_bHasEscapeSequences = state; +} + + +//----------------------------------------------------------------------------- +// Purpose: if parser should evaluate conditional blocks ( [$WINDOWS] etc. ) +//----------------------------------------------------------------------------- +void KeyValues::UsesConditionals(bool state) +{ + m_bEvaluateConditionals = state; +} + + +//----------------------------------------------------------------------------- +// Purpose: Load keyValues from disk +//----------------------------------------------------------------------------- +bool KeyValues::LoadFromFile( IBaseFileSystem *filesystem, const char *resourceName, const char *pathID, bool refreshCache ) +{ + Assert(filesystem); +#ifdef WIN32 + Assert( IsX360() || ( IsPC() && _heapchk() == _HEAPOK ) ); +#endif + +#ifdef STAGING_ONLY + static bool s_bCacheEnabled = !!CommandLine()->FindParm( "-enable_keyvalues_cache" ); + const bool bUseCache = s_bCacheEnabled && ( s_pfGetSymbolForString == KeyValues::GetSymbolForStringClassic ); +#else + /* + People are cheating with the keyvalue cache enabled by doing the below, so disable it. + + For example if one is to allow a blue demoman texture on sv_pure they + change it to this, "$basetexture" "temp/demoman_blue". Remember to move the + demoman texture to the temp folder in the materials folder. It will likely + not be there so make a new folder for it. Once the directory in the + demoman_blue vmt is changed to the temp folder and the vtf texture is in + the temp folder itself you are finally done. + + I packed my mods into a vpk but I don't think it's required. Once in game + you must create a server via the create server button and select the map + that will load the custom texture before you join a valve server. I suggest + you only do this with player textures and such as they are always loaded. + After you load the map you join the valve server and the textures should + appear and work on valve servers. + + This can be done on any sv_pure 1 server but it depends on what is type of + files are allowed. All valve servers allow temp files so that is the + example I used here." + + So all vmt's files can bypass sv_pure 1. And I believe this mod is mostly + made of vmt files, so valve's sv_pure 1 bull is pretty redundant. + */ + const bool bUseCache = false; +#endif + + // If pathID is null, we cannot cache the result because that has a weird iterate-through-a-bunch-of-locations behavior. + const bool bUseCacheForRead = bUseCache && !refreshCache && pathID != NULL; + const bool bUseCacheForWrite = bUseCache && pathID != NULL; + + COM_TimestampedLog( "KeyValues::LoadFromFile(%s%s%s): Begin", pathID ? pathID : "", pathID && resourceName ? "/" : "", resourceName ? resourceName : "" ); + + // Keep a cache of keyvalues, try to load it here. + if ( bUseCacheForRead && KeyValuesSystem()->LoadFileKeyValuesFromCache( this, resourceName, pathID, filesystem ) ) { + COM_TimestampedLog( "KeyValues::LoadFromFile(%s%s%s): End / CacheHit", pathID ? pathID : "", pathID && resourceName ? "/" : "", resourceName ? resourceName : "" ); + return true; + } + + FileHandle_t f = filesystem->Open(resourceName, "rb", pathID); + if ( !f ) + { + COM_TimestampedLog("KeyValues::LoadFromFile(%s%s%s): End / FileNotFound", pathID ? pathID : "", pathID && resourceName ? "/" : "", resourceName ? resourceName : ""); + return false; + } + + s_LastFileLoadingFrom = (char*)resourceName; + + // load file into a null-terminated buffer + int fileSize = filesystem->Size( f ); + unsigned bufSize = ((IFileSystem *)filesystem)->GetOptimalReadSize( f, fileSize + 2 ); + + char *buffer = (char*)((IFileSystem *)filesystem)->AllocOptimalReadBuffer( f, bufSize ); + Assert( buffer ); + + // read into local buffer + bool bRetOK = ( ((IFileSystem *)filesystem)->ReadEx( buffer, bufSize, fileSize, f ) != 0 ); + + filesystem->Close( f ); // close file after reading + + if ( bRetOK ) + { + buffer[fileSize] = 0; // null terminate file as EOF + buffer[fileSize+1] = 0; // double NULL terminating in case this is a unicode file + bRetOK = LoadFromBuffer( resourceName, buffer, filesystem ); + } + + // The cache relies on the KeyValuesSystem string table, which will only be valid if we're + // using classic mode. + if ( bUseCacheForWrite && bRetOK ) + { + KeyValuesSystem()->AddFileKeyValuesToCache( this, resourceName, pathID ); + } + + ( (IFileSystem *)filesystem )->FreeOptimalReadBuffer( buffer ); + + COM_TimestampedLog("KeyValues::LoadFromFile(%s%s%s): End / Success", pathID ? pathID : "", pathID && resourceName ? "/" : "", resourceName ? resourceName : ""); + + return bRetOK; +} + +//----------------------------------------------------------------------------- +// Purpose: Save the keyvalues to disk +// Creates the path to the file if it doesn't exist +//----------------------------------------------------------------------------- +bool KeyValues::SaveToFile( IBaseFileSystem *filesystem, const char *resourceName, const char *pathID, bool sortKeys /*= false*/, bool bAllowEmptyString /*= false*/, bool bCacheResult /*= false*/ ) +{ + // create a write file + FileHandle_t f = filesystem->Open(resourceName, "wb", pathID); + + if ( f == FILESYSTEM_INVALID_HANDLE ) + { + DevMsg(1, "KeyValues::SaveToFile: couldn't open file \"%s\" in path \"%s\".\n", + resourceName?resourceName:"NULL", pathID?pathID:"NULL" ); + return false; + } + + KeyValuesSystem()->InvalidateCacheForFile( resourceName, pathID ); + if ( bCacheResult ) { + KeyValuesSystem()->AddFileKeyValuesToCache( this, resourceName, pathID ); + } + RecursiveSaveToFile(filesystem, f, NULL, 0, sortKeys, bAllowEmptyString ); + filesystem->Close(f); + + return true; +} + +//----------------------------------------------------------------------------- +// Purpose: Write out a set of indenting +//----------------------------------------------------------------------------- +void KeyValues::WriteIndents( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, int indentLevel ) +{ + for ( int i = 0; i < indentLevel; i++ ) + { + INTERNALWRITE( "\t", 1 ); + } +} + +//----------------------------------------------------------------------------- +// Purpose: Write out a string where we convert the double quotes to backslash double quote +//----------------------------------------------------------------------------- +void KeyValues::WriteConvertedString( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, const char *pszString ) +{ + // handle double quote chars within the string + // the worst possible case is that the whole string is quotes + int len = Q_strlen(pszString); + char *convertedString = (char *) _alloca ((len + 1) * sizeof(char) * 2); + int j=0; + for (int i=0; i <= len; i++) + { + if (pszString[i] == '\"') + { + convertedString[j] = '\\'; + j++; + } + else if ( m_bHasEscapeSequences && pszString[i] == '\\' ) + { + convertedString[j] = '\\'; + j++; + } + convertedString[j] = pszString[i]; + j++; + } + + INTERNALWRITE(convertedString, Q_strlen(convertedString)); +} + + +void KeyValues::InternalWrite( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, const void *pData, int len ) +{ + if ( filesystem ) + { + filesystem->Write( pData, len, f ); + } + + if ( pBuf ) + { + pBuf->Put( pData, len ); + } +} + +//----------------------------------------------------------------------------- +// Purpose: Save keyvalues from disk, if subkey values are detected, calls +// itself to save those +//----------------------------------------------------------------------------- +void KeyValues::RecursiveSaveToFile( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, int indentLevel, bool sortKeys, bool bAllowEmptyString ) +{ + // write header + WriteIndents( filesystem, f, pBuf, indentLevel ); + INTERNALWRITE("\"", 1); + WriteConvertedString(filesystem, f, pBuf, GetName()); + INTERNALWRITE("\"\n", 2); + WriteIndents( filesystem, f, pBuf, indentLevel ); + INTERNALWRITE("{\n", 2); + + // loop through all our keys writing them to disk + if ( sortKeys ) + { + CUtlSortVector< KeyValues*, CUtlSortVectorKeyValuesByName > vecSortedKeys; + + for ( KeyValues *dat = m_pSub; dat != NULL; dat = dat->m_pPeer ) + { + vecSortedKeys.InsertNoSort(dat); + } + vecSortedKeys.RedoSort(); + + FOR_EACH_VEC( vecSortedKeys, i ) + { + SaveKeyToFile( vecSortedKeys[i], filesystem, f, pBuf, indentLevel, sortKeys, bAllowEmptyString ); + } + } + else + { + for ( KeyValues *dat = m_pSub; dat != NULL; dat = dat->m_pPeer ) + SaveKeyToFile( dat, filesystem, f, pBuf, indentLevel, sortKeys, bAllowEmptyString ); + } + + // write tail + WriteIndents(filesystem, f, pBuf, indentLevel); + INTERNALWRITE("}\n", 2); +} + +void KeyValues::SaveKeyToFile( KeyValues *dat, IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, int indentLevel, bool sortKeys, bool bAllowEmptyString ) +{ + if ( dat->m_pSub ) + { + dat->RecursiveSaveToFile( filesystem, f, pBuf, indentLevel + 1, sortKeys, bAllowEmptyString ); + } + else + { + // only write non-empty keys + + switch (dat->m_iDataType) + { + case TYPE_STRING: + { + if ( dat->m_sValue && ( bAllowEmptyString || *(dat->m_sValue) ) ) + { + WriteIndents(filesystem, f, pBuf, indentLevel + 1); + INTERNALWRITE("\"", 1); + WriteConvertedString(filesystem, f, pBuf, dat->GetName()); + INTERNALWRITE("\"\t\t\"", 4); + + WriteConvertedString(filesystem, f, pBuf, dat->m_sValue); + + INTERNALWRITE("\"\n", 2); + } + break; + } + case TYPE_WSTRING: + { + if ( dat->m_wsValue ) + { + static char buf[KEYVALUES_TOKEN_SIZE]; + // make sure we have enough space + int result = Q_UnicodeToUTF8( dat->m_wsValue, buf, KEYVALUES_TOKEN_SIZE); + if (result) + { + WriteIndents(filesystem, f, pBuf, indentLevel + 1); + INTERNALWRITE("\"", 1); + INTERNALWRITE(dat->GetName(), Q_strlen(dat->GetName())); + INTERNALWRITE("\"\t\t\"", 4); + + WriteConvertedString(filesystem, f, pBuf, buf); + + INTERNALWRITE("\"\n", 2); + } + } + break; + } + + case TYPE_INT: + { + WriteIndents(filesystem, f, pBuf, indentLevel + 1); + INTERNALWRITE("\"", 1); + INTERNALWRITE(dat->GetName(), Q_strlen(dat->GetName())); + INTERNALWRITE("\"\t\t\"", 4); + + char buf[32]; + Q_snprintf(buf, sizeof( buf ), "%d", dat->m_iValue); + + INTERNALWRITE(buf, Q_strlen(buf)); + INTERNALWRITE("\"\n", 2); + break; + } + + case TYPE_UINT64: + { + WriteIndents(filesystem, f, pBuf, indentLevel + 1); + INTERNALWRITE("\"", 1); + INTERNALWRITE(dat->GetName(), Q_strlen(dat->GetName())); + INTERNALWRITE("\"\t\t\"", 4); + + char buf[32]; + // write "0x" + 16 char 0-padded hex encoded 64 bit value +#ifdef WIN32 + Q_snprintf( buf, sizeof( buf ), "0x%016I64X", *( (uint64 *)dat->m_sValue ) ); +#else + Q_snprintf( buf, sizeof( buf ), "0x%016llX", *( (uint64 *)dat->m_sValue ) ); +#endif + + INTERNALWRITE(buf, Q_strlen(buf)); + INTERNALWRITE("\"\n", 2); + break; + } + + case TYPE_FLOAT: + { + WriteIndents(filesystem, f, pBuf, indentLevel + 1); + INTERNALWRITE("\"", 1); + INTERNALWRITE(dat->GetName(), Q_strlen(dat->GetName())); + INTERNALWRITE("\"\t\t\"", 4); + + char buf[48]; + Q_snprintf(buf, sizeof( buf ), "%f", dat->m_flValue); + + INTERNALWRITE(buf, Q_strlen(buf)); + INTERNALWRITE("\"\n", 2); + break; + } + case TYPE_COLOR: + DevMsg(1, "KeyValues::RecursiveSaveToFile: TODO, missing code for TYPE_COLOR.\n"); + break; + + default: + break; + } + } +} + +//----------------------------------------------------------------------------- +// Purpose: looks up a key by symbol name +//----------------------------------------------------------------------------- +KeyValues *KeyValues::FindKey(int keySymbol) const +{ + for (KeyValues *dat = m_pSub; dat != NULL; dat = dat->m_pPeer) + { + if (dat->m_iKeyName == keySymbol) + return dat; + } + + return NULL; +} + +//----------------------------------------------------------------------------- +// 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) +//----------------------------------------------------------------------------- +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 = s_pfGetSymbolForString( 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); + + dat->UsesEscapeSequences( m_bHasEscapeSequences != 0 ); // use same format as parent + dat->UsesConditionals( m_bEvaluateConditionals != 0 ); + + // 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; +} + +//----------------------------------------------------------------------------- +// Purpose: Create a new key, with an autogenerated name. +// Name is guaranteed to be an integer, of value 1 higher than the highest +// other integer key name +//----------------------------------------------------------------------------- +KeyValues *KeyValues::CreateNewKey() +{ + int newID = 1; + + // search for any key with higher values + KeyValues *pLastChild = NULL; + for (KeyValues *dat = m_pSub; dat != NULL; dat = dat->m_pPeer) + { + // case-insensitive string compare + int val = atoi(dat->GetName()); + if (newID <= val) + { + newID = val + 1; + } + + pLastChild = dat; + } + + char buf[12]; + Q_snprintf( buf, sizeof(buf), "%d", newID ); + + return CreateKeyUsingKnownLastChild( buf, pLastChild ); +} + + +//----------------------------------------------------------------------------- +// Create a key +//----------------------------------------------------------------------------- +KeyValues* KeyValues::CreateKey( const char *keyName ) +{ + KeyValues *pLastChild = FindLastSubKey(); + return CreateKeyUsingKnownLastChild( keyName, pLastChild ); +} + +//----------------------------------------------------------------------------- +KeyValues* KeyValues::CreateKeyUsingKnownLastChild( const char *keyName, KeyValues *pLastChild ) +{ + // Create a new key + KeyValues* dat = new KeyValues( keyName ); + + dat->UsesEscapeSequences( m_bHasEscapeSequences != 0 ); // use same format as parent does + dat->UsesConditionals( m_bEvaluateConditionals != 0 ); + + // add into subkey list + AddSubkeyUsingKnownLastChild( dat, pLastChild ); + + return dat; +} + +//----------------------------------------------------------------------------- +void KeyValues::AddSubkeyUsingKnownLastChild( KeyValues *pSubkey, KeyValues *pLastChild ) +{ + // Make sure the subkey isn't a child of some other keyvalues + Assert( pSubkey != NULL ); + Assert( pSubkey->m_pPeer == NULL ); + + // Empty child list? + if ( pLastChild == NULL ) + { + Assert( m_pSub == NULL ); + m_pSub = pSubkey; + } + else + { + Assert( m_pSub != NULL ); + Assert( pLastChild->m_pPeer == NULL ); + +// // In debug, make sure that they really do know which child is the last one +// #ifdef _DEBUG +// KeyValues *pTempDat = m_pSub; +// while ( pTempDat->GetNextKey() != NULL ) +// { +// pTempDat = pTempDat->GetNextKey(); +// } +// Assert( pTempDat == pLastChild ); +// #endif + + pLastChild->SetNextKey( pSubkey ); + } +} + + +//----------------------------------------------------------------------------- +// Adds a subkey. Make sure the subkey isn't a child of some other keyvalues +//----------------------------------------------------------------------------- +void KeyValues::AddSubKey( KeyValues *pSubkey ) +{ + // Make sure the subkey isn't a child of some other keyvalues + Assert( pSubkey != NULL ); + Assert( pSubkey->m_pPeer == NULL ); + + // add into subkey list + if ( m_pSub == NULL ) + { + m_pSub = pSubkey; + } + else + { + KeyValues *pTempDat = m_pSub; + while ( pTempDat->GetNextKey() != NULL ) + { + pTempDat = pTempDat->GetNextKey(); + } + + pTempDat->SetNextKey( pSubkey ); + } +} + + + +//----------------------------------------------------------------------------- +// Purpose: Remove a subkey from the list +//----------------------------------------------------------------------------- +void KeyValues::RemoveSubKey(KeyValues *subKey) +{ + if (!subKey) + return; + + // check the list pointer + if (m_pSub == subKey) + { + m_pSub = subKey->m_pPeer; + } + else + { + // look through the list + KeyValues *kv = m_pSub; + while (kv->m_pPeer) + { + if (kv->m_pPeer == subKey) + { + kv->m_pPeer = subKey->m_pPeer; + break; + } + + kv = kv->m_pPeer; + } + } + + subKey->m_pPeer = NULL; +} + + + +//----------------------------------------------------------------------------- +// Purpose: Locate last child. Returns NULL if we have no children +//----------------------------------------------------------------------------- +KeyValues *KeyValues::FindLastSubKey() +{ + + // No children? + if ( m_pSub == NULL ) + return NULL; + + // Scan for the last one + KeyValues *pLastChild = m_pSub; + while ( pLastChild->m_pPeer ) + pLastChild = pLastChild->m_pPeer; + return pLastChild; +} + +//----------------------------------------------------------------------------- +// 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: Get the integer value of a keyName. Default value is returned +// if the keyName can't be found. +//----------------------------------------------------------------------------- +int KeyValues::GetInt( const char *keyName, int defaultValue ) +{ + KeyValues *dat = FindKey( keyName, false ); + if ( dat ) + { + switch ( dat->m_iDataType ) + { + case TYPE_STRING: + return atoi(dat->m_sValue); + case TYPE_WSTRING: + return _wtoi(dat->m_wsValue); + 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; +} + +//----------------------------------------------------------------------------- +// Purpose: Get the integer value of a keyName. Default value is returned +// if the keyName can't be found. +//----------------------------------------------------------------------------- +uint64 KeyValues::GetUint64( const char *keyName, uint64 defaultValue ) +{ + KeyValues *dat = FindKey( keyName, false ); + if ( dat ) + { + switch ( dat->m_iDataType ) + { + case TYPE_STRING: + return (uint64)Q_atoi64(dat->m_sValue); + case TYPE_WSTRING: + return _wtoi64(dat->m_wsValue); + case TYPE_FLOAT: + return (int)dat->m_flValue; + case TYPE_UINT64: + return *((uint64 *)dat->m_sValue); + case TYPE_INT: + case TYPE_PTR: + default: + return dat->m_iValue; + }; + } + return defaultValue; +} + +//----------------------------------------------------------------------------- +// Purpose: Get the pointer value of a keyName. Default value is returned +// if the keyName can't be found. +//----------------------------------------------------------------------------- +void *KeyValues::GetPtr( const char *keyName, void *defaultValue ) +{ + KeyValues *dat = FindKey( keyName, false ); + if ( dat ) + { + switch ( dat->m_iDataType ) + { + case TYPE_PTR: + return dat->m_pValue; + + case TYPE_WSTRING: + case TYPE_STRING: + case TYPE_FLOAT: + case TYPE_INT: + case TYPE_UINT64: + default: + return NULL; + }; + } + return defaultValue; +} + +//----------------------------------------------------------------------------- +// Purpose: Get the float value of a keyName. Default value is returned +// if the keyName can't be found. +//----------------------------------------------------------------------------- +float KeyValues::GetFloat( const char *keyName, float defaultValue ) +{ + KeyValues *dat = FindKey( keyName, false ); + if ( dat ) + { + switch ( dat->m_iDataType ) + { + case TYPE_STRING: + return (float)atof(dat->m_sValue); + case TYPE_WSTRING: +#ifdef WIN32 + return (float) _wtof(dat->m_wsValue); // no wtof +#else + Assert( !"impl me" ); + return 0.0; +#endif + case TYPE_FLOAT: + return dat->m_flValue; + case TYPE_INT: + return (float)dat->m_iValue; + case TYPE_UINT64: + return (float)(*((uint64 *)dat->m_sValue)); + case TYPE_PTR: + default: + return 0.0f; + }; + } + return defaultValue; +} + +//----------------------------------------------------------------------------- +// Purpose: Get the string pointer of a keyName. Default value is returned +// if the keyName can't be found. +//----------------------------------------------------------------------------- +const char *KeyValues::GetString( const char *keyName, const char *defaultValue ) +{ + KeyValues *dat = FindKey( keyName, false ); + if ( dat ) + { + // convert the data to string form then return it + char buf[64]; + switch ( dat->m_iDataType ) + { + case TYPE_FLOAT: + Q_snprintf( buf, sizeof( buf ), "%f", dat->m_flValue ); + SetString( keyName, buf ); + break; + case TYPE_PTR: + Q_snprintf( buf, sizeof( buf ), "%lld", (int64)(size_t)dat->m_pValue ); + SetString( keyName, buf ); + break; + case TYPE_INT: + Q_snprintf( buf, sizeof( buf ), "%d", dat->m_iValue ); + SetString( keyName, buf ); + break; + case TYPE_UINT64: + Q_snprintf( buf, sizeof( buf ), "%lld", *((uint64 *)(dat->m_sValue)) ); + SetString( keyName, buf ); + break; + + case TYPE_WSTRING: + { + // convert the string to char *, set it for future use, and return it + char wideBuf[512]; + int result = Q_UnicodeToUTF8(dat->m_wsValue, wideBuf, 512); + if ( result ) + { + // note: this will copy wideBuf + SetString( keyName, wideBuf ); + } + else + { + return defaultValue; + } + break; + } + case TYPE_STRING: + break; + default: + return defaultValue; + }; + + return dat->m_sValue; + } + return defaultValue; +} + + +const wchar_t *KeyValues::GetWString( const char *keyName, const wchar_t *defaultValue) +{ + KeyValues *dat = FindKey( keyName, false ); + if ( dat ) + { + wchar_t wbuf[64]; + switch ( dat->m_iDataType ) + { + case TYPE_FLOAT: + swprintf(wbuf, Q_ARRAYSIZE(wbuf), L"%f", dat->m_flValue); + SetWString( keyName, wbuf); + break; + case TYPE_PTR: + swprintf( wbuf, Q_ARRAYSIZE(wbuf), L"%lld", (int64)(size_t)dat->m_pValue ); + SetWString( keyName, wbuf ); + break; + case TYPE_INT: + swprintf( wbuf, Q_ARRAYSIZE(wbuf), L"%d", dat->m_iValue ); + SetWString( keyName, wbuf ); + break; + case TYPE_UINT64: + { + swprintf( wbuf, Q_ARRAYSIZE(wbuf), L"%lld", *((uint64 *)(dat->m_sValue)) ); + SetWString( keyName, wbuf ); + } + break; + + case TYPE_WSTRING: + break; + case TYPE_STRING: + { + int bufSize = Q_strlen(dat->m_sValue) + 1; + wchar_t *pWBuf = new wchar_t[ bufSize ]; + int result = Q_UTF8ToUnicode(dat->m_sValue, pWBuf, bufSize * sizeof( wchar_t ) ); + if ( result >= 0 ) // may be a zero length string + { + SetWString( keyName, pWBuf); + } + else + { + delete [] pWBuf; + return defaultValue; + } + delete [] pWBuf; + break; + } + default: + return defaultValue; + }; + + return (const wchar_t* )dat->m_wsValue; + } + return defaultValue; +} + +//----------------------------------------------------------------------------- +// Purpose: Get a bool interpretation of the key. +//----------------------------------------------------------------------------- +bool KeyValues::GetBool( const char *keyName, bool defaultValue, bool* optGotDefault ) +{ + if ( FindKey( keyName ) ) + { + if ( optGotDefault ) + (*optGotDefault) = false; + return 0 != GetInt( keyName, 0 ); + } + + if ( optGotDefault ) + (*optGotDefault) = true; + + return defaultValue; +} + +//----------------------------------------------------------------------------- +// Purpose: Gets a color +//----------------------------------------------------------------------------- +Color KeyValues::GetColor( const char *keyName ) +{ + Color color(0, 0, 0, 0); + KeyValues *dat = FindKey( keyName, false ); + if ( dat ) + { + if ( dat->m_iDataType == TYPE_COLOR ) + { + color[0] = dat->m_Color[0]; + color[1] = dat->m_Color[1]; + color[2] = dat->m_Color[2]; + color[3] = dat->m_Color[3]; + } + else if ( dat->m_iDataType == TYPE_FLOAT ) + { + color[0] = dat->m_flValue; + } + else if ( dat->m_iDataType == TYPE_INT ) + { + color[0] = dat->m_iValue; + } + else if ( dat->m_iDataType == TYPE_STRING ) + { + // parse the colors out of the string + float a = 0.0f, b = 0.0f, c = 0.0f, d = 0.0f; + sscanf(dat->m_sValue, "%f %f %f %f", &a, &b, &c, &d); + color[0] = (unsigned char)a; + color[1] = (unsigned char)b; + color[2] = (unsigned char)c; + color[3] = (unsigned char)d; + } + } + return color; +} + +//----------------------------------------------------------------------------- +// Purpose: Sets a color +//----------------------------------------------------------------------------- +void KeyValues::SetColor( const char *keyName, Color value) +{ + KeyValues *dat = FindKey( keyName, true ); + + if ( dat ) + { + dat->m_iDataType = TYPE_COLOR; + dat->m_Color[0] = value[0]; + dat->m_Color[1] = value[1]; + dat->m_Color[2] = value[2]; + dat->m_Color[3] = value[3]; + } +} + +void KeyValues::SetStringValue( char const *strValue ) +{ + // delete the old value + delete [] m_sValue; + // make sure we're not storing the WSTRING - as we're converting over to STRING + delete [] m_wsValue; + m_wsValue = NULL; + + if (!strValue) + { + // ensure a valid value + strValue = ""; + } + + // allocate memory for the new value and copy it in + int len = Q_strlen( strValue ); + m_sValue = new char[len + 1]; + Q_memcpy( m_sValue, strValue, len+1 ); + + m_iDataType = TYPE_STRING; +} + +//----------------------------------------------------------------------------- +// Purpose: Set the string value of a keyName. +//----------------------------------------------------------------------------- +void KeyValues::SetString( const char *keyName, const char *value ) +{ + KeyValues *dat = FindKey( keyName, true ); + + if ( dat ) + { + if ( dat->m_iDataType == TYPE_STRING && dat->m_sValue == value ) + { + return; + } + + // 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; + } +} + +//----------------------------------------------------------------------------- +// Purpose: Set the string value of a keyName. +//----------------------------------------------------------------------------- +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 = Q_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; + } +} + +//----------------------------------------------------------------------------- +// 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 integer value of a keyName. +//----------------------------------------------------------------------------- +void KeyValues::SetUint64( const char *keyName, uint64 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; + + dat->m_sValue = new char[sizeof(uint64)]; + *((uint64 *)dat->m_sValue) = value; + dat->m_iDataType = TYPE_UINT64; + } +} + +//----------------------------------------------------------------------------- +// Purpose: Set the float value of a keyName. +//----------------------------------------------------------------------------- +void KeyValues::SetFloat( const char *keyName, float value ) +{ + KeyValues *dat = FindKey( keyName, true ); + + if ( dat ) + { + dat->m_flValue = value; + dat->m_iDataType = TYPE_FLOAT; + } +} + +void KeyValues::SetName( const char * setName ) +{ + m_iKeyName = s_pfGetSymbolForString( setName, true ); +} + +//----------------------------------------------------------------------------- +// Purpose: Set the pointer value of a keyName. +//----------------------------------------------------------------------------- +void KeyValues::SetPtr( const char *keyName, void *value ) +{ + KeyValues *dat = FindKey( keyName, true ); + + if ( dat ) + { + dat->m_pValue = value; + dat->m_iDataType = TYPE_PTR; + } +} + +//----------------------------------------------------------------------------- +// Purpose: Copies the tree from the other KeyValues into this one, recursively +// beginning with the root specified by rootSrc. +//----------------------------------------------------------------------------- +void KeyValues::CopyKeyValuesFromRecursive( const KeyValues& rootSrc ) +{ + // This code used to be recursive, which was more elegant. Unfortunately, it also blew the stack for large + // KeyValues. So now we have the iterative version which is uglier but doesn't blow the stack. + // This uses breadth-first traversal. + + struct CopyStruct + { + KeyValues* dst; + const KeyValues* src; + }; + + char tmp[256]; + KeyValues* localDst = NULL; + + CUtlQueue nodeQ; + nodeQ.Insert({ this, &rootSrc }); + + while ( nodeQ.Count() > 0 ) + { + CopyStruct cs = nodeQ.RemoveAtHead(); + + // Process all the siblings of the current node. If anyone has a child, add it to the queue. + while (cs.src) + { + Assert( (cs.src != NULL) == (cs.dst != NULL) ); + + // Copy the node contents + cs.dst->CopyKeyValue( *cs.src, sizeof(tmp), tmp ); + + // Add children to the queue to process later. + if (cs.src->m_pSub) { + cs.dst->m_pSub = localDst = new KeyValues( NULL ); + nodeQ.Insert({ localDst, cs.src->m_pSub }); + } + + // Process siblings until we hit the end of the line. + if (cs.src->m_pPeer) { + cs.dst->m_pPeer = new KeyValues( NULL ); + } + else { + cs.dst->m_pPeer = NULL; + } + + // Advance to the next peer. + cs.src = cs.src->m_pPeer; + cs.dst = cs.dst->m_pPeer; + } + } +} + +//----------------------------------------------------------------------------- +// Purpose: Copies a single KeyValue from src to this, using the provided temporary +// buffer if the keytype requires it. Does NOT recurse. +//----------------------------------------------------------------------------- +void KeyValues::CopyKeyValue( const KeyValues& src, size_t tmpBufferSizeB, char* tmpBuffer ) +{ + m_iKeyName = src.GetNameSymbol(); + + if ( src.m_pSub ) + return; + + m_iDataType = src.m_iDataType; + + switch( src.m_iDataType ) + { + case TYPE_NONE: + break; + case TYPE_STRING: + if( src.m_sValue ) + { + int len = Q_strlen(src.m_sValue) + 1; + m_sValue = new char[len]; + Q_strncpy( m_sValue, src.m_sValue, len ); + } + break; + case TYPE_INT: + { + m_iValue = src.m_iValue; + Q_snprintf( tmpBuffer, tmpBufferSizeB, "%d", m_iValue ); + int len = Q_strlen(tmpBuffer) + 1; + m_sValue = new char[len]; + Q_strncpy( m_sValue, tmpBuffer, len ); + } + break; + case TYPE_FLOAT: + { + m_flValue = src.m_flValue; + Q_snprintf( tmpBuffer, tmpBufferSizeB, "%f", m_flValue ); + int len = Q_strlen(tmpBuffer) + 1; + m_sValue = new char[len]; + Q_strncpy( m_sValue, tmpBuffer, len ); + } + break; + case TYPE_PTR: + { + m_pValue = src.m_pValue; + } + break; + case TYPE_UINT64: + { + m_sValue = new char[sizeof(uint64)]; + Q_memcpy( m_sValue, src.m_sValue, sizeof(uint64) ); + } + break; + case TYPE_COLOR: + { + m_Color[0] = src.m_Color[0]; + m_Color[1] = src.m_Color[1]; + m_Color[2] = src.m_Color[2]; + m_Color[3] = src.m_Color[3]; + } + break; + + default: + { + // do nothing . .what the heck is this? + Assert( 0 ); + } + break; + } +} + +KeyValues& KeyValues::operator=( const KeyValues& src ) +{ + RemoveEverything(); + Init(); // reset all values + CopyKeyValuesFromRecursive( src ); + return *this; +} + + +//----------------------------------------------------------------------------- +// Make a new copy of all subkeys, add them all to the passed-in keyvalues +//----------------------------------------------------------------------------- +void KeyValues::CopySubkeys( KeyValues *pParent ) const +{ + // recursively copy subkeys + // Also maintain ordering.... + KeyValues *pPrev = NULL; + for ( KeyValues *sub = m_pSub; sub != NULL; sub = sub->m_pPeer ) + { + // take a copy of the subkey + KeyValues *dat = sub->MakeCopy(); + + // add into subkey list + if (pPrev) + { + pPrev->m_pPeer = dat; + } + else + { + pParent->m_pSub = dat; + } + dat->m_pPeer = NULL; + pPrev = dat; + } +} + + +//----------------------------------------------------------------------------- +// Purpose: Makes a copy of the whole key-value pair set +//----------------------------------------------------------------------------- +KeyValues *KeyValues::MakeCopy( void ) const +{ + KeyValues *newKeyValue = new KeyValues(GetName()); + + newKeyValue->UsesEscapeSequences( m_bHasEscapeSequences != 0 ); + newKeyValue->UsesConditionals( m_bEvaluateConditionals != 0 ); + + // copy data + newKeyValue->m_iDataType = m_iDataType; + switch ( m_iDataType ) + { + case TYPE_STRING: + { + if ( m_sValue ) + { + int len = Q_strlen( m_sValue ); + Assert( !newKeyValue->m_sValue ); + newKeyValue->m_sValue = new char[len + 1]; + Q_memcpy( newKeyValue->m_sValue, m_sValue, len+1 ); + } + } + break; + case TYPE_WSTRING: + { + if ( m_wsValue ) + { + int len = Q_wcslen( m_wsValue ); + newKeyValue->m_wsValue = new wchar_t[len+1]; + Q_memcpy( newKeyValue->m_wsValue, m_wsValue, (len+1)*sizeof(wchar_t)); + } + } + break; + + case TYPE_INT: + newKeyValue->m_iValue = m_iValue; + break; + + case TYPE_FLOAT: + newKeyValue->m_flValue = m_flValue; + break; + + case TYPE_PTR: + newKeyValue->m_pValue = m_pValue; + break; + + case TYPE_COLOR: + newKeyValue->m_Color[0] = m_Color[0]; + newKeyValue->m_Color[1] = m_Color[1]; + newKeyValue->m_Color[2] = m_Color[2]; + newKeyValue->m_Color[3] = m_Color[3]; + break; + + case TYPE_UINT64: + newKeyValue->m_sValue = new char[sizeof(uint64)]; + Q_memcpy( newKeyValue->m_sValue, m_sValue, sizeof(uint64) ); + break; + }; + + // recursively copy subkeys + CopySubkeys( newKeyValue ); + return newKeyValue; +} + +//----------------------------------------------------------------------------- +// Purpose: +//----------------------------------------------------------------------------- +KeyValues *KeyValues::MakeCopy( bool copySiblings ) const +{ + KeyValues* rootDest = MakeCopy(); + if ( !copySiblings ) + return rootDest; + + const KeyValues* curSrc = GetNextKey(); + KeyValues* curDest = rootDest; + while (curSrc) { + curDest->SetNextKey( curSrc->MakeCopy() ); + curDest = curDest->GetNextKey(); + curSrc = curSrc->GetNextKey(); + } + + return rootDest; +} + +//----------------------------------------------------------------------------- +// Purpose: Check if a keyName has no value assigned to it. +//----------------------------------------------------------------------------- +bool KeyValues::IsEmpty(const char *keyName) +{ + KeyValues *dat = FindKey(keyName, false); + if (!dat) + return true; + + if (dat->m_iDataType == TYPE_NONE && dat->m_pSub == NULL) + return true; + + return false; +} + +//----------------------------------------------------------------------------- +// Purpose: Clear out all subkeys, and the current value +//----------------------------------------------------------------------------- +void KeyValues::Clear( void ) +{ + delete m_pSub; + m_pSub = NULL; + m_iDataType = TYPE_NONE; +} + +//----------------------------------------------------------------------------- +// Purpose: Get the data type of the value stored in a keyName +//----------------------------------------------------------------------------- +KeyValues::types_t KeyValues::GetDataType(const char *keyName) +{ + KeyValues *dat = FindKey(keyName, false); + if (dat) + return (types_t)dat->m_iDataType; + + return TYPE_NONE; +} + +//----------------------------------------------------------------------------- +// Purpose: Deletion, ensures object gets deleted from correct heap +//----------------------------------------------------------------------------- +void KeyValues::deleteThis() +{ + delete this; +} + +//----------------------------------------------------------------------------- +// Purpose: +// Input : includedKeys - +//----------------------------------------------------------------------------- +void KeyValues::AppendIncludedKeys( CUtlVector< KeyValues * >& includedKeys ) +{ + // Append any included keys, too... + KeyValues *insertSpot = this; + int includeCount = includedKeys.Count(); + for ( int i = 0; i < includeCount; i++ ) + { + KeyValues *kv = includedKeys[ i ]; + Assert( kv ); + + while ( insertSpot->GetNextKey() ) + { + insertSpot = insertSpot->GetNextKey(); + } + + insertSpot->SetNextKey( kv ); + } +} + +void KeyValues::ParseIncludedKeys( char const *resourceName, const char *filetoinclude, + IBaseFileSystem* pFileSystem, const char *pPathID, CUtlVector< KeyValues * >& includedKeys ) +{ + Assert( resourceName ); + Assert( filetoinclude ); + Assert( pFileSystem ); + + // Load it... + if ( !pFileSystem ) + { + return; + } + + // Get relative subdirectory + char fullpath[ 512 ]; + Q_strncpy( fullpath, resourceName, sizeof( fullpath ) ); + + // Strip off characters back to start or first / + int len = Q_strlen( fullpath ); + for (;;) + { + if ( len <= 0 ) + { + break; + } + + if ( fullpath[ len - 1 ] == '\\' || + fullpath[ len - 1 ] == '/' ) + { + break; + } + + // zero it + fullpath[ len - 1 ] = 0; + --len; + } + + // Append included file + Q_strncat( fullpath, filetoinclude, sizeof( fullpath ), COPY_ALL_CHARACTERS ); + + KeyValues *newKV = new KeyValues( fullpath ); + + // CUtlSymbol save = s_CurrentFileSymbol; // did that had any use ??? + + newKV->UsesEscapeSequences( m_bHasEscapeSequences != 0 ); // use same format as parent + newKV->UsesConditionals( m_bEvaluateConditionals != 0 ); + + if ( newKV->LoadFromFile( pFileSystem, fullpath, pPathID ) ) + { + includedKeys.AddToTail( newKV ); + } + else + { + DevMsg( "KeyValues::ParseIncludedKeys: Couldn't load included keyvalue file %s\n", fullpath ); + newKV->deleteThis(); + } + + // s_CurrentFileSymbol = save; +} + +//----------------------------------------------------------------------------- +// Purpose: +// Input : baseKeys - +//----------------------------------------------------------------------------- +void KeyValues::MergeBaseKeys( CUtlVector< KeyValues * >& baseKeys ) +{ + int includeCount = baseKeys.Count(); + int i; + for ( i = 0; i < includeCount; i++ ) + { + KeyValues *kv = baseKeys[ i ]; + Assert( kv ); + + RecursiveMergeKeyValues( kv ); + } +} + +//----------------------------------------------------------------------------- +// Purpose: +// Input : baseKV - keyvalues we're basing ourselves on +//----------------------------------------------------------------------------- +void KeyValues::RecursiveMergeKeyValues( KeyValues *baseKV ) +{ + // Merge ourselves + // we always want to keep our value, so nothing to do here + + // Now merge our children + for ( KeyValues *baseChild = baseKV->m_pSub; baseChild != NULL; baseChild = baseChild->m_pPeer ) + { + // for each child in base, see if we have a matching kv + + bool bFoundMatch = false; + + // If we have a child by the same name, merge those keys + for ( KeyValues *newChild = m_pSub; newChild != NULL; newChild = newChild->m_pPeer ) + { + if ( !Q_strcmp( baseChild->GetName(), newChild->GetName() ) ) + { + newChild->RecursiveMergeKeyValues( baseChild ); + bFoundMatch = true; + break; + } + } + + // If not merged, append this key + if ( !bFoundMatch ) + { + KeyValues *dat = baseChild->MakeCopy(); + Assert( dat ); + AddSubKey( dat ); + } + } +} + +//----------------------------------------------------------------------------- +// Returns whether a keyvalues conditional evaluates to true or false +// Needs more flexibility with conditionals, checking convars would be nice. +//----------------------------------------------------------------------------- +bool EvaluateConditional( const char *str ) +{ + if ( !str ) + return false; + + if ( *str == '[' ) + str++; + + bool bNot = false; // should we negate this command? + if ( *str == '!' ) + bNot = true; + + if ( Q_stristr( str, "$X360" ) ) + return IsX360() ^ bNot; + + if ( Q_stristr( str, "$WIN32" ) ) + return IsPC() ^ bNot; // hack hack - for now WIN32 really means IsPC + + if ( Q_stristr( str, "$WINDOWS" ) ) + return IsWindows() ^ bNot; + + if ( Q_stristr( str, "$OSX" ) ) + return IsOSX() ^ bNot; + + if ( Q_stristr( str, "$LINUX" ) ) + return IsLinux() ^ bNot; + + if ( Q_stristr( str, "$POSIX" ) ) + return IsPosix() ^ bNot; + + return false; +} + + +//----------------------------------------------------------------------------- +// Read from a buffer... +//----------------------------------------------------------------------------- +bool KeyValues::LoadFromBuffer( char const *resourceName, CUtlBuffer &buf, IBaseFileSystem* pFileSystem, const char *pPathID ) +{ + KeyValues *pPreviousKey = NULL; + KeyValues *pCurrentKey = this; + CUtlVector< KeyValues * > includedKeys; + CUtlVector< KeyValues * > baseKeys; + bool wasQuoted; + bool wasConditional; + g_KeyValuesErrorStack.SetFilename( resourceName ); + do + { + bool bAccepted = true; + + // the first thing must be a key + const char *s = ReadToken( buf, wasQuoted, wasConditional ); + if ( !buf.IsValid() || !s || *s == 0 ) + break; + + if ( !Q_stricmp( s, "#include" ) ) // special include macro (not a key name) + { + s = ReadToken( buf, wasQuoted, wasConditional ); + // Name of subfile to load is now in s + + if ( !s || *s == 0 ) + { + g_KeyValuesErrorStack.ReportError("#include is NULL " ); + } + else + { + ParseIncludedKeys( resourceName, s, pFileSystem, pPathID, includedKeys ); + } + + continue; + } + else if ( !Q_stricmp( s, "#base" ) ) + { + s = ReadToken( buf, wasQuoted, wasConditional ); + // Name of subfile to load is now in s + + if ( !s || *s == 0 ) + { + g_KeyValuesErrorStack.ReportError("#base is NULL " ); + } + else + { + ParseIncludedKeys( resourceName, s, pFileSystem, pPathID, baseKeys ); + } + + continue; + } + + if ( !pCurrentKey ) + { + pCurrentKey = new KeyValues( s ); + Assert( pCurrentKey ); + + pCurrentKey->UsesEscapeSequences( m_bHasEscapeSequences != 0 ); // same format has parent use + pCurrentKey->UsesConditionals( m_bEvaluateConditionals != 0 ); + + if ( pPreviousKey ) + { + pPreviousKey->SetNextKey( pCurrentKey ); + } + } + else + { + pCurrentKey->SetName( s ); + } + + // get the '{' + s = ReadToken( buf, wasQuoted, wasConditional ); + + if ( wasConditional ) + { + bAccepted = !m_bEvaluateConditionals || EvaluateConditional( s ); + + // Now get the '{' + s = ReadToken( buf, wasQuoted, wasConditional ); + } + + if ( s && *s == '{' && !wasQuoted ) + { + // header is valid so load the file + pCurrentKey->RecursiveLoadFromBuffer( resourceName, buf ); + } + else + { + g_KeyValuesErrorStack.ReportError("LoadFromBuffer: missing {" ); + } + + if ( !bAccepted ) + { + if ( pPreviousKey ) + { + pPreviousKey->SetNextKey( NULL ); + } + pCurrentKey->Clear(); + } + else + { + pPreviousKey = pCurrentKey; + pCurrentKey = NULL; + } + } while ( buf.IsValid() ); + + AppendIncludedKeys( includedKeys ); + { + // delete included keys! + int i; + for ( i = includedKeys.Count() - 1; i > 0; i-- ) + { + KeyValues *kv = includedKeys[ i ]; + kv->deleteThis(); + } + } + + MergeBaseKeys( baseKeys ); + { + // delete base keys! + int i; + for ( i = baseKeys.Count() - 1; i >= 0; i-- ) + { + KeyValues *kv = baseKeys[ i ]; + kv->deleteThis(); + } + } + + g_KeyValuesErrorStack.SetFilename( "" ); + + return true; +} + + +//----------------------------------------------------------------------------- +// Read from a buffer... +//----------------------------------------------------------------------------- +bool KeyValues::LoadFromBuffer( char const *resourceName, const char *pBuffer, IBaseFileSystem* pFileSystem, const char *pPathID ) +{ + if ( !pBuffer ) + return true; + + COM_TimestampedLog("KeyValues::LoadFromBuffer(%s%s%s): Begin", pPathID ? pPathID : "", pPathID && resourceName ? "/" : "", resourceName ? resourceName : ""); + + int nLen = Q_strlen( pBuffer ); + CUtlBuffer buf( pBuffer, nLen, CUtlBuffer::READ_ONLY | CUtlBuffer::TEXT_BUFFER ); + + // Translate Unicode files into UTF-8 before proceeding + if ( nLen > 2 && (uint8)pBuffer[0] == 0xFF && (uint8)pBuffer[1] == 0xFE ) + { + int nUTF8Len = V_UnicodeToUTF8( (wchar_t*)(pBuffer+2), NULL, 0 ); + char *pUTF8Buf = new char[nUTF8Len]; + V_UnicodeToUTF8( (wchar_t*)(pBuffer+2), pUTF8Buf, nUTF8Len ); + buf.AssumeMemory( pUTF8Buf, nUTF8Len, nUTF8Len, CUtlBuffer::READ_ONLY | CUtlBuffer::TEXT_BUFFER ); + } + + bool retVal = LoadFromBuffer( resourceName, buf, pFileSystem, pPathID ); + + COM_TimestampedLog("KeyValues::LoadFromBuffer(%s%s%s): End", pPathID ? pPathID : "", pPathID && resourceName ? "/" : "", resourceName ? resourceName : ""); + + return retVal; +} + +//----------------------------------------------------------------------------- +// Purpose: +//----------------------------------------------------------------------------- +void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &buf ) +{ + CKeyErrorContext errorReport(this); + bool wasQuoted; + bool wasConditional; + if ( errorReport.GetStackLevel() > 100 ) + { + g_KeyValuesErrorStack.ReportError( "RecursiveLoadFromBuffer: recursion overflow" ); + return; + } + + // keep this out of the stack until a key is parsed + CKeyErrorContext errorKey( INVALID_KEY_SYMBOL ); + + // Locate the last child. (Almost always, we will not have any children.) + // We maintain the pointer to the last child here, so we don't have to re-locate + // it each time we append the next subkey, which causes O(N^2) time + KeyValues *pLastChild = FindLastSubKey();; + + // Keep parsing until we hit the closing brace which terminates this block, or a parse error + while ( 1 ) + { + bool bAccepted = true; + + // get the key name + const char * name = ReadToken( buf, wasQuoted, wasConditional ); + + if ( !name ) // EOF stop reading + { + g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got EOF instead of keyname" ); + break; + } + + if ( !*name ) // empty token, maybe "" or EOF + { + g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got empty keyname" ); + break; + } + + if ( *name == '}' && !wasQuoted ) // top level closed, stop reading + break; + + // Always create the key; note that this could potentially + // cause some duplication, but that's what we want sometimes + KeyValues *dat = CreateKeyUsingKnownLastChild( name, pLastChild ); + + errorKey.Reset( dat->GetNameSymbol() ); + + // get the value + const char * value = ReadToken( buf, wasQuoted, wasConditional ); + + if ( wasConditional && value ) + { + bAccepted = !m_bEvaluateConditionals || EvaluateConditional( value ); + + // get the real value + value = ReadToken( buf, wasQuoted, wasConditional ); + } + + if ( !value ) + { + g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got NULL key" ); + break; + } + + if ( *value == '}' && !wasQuoted ) + { + g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got } in key" ); + break; + } + + if ( *value == '{' && !wasQuoted ) + { + // this isn't a key, it's a section + errorKey.Reset( INVALID_KEY_SYMBOL ); + // sub value list + dat->RecursiveLoadFromBuffer( resourceName, buf ); + } + else + { + if ( wasConditional ) + { + g_KeyValuesErrorStack.ReportError("RecursiveLoadFromBuffer: got conditional between key and value" ); + break; + } + + if (dat->m_sValue) + { + delete[] dat->m_sValue; + dat->m_sValue = NULL; + } + + int len = Q_strlen( value ); + + // Here, let's determine if we got a float or an int.... + char* pIEnd; // pos where int scan ended + char* pFEnd; // pos where float scan ended + const char* pSEnd = value + len ; // pos where token ends + + int ival = strtol( value, &pIEnd, 10 ); + float fval = (float)strtod( value, &pFEnd ); + bool bOverflow = ( ival == LONG_MAX || ival == LONG_MIN ) && errno == ERANGE; +#ifdef POSIX + // strtod supports hex representation in strings under posix but we DON'T + // want that support in keyvalues, so undo it here if needed + if ( len > 1 && tolower(value[1]) == 'x' ) + { + fval = 0.0f; + pFEnd = (char *)value; + } +#endif + + if ( *value == 0 ) + { + dat->m_iDataType = TYPE_STRING; + } + else if ( ( 18 == len ) && ( value[0] == '0' ) && ( value[1] == 'x' ) ) + { + // an 18-byte value prefixed with "0x" (followed by 16 hex digits) is an int64 value + int64 retVal = 0; + for( int i=2; i < 2 + 16; i++ ) + { + char digit = value[i]; + if ( digit >= 'a' ) + digit -= 'a' - ( '9' + 1 ); + else + if ( digit >= 'A' ) + digit -= 'A' - ( '9' + 1 ); + retVal = ( retVal * 16 ) + ( digit - '0' ); + } + dat->m_sValue = new char[sizeof(uint64)]; + *((uint64 *)dat->m_sValue) = retVal; + dat->m_iDataType = TYPE_UINT64; + } + else if ( (pFEnd > pIEnd) && (pFEnd == pSEnd) ) + { + dat->m_flValue = fval; + dat->m_iDataType = TYPE_FLOAT; + } + else if (pIEnd == pSEnd && !bOverflow) + { + dat->m_iValue = ival; + dat->m_iDataType = TYPE_INT; + } + else + { + dat->m_iDataType = TYPE_STRING; + } + + if (dat->m_iDataType == TYPE_STRING) + { + // copy in the string information + dat->m_sValue = new char[len+1]; + Q_memcpy( dat->m_sValue, value, len+1 ); + } + + // Look ahead one token for a conditional tag + int prevPos = buf.TellGet(); + const char *peek = ReadToken( buf, wasQuoted, wasConditional ); + if ( wasConditional ) + { + bAccepted = !m_bEvaluateConditionals || EvaluateConditional( peek ); + } + else + { + buf.SeekGet( CUtlBuffer::SEEK_HEAD, prevPos ); + } + } + + Assert( dat->m_pPeer == NULL ); + if ( bAccepted ) + { + Assert( pLastChild == NULL || pLastChild->m_pPeer == dat ); + pLastChild = dat; + } + else + { + //this->RemoveSubKey( dat ); + if ( pLastChild == NULL ) + { + Assert( m_pSub == dat ); + m_pSub = NULL; + } + else + { + Assert( pLastChild->m_pPeer == dat ); + pLastChild->m_pPeer = NULL; + } + + dat->deleteThis(); + dat = NULL; + } + } +} + + + +// writes KeyValue as binary data to buffer +bool KeyValues::WriteAsBinary( CUtlBuffer &buffer ) +{ + if ( buffer.IsText() ) // must be a binary buffer + return false; + + if ( !buffer.IsValid() ) // must be valid, no overflows etc + return false; + + // Write subkeys: + + // loop through all our peers + for ( KeyValues *dat = this; dat != NULL; dat = dat->m_pPeer ) + { + // write type + buffer.PutUnsignedChar( dat->m_iDataType ); + + // write name + buffer.PutString( dat->GetName() ); + + // write type + switch (dat->m_iDataType) + { + case TYPE_NONE: + { + dat->m_pSub->WriteAsBinary( buffer ); + break; + } + case TYPE_STRING: + { + if (dat->m_sValue && *(dat->m_sValue)) + { + buffer.PutString( dat->m_sValue ); + } + else + { + buffer.PutString( "" ); + } + break; + } + case TYPE_WSTRING: + { + Assert( !"TYPE_WSTRING" ); + break; + } + + case TYPE_INT: + { + buffer.PutInt( dat->m_iValue ); + break; + } + + case TYPE_UINT64: + { + buffer.PutDouble( *((double *)dat->m_sValue) ); + break; + } + + case TYPE_FLOAT: + { + buffer.PutFloat( dat->m_flValue ); + break; + } + case TYPE_COLOR: + { + buffer.PutUnsignedChar( dat->m_Color[0] ); + buffer.PutUnsignedChar( dat->m_Color[1] ); + buffer.PutUnsignedChar( dat->m_Color[2] ); + buffer.PutUnsignedChar( dat->m_Color[3] ); + break; + } + case TYPE_PTR: + { + buffer.PutUnsignedInt( (int)dat->m_pValue ); + } + + default: + break; + } + } + + // write tail, marks end of peers + buffer.PutUnsignedChar( TYPE_NUMTYPES ); + + return buffer.IsValid(); +} + +// read KeyValues from binary buffer, returns true if parsing was successful +bool KeyValues::ReadAsBinary( CUtlBuffer &buffer, int nStackDepth ) +{ + if ( buffer.IsText() ) // must be a binary buffer + return false; + + if ( !buffer.IsValid() ) // must be valid, no overflows etc + return false; + + RemoveEverything(); // remove current content + Init(); // reset + + if ( nStackDepth > 100 ) + { + AssertMsgOnce( false, "KeyValues::ReadAsBinary() stack depth > 100\n" ); + return false; + } + + KeyValues *dat = this; + types_t type = (types_t)buffer.GetUnsignedChar(); + + // loop through all our peers + while ( true ) + { + if ( type == TYPE_NUMTYPES ) + break; // no more peers + + dat->m_iDataType = type; + + { + char token[KEYVALUES_TOKEN_SIZE]; + buffer.GetString( token ); + token[KEYVALUES_TOKEN_SIZE-1] = 0; + dat->SetName( token ); + } + + switch ( type ) + { + case TYPE_NONE: + { + dat->m_pSub = new KeyValues(""); + dat->m_pSub->ReadAsBinary( buffer, nStackDepth + 1 ); + break; + } + case TYPE_STRING: + { + char token[KEYVALUES_TOKEN_SIZE]; + buffer.GetString( token ); + token[KEYVALUES_TOKEN_SIZE-1] = 0; + + int len = Q_strlen( token ); + dat->m_sValue = new char[len + 1]; + Q_memcpy( dat->m_sValue, token, len+1 ); + + break; + } + case TYPE_WSTRING: + { + Assert( !"TYPE_WSTRING" ); + break; + } + + case TYPE_INT: + { + dat->m_iValue = buffer.GetInt(); + break; + } + + case TYPE_UINT64: + { + dat->m_sValue = new char[sizeof(uint64)]; + *((uint64 *)dat->m_sValue) = buffer.GetInt64(); + break; + } + + case TYPE_FLOAT: + { + dat->m_flValue = buffer.GetFloat(); + break; + } + case TYPE_COLOR: + { + dat->m_Color[0] = buffer.GetUnsignedChar(); + dat->m_Color[1] = buffer.GetUnsignedChar(); + dat->m_Color[2] = buffer.GetUnsignedChar(); + dat->m_Color[3] = buffer.GetUnsignedChar(); + break; + } + case TYPE_PTR: + { + dat->m_pValue = (void*)buffer.GetUnsignedInt(); + } + + default: + break; + } + + if ( !buffer.IsValid() ) // error occured + return false; + + type = (types_t)buffer.GetUnsignedChar(); + + if ( type == TYPE_NUMTYPES ) + break; + + // new peer follows + dat->m_pPeer = new KeyValues(""); + dat = dat->m_pPeer; + } + + return buffer.IsValid(); +} + +#include "tier0/memdbgoff.h" + +//----------------------------------------------------------------------------- +// Purpose: memory allocator +//----------------------------------------------------------------------------- +void *KeyValues::operator new( size_t iAllocSize ) +{ + MEM_ALLOC_CREDIT(); + return KeyValuesSystem()->AllocKeyValuesMemory( (int)iAllocSize ); +} + +void *KeyValues::operator new( size_t iAllocSize, int nBlockUse, const char *pFileName, int nLine ) +{ + MemAlloc_PushAllocDbgInfo( pFileName, nLine ); + void *p = KeyValuesSystem()->AllocKeyValuesMemory( (int)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); +} + +void KeyValues::UnpackIntoStructure( KeyValuesUnpackStructure const *pUnpackTable, void *pDest, size_t DestSizeInBytes ) +{ +#ifdef DBGFLAG_ASSERT + void *pDestEnd = ( char * )pDest + DestSizeInBytes + 1; +#endif + + uint8 *dest=(uint8 *) pDest; + while( pUnpackTable->m_pKeyName ) + { + uint8 *dest_field=dest+pUnpackTable->m_nFieldOffset; + KeyValues *find_it=FindKey( pUnpackTable->m_pKeyName ); + + switch( pUnpackTable->m_eDataType ) + { + case UNPACK_TYPE_FLOAT: + { + Assert( dest_field + sizeof( float ) < pDestEnd ); + + float default_value=(pUnpackTable->m_pKeyDefault)?atof(pUnpackTable->m_pKeyDefault):0.0; + *( ( float *) dest_field)=GetFloat( pUnpackTable->m_pKeyName, default_value ); + break; + } + break; + + case UNPACK_TYPE_VECTOR: + { + Assert( dest_field + sizeof( Vector ) < pDestEnd ); + + Vector *dest_v=(Vector *) dest_field; + char const *src_string= + GetString( pUnpackTable->m_pKeyName, pUnpackTable->m_pKeyDefault ); + if ( (!src_string) || + ( sscanf(src_string,"%f %f %f", + &(dest_v->x), &(dest_v->y), &(dest_v->z)) != 3)) + dest_v->Init( 0, 0, 0 ); + } + break; + + case UNPACK_TYPE_FOUR_FLOATS: + { + Assert( dest_field + sizeof( float ) * 4 < pDestEnd ); + + float *dest_f=(float *) dest_field; + char const *src_string= + GetString( pUnpackTable->m_pKeyName, pUnpackTable->m_pKeyDefault ); + if ( (!src_string) || + ( sscanf(src_string,"%f %f %f %f", + dest_f,dest_f+1,dest_f+2,dest_f+3)) != 4) + memset( dest_f, 0, 4*sizeof(float) ); + } + break; + + case UNPACK_TYPE_TWO_FLOATS: + { + Assert( dest_field + sizeof( float ) * 2 < pDestEnd ); + + float *dest_f=(float *) dest_field; + char const *src_string= + GetString( pUnpackTable->m_pKeyName, pUnpackTable->m_pKeyDefault ); + if ( (!src_string) || + ( sscanf(src_string,"%f %f", + dest_f,dest_f+1)) != 2) + memset( dest_f, 0, 2*sizeof(float) ); + } + break; + + case UNPACK_TYPE_STRING: + { + Assert( dest_field + pUnpackTable->m_nFieldSize < pDestEnd ); + + char *dest_s=(char *) dest_field; + strncpy( dest_s, GetString( pUnpackTable->m_pKeyName, + pUnpackTable->m_pKeyDefault ), + pUnpackTable->m_nFieldSize ); + + } + break; + + case UNPACK_TYPE_INT: + { + Assert( dest_field + sizeof( int ) < pDestEnd ); + + int *dest_i=(int *) dest_field; + int default_int=0; + if ( pUnpackTable->m_pKeyDefault) + default_int = atoi( pUnpackTable->m_pKeyDefault ); + *(dest_i)=GetInt( pUnpackTable->m_pKeyName, default_int ); + } + break; + + case UNPACK_TYPE_VECTOR_COLOR: + { + Assert( dest_field + sizeof( Vector ) < pDestEnd ); + + Vector *dest_v=(Vector *) dest_field; + if (find_it) + { + Color c=GetColor( pUnpackTable->m_pKeyName ); + dest_v->x = c.r(); + dest_v->y = c.g(); + dest_v->z = c.b(); + } + else + { + if ( pUnpackTable->m_pKeyDefault ) + sscanf(pUnpackTable->m_pKeyDefault,"%f %f %f", + &(dest_v->x), &(dest_v->y), &(dest_v->z)); + else + dest_v->Init( 0, 0, 0 ); + } + *(dest_v) *= (1.0/255); + } + } + pUnpackTable++; + } +} + +//----------------------------------------------------------------------------- +// Helper function for processing a keyvalue tree for console resolution support. +// Alters key/values for easier console video resolution support. +// If running SD (640x480), the presence of "???_lodef" creates or slams "???". +// If running HD (1280x720), the presence of "???_hidef" creates or slams "???". +//----------------------------------------------------------------------------- +bool KeyValues::ProcessResolutionKeys( const char *pResString ) +{ + if ( !pResString ) + { + // not for pc, console only + return false; + } + + KeyValues *pSubKey = GetFirstSubKey(); + if ( !pSubKey ) + { + // not a block + return false; + } + + for ( ; pSubKey != NULL; pSubKey = pSubKey->GetNextKey() ) + { + // recursively descend each sub block + pSubKey->ProcessResolutionKeys( pResString ); + + // check to see if our substring is present + if ( Q_stristr( pSubKey->GetName(), pResString ) != NULL ) + { + char normalKeyName[128]; + V_strncpy( normalKeyName, pSubKey->GetName(), sizeof( normalKeyName ) ); + + // substring must match exactly, otherwise keys like "_lodef" and "_lodef_wide" would clash. + char *pString = Q_stristr( normalKeyName, pResString ); + if ( pString && !Q_stricmp( pString, pResString ) ) + { + *pString = '\0'; + + // find and delete the original key (if any) + KeyValues *pKey = FindKey( normalKeyName ); + if ( pKey ) + { + // remove the key + RemoveSubKey( pKey ); + } + + // rename the marked key + pSubKey->SetName( normalKeyName ); + } + } + } + + return true; +} + + + +// +// KeyValues dumping implementation +// +bool KeyValues::Dump( IKeyValuesDumpContext *pDump, int nIndentLevel /* = 0 */ ) +{ + if ( !pDump->KvBeginKey( this, nIndentLevel ) ) + return false; + + // Dump values + for ( KeyValues *val = this ? GetFirstValue() : NULL; val; val = val->GetNextValue() ) + { + if ( !pDump->KvWriteValue( val, nIndentLevel + 1 ) ) + return false; + } + + // Dump subkeys + for ( KeyValues *sub = this ? GetFirstTrueSubKey() : NULL; sub; sub = sub->GetNextTrueSubKey() ) + { + if ( !sub->Dump( pDump, nIndentLevel + 1 ) ) + return false; + } + + return pDump->KvEndKey( this, nIndentLevel ); +} + +bool IKeyValuesDumpContextAsText::KvBeginKey( KeyValues *pKey, int nIndentLevel ) +{ + if ( pKey ) + { + return + KvWriteIndent( nIndentLevel ) && + KvWriteText( pKey->GetName() ) && + KvWriteText( " {\n" ); + } + else + { + return + KvWriteIndent( nIndentLevel ) && + KvWriteText( "<< NULL >>\n" ); + } +} + +bool IKeyValuesDumpContextAsText::KvWriteValue( KeyValues *val, int nIndentLevel ) +{ + if ( !val ) + { + return + KvWriteIndent( nIndentLevel ) && + KvWriteText( "<< NULL >>\n" ); + } + + if ( !KvWriteIndent( nIndentLevel ) ) + return false; + + if ( !KvWriteText( val->GetName() ) ) + return false; + + if ( !KvWriteText( " " ) ) + return false; + + switch ( val->GetDataType() ) + { + case KeyValues::TYPE_STRING: + { + if ( !KvWriteText( val->GetString() ) ) + return false; + } + break; + + case KeyValues::TYPE_INT: + { + int n = val->GetInt(); + char *chBuffer = ( char * ) stackalloc( 128 ); + V_snprintf( chBuffer, 128, "int( %d = 0x%X )", n, n ); + if ( !KvWriteText( chBuffer ) ) + return false; + } + break; + + case KeyValues::TYPE_FLOAT: + { + float fl = val->GetFloat(); + char *chBuffer = ( char * ) stackalloc( 128 ); + V_snprintf( chBuffer, 128, "float( %f )", fl ); + if ( !KvWriteText( chBuffer ) ) + return false; + } + break; + + case KeyValues::TYPE_PTR: + { + void *ptr = val->GetPtr(); + char *chBuffer = ( char * ) stackalloc( 128 ); + V_snprintf( chBuffer, 128, "ptr( 0x%p )", ptr ); + if ( !KvWriteText( chBuffer ) ) + return false; + } + break; + + case KeyValues::TYPE_WSTRING: + { + wchar_t const *wsz = val->GetWString(); + int nLen = V_wcslen( wsz ); + int numBytes = nLen*2 + 64; + char *chBuffer = ( char * ) stackalloc( numBytes ); + V_snprintf( chBuffer, numBytes, "%ls [wstring, len = %d]", wsz, nLen ); + if ( !KvWriteText( chBuffer ) ) + return false; + } + break; + + case KeyValues::TYPE_UINT64: + { + uint64 n = val->GetUint64(); + char *chBuffer = ( char * ) stackalloc( 128 ); + V_snprintf( chBuffer, 128, "u64( %lld = 0x%llX )", n, n ); + if ( !KvWriteText( chBuffer ) ) + return false; + } + break; + + default: + break; + { + int n = val->GetDataType(); + char *chBuffer = ( char * ) stackalloc( 128 ); + V_snprintf( chBuffer, 128, "??kvtype[%d]", n ); + if ( !KvWriteText( chBuffer ) ) + return false; + } + break; + } + + return KvWriteText( "\n" ); +} + +bool IKeyValuesDumpContextAsText::KvEndKey( KeyValues *pKey, int nIndentLevel ) +{ + if ( pKey ) + { + return + KvWriteIndent( nIndentLevel ) && + KvWriteText( "}\n" ); + } + else + { + return true; + } +} + +bool IKeyValuesDumpContextAsText::KvWriteIndent( int nIndentLevel ) +{ + int numIndentBytes = ( nIndentLevel * 2 + 1 ); + char *pchIndent = ( char * ) stackalloc( numIndentBytes ); + memset( pchIndent, ' ', numIndentBytes - 1 ); + pchIndent[ numIndentBytes - 1 ] = 0; + return KvWriteText( pchIndent ); +} + + +bool CKeyValuesDumpContextAsDevMsg::KvBeginKey( KeyValues *pKey, int nIndentLevel ) +{ + static ConVarRef r_developer( "developer" ); + if ( r_developer.IsValid() && r_developer.GetInt() < m_nDeveloperLevel ) + // If "developer" is not the correct level, then avoid evaluating KeyValues tree early + return false; + else + return IKeyValuesDumpContextAsText::KvBeginKey( pKey, nIndentLevel ); +} + +bool CKeyValuesDumpContextAsDevMsg::KvWriteText( char const *szText ) +{ + if ( m_nDeveloperLevel > 0 ) + { + DevMsg( m_nDeveloperLevel, "%s", szText ); + } + else + { + Msg( "%s", szText ); + } + return true; +} diff --git a/cathook/src/sdk/tier1.cpp b/cathook/src/sdk/tier1.cpp new file mode 100644 index 00000000..d683b871 --- /dev/null +++ b/cathook/src/sdk/tier1.cpp @@ -0,0 +1,63 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: A higher level link library for general use in the game and tools. +// +//===========================================================================// + +#include +#include "tier0/dbg.h" +#include "vstdlib/iprocessutils.h" +#include "icvar.h" + + +//----------------------------------------------------------------------------- +// These tier1 libraries must be set by any users of this library. +// They can be set by calling ConnectTier1Libraries or InitDefaultFileSystem. +// It is hoped that setting this, and using this library will be the common mechanism for +// allowing link libraries to access tier1 library interfaces +//----------------------------------------------------------------------------- +ICvar *cvar = 0; +ICvar *g_pCVar = 0; +IProcessUtils *g_pProcessUtils = 0; +static bool s_bConnected = false; + +// for utlsortvector.h +#ifndef _WIN32 + void *g_pUtlSortVectorQSortContext = NULL; +#endif + + +//----------------------------------------------------------------------------- +// Call this to connect to all tier 1 libraries. +// It's up to the caller to check the globals it cares about to see if ones are missing +//----------------------------------------------------------------------------- +void ConnectTier1Libraries( CreateInterfaceFn *pFactoryList, int nFactoryCount ) +{ + // Don't connect twice.. + if ( s_bConnected ) + return; + + s_bConnected = true; + + for ( int i = 0; i < nFactoryCount; ++i ) + { + if ( !g_pCVar ) + { + cvar = g_pCVar = ( ICvar * )pFactoryList[i]( CVAR_INTERFACE_VERSION, NULL ); + } + if ( !g_pProcessUtils ) + { + g_pProcessUtils = ( IProcessUtils * )pFactoryList[i]( PROCESS_UTILS_INTERFACE_VERSION, NULL ); + } + } +} + +void DisconnectTier1Libraries() +{ + if ( !s_bConnected ) + return; + + g_pCVar = cvar = 0; + g_pProcessUtils = NULL; + s_bConnected = false; +}