summaryrefslogtreecommitdiff
path: root/internal_rewrite/simple_settings.hpp
blob: 9dffa55573b93611a334ee40011dfd2245c359dd (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#pragma once
#include <Windows.h>
#include <memory>
#include <vector>
#include <string>
#include <Shlwapi.h>

#include "util.hpp"
#include "fnv.hpp"

#pragma comment( lib, "Shlwapi.lib" )

//nama stuff
extern void simple_save( const char* path, const char* name, const void* src, size_t size, const char* filename );
extern void simple_load( const char* path, const char* name, void* dest, size_t size, const char* filename );

class ISettingNode {
public:
	virtual ~ISettingNode( ) = default;

	virtual void load( const char* path, const char* file ) = 0;
	virtual void save( const char* path, const char* file ) const = 0;
	virtual void reset( ) const = 0;
	virtual void register_( ISettingNode* node_ptr ) = 0;
	virtual bool has_nodes( ) { return false; }
	virtual hash_t get_hash( ) { return 0; }
};

class SettingHolder : public ISettingNode {
public:
	__forceinline SettingHolder( const char* name ) : name_( name ) {}
	__forceinline SettingHolder( SettingHolder* holder_ptr, const char* name ) : name_( name ) { holder_ptr->register_( this ); }

	void register_( ISettingNode* node_ptr ) override {
		setting_nodes_.push_back( node_ptr );
	}

	__declspec( noinline ) void load( const char* path, const char* file ) override
	{
		static char full_path[1024];
		memset( full_path, 0, 1024 );

		strcpy_s( full_path, path );
		strcat_s( full_path, "_" );
		strcat_s( full_path, name_.c_str( ) );

		for ( auto x : setting_nodes_ )
			x->load( full_path, file );
	}

	__declspec( noinline ) void save( const char* path, const char* file ) const override
	{
		static char full_path[1024];
		memset( full_path, 0, 1024 );

		strcpy_s( full_path, path );
		strcat_s( full_path, "_" );
		strcat_s( full_path, name_.c_str( ) );
		for ( auto x : setting_nodes_ )
			x->save( full_path, file );
	}

	__declspec( noinline ) void reset( ) const override {
		for( auto x : setting_nodes_ )
			x->reset( );
	}

	auto& get_nodes( ) {
		return setting_nodes_;
	}

	virtual bool has_nodes( ) {
		return true;
	}

private:
	std::string name_;
	bool has_nodes_;
	std::vector<ISettingNode*> setting_nodes_;
};

class ISetting : public ISettingNode {
	void register_( ISettingNode* node_ptr ) override {}
public:
	virtual void set( float value ) = 0;
	virtual void set( int value ) = 0;
	virtual void set( ulong_t value ) = 0;
	virtual std::string get_string( ) = 0;
	virtual hash_t get_hash( ) = 0;
	virtual std::string get_data_string( ) = 0;
	virtual void load_from_string( const std::string& ) = 0;
};

template < size_t L >
class SettingString : ISetting {
public:
	__forceinline SettingString( SettingHolder* holder_ptr, hash_t hash ) : 
		m_name( name ), m_value( "" ) {
		holder_ptr->register_( this );
	};

	__declspec( noinline ) void load( const char* path, const char* file ) override {
		static char str[ MAX_PATH ];
		
		GetModuleFileNameA( nullptr, str, MAX_PATH - 1 );
		PathRemoveFileSpecA( str );
		strcat_s< MAX_PATH >( str, path );
		strcat_s< MAX_PATH >( str, "\\" );

		GetPrivateProfileStringA( path, std::to_string( m_name ).c_str( ), "", m_value, L, file );
	}

	__declspec( noinline ) void save( const char* path, const char* file ) const override {
		static char str[ MAX_PATH ];

		GetModuleFileNameA( nullptr, str, MAX_PATH - 1 );
		PathRemoveFileSpecA( str );
		strcat_s< MAX_PATH >( str, path );
		strcat_s< MAX_PATH >( str, "\\" );

		WritePrivateProfileStringA( path, std::to_string( m_name ).c_str( ), m_value, file );
	}


	char* get( ) {
		return m_value;
	}

private:
	char m_value[ L ];
	hash_t m_name;
};

template < typename T >
class con_var : public ISetting {
public:
	__declspec( noinline ) con_var( SettingHolder* holder_ptr, hash_t name ) :
		name_( name ), 
		original_{ },
		is_float_( std::is_floating_point_v< T > ),
		is_integral_( std::is_integral_v< T > ) { 
		holder_ptr->register_( this );
	}
	__declspec( noinline ) con_var( SettingHolder* holder_ptr, hash_t name, const T& rhs ) :
		value_( rhs ), 
		name_( name ),
		original_( value_ ),
		is_float_( std::is_floating_point_v< T > ),
		is_integral_( std::is_integral_v< T > ) {
		holder_ptr->register_( this );
	}

	__declspec( noinline ) void load( const char* path, const char* file ) override { 
		static char str[ MAX_PATH ];
		static char full_path[ MAX_PATH ];
		static bool taken = false;

		if( !taken ) {
			GetModuleFileNameA( nullptr, str, MAX_PATH - 1 );
			PathRemoveFileSpecA( str );
			strcat_s< MAX_PATH >( str, "\\csgo\\cfg\\" );

			taken = true;
		}

		memset( full_path, 0, MAX_PATH );
		memcpy( full_path, str, MAX_PATH );
		strcat_s< MAX_PATH >( full_path, file );

		simple_load( path, std::to_string( name_ ).c_str( ), &value_, sizeof( value_ ), full_path ); 
	}

	__declspec( noinline ) void save( const char* path, const char* file ) const override { 
		static char str[ MAX_PATH ];
		static char full_path[ MAX_PATH ];
		static bool taken = false;

		if( !taken ) {
			GetModuleFileNameA( nullptr, str, MAX_PATH - 1 );
			PathRemoveFileSpecA( str );
			strcat_s< MAX_PATH >( str, "\\csgo\\cfg\\" );
			
			taken = true;
		}

		memset( full_path, 0, MAX_PATH );
		memcpy( full_path, str, MAX_PATH );
		strcat_s< MAX_PATH >( full_path, file );

		simple_save( path, std::to_string( name_ ).c_str( ), &value_, sizeof( value_ ), full_path ); 
	}

	__declspec( noinline ) void reset( ) const override {
		// now this is really epic.
		memcpy( ( void* )&value_, ( void* )&original_, sizeof( original_ ) );
	}

	__forceinline operator T&( ) { return value_; }
	__forceinline T* operator &( ) { return &value_; }

	__forceinline T& operator ()( ) { return value_; }

	virtual std::string get_string( ) {
		if constexpr( std::is_arithmetic_v< T > ) {
			return std::to_string( value_ );
		}
		else {
			return util::to_hex_str( *( ulong_t* )( &value_ ) );
		}
	}

	virtual void set( float value ) {
		if constexpr( std::is_arithmetic_v< T > ) {
			value_ = ( T )( value );
		}
	}

	virtual void set( int value ) {
		if constexpr( std::is_arithmetic_v< T > ) {
			value_ = ( T )( value );
		}
		else {
			value_ = *( T* )( &value );
		}
	}

	virtual void set( ulong_t value ) {
		if constexpr( sizeof( T ) == sizeof( ulong_t ) ) {
			*( ulong_t* )( &value_ ) = value;
			return;
		}

		memcpy( &value_, &value, sizeof( value_ ) );
	}

	virtual hash_t get_hash( ) {
		return name_;
	}

	__forceinline bool is_floating_point( ) {
		return is_float_;
	}

	__forceinline bool is_integral( ) {
		return is_integral_;
	}

	virtual std::string get_data_string( ) override {
		auto buffer = reinterpret_cast< char* >( _alloca( sizeof( T ) * 2 + 1 ) );
		auto data = reinterpret_cast< const uint8_t* >( &value_ );

		for( size_t i = 0; i < sizeof( T ); i++ )
			sprintf( &buffer[ 2 * i ], "%02X", data[ i ] );

		return buffer;
	}

	virtual void load_from_string( const std::string& str ) override {
		auto data = reinterpret_cast< uint8_t* >( &value_ );
		auto buffer = str.data( );

		if( *buffer == 0 )
			return;

		for( size_t i = 0; i < sizeof( T ); i++ ) {
			unsigned temp;
			sscanf( &buffer[ 2 * i ], "%02X", &temp );
			data[ i ] = temp;
		}
	}


private:
	hash_t name_;
	bool is_float_;
	bool is_integral_;
	T value_;
	T original_;
};