summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Authorization.cpp
blob: ca9b8642dc920a487a68cf60f3570aa39f51a053 (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
/*  Project Starshatter 4.5
	Destroyer Studios LLC
	Copyright © 1997-2004. All Rights Reserved.

	SUBSYSTEM:    Stars.exe
	FILE:         Authorization.cpp
	AUTHOR:       John DiCamillo


	OVERVIEW
	========
	Authorization Sprite animation class
*/

#include "MemDebug.h"
#include "Authorization.h"

#include "Game.h"
#include "Text.h"

// +--------------------------------------------------------------------+

static Text GetCDKeyFromRegistry()
{
	Text  cdkey;
	BYTE  cdbuf[64];
	DWORD cdlen = 0;
	HKEY  hkey  = 0;

	ZeroMemory(cdbuf, sizeof(cdbuf));

	RegOpenKeyEx(HKEY_LOCAL_MACHINE,
	"SOFTWARE\\Matrix Games\\Starshatter",
	0,
	KEY_QUERY_VALUE,
	&hkey);

	if (hkey) {
		cdlen  = 64;

		LONG result =
		RegQueryValueEx(hkey,
		"authorized",
		NULL,
		NULL,
		cdbuf,
		&cdlen);

		if (result == ERROR_SUCCESS && cdlen > 0)
		cdkey = (const char*) cdbuf;

		RegCloseKey(hkey);
	}

	return cdkey;
}

static Text GetCDKeyFromIniFile()
{
	Text cdkey;
	char cdbuf[256];

	ZeroMemory(cdbuf, sizeof(cdbuf));

	FILE* f;
	fopen_s(&f, "maga.mg", "r");
	if (f) {
		bool found_section = false;

		while (fgets(cdbuf, sizeof(cdbuf)-1, f)) {
			Text line = Text(cdbuf).trim();
			line.setSensitive(false);

			if (line == "[SerialNumber]")
			found_section = true;

			if (found_section) {
				if (line.indexOf("serial") == 0) {
					// found the proper line in the proper section,
					// now we need to parse the 'name = value' sentence:

					const char* p = line.data();

					// find the equal sign:
					while (p && *p && *p != '=')
					p++;

					// skip the equal sign:
					p++;

					// find the string after the equal sign:
					while (p && *p && isspace(*p))
					p++;

					if (p && *p) {
						// deal with quoted strings:
						int cutoff = sizeof(cdbuf)-1;

						if (*p == '"') {
							char* s = cdbuf;
							p++;

							while (*p && *p != '"' && cutoff-- > 0) {
								*s++ = *p++;
							}

							*s = 0;
						}

						// and unquoted strings:
						else {
							char* s = cdbuf;

							while (*p && cutoff-- > 0) {
								*s++ = *p++;
							}

							*s = 0;
						}

						cdkey = cdbuf;
					}
				}
			}
		}

		fclose(f);
	}

	return cdkey;
}

// +--------------------------------------------------------------------+

static char serial_number[64];
int execRegistrationProgram();

bool
Authorization::IsUserAuthorized()
{
	// XXX DEBUG ONLY!
	// return true;

	int authcode = execRegistrationProgram();
	if (authcode != 1) {
		::Print("Authorization failed, code = %d\n", authcode);
	}

	return (authcode == 1);
}

// +--------------------------------------------------------------------+

const char*
Authorization::GetSerialNumber()
{
	return serial_number;
}

// +--------------------------------------------------------------------+

int execRegistrationProgram()
{
	int  result = 999;
	char cmdline[256];
	strcpy_s(cmdline, "SS2rez");

	STARTUPINFO s;
	ZeroMemory(&s, sizeof(s));
	s.cb = sizeof(s);

	PROCESS_INFORMATION pi;
	ZeroMemory(&pi, sizeof(pi));

	if (CreateProcess("SS2rez.exe", cmdline, 0, 0, 0, 0, 0, 0, &s, &pi)) {
		DWORD  exitcode = STILL_ACTIVE;

		WaitForSingleObject(pi.hProcess, 20000);
		GetExitCodeProcess(pi.hProcess, &exitcode);

		if (exitcode != STILL_ACTIVE)
		result = exitcode;

		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
	}
	else {
		TCHAR message[256];
		DWORD errcode = GetLastError();

		::Print("  WARN: Failed to create authorization process: %08X.\n", errcode);

		if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, errcode, 0, message, 256, 0)) {
			::Print("        ");
			::Print(message);
			::Print("\n");
		}
	}

	return result;
}