1 |
/* PCSX2 - PS2 Emulator for PCs |
2 |
* Copyright (C) 2002-2009 PCSX2 Dev Team |
3 |
* |
4 |
* PCSX2 is free software: you can redistribute it and/or modify it under the terms |
5 |
* of the GNU Lesser General Public License as published by the Free Software Found- |
6 |
* ation, either version 3 of the License, or (at your option) any later version. |
7 |
* |
8 |
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
9 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
10 |
* PURPOSE. See the GNU General Public License for more details. |
11 |
* |
12 |
* You should have received a copy of the GNU General Public License along with PCSX2. |
13 |
* If not, see <http://www.gnu.org/licenses/>. |
14 |
*/ |
15 |
|
16 |
|
17 |
/************************** |
18 |
* |
19 |
* patchbrowser.c contains all the src of patchbrowser window |
20 |
* no interaction with emulation code |
21 |
***************************/ |
22 |
|
23 |
#include "Win32.h" |
24 |
#include "Common.h" |
25 |
|
26 |
/* |
27 |
* TODO: |
28 |
* - not topmost |
29 |
* - resize stuff |
30 |
* - ask to save in exit (check if changed) |
31 |
*/ |
32 |
BOOL CALLBACK PatchBDlgProc(HWND hW, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
33 |
|
34 |
int tmpi,i; |
35 |
char fileName[MAX_PATH], *tmpStr; |
36 |
FILE *fp; |
37 |
|
38 |
switch(uMsg) { |
39 |
|
40 |
case WM_INITDIALOG: |
41 |
SetWindowText(hW, "Patches Browser"); |
42 |
Button_SetText(GetDlgItem(hW,IDC_REFRESHPATCHLIST), "Refresh List"); |
43 |
Button_SetText(GetDlgItem(hW,IDC_NEWPATCH), "New Patch"); |
44 |
Button_SetText(GetDlgItem(hW,IDC_SAVEPATCH), "Save Patch"); |
45 |
Button_SetText(GetDlgItem(hW,IDC_EXITPB), "Exit"); |
46 |
Static_SetText(GetDlgItem(hW,IDC_GAMENAMESEARCH), "Search game name patch:"); |
47 |
//List Patches |
48 |
ListPatches ((HWND) hW); |
49 |
return TRUE; |
50 |
|
51 |
case WM_COMMAND: |
52 |
switch(LOWORD(wParam)) { |
53 |
|
54 |
case IDC_NEWPATCH: |
55 |
|
56 |
i = Save_Patch_Proc(fileName); |
57 |
if ( i == FALSE || (fp = fopen(fileName,"a")) == NULL ) { |
58 |
MessageBox(hW,(LPCTSTR)"Couldn't create the file.",NULL,(UINT)MB_ICONERROR); |
59 |
return FALSE; |
60 |
} |
61 |
|
62 |
fclose(fp); |
63 |
i = MessageBox(hW,(LPCTSTR)"File created sucessfully.\nClear textbox?",NULL,(UINT)(MB_YESNO|MB_ICONQUESTION)); |
64 |
if (i==IDYES) SetDlgItemText(hW, IDC_PATCHTEXT, (LPCTSTR)""); |
65 |
|
66 |
return TRUE; |
67 |
|
68 |
case IDC_SAVEPATCH: |
69 |
|
70 |
i = Save_Patch_Proc(fileName); |
71 |
if ( i == FALSE || (fp = fopen(fileName,"w")) == NULL ) { |
72 |
MessageBox(hW,(LPCTSTR)"Couldn't save the file.",NULL,(UINT)MB_ICONERROR); |
73 |
return FALSE; |
74 |
} |
75 |
|
76 |
tmpi = SendDlgItemMessage(hW, IDC_PATCHTEXT, EM_GETLINECOUNT, (WPARAM)NULL, (LPARAM)NULL); |
77 |
i=0; |
78 |
for (;tmpi>=0;tmpi--) |
79 |
i += SendDlgItemMessage(hW, IDC_PATCHTEXT, EM_LINELENGTH, (WPARAM)tmpi, (LPARAM)NULL); |
80 |
|
81 |
tmpStr = (char *) malloc(i); |
82 |
sprintf(tmpStr,""); |
83 |
SendDlgItemMessage(hW, IDC_PATCHTEXT, WM_GETTEXT, (WPARAM)i, (LPARAM)tmpStr); |
84 |
|
85 |
//remove \r |
86 |
for (i=0,tmpi=0; tmpStr[i]!='\0'; i++) |
87 |
if (tmpStr[i] != '\r') |
88 |
tmpStr[tmpi++] = tmpStr[i]; |
89 |
tmpStr[tmpi] = '\0'; |
90 |
|
91 |
fputs(tmpStr,fp); |
92 |
|
93 |
fclose(fp); |
94 |
free(tmpStr); |
95 |
|
96 |
MessageBox(hW,(LPCTSTR)"File saved sucessfully.",NULL,(UINT)MB_ICONINFORMATION); |
97 |
|
98 |
return TRUE; |
99 |
|
100 |
case IDC_REFRESHPATCHLIST: |
101 |
|
102 |
//List Patches |
103 |
ListPatches ((HWND) hW); |
104 |
|
105 |
return TRUE; |
106 |
|
107 |
case IDC_EXITPB: |
108 |
|
109 |
//Close Dialog |
110 |
EndDialog(hW, FALSE); |
111 |
|
112 |
return TRUE; |
113 |
|
114 |
case IDC_PATCHCRCLIST: |
115 |
|
116 |
//Get selected item |
117 |
tmpi = SendDlgItemMessage(hW, IDC_PATCHCRCLIST, LB_GETCURSEL, 0, 0); |
118 |
SendDlgItemMessage(hW, IDC_PATCHCRCLIST, LB_GETTEXT, (WPARAM)tmpi, (LPARAM)fileName); |
119 |
|
120 |
return ReadPatch ((HWND) hW, fileName); |
121 |
|
122 |
case IDC_PATCHNAMELIST: |
123 |
|
124 |
//Get selected item |
125 |
tmpi = SendDlgItemMessage(hW, IDC_PATCHNAMELIST, LB_GETCURSEL, 0, 0); |
126 |
SendDlgItemMessage(hW, IDC_PATCHNAMELIST, LB_GETTEXT, (WPARAM)tmpi, (LPARAM)fileName); |
127 |
|
128 |
//another small hack :p |
129 |
//eg. SOCOM Demo PAL (7dd01dd9.pnach) |
130 |
for (i=0;i<(int)strlen(fileName);i++) |
131 |
if (fileName[i] == '(') tmpi = i; |
132 |
|
133 |
sprintf(fileName,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c", |
134 |
fileName[tmpi+1],fileName[tmpi+2],fileName[tmpi+3], |
135 |
fileName[tmpi+4],fileName[tmpi+5],fileName[tmpi+6], |
136 |
fileName[tmpi+7],fileName[tmpi+8],fileName[tmpi+9], |
137 |
fileName[tmpi+10],fileName[tmpi+11],fileName[tmpi+12], |
138 |
fileName[tmpi+13],fileName[tmpi+14]); |
139 |
|
140 |
//sanity check |
141 |
if (fileName[tmpi+15] != ')') return FALSE; |
142 |
|
143 |
return ReadPatch ((HWND) hW, fileName); |
144 |
|
145 |
case IDC_SEARCHPATCHTEXT: |
146 |
|
147 |
//get text |
148 |
SendDlgItemMessage(hW, IDC_SEARCHPATCHTEXT, EM_GETLINE, 0, (LPARAM)fileName); |
149 |
//search |
150 |
tmpi = SendDlgItemMessage(hW, IDC_PATCHNAMELIST, LB_FINDSTRING, (WPARAM)-1, (LPARAM)fileName); |
151 |
//select match item |
152 |
SendDlgItemMessage(hW, IDC_PATCHNAMELIST, LB_SETCURSEL, (WPARAM)tmpi, (LPARAM)NULL); |
153 |
|
154 |
return TRUE; |
155 |
} |
156 |
return TRUE; |
157 |
|
158 |
case WM_CLOSE: |
159 |
EndDialog(hW, FALSE); |
160 |
break; |
161 |
|
162 |
} |
163 |
return FALSE; |
164 |
} |
165 |
void ListPatches (HWND hW) { |
166 |
|
167 |
int i, tmpi, filesize, totalPatch=0, totalSize=0; |
168 |
char tmpStr[MAX_PATH], *fileData; |
169 |
WIN32_FIND_DATA FindData; |
170 |
HANDLE Find; |
171 |
FILE *fp; |
172 |
|
173 |
//clear listbox's |
174 |
SendDlgItemMessage(hW, IDC_PATCHCRCLIST, (UINT)LB_RESETCONTENT, (WPARAM)NULL, (LPARAM)NULL); |
175 |
SendDlgItemMessage(hW, IDC_PATCHNAMELIST, (UINT)LB_RESETCONTENT, (WPARAM)NULL, (LPARAM)NULL); |
176 |
|
177 |
//sprintf(tmpStr,"%s*.pnach", Config.PatchDir) |
178 |
sprintf(tmpStr, "patches\\*.pnach"); |
179 |
|
180 |
Find = FindFirstFile(tmpStr, &FindData); |
181 |
|
182 |
do { |
183 |
if (Find==INVALID_HANDLE_VALUE) break; |
184 |
|
185 |
sprintf(tmpStr,"%s", FindData.cFileName); |
186 |
|
187 |
//add file name to crc list |
188 |
SendDlgItemMessage(hW, IDC_PATCHCRCLIST, (UINT) LB_ADDSTRING, (WPARAM)NULL, (LPARAM)tmpStr); |
189 |
|
190 |
//sprintf(tmpStr,"%s%s", Config.PatchDir, FindData.cFileName) |
191 |
sprintf(tmpStr,"patches\\%s", FindData.cFileName); |
192 |
|
193 |
fp = fopen(tmpStr, "r"); |
194 |
if (fp == NULL) break; |
195 |
|
196 |
fseek(fp, 0, SEEK_END); |
197 |
filesize = ftell(fp); |
198 |
totalSize += filesize; |
199 |
fseek(fp, 0, SEEK_SET); |
200 |
|
201 |
fileData = (char *) malloc(filesize+1024); |
202 |
sprintf(fileData,""); |
203 |
|
204 |
//read file |
205 |
while((tmpi=fgetc(fp)) != EOF) |
206 |
sprintf(fileData,"%s%c",fileData,tmpi); |
207 |
|
208 |
//small hack :p |
209 |
for(i=0;i<filesize;i++) { |
210 |
if (fileData[i] == 'g' && fileData[i+1] == 'a' && |
211 |
fileData[i+2] == 'm' && fileData[i+3] == 'e' && |
212 |
fileData[i+4] == 't' && fileData[i+5] == 'i' && |
213 |
fileData[i+6] == 't' && fileData[i+7] == 'l' && |
214 |
fileData[i+8] == 'e' && fileData[i+9] == '=') { |
215 |
|
216 |
for(i=i+10,tmpi=0;i<filesize;i++,tmpi++) { |
217 |
if (fileData[i] == '\n') break; |
218 |
tmpStr[tmpi] = fileData[i]; |
219 |
} |
220 |
tmpStr[tmpi] = '\0'; |
221 |
break; |
222 |
} |
223 |
} |
224 |
|
225 |
//remove " in the string |
226 |
for (i=0,tmpi=0; tmpStr[i]!='\0'; i++) |
227 |
if (tmpStr[i] != '"') |
228 |
tmpStr[tmpi++] = tmpStr[i]; |
229 |
tmpStr[tmpi] = '\0'; |
230 |
|
231 |
//remove spaces at the left of the string |
232 |
sprintf(tmpStr,lTrim(tmpStr)); |
233 |
|
234 |
sprintf(tmpStr,"%s (%s)",tmpStr,FindData.cFileName); |
235 |
|
236 |
//add game name to patch name list |
237 |
SendDlgItemMessage(hW, IDC_PATCHNAMELIST, (UINT) LB_ADDSTRING, (WPARAM)NULL, (LPARAM)tmpStr); |
238 |
|
239 |
totalPatch++; |
240 |
sprintf(fileData,""); |
241 |
fclose(fp); |
242 |
|
243 |
} while (FindNextFile(Find,&FindData)); |
244 |
|
245 |
if (Find!=INVALID_HANDLE_VALUE) FindClose(Find); |
246 |
|
247 |
sprintf(tmpStr,"Patches Browser | Patches Found: %d | Total Filesize: %.2f KB", |
248 |
totalPatch,(float)totalSize/1024); |
249 |
SetWindowText(hW, tmpStr); |
250 |
} |
251 |
|
252 |
int ReadPatch (HWND hW, char fileName[1024]) { |
253 |
|
254 |
FILE * fp; |
255 |
int tmpi, filesize, i; |
256 |
char filePath[MAX_PATH],*fileData; |
257 |
|
258 |
sprintf(filePath,"patches\\%s", fileName); |
259 |
|
260 |
fp = fopen(filePath, "r"); |
261 |
if (fp == NULL) return FALSE; |
262 |
|
263 |
fseek(fp, 0, SEEK_END); |
264 |
filesize = ftell(fp); |
265 |
fseek(fp, 0, SEEK_SET); |
266 |
|
267 |
//filesize+1024 cause bellow i'm gonna add stuff to the string |
268 |
//TODO: change the 1024 to a more apropriated value |
269 |
fileData = (char *) malloc(filesize+1024); |
270 |
sprintf(fileData,""); |
271 |
|
272 |
while((tmpi=fgetc(fp)) != EOF) |
273 |
sprintf(fileData,"%s%c",fileData,tmpi); |
274 |
|
275 |
//for some reason windows editbox only show the newline |
276 |
//when it have \r\n don't ask me why :p |
277 |
//so what i did was to every \n put \r\n |
278 |
for (i=0;i<filesize;i++) { |
279 |
if (fileData[i] == '\n' ) { |
280 |
for (tmpi=filesize;tmpi>i;tmpi--) |
281 |
fileData[tmpi] = fileData[tmpi-1]; |
282 |
fileData[i] = '\r'; |
283 |
fileData[i+1] = '\n'; |
284 |
i++; |
285 |
} |
286 |
} |
287 |
|
288 |
SetDlgItemText(hW, IDC_PATCHTEXT, (LPCTSTR)fileData); |
289 |
|
290 |
sprintf(fileData,""); |
291 |
fclose(fp); |
292 |
|
293 |
return TRUE; |
294 |
} |
295 |
|
296 |
|
297 |
//Left Trim (remove the spaces at the left of a string) |
298 |
char * lTrim (char *s) { |
299 |
int count=0,i,tmpi; |
300 |
|
301 |
for (i=0;i<(int)strlen(s); i++) { |
302 |
if (s[i] == ' ') count++; |
303 |
else { |
304 |
for (tmpi=0;tmpi<(int)strlen(s);tmpi++) |
305 |
s[tmpi] = s[tmpi+count]; |
306 |
break; |
307 |
} |
308 |
} |
309 |
return s; |
310 |
} |
311 |
|
312 |
|
313 |
BOOL Save_Patch_Proc( char * filename ) { |
314 |
|
315 |
OPENFILENAME ofn; |
316 |
char szFileName[ 256 ]; |
317 |
char szFileTitle[ 256 ]; |
318 |
char * filter = "Patch Files (*.pnach)\0*.pnach\0ALL Files (*.*)\0*.*"; |
319 |
|
320 |
memset( &szFileName, 0, sizeof( szFileName ) ); |
321 |
memset( &szFileTitle, 0, sizeof( szFileTitle ) ); |
322 |
|
323 |
ofn.lStructSize = sizeof( OPENFILENAME ); |
324 |
ofn.hwndOwner = gApp.hWnd; |
325 |
ofn.lpstrFilter = filter; |
326 |
ofn.lpstrCustomFilter = NULL; |
327 |
ofn.nMaxCustFilter = 0; |
328 |
ofn.nFilterIndex = 1; |
329 |
ofn.lpstrFile = szFileName; |
330 |
ofn.nMaxFile = 256; |
331 |
ofn.lpstrInitialDir = NULL; |
332 |
ofn.lpstrFileTitle = szFileTitle; |
333 |
ofn.nMaxFileTitle = 256; |
334 |
ofn.lpstrTitle = NULL; |
335 |
ofn.lpstrDefExt = "TXT"; |
336 |
ofn.Flags = OFN_EXPLORER | OFN_LONGNAMES | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST; |
337 |
|
338 |
if ( GetSaveFileName( &ofn ) ) { |
339 |
|
340 |
strcpy( filename, szFileName ); |
341 |
|
342 |
return TRUE; |
343 |
} |
344 |
else { |
345 |
return FALSE; |
346 |
} |
347 |
} |