Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "../hookableFunction.h"
- #include <filesystem>
- #include "detours.h"
- namespace fs = std::filesystem;
- hookable<HANDLE(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE), stdcall_t> create_filea;
- class LayerFS
- {
- static inline std::map<fs::path, fs::path> vfs_map;
- static auto convert_to_vfs(const fs::path& in, const fs::path& path = fs::current_path()) -> fs::path
- {
- auto in_str = in.string();
- auto path_str = path.string();
- auto iter = in_str.find(path_str);
- if (iter != std::string::npos)
- return fs::path(in_str.erase(iter, path_str.length()));
- return fs::path(in);
- }
- static auto build_vfs_tree(const fs::path& in, const fs::path& root = "/") -> void
- {
- for (auto& value : fs::directory_iterator(in))
- {
- if (value.is_directory())
- {
- build_vfs_tree(value, root);
- continue;
- }
- auto key = convert_to_vfs(value.path(), root);
- vfs_map[key] = value.path();
- }
- }
- static auto build_vfs() -> void
- {
- auto path = fs::current_path() / "mod";
- if (!fs::exists(path))
- return;
- for (auto& mod : fs::directory_iterator(path))
- {
- if (!mod.is_directory())
- continue;
- printf("Found mod directory %s\n", mod.path().string().c_str());
- build_vfs_tree(mod, mod);
- printf("Created %d File Mappings\n", vfs_map.size());
- }
- }
- static auto __stdcall create_file(LPCSTR a, DWORD b, DWORD c, LPSECURITY_ATTRIBUTES d, DWORD e, DWORD f, HANDLE g) -> HANDLE
- {
- auto which = convert_to_vfs(a);
- if (vfs_map.contains(which))
- return create_filea.original(vfs_map[which].string().c_str(), b, c, d, e, f, g);
- return create_filea.original(a, b, c, d, e, f, g);
- }
- public:
- static auto enable() -> void
- {
- build_vfs();
- create_filea.reset(CreateFileA);
- DetourTransactionBegin();
- DetourUpdateThread(GetCurrentThread());
- DetourAttach(create_filea.get(), hook(&create_file));
- DetourTransactionCommit();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment