Skip to content

Commit

Permalink
Added object type info and automatic uninstall script generation
Browse files Browse the repository at this point in the history
  • Loading branch information
wghost committed Dec 11, 2013
1 parent 377fb39 commit 4a7b63b
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 53 deletions.
21 changes: 4 additions & 17 deletions ExtractNameLists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,9 @@ int main(int argN, char* argV[])
for (int i = 1; i < package.GetObjectListSize(); ++i)
{
ObjectListEntry EntryToRead = package.GetObjectListEntryByIdx(i);

cout << hex << showbase << "Index: " << i << " (" << dec << i << ")"
<< ", Name: " << package.GetObjectNameByIdx(i) << endl;

cout << hex << "Object type: " << EntryToRead.ObjType << endl
<< "Parent class reference: " << EntryToRead.ParentClassRef << endl
<< "Owner reference: " << EntryToRead.OwnerRef << endl
<< "Index to name list table: " << EntryToRead.NameListIdx << endl
<< "Property flags: " << EntryToRead.PropertyFlags << endl
<< "Object file size: " << EntryToRead.ObjectFileSize << endl
<< "Object data offset: " << EntryToRead.DataOffset << endl
<< "Num additional fields: " << EntryToRead.NumAdditionalFields << endl;
<< "; Name: " << package.GetObjectNameByIdx(i)
<< "; Type: " << package.GetObjectOrImportNameByIdx(EntryToRead.ObjTypeRef) << endl;
}

cout << "Import list table:" << endl;
Expand All @@ -91,12 +82,8 @@ int main(int argN, char* argV[])
ImportListEntry EntryToRead = package.GetImportListEntryByIdx(i);

cout << hex << showbase << "Index: " << i << " (" << dec << i << ")"
<< ", Name: " << package.GetImportNameByIdx(i) << endl;

cout << hex << "Object type: " << EntryToRead.ObjType << endl
<< "Package ID: " << EntryToRead.PackageID << endl
<< "Owner reference: " << EntryToRead.OwnerRef << endl
<< "Index to name list table: " << EntryToRead.NameListIdx << endl;
<< "; Name: " << package.GetImportNameByIdx(i)
<< "; Type: " << package.GetNameByIdx(EntryToRead.ObjTypeIdx) << endl;
}

return 0;
Expand Down
6 changes: 5 additions & 1 deletion FindObjectEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ int main(int argN, char* argV[])

ObjectListEntry EntryToRead = package.GetObjectListEntryByIdx(idx);
cout << hex << showbase << "Object table index: " << idx << " (" << dec << idx << ")" << endl
<< hex << "Object type: " << EntryToRead.ObjType << endl
<< hex << "Object type: " << EntryToRead.ObjTypeRef << endl
<< "Type string: " << package.GetObjectOrImportNameByIdx(EntryToRead.ObjTypeRef) << endl
<< "Parent class reference: " << EntryToRead.ParentClassRef << endl
<< "Parent class string: " << package.GetObjectOrImportNameByIdx(EntryToRead.ParentClassRef) << endl
<< "Owner reference: " << EntryToRead.OwnerRef << endl
<< "Owner string: " << package.GetObjectOrImportNameByIdx(EntryToRead.OwnerRef) << endl
<< "Index to name list table: " << EntryToRead.NameListIdx << endl
<< "Name list string: " << package.GetNameByIdx(EntryToRead.NameListIdx) << endl
<< "Unknown Field 5: " << EntryToRead.Field5 << endl
<< "Unknown Field 6: " << EntryToRead.Field5 << endl
<< "Property flags: " << EntryToRead.PropertyFlags << endl
Expand Down
20 changes: 20 additions & 0 deletions ModParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
#include <iostream>
#include <sstream>

const char chLookup[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

std::string hex2str(char *data, size_t dataSize)
{
std::string out = "";
for (unsigned i = 0; i < dataSize; ++i)
{
if ((i%16 == 0) && (i != 0))
out += '\n';
uint8_t ch = data[i];
uint8_t up = ((ch & 0xF0) >> 4);
uint8_t lw = (ch & 0x0F);
out += chLookup[up];
out += chLookup[lw];
out += ' ';
}
out += '\n';
return out;
}

std::string ModParser::GetText()
{
std::string str = "";
Expand Down
2 changes: 2 additions & 0 deletions ModParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <fstream>
#include <vector>

std::string hex2str(char *data, size_t dataSize);

class ModParser
{
public:
Expand Down
51 changes: 34 additions & 17 deletions MoveExpandFunction.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <sstream>
#include <cstring>

#include "UPKUtils.h"

Expand All @@ -11,7 +12,7 @@ int main(int argN, char* argV[])

if (argN < 3 || argN > 4)
{
cerr << "Usage: FindObjectEntry UnpackedResourceFile.upk FunctionName [NewFunctionSize]" << endl;
cerr << "Usage: FindObjectEntry UnpackedResourceFile.upk FunctionName [NewFunctionSize or /u]" << endl;
return 1;
}

Expand All @@ -37,33 +38,49 @@ int main(int argN, char* argV[])

ObjectListEntry EntryToRead = package.GetObjectListEntryByIdx(idx);

uint32_t newFunctionSize = 0;
cout << "Function size: " << hex << showbase << EntryToRead.ObjectFileSize << endl;
cout << "Function offset: " << hex << showbase << EntryToRead.DataOffset << endl;

cout << "Function size: " << EntryToRead.ObjectFileSize << endl;
uint32_t newFunctionSize = 0;
bool bUndoMove = false;

if (argN == 4)
{
string str(argV[3]);
if (strcmp(argV[3], "/u") == 0)
{
bUndoMove = true;
}
else
{
string str(argV[3]);

istringstream ss(str);
istringstream ss(str);

if (str.find("0x") != string::npos)
ss >> hex >> newFunctionSize;
else
ss >> dec >> newFunctionSize;
if (str.find("0x") != string::npos)
ss >> hex >> newFunctionSize;
else
ss >> dec >> newFunctionSize;

cout << "Resize function to: " << newFunctionSize << endl;
cout << "Resize function to: " << hex << showbase << newFunctionSize << endl;

if (newFunctionSize <= EntryToRead.ObjectFileSize)
{
cerr << "Can't expand function: existing function size is greater than specified value!" << endl;
return 1;
if (newFunctionSize <= EntryToRead.ObjectFileSize)
{
cerr << "Can't expand function: existing function size is greater than specified value!" << endl;
return 1;
}
}
}

package.MoveObject(idx, newFunctionSize, true);

cout << "Object moved successfully!" << endl;
if (!bUndoMove)
{
package.MoveObject(idx, newFunctionSize);
cout << "Object moved successfully!" << endl;
}
else
{
package.UndoMoveObject(idx);
cout << "Object restored successfully!" << endl;
}

return 0;
}
Expand Down
70 changes: 65 additions & 5 deletions PatchUPK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

using namespace std;

std::string GetFilename(std::string str)
{
unsigned found = str.find_last_of("/\\");
return str.substr(found + 1);
}

int string2int(string str)
{
int val = 0;
Expand Down Expand Up @@ -61,6 +67,7 @@ int main(int argN, char* argV[])
parser.AddKeyName(string("FUNCTION_FILE"));
parser.AddKeyName(string("NAMELIST_NAME"));
parser.AddKeyName(string("EXPAND_FUNCTION"));
parser.AddKeyName(string("EXPAND_UNDO"));

parser.AddSectionName(string("[MODDED_HEX]"));
parser.AddSectionName(string("[BEFORE_HEX]"));
Expand All @@ -78,7 +85,7 @@ int main(int argN, char* argV[])
int functionIdx = -1, namelistIdx = -1, functionSize = 0;
size_t colonPos = string::npos;

std::vector<char> dataChunk;
std::vector<char> dataChunk, backupDataChunk;
fstream binFile;
size_t binFileSize = 0;

Expand All @@ -90,6 +97,11 @@ int main(int argN, char* argV[])
return 1;
}

ostringstream backupString;
backupString << "MOD_NAME=" << GetFilename(argV[1]) << " uninstall script\n"
<< "AUTHOR=PatchUPK\n"
<< "DESCRIPTION=This is automatically generated uninstall script. Do not change anything!\n\n";

while (idx != -1)
{
if (parser.IsKey())
Expand Down Expand Up @@ -125,6 +137,7 @@ int main(int argN, char* argV[])
rel_offset = 0;
functionName = "";
functionFile = "";
backupString << "UPK_FILE=" << upkFileName << "\n\n";
break;
case 4:
if (!package.good())
Expand Down Expand Up @@ -234,7 +247,7 @@ int main(int argN, char* argV[])
}
offset = package.GetObjectListEntryByIdx(functionIdx).DataOffset;
cout << "Writing new data at " << hex << showbase << offset << dec << " in " << upkFileName << endl;
if (!package.WriteObjectData(functionIdx, dataChunk))
if (!package.WriteObjectData(functionIdx, dataChunk, &backupDataChunk))
{
cerr << "Error writing to upk file: " << upkFileName << endl;
return 1;
Expand All @@ -243,12 +256,16 @@ int main(int argN, char* argV[])
else
{
cout << "Writing new data at " << hex << showbase << offset << dec << " in " << upkFileName << endl;
if (!package.WriteData(offset, dataChunk))
if (!package.WriteData(offset, dataChunk, &backupDataChunk))
{
cerr << "Error writing to upk file: " << upkFileName << endl;
return 1;
}
}
backupString << "OFFSET=" << offset << "\n"
<< "[MODDED_HEX]\n"
<< hex2str(backupDataChunk.data(), backupDataChunk.size())
<< "\n";
cout << "Write successfull!" << endl;
dataChunk.clear();
offset = 0;
Expand Down Expand Up @@ -289,6 +306,7 @@ int main(int argN, char* argV[])
cerr << "Error writing to upk file: " << upkFileName << endl;
return 1;
}
backupString << "NAMELIST_NAME=" << newName << ":" << namelistName << "\n\n";
offset = 0;
rel_offset = 0;
functionName = "";
Expand Down Expand Up @@ -322,8 +340,34 @@ int main(int argN, char* argV[])
cerr << "Existing function size is greater than specified value!" << endl;
return 1;
}
package.MoveObject(functionIdx, functionSize, true);
package.MoveObject(functionIdx, functionSize);
cout << "Function moved successfully!" << endl;
backupString << "EXPAND_UNDO=" << functionName << "\n\n";
offset = 0;
rel_offset = 0;
functionName = "";
functionFile = "";
break;
case 10:
if (!package.good())
{
cerr << "Package file not set!" << endl;
return 1;
}
functionName = parser.GetStringValue();
cout << "Undo function expand: " << functionName << endl;
functionIdx = package.FindObjectListEntryByName(functionName);
if (functionIdx <= 0)
{
cerr << "Can't find function " << functionName << " in package " << upkFileName << endl;
return 1;
}
if (!package.UndoMoveObject(functionIdx))
{
cerr << "Can't undo function expand!" << endl;
return 1;
}
cout << "Function restored successfully!" << endl;
offset = 0;
rel_offset = 0;
functionName = "";
Expand Down Expand Up @@ -362,12 +406,16 @@ int main(int argN, char* argV[])
return 1;
}
cout << "Writing new data at " << hex << showbase << offset + rel_offset << dec << " in " << upkFileName << endl;
if (!package.WriteData(offset + rel_offset, dataChunk))
if (!package.WriteData(offset + rel_offset, dataChunk, &backupDataChunk))
{
cerr << "Error writing to upk file: " << upkFileName << endl;
return 1;
}
cout << "Write successfull!" << endl;
backupString << "OFFSET=" << (offset + rel_offset) << "\n"
<< "[MODDED_HEX]\n"
<< hex2str(backupDataChunk.data(), backupDataChunk.size())
<< "\n";
if (rel_offset == 0)
offset = 0;
rel_offset = 0;
Expand Down Expand Up @@ -412,5 +460,17 @@ int main(int argN, char* argV[])
idx = parser.FindNext();
}

if (string(argV[1]).find(".uninstall.txt") == string::npos)
{
ofstream uninstFile(string(argV[1]) + ".uninstall.txt");
if (!uninstFile.good())
{
cerr << "Error saving uninstall script!" << endl;
return 1;
}
uninstFile << backupString.str();
cout << "Uninstall script saved to " << argV[1] << ".uninstall.txt" << endl;
}

return 0;
}
Loading

0 comments on commit 4a7b63b

Please sign in to comment.