summaryrefslogtreecommitdiff
path: root/devtools/tagbuild/tagbuild.cpp
blob: 600a6cb8f76d235f870d1cb26ecbc66044a1f991 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
// tagbuild.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <process.h>
#include <string.h>
#include "interface.h"
#include "imysqlwrapper.h"

const char *search = "VLV_INTERNAL                    ";

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void printusage( void )
{
	printf( "usage:  tagbuild engine.dll buildid <hostname database username password>\n" );

	// Exit app
	exit( 1 );
}

int main(int argc, char* argv[])
{
	if ( argc != 3 && argc != 7 )
	{
		printusage();
	}

	int idlen = strlen( argv[ 2 ] );

	if ( idlen > 32 )
	{
		printf( "id string '%s' is %i long, 32 is max\n", argv[ 2 ], idlen );
		printusage();
	}

	// Open for reading and writing
	FILE *fp = fopen( argv[ 1 ], "r+" );
	if ( !fp )
	{
		printf( "unable to open %s\n", argv[ 1 ] );
		exit( 1 );
	}

	fseek( fp, 0, SEEK_END );
	int size = ftell( fp );
	fseek( fp, 0, SEEK_SET );

	int searchlen = strlen( search );

	char alreadytagged[ 32 ];
	Q_memset( alreadytagged, ' ', sizeof( alreadytagged ) );
	alreadytagged[ 31 ] = 0;
	// Copies in the id, but doesn't copy the terminating '\0'
	Q_memcpy( alreadytagged, argv[ 2 ], Q_strlen( argv[ 2 ] ) );

	bool found = false;
	char *buf = new char[ size ];
	if ( buf )
	{
		fread( buf, size, 1, fp );

		// Now search
		char *start = buf;
		while ( start - buf < ( size - searchlen ) )
		{
			if ( !memcmp( start, search, searchlen - 1 ) )
			{
				int pos = start - buf;

				printf( "Found placeholder at %i, writing '%s' into file\n",
					pos, argv[ 2 ] );

				fseek( fp, pos, SEEK_SET );
				fwrite( argv[ 2 ], idlen, 1, fp );
				while ( idlen < searchlen )
				{
					fputc( ' ', fp );
					++idlen;
				}

				found = true;
				break;
			}
			else if ( !memcmp( start, alreadytagged, searchlen - 1 ) )
			{
				int pos = start - buf;

				printf( "Found tag at %i (%s)\n", pos, argv[ 2 ] );
				found = true;
				break;
			}

			++start;
		}

		if ( !found )
		{
			printf( "Couldn't find search string '%s' in binary data\n", search );
		}

	}
	else
	{
		printf( "unable to allocate %i bytes for %s\n", size, argv[ 1 ] );
	}


	fclose( fp );

	if ( argc <= 3 )
	{
		printf( "Skipping database insertion\n" );
	}
	else
	{
		// Now connect to steamweb and update the engineaccess table
		CSysModule *sql = Sys_LoadModule( "mysql_wrapper" );
		if ( sql )
		{
			CreateInterfaceFn factory = Sys_GetFactory( sql );
			if ( factory )
			{
				IMySQL *mysql = ( IMySQL * )factory( MYSQL_WRAPPER_VERSION_NAME, NULL );
				if ( mysql )
				{
					if ( mysql->InitMySQL( argv[ 4 ], argv[ 3 ], argv[ 5 ], argv[ 6 ] ) )
					{
						char q[ 512 ];

						Q_snprintf( q, sizeof( q ), "insert into engineaccess (BuildIdentifier,AllowAccess) values (\"%s\",1);",
							argv[2] );

						int retcode = mysql->Execute( q );
						if ( retcode != 0 )
						{
							printf( "Query %s failed\n", q );
						}
						else
						{
							printf( "Successfully added buildidentifier '%s' to %s:%s\n",
								argv[ 2 ], argv[ 3 ], argv[ 4 ] );
						}
					}
					else
					{
						printf( "InitMySQL failed\n" );
					}

					mysql->Release();
				}
				else
				{
					printf( "Unable to connect via mysql_wrapper\n");
				}
			}
			else
			{
				printf( "Unable to get factory from mysql_wrapper.dll, not updating access mysql table!!!" );
			}

			Sys_UnloadModule( sql );
		}
		else
		{
			printf( "Unable to load mysql_wrapper.dll, not updating access mysql table!!!" );
		}
	}

	return 0;
}