Added Logger to TAL (#13)

* Added Logger for TAL with __VA_ARGS__ support

* Added Tatacon entries in SDL DB
* Added comments to exported config file

* Included keys for LayeredFS

* Cleaned up a bit of code in JPN39 patches
* Formatted all files using clang-format

* Force invalid cards on original readers
You can now use any cards with the original namco readers.

* Move helper functions to cpp file

---------

Co-authored-by: ptmaster <lty2008one@163.com>
This commit is contained in:
KIT!
2024-11-24 17:44:03 +01:00
committed by Farewell_
co-authored by ptmaster
parent 3933ecab00
commit d0374dca1b
22 changed files with 996 additions and 810 deletions
+45 -56
View File
@@ -3,22 +3,21 @@
#include <zlib.h>
bool useLayeredFs = false;
std::string datatableKey = "0000000000000000000000000000000000000000000000000000000000000000";
std::string fumenKey = "0000000000000000000000000000000000000000000000000000000000000000";
std::string datatableKey = "3530304242323633353537423431384139353134383346433246464231354534";
std::string fumenKey = "4434423946383537303842433443383030333843444132343339373531353830";
#define CRCPOLY 0x82F63B78
namespace patches::LayeredFs {
class RegisteredHandler {
public:
std::function<std::string(std::string, std::string)> handlerMethod;
RegisteredHandler (const std::function<std::string(std::string, std::string)> &handlerMethod) {
this->handlerMethod = handlerMethod;
}
std::function<std::string (std::string, std::string)> handlerMethod;
RegisteredHandler (const std::function<std::string (std::string, std::string)> &handlerMethod) { this->handlerMethod = handlerMethod; }
};
std::vector<RegisteredHandler *> beforeHandlers = {};
std::vector<RegisteredHandler *> afterHandlers = {};
std::vector<RegisteredHandler *> afterHandlers = {};
uint32_t
CRC32C (uint32_t crc, const unsigned char *buf, size_t len) {
@@ -161,12 +160,10 @@ EncryptFile (const std::string &input_file, const std::string &hex_key) {
bool
IsFumenEncrypted (const std::string &filename) {
// Check if the filename ends with ".bin"
if (filename.size() < 4 || filename.substr(filename.size() - 4) != ".bin") {
std::cout << "Not a Fumen file: " << filename << std::endl;
if (filename.size () < 4 || filename.substr (filename.size () - 4) != ".bin")
return true; // If it doesn't we return early, as the file we're seeing isn't a fumen !
}
std::ifstream file(filename, std::ios::binary);
std::ifstream file (filename, std::ios::binary);
file.seekg (0x214, std::ios::beg);
std::vector<unsigned char> buffer (24);
file.read (reinterpret_cast<char *> (buffer.data ()), buffer.size ());
@@ -180,7 +177,7 @@ IsFumenEncrypted (const std::string &filename) {
std::string
LayeredFsHandler (const std::string originalFileName, const std::string currentFileName) {
std::filesystem::path path (originalFileName.c_str());
std::filesystem::path path (originalFileName.c_str ());
if (!path.is_absolute ()) path = std::filesystem::absolute (path);
auto originalDataFolder = std::filesystem::current_path ().parent_path ().parent_path () / "Data" / "x64";
auto originalLayeredFsFolder = std::filesystem::current_path ().parent_path ().parent_path () / "Data_mods" / "x64";
@@ -197,22 +194,24 @@ LayeredFsHandler (const std::string originalFileName, const std::string currentF
if (std::filesystem::exists (newPath)) { // If a file exists in the datamod folder
if (IsFumenEncrypted (newPath)) { // And if it's an encrypted fumen or a different type of file, use it.
std::cout << "Redirecting " << std::filesystem::relative (path).string () << std::endl;
LogMessage (LOG_LEVEL_DEBUG, ("Redirecting " + std::filesystem::relative (path).string ()).c_str ());
return newPath;
} else { // Otherwise if it's an unencrypted fumen.
if (!std::filesystem::exists (encPath)) { // We check if we don't already have a cached file.
if (fumenKey.length () == 64) {
std::cout << "Encrypting " << std::filesystem::relative (newPath) << std::endl; // If we don't we encrypt the file
LogMessage (LOG_LEVEL_DEBUG,
("Encrypting " + std::filesystem::relative (newPath).string ()).c_str ()); // If we don't we encrypt the file
std::ifstream crc_file (newPath, std::ios::binary);
std::vector<uint8_t> crc_vector ((std::istreambuf_iterator<char> (crc_file)), std::istreambuf_iterator<char> ());
uint32_t crc = CRC32C (0, crc_vector.data (), crc_vector.size ());
WriteFile (encPath, EncryptFile (newPath, fumenKey), crc); // And we save it
} else {
std::cout << "Missing or invalid fumen key: " << std::filesystem::relative (newPath) << " couldn't be encrypted."
<< std::endl;
LogMessage (
LOG_LEVEL_ERROR,
("Missing or invalid fumen key: " + std::filesystem::relative (newPath).string () + " couldn't be encrypted.").c_str ());
encPath = path.string ();
}
} else std::cout << "Using cached file for " << std::filesystem::relative (newPath) << std::endl;
} else LogMessage (LOG_LEVEL_DEBUG, ("Using cached file for: " + std::filesystem::relative (newPath).string ()).c_str ());
return encPath;
}
}
@@ -232,58 +231,55 @@ LayeredFsHandler (const std::string originalFileName, const std::string currentF
if (!std::filesystem::exists (encPath) || crcBool) { // And if it hasn't been encrypted before
if (datatableKey.length () == 64) {
std::cout << "Encrypting " << std::filesystem::relative (json_path) << std::endl; // Encrypt the file
// Encrypt the file
LogMessage (LOG_LEVEL_DEBUG, ("Encrypting " + std::filesystem::relative (json_path).string ()).c_str ());
std::ifstream crc_file (json_path.string (), std::ios::binary);
std::vector<uint8_t> crc_vector ((std::istreambuf_iterator<char> (crc_file)), std::istreambuf_iterator<char> ());
uint32_t crc = CRC32C (0, crc_vector.data (), crc_vector.size ());
WriteFile (encPath, EncryptFile (json_path.string (), datatableKey), crc); // And save it
} else {
std::cout << "Missing or invalid datatable key: " << std::filesystem::relative (newPath) << " couldn't be encrypted."
<< std::endl;
LogMessage (
LOG_LEVEL_ERROR,
("Missing or invalid datatable key: " + std::filesystem::relative (newPath).string () + " couldn't be encrypted.").c_str ());
encPath = path.string ();
}
} else
std::cout << "Using cached file for " << std::filesystem::relative (json_path)
<< std::endl; // Otherwise use the already encrypted file.
// Otherwise use the already encrypted file.
LogMessage (LOG_LEVEL_DEBUG, ("Using cached file for: " + std::filesystem::relative (json_path).string ()).c_str ());
return encPath;
}
}
return "";
return ""; // we return an empty string, causing the rest of CreateFileAHook to not update the returned value
}
HOOK (
HANDLE, CreateFileAHook, PROC_ADDRESS ("kernel32.dll", "CreateFileA"),
LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile
) {
// std::wcout << "CreateFileA: file " << lpFileName << std::endl;
std::string originalFileName = std::string(lpFileName);
std::string currentFileName = originalFileName;
HOOK (HANDLE, CreateFileAHook, PROC_ADDRESS ("kernel32.dll", "CreateFileA"), LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) {
std::string originalFileName = std::string (lpFileName);
std::string currentFileName = originalFileName;
LogMessage (LOG_LEVEL_HOOKS, ("CreateFileA: " + originalFileName).c_str ());
if (!beforeHandlers.empty()) {
if (!beforeHandlers.empty ()) {
for (auto handler : beforeHandlers) {
std::string result = handler->handlerMethod(originalFileName, currentFileName);
std::string result = handler->handlerMethod (originalFileName, currentFileName);
if (result != "") currentFileName = result;
}
}
if (useLayeredFs) {
std::string result = LayeredFsHandler(originalFileName, currentFileName);
std::string result = LayeredFsHandler (originalFileName, currentFileName);
if (result != "") currentFileName = result;
}
}
if (!afterHandlers.empty()) {
if (!afterHandlers.empty ()) {
for (auto handler : afterHandlers) {
std::string result = handler->handlerMethod(originalFileName, currentFileName);
std::string result = handler->handlerMethod (originalFileName, currentFileName);
if (result != "") currentFileName = result;
}
}
return originalCreateFileAHook.call<HANDLE> (
currentFileName.c_str(), dwDesiredAccess, dwShareMode, lpSecurityAttributes,
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile
);
return originalCreateFileAHook.call<HANDLE> (currentFileName.c_str (), dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes, hTemplateFile);
}
// HOOK (HANDLE, CreateFileWHook, PROC_ADDRESS ("kernel32.dll", "CreateFileW"), LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
@@ -315,34 +311,27 @@ HOOK (
void
Init () {
LogMessage (LOG_LEVEL_DEBUG, "Init LayeredFs patches");
auto configPath = std::filesystem::current_path () / "config.toml";
std::unique_ptr<toml_table_t, void (*) (toml_table_t *)> config_ptr (openConfig (configPath), toml_free);
if (config_ptr) {
auto layeredFs = openConfigSection (config_ptr.get(), "layeredfs");
if (layeredFs) {
useLayeredFs = readConfigBool (layeredFs, "enabled", useLayeredFs);
datatableKey = readConfigString (layeredFs, "datatable_key", datatableKey);
fumenKey = readConfigString (layeredFs, "fumen_key", fumenKey);
}
auto layeredFs = openConfigSection (config_ptr.get (), "layeredfs");
if (layeredFs) useLayeredFs = readConfigBool (layeredFs, "enabled", useLayeredFs);
}
if (useLayeredFs) {
std::wcout << "Using LayeredFs!" << std::endl;
}
register_cipher (&aes_desc);
INSTALL_HOOK (CreateFileAHook);
// INSTALL_HOOK (CreateFileWHook);
}
void
RegisterBefore (const std::function<std::string(const std::string, const std::string)> &fileHandler) {
beforeHandlers.push_back (new RegisteredHandler(fileHandler));
RegisterBefore (const std::function<std::string (const std::string, const std::string)> &fileHandler) {
beforeHandlers.push_back (new RegisteredHandler (fileHandler));
}
void
RegisterAfter (const std::function<std::string(const std::string, const std::string)> &fileHandler) {
afterHandlers.push_back (new RegisteredHandler(fileHandler));
RegisterAfter (const std::function<std::string (const std::string, const std::string)> &fileHandler) {
afterHandlers.push_back (new RegisteredHandler (fileHandler));
}
} // namespace patches::LayeredFs