Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Authorization.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: Authorization.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Authorization Sprite animation class
13 */
14 
15 #include "MemDebug.h"
16 #include "Authorization.h"
17 
18 #include "Game.h"
19 #include "Text.h"
20 
21 // +--------------------------------------------------------------------+
22 
23 static Text GetCDKeyFromRegistry()
24 {
25  Text cdkey;
26  BYTE cdbuf[64];
27  DWORD cdlen = 0;
28  HKEY hkey = 0;
29 
30  ZeroMemory(cdbuf, sizeof(cdbuf));
31 
32  RegOpenKeyEx(HKEY_LOCAL_MACHINE,
33  "SOFTWARE\\Matrix Games\\Starshatter",
34  0,
35  KEY_QUERY_VALUE,
36  &hkey);
37 
38  if (hkey) {
39  cdlen = 64;
40 
41  LONG result =
42  RegQueryValueEx(hkey,
43  "authorized",
44  NULL,
45  NULL,
46  cdbuf,
47  &cdlen);
48 
49  if (result == ERROR_SUCCESS && cdlen > 0)
50  cdkey = (const char*) cdbuf;
51 
52  RegCloseKey(hkey);
53  }
54 
55  return cdkey;
56 }
57 
58 static Text GetCDKeyFromIniFile()
59 {
60  Text cdkey;
61  char cdbuf[256];
62 
63  ZeroMemory(cdbuf, sizeof(cdbuf));
64 
65  FILE* f;
66  fopen_s(&f, "maga.mg", "r");
67  if (f) {
68  bool found_section = false;
69 
70  while (fgets(cdbuf, sizeof(cdbuf)-1, f)) {
71  Text line = Text(cdbuf).trim();
72  line.setSensitive(false);
73 
74  if (line == "[SerialNumber]")
75  found_section = true;
76 
77  if (found_section) {
78  if (line.indexOf("serial") == 0) {
79  // found the proper line in the proper section,
80  // now we need to parse the 'name = value' sentence:
81 
82  const char* p = line.data();
83 
84  // find the equal sign:
85  while (p && *p && *p != '=')
86  p++;
87 
88  // skip the equal sign:
89  p++;
90 
91  // find the string after the equal sign:
92  while (p && *p && isspace(*p))
93  p++;
94 
95  if (p && *p) {
96  // deal with quoted strings:
97  int cutoff = sizeof(cdbuf)-1;
98 
99  if (*p == '"') {
100  char* s = cdbuf;
101  p++;
102 
103  while (*p && *p != '"' && cutoff-- > 0) {
104  *s++ = *p++;
105  }
106 
107  *s = 0;
108  }
109 
110  // and unquoted strings:
111  else {
112  char* s = cdbuf;
113 
114  while (*p && cutoff-- > 0) {
115  *s++ = *p++;
116  }
117 
118  *s = 0;
119  }
120 
121  cdkey = cdbuf;
122  }
123  }
124  }
125  }
126 
127  fclose(f);
128  }
129 
130  return cdkey;
131 }
132 
133 // +--------------------------------------------------------------------+
134 
135 static char serial_number[64];
137 
138 bool
140 {
141  // XXX DEBUG ONLY!
142  // return true;
143 
144  int authcode = execRegistrationProgram();
145  if (authcode != 1) {
146  ::Print("Authorization failed, code = %d\n", authcode);
147  }
148 
149  return (authcode == 1);
150 }
151 
152 // +--------------------------------------------------------------------+
153 
154 const char*
156 {
157  return serial_number;
158 }
159 
160 // +--------------------------------------------------------------------+
161 
163 {
164  int result = 999;
165  char cmdline[256];
166  strcpy_s(cmdline, "SS2rez");
167 
168  STARTUPINFO s;
169  ZeroMemory(&s, sizeof(s));
170  s.cb = sizeof(s);
171 
172  PROCESS_INFORMATION pi;
173  ZeroMemory(&pi, sizeof(pi));
174 
175  if (CreateProcess("SS2rez.exe", cmdline, 0, 0, 0, 0, 0, 0, &s, &pi)) {
176  DWORD exitcode = STILL_ACTIVE;
177 
178  WaitForSingleObject(pi.hProcess, 20000);
179  GetExitCodeProcess(pi.hProcess, &exitcode);
180 
181  if (exitcode != STILL_ACTIVE)
182  result = exitcode;
183 
184  CloseHandle(pi.hProcess);
185  CloseHandle(pi.hThread);
186  }
187  else {
188  TCHAR message[256];
189  DWORD errcode = GetLastError();
190 
191  ::Print(" WARN: Failed to create authorization process: %08X.\n", errcode);
192 
193  if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, errcode, 0, message, 256, 0)) {
194  ::Print(" ");
195  ::Print(message);
196  ::Print("\n");
197  }
198  }
199 
200  return result;
201 }
202 
203