Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
findpatch.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "findpatch.h"
20
21//
22// Locate a patch file
23// If a patch can be found then TRUE is returned and the name is filled in,
24// otherwise FALSE is returned.
25//
26// Patch Types:
27// - *.rtp = RTPatch file that can be applied right now
28// - *.exe = Executable that should be put in the RunOnce registry entry & reboot
29// - *.exn = Executable that should be run right now
30// - *.web = Link to a web page that will have the patch
31// - else = File is ignored, possibly a resource file for one of the other types
32//
33int Find_Patch(OUT char *filename,int maxlen, ConfigFile &config)
34{
35 WIN32_FIND_DATA findData;
36 char string[128];
37 HANDLE hFile;
38 char *extensions[]={"web","exe","exn","rtp",NULL};
39 int i;
40 int skuIndex=0;
41 Wstring key;
42 Wstring path;
43 Wstring sku;
44 char gamePath[MAX_PATH] = "";
45 bit8 ok;
46
47
48 while(1)
49 {
50 //
51 // Loop through the apps we're responsible for
52 //
53 skuIndex++;
54 ok=Get_App_Dir(gamePath,MAX_PATH,config,skuIndex);
55 if (ok==FALSE)
56 break;
57
58 i=0;
59 while(extensions[i++])
60 {
61 _chdir(gamePath); // goto the directory with the game
62
63 // should probably get the registry entry for the wchat install path
64 sprintf(string,"patches\\*.%s",extensions[i]);
65 hFile=FindFirstFile(string,&findData);
66 if (hFile!=INVALID_HANDLE_VALUE)
67 {
68 _getcwd(filename,MAX_PATH);
69 strcat(filename,"\\patches\\");
70 strcat(filename,findData.cFileName);
71 FindClose(hFile);
72 return(skuIndex);
73 }
74 sprintf(string,"download\\*.%s",extensions[i]);
75 hFile=FindFirstFile(string,&findData);
76 if (hFile!=INVALID_HANDLE_VALUE)
77 {
78 _getcwd(filename,MAX_PATH);
79 strcat(filename,"\\download\\");
80 strcat(filename,findData.cFileName);
81 FindClose(hFile);
82 return(skuIndex);
83 }
84 }
85 }
86 return(FALSE);
87}
88
89
90//
91// Get the directory for the N'th application in the config file
92//
93// Returns FALSE if not in the config file or invalid for some reason.
94//
95bit8 Get_App_Dir(OUT char *filename,int maxlen, ConfigFile &config,int index)
96{
97 char string[128];
98 Wstring key;
99 Wstring path;
100 Wstring sku;
101 int temp;
102 char gamePath[MAX_PATH];
103
104
105 sprintf(string,"SKU%d",index);
106
107 // Can't find this product
108 if (config.getString(string,key)==FALSE)
109 return(FALSE);
110
111
112 DBGMSG("KEY = "<<key.get());
113 // Get the InstallPath from the specified registry key
114 temp=0;
115 temp=key.getToken(temp," ",sku);
116 path=key;
117 path.remove(0,temp);
118 while((*(path.get()))==' ') // remove leading spaces
119 path.remove(0,1);
120
121
122 DBGMSG("CONFIG: SKU = "<<sku.get()<<" PATH = '"<<path.get()<<"'");
123 HKEY regKey;
124 LONG regRetval;
126 regRetval=RegOpenKeyEx(HKEY_LOCAL_MACHINE,path.get(),0,KEY_READ,&regKey);
127 if (regRetval!=ERROR_SUCCESS)
128 {
129 DBGMSG("RegOpenKey failed");
130 return(FALSE);
131 }
132 DWORD type;
133 DWORD length=MAX_PATH;
134 regRetval=RegQueryValueEx(regKey,"InstallPath",NULL,&type,(uint8 *)gamePath,
135 &length);
136 DBGMSG("GAME PATH = "<<gamePath);
137 if ((regRetval!=ERROR_SUCCESS)||(type!=REG_SZ))
138 {
139 DBGMSG("Reg failure");
140 return(FALSE);
141 }
142
143 // Find the last '\\' in a string and put a 0 after it
144 // If you only put a directory in the InstallPath key instead of a full
145 // path to a file, you better end the directory with a trailing '\\'!!!
146 char *cptr=gamePath;
147 char *tempPtr;
148 while( (tempPtr=strchr(cptr,'\\')) !=NULL)
149 cptr=tempPtr+1;
150 if (cptr)
151 *cptr=0;
152
153 DBGMSG("Game path = "<<gamePath);
154 strncpy(filename,gamePath,maxlen);
155
156 return(TRUE);
157}
158
159
160
161
162
163//
164// Delete any patch files
165//
167{
168 char dir[MAX_PATH];
169 int i=1;
170 WIN32_FIND_DATA findData;
171 HANDLE hFile;
172
173 DBGMSG("IN DELPATCH");
174
175 //
176 // Loop through all the application directories in the config file
177 //
178 while (Get_App_Dir(dir,MAX_PATH,config,i++)==TRUE)
179 {
180 // Make sure path is at least 3 for "c:\". I really hope nobody's
181 // dumb enough to install a game to the root directory. (It's OK though
182 // since only the '\patches' folder is cleared out.
183 if (strlen(dir)<3)
184 continue;
185
186 //
187 // Delete everything in case a .exe patch had some data files it used.
188 //
189 strcat(dir,"patches\\*.*");
190
191 DBGMSG("DELPATCH: "<<dir);
192
193 hFile=FindFirstFile(dir,&findData);
194 if (hFile!=INVALID_HANDLE_VALUE)
195 {
196 if (findData.cFileName[0]!='.')
197 {
198 //_unlink(findData.cFileName);
199 DBGMSG("UNLINK: "<<findData.cFileName);
200 }
201 while(FindNextFile(hFile,&findData))
202 {
203 if (findData.cFileName[0]!='.')
204 {
205 //_unlink(findData.cFileName);
206 DBGMSG("UNLINK: "<<findData.cFileName);
207 }
208 }
209 } // If there's at least one file
210 FindClose(hFile);
211 } // while there's apps in config
212 return;
213}
#define NULL
Definition BaseType.h:92
#define TRUE
Definition BaseType.h:109
#define FALSE
Definition BaseType.h:113
#define DBGMSG(X)
Definition wdebug.h:128
char bit8
Definition wstypes.h:61
#define OUT
Definition wstypes.h:58
unsigned char uint8
Definition bittype.h:44
unsigned long DWORD
Definition bittype.h:57
char dir[_MAX_DIR]
Definition autorun.cpp:215
bit8 getString(IN Wstring &key, OUT Wstring &value)
char remove(sint32 pos, sint32 count)
Definition wstring.cpp:236
char * get(void)
Definition wstring.cpp:336
sint32 getToken(int offset, char *delim, Wstring &out)
Definition wstring.cpp:566
bit8 Get_App_Dir(OUT char *filename, int maxlen, ConfigFile &config, int index)
Definition findpatch.cpp:95
void Delete_Patches(ConfigFile &config)
int Find_Patch(OUT char *filename, int maxlen, ConfigFile &config)
Definition findpatch.cpp:33