-
Notifications
You must be signed in to change notification settings - Fork 0
/
strip.cpp
220 lines (182 loc) · 5.03 KB
/
strip.cpp
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
204
205
206
207
208
209
210
211
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define RESOURCE_1_NAME "resource.pak"
//#define RESOURCE_2_NAME "embed.pak"
//#define RESOURCE_3_NAME "frontend.pak"
#define RESOURCE_TYPE 20480
#define RESOURCE_1 1
//#define RESOURCE_2 2
//#define RESOURCE_3 3
int pullPakFiles(int argc, char** argv)
{
HMODULE hExe; // handle to .EXE file
if(argc != 2)
{
cout << "Usage: strip.exe [little inferno executable filename]" << endl;
return 1;
}
// Load the .EXE whose resources you want to list.
//cout << "Loading libraries from executable..." << endl;
//cout << "ticks before: " << GetTickCount() << endl;
hExe = LoadLibrary(TEXT(argv[1]));
//cout << "ticks after: " << GetTickCount() << endl;
if (hExe == NULL)
{
cout << "Unable to load " << argv[1] << endl;
return 1;
}
//cout << "50 percent done" << endl;
//Grab resources
int iResource = RESOURCE_1;
//for(int iResource = RESOURCE_1; iResource <= RESOURCE_3; iResource++)
{
HRSRC hResource = FindResource(hExe, MAKEINTRESOURCE(iResource), MAKEINTRESOURCE(RESOURCE_TYPE));
if (!hResource)
{
cout << "Error: Unable to find resource " << iResource << " in executable" << endl;
return 1;
}
DWORD resLen = SizeofResource(hExe, hResource);
if (!resLen)
{
cout << "Error: size of resource = 0" << endl;
return 1;
}
if(resLen == 4) //4 bytes means this resource has been pulled already
{
cout << "This executable has already been stripped. Aborting." << endl;
return 1;
}
char* cName = RESOURCE_1_NAME;
/*char cName[512];
switch(iResource)
{
case RESOURCE_1:
sprintf(cName, "%s", RESOURCE_1_NAME);
break;
case RESOURCE_2:
sprintf(cName, "%s", RESOURCE_2_NAME);
break;
default:
sprintf(cName, "%s", RESOURCE_3_NAME);
break;
}*/
//cout << "Stripping resource " << cName << endl;
HGLOBAL hgResData = LoadResource(hExe, hResource);
if (!hgResData)
{
cout << "Error: Unable to load resource " << iResource << endl;
return 1;
}
char* pBuffer = (char*)LockResource(hgResData);
if(pBuffer == NULL)
{
cout << "Error: Unable to lock resource " << iResource << endl;
return 1;
}
HANDLE hFile = CreateFile(cName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
cout << "Error: Unable to open output file " << cName << endl;
return 1;
}
//if(iResource != RESOURCE_1)
{
DWORD dwLen = 0;
if ((!(WriteFile(hFile, pBuffer, resLen, &dwLen, NULL))) ||
(dwLen != resLen))
{
CloseHandle(hFile);
cout << "Error: Unable to write resource data to file " << cName << endl;
return (1);
}
CloseHandle(hFile);
}/*
else //Progress bar sort of thing for resource.pak
{
DWORD writtenLen = 0;
DWORD segmentLen = resLen / 50;
int iCurProgress = 50;
for(;(resLen - writtenLen) > segmentLen; writtenLen += segmentLen)
{
DWORD dwLen = 0;
if ((!(WriteFile(hFile, &pBuffer[writtenLen], segmentLen, &dwLen, NULL))) ||
(dwLen != segmentLen))
{
CloseHandle(hFile);
cout << "Error: Unable to write resource data to file " << cName << endl;
return (1);
}
cout << iCurProgress++ << " percent done" << endl;
}
if(writtenLen < resLen)
{
DWORD dwLen = 0;
if ((!(WriteFile(hFile, &pBuffer[writtenLen], (resLen - writtenLen), &dwLen, NULL))) ||
(dwLen != (resLen - writtenLen)))
{
CloseHandle(hFile);
cout << "Error: Unable to write resource data to file " << cName << endl;
return (1);
}
}
CloseHandle(hFile);
}*/
}
FreeLibrary(hExe);
return 0;
}
int main(int argc, char** argv)
{
cout << "Warning! This will modify your game's executable. Be sure to back it up first. Are you sure you wish to continue? (y/n) ";
char c = 'n';
cin >> c;
if(c != 'y')
{
cout << "Abort." << endl;
return 0;
}
if(pullPakFiles(argc,argv))
return 1;
HANDLE hUpdateRes; // update resource handle
BOOL result;
unsigned char* cZero = (unsigned char*) malloc(4);
memset(cZero, 0, 4);
// Load the .EXE to update the resources
hUpdateRes = BeginUpdateResource(argv[1], FALSE);
if (hUpdateRes == NULL)
{
cout << "Could not open file " << argv[1] << " for writing.";
return 1;
}
//Grab resources
WORD iResource = RESOURCE_1;
//for(WORD iResource = RESOURCE_1; iResource <= RESOURCE_3; iResource++)
{
// Add this resource to the update list.
result = UpdateResource(hUpdateRes,
MAKEINTRESOURCE(RESOURCE_TYPE),
MAKEINTRESOURCE(iResource),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
cZero,
4);
if (result == FALSE)
{
cout << "Could not update resource " << iResource << ": " << GetLastError() << endl;
return 1;
}
}
// Finish writing changes to the executable and then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
cout << "Could not write changes to the file " << argv[1] << endl;
return 1;
}
//Done
free(cZero);
//cout << "100 percent done" << endl;
return 0;
}