Skip to content

Commit

Permalink
Merge commit '0426a1d4b6d02d05b15453633bf50443a75813c6' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Sep 2, 2023
2 parents 8b209f9 + 0426a1d commit 266d57b
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 13 deletions.
19 changes: 15 additions & 4 deletions src/hashutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool hashutils::Extract(LPCCH type, UINT64 hash, LPCH out, SIZE_T outSize) {
return true;
}

LPCCH hashutils::ExtractTmp(LPCCH type, UINT64 hash) {
LPCH hashutils::ExtractTmp(LPCCH type, UINT64 hash) {
ReadDefaultFile();
Extract(type, hash, g_buffer, 2048);
return g_buffer;
Expand Down Expand Up @@ -108,11 +108,22 @@ UINT32 hashutils::Hash32(LPCCH str) {
return 0x8001 * ((9 * hash) ^ ((9 * hash) >> 11));
}

UINT64 hashutils::Hash64(LPCCH str) {
UINT64 hash = 0xcbf29ce484222325LL;
UINT64 hashutils::Hash64(LPCCH str, UINT64 start) {
UINT64 hash = start;

for (LPCCH data = str; *data; data++) {
hash = hash ^ tolower(*data);
if (*data >= 'A' && *data <= 'Z') {
// to lower
hash = hash ^ (UINT8)(*data - 'A');
}
else if (*data == '\\') {
// replace path char
hash = hash ^ '/';
}
else {
hash = hash ^ *data;
}

hash *= 0x100000001b3;
}

Expand Down
73 changes: 71 additions & 2 deletions src/hashutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,85 @@
#include <Windows.h>

namespace hashutils {
/*
* Read the default hash file
*/
void ReadDefaultFile();
/*
* Load a hash file
*/
void LoadMap(LPCWCH file);
/*
* Add a hash into the map
*/
void Add(LPCCH str);
/*
* Extract a hash into a buffer
* @param type Hash type
* @param hash Hashed value
* @param out Out buffer
* @param outSize Out size
* @return if the hash was in the hash map
*/
bool Extract(LPCCH type, UINT64 hash, LPCH out, SIZE_T outSize);
LPCCH ExtractTmp(LPCCH type, UINT64 hash);
/*
* Extract a hash into a buffer
* @param type Hash type
* @param hash Hashed value
* @return non thread-safe temporary pointer to a representation of this hash, the result is valid until the next call to ExtractTmp
*/
LPCH ExtractTmp(LPCCH type, UINT64 hash);
/*
* Call ExtractTmp for a script name and apply formatting
* @param hash Script hash
* @return script formatted value, same condition as ExtractTmp
*/
inline LPCH ExtractTmpScript(UINT64 hash) {
LPCH unhash = ExtractTmp("script", hash);

// replace '/' -> '\' in script
for (LPCH script = unhash; *script; script++) {
if (*script == '/') {
*script = '\\';
}
}

return unhash;
}
/*
* Extract a pointer to a hash value
* @param hash Hashed value
* @return String to the value or NULL if the hash isn't in the hash map
*/
LPCCH ExtractPtr(UINT64 hash);
/*
* @return the size of the hash map
*/
SIZE_T Size();

/*
* Compute the hash32 on a string (canon id)
* @param str String to compute
* @return hashed value
*/
UINT32 Hash32(LPCCH str);
UINT64 Hash64(LPCCH str);
/*
* Compute the hash64 on a string (fnva1), path are unformatted
* @param str String to compute
* @param start Start value, can be a previous hash to concatenate hashes
* @return Hashed value
*/
UINT64 Hash64(LPCCH str, UINT64 start = 0xcbf29ce484222325LL);
/*
* Compute the hash32 on a string (canon id), but allow pattern like "function_123456"
* @param str String to compute
* @return Hashed value
*/
UINT32 Hash32Pattern(LPCCH str);
/*
* Compute the hash64 on a string (fnva1), but allow pattern like "hash_123456", path are unformatted
* @param str String to compute
* @return Hashed value
*/
UINT64 Hash64Pattern(LPCCH str);
}
9 changes: 5 additions & 4 deletions src/tools/gsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ int GscInfoHandleData(tool::gsc::T8GSCOBJ* data, size_t size, const char* path,
std::cerr << "Can't open output file " << asmfnamebuff << "\n";
return -1;
}
std::cout << "Decompiling into '" << asmfnamebuff << "'...\n";
if (opt.m_copyright) {
asmout << "// " << opt.m_copyright << "\n";
}
Expand Down Expand Up @@ -267,7 +268,7 @@ int GscInfoHandleData(tool::gsc::T8GSCOBJ* data, size_t size, const char* path,
UINT64 *includes = reinterpret_cast<UINT64*>(&data->magic[data->include_offset]);

for (size_t i = 0; i < data->include_count; i++) {
asmout << "#include " << hashutils::ExtractTmp("script", includes[i]) << ";\n";
asmout << "#using " << hashutils::ExtractTmpScript(includes[i]) << ";\n";
}
if (data->include_count) {
asmout << "\n";
Expand Down Expand Up @@ -1128,15 +1129,15 @@ void tool::gsc::T8GSCExport::DumpFunctionHeader(std::ostream& asmout, BYTE* gscF
if (!specialClassMember) {
asmout << "function ";
}
if (flags & T8GSCExportFlags::PRIVATE) {
asmout << "private ";
}
if (flags & T8GSCExportFlags::AUTOEXEC) {
asmout << "autoexec ";
}
if (flags & T8GSCExportFlags::EVENT) {
asmout << "event_handler[" << hashutils::ExtractTmp("event", callback_event) << "] " << std::flush;
}
if (flags & T8GSCExportFlags::PRIVATE) {
asmout << "private ";
}

if (ctx.m_opt.m_dasm && (classMember || (flags & T8GSCExportFlags::CLASS_DESTRUCTOR))) {
asmout << hashutils::ExtractTmp("class", name_space)
Expand Down
28 changes: 25 additions & 3 deletions src/tools/gsc_opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class ASMContextNodeFuncRef : public ASMContextNode {
if (m_nsp) {
out << hashutils::ExtractTmp("namespace", m_nsp) << std::flush;
if (m_script) {
out << "<" << hashutils::ExtractTmp("script", m_script) << ">" << std::flush;
out << "<" << hashutils::ExtractTmpScript(m_script) << ">" << std::flush;
}
out << "::";
}
Expand Down Expand Up @@ -2367,6 +2367,28 @@ class OPCodeInfoEnd : public OPCodeInfo {
}
};

class OPCodeInfoClearArray : public OPCodeInfo {
public:
OPCodeInfoClearArray(OPCode id, LPCCH name) : OPCodeInfo(id, name) {
}
using OPCodeInfo::OPCodeInfo;

int Dump(std::ostream& out, UINT16 value, ASMContext& context, tool::gsc::T8GSCOBJContext& objctx) const override {
if (context.m_runDecompiler) {
auto* fieldId = context.GetFieldIdASMCNode();
auto* key = context.PopASMCNode();

ASMContextNode* accessNode = new ASMContextNodeArrayAccess(fieldId, key);
context.PushASMCNode(new ASMContextNodeLeftRightOperator(accessNode, new ASMContextNodeValue<LPCCH>("undefined"), " = ", PRIORITY_SET, TYPE_SET));

context.CompleteStatement();
}
out << "\n";

return 0;
}
};

class OPCodeInfoStatement : public OPCodeInfo {
LPCCH m_operatorName;
public:
Expand Down Expand Up @@ -3059,7 +3081,7 @@ class OPCodeInfoT8CGetLazyFunction : public OPCodeInfo {
base += 16;

out << "@" << hashutils::ExtractTmp("namespace", nsp)
<< "<" << std::flush << hashutils::ExtractTmp("script", script)
<< "<" << std::flush << hashutils::ExtractTmpScript(script)
<< ">::" << std::flush << hashutils::ExtractTmp("function", function) << std::endl;

if (context.m_runDecompiler) {
Expand Down Expand Up @@ -3279,7 +3301,7 @@ void tool::gsc::opcode::RegisterOpCodes() {
RegisterOpCodeHandler(new OPCodeInfoPreScriptCall(OPCODE_PreScriptCall, "PreScriptCall"));

RegisterOpCodeHandler(new OPCodeInfoGetConstant<LPCCH>(OPCODE_EmptyArray, "EmptyArray", "[]"));
RegisterOpCodeHandler(new OPCodeInfoGetConstantSet<LPCCH>(OPCODE_ClearArray, "ClearArray", "[]", true));
RegisterOpCodeHandler(new OPCodeInfoClearArray(OPCODE_ClearArray, "ClearArray"));
RegisterOpCodeHandler(new OPCodeInfoGetConstantRef(OPCODE_GetSelfObject, "GetSelfObject", "self"));

// class stuff
Expand Down

0 comments on commit 266d57b

Please sign in to comment.