summaryrefslogtreecommitdiff
path: root/cheat/internal_rewrite/KeyValues.cpp
blob: 300b6f1bbc9fe90505b91480c705f62b68c1de6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "KeyValues.hpp"
#include "pattern.hpp"
#include "interface.hpp"

void IKeyValuesSystem::FreeKeyValuesMemory( void* memory ) {
	return util::get_vfunc< 2, void >( this, memory );
}

int IKeyValuesSystem::GetSymbolForString( const char* str, bool create ) {
	static auto offset = pattern::first_code_match( GetModuleHandleA( xors( "vstdlib.dll" ) ), xors( "55 8B EC 83 EC 0C 53 8B 5D 08 57" ) );
	using fn = int( __thiscall* )( void*, const char*, bool );

	int symbol = ( ( fn )( offset ) )( this, str, create );
	return symbol;
}

const char* IKeyValuesSystem::GetStringForSymbol( int symbol ) {
	static auto offset = pattern::first_code_match( GetModuleHandleA( xors( "vstdlib.dll" ) ), xors( "55 8B EC 8B 55 08 83 FA FF" ) );
	using fn = const char*( __thiscall* )( void*, int );

	auto result = ( ( fn )( offset ) )( this, symbol );
	printf( "%d: %d\n", symbol, *( int* )( &result ) );

	return result;
}

KeyValues::KeyValues( const char* setName ) {
	static void( __thiscall* keyvalues_constructor )( KeyValues*, const char* ) = 0;

	if( !keyvalues_constructor ) {
		auto address = pattern::first_code_match( g_csgo.m_chl.dll( ),
			xors( "68 ? ? ? ? 8B C8 E8 ? ? ? ? 89 45 FC EB 07 C7 45 ? ? ? ? ? 8B 03 56" ),
			0x8 );

		keyvalues_constructor = decltype( keyvalues_constructor )( address + *( uintptr_t* )( address ) + 4 );
	}

	keyvalues_constructor( this, setName );
}

KeyValues::~KeyValues( ) {
	static bool( __thiscall* keyvalues_remove_everything )( KeyValues* ) = 0;

	if( !keyvalues_remove_everything ) {
		auto address = pattern::first_code_match( g_csgo.m_chl.dll( ),
			xors( "E8 ? ? ? ? 81 23 ? ? ? ?" ),
			0x1 );

		keyvalues_remove_everything = decltype( keyvalues_remove_everything )( address + *( uintptr_t* )( address ) + 4 );
	}

	keyvalues_remove_everything( this );
	g_csgo.m_keyvalues->FreeKeyValuesMemory( this );
	
}


bool KeyValues::LoadFromBuffer( char const *resourceName, const char *pBuffer, void* pFileSystem, const char *pPathID, bool( *unknown )( const char* ) ) {
	static auto load_from_buffer = pattern::first_code_match( g_csgo.m_chl.dll( ), xors( "55 8B EC 83 E4 F8 83 EC 34 53 8B 5D 0C 89 4C 24 04" ) );
	return reinterpret_cast< bool( __thiscall* )( KeyValues*, const char*, const char*, void*, const char*, bool( *)( const char* ) ) >( load_from_buffer )
		( this, resourceName, pBuffer, pFileSystem, pPathID, unknown );
}

const char* KeyValues::get_string( const char* name, const char* value ) {
	static auto offset = pattern::first_code_match( g_csgo.m_chl.dll( ), xors( "55 8B EC 83 E4 C0 81 EC ?? ?? ?? ?? 53 8B 5D 08" ) );
	using fn = const char*( __thiscall* )( void*, const char*, const char* );

	auto result = ( ( fn )( offset ) )( this, name, value );

	return result;

}

KeyValues* KeyValues::FindKey( const char* name ) {
	auto key = g_csgo.m_keyvalues->GetSymbolForString( name, false );
	for( auto dat = m_pSub; !!dat; dat = dat->m_pPeer ) {
		if( dat->m_iKeyName == key ) {
			return dat;
		}
	}

	return nullptr;
}