Guest User

Untitled

a guest
Feb 16th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #pragma once
  2. #include "../hookableFunction.h"
  3. #include <filesystem>
  4. #include "detours.h"
  5.  
  6. namespace fs = std::filesystem;
  7.  
  8. hookable<HANDLE(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE), stdcall_t> create_filea;
  9.  
  10. class LayerFS
  11. {
  12.     static inline std::map<fs::path, fs::path> vfs_map;
  13.  
  14.     static auto convert_to_vfs(const fs::path& in, const fs::path& path = fs::current_path()) -> fs::path
  15.     {
  16.         auto in_str = in.string();
  17.         auto path_str = path.string();
  18.         auto iter = in_str.find(path_str);
  19.  
  20.         if (iter != std::string::npos)
  21.             return fs::path(in_str.erase(iter, path_str.length()));
  22.         return fs::path(in);
  23.     }
  24.    
  25.     static auto build_vfs_tree(const fs::path& in, const fs::path& root = "/") -> void
  26.     {
  27.         for (auto& value : fs::directory_iterator(in))
  28.         {
  29.             if (value.is_directory())
  30.             {
  31.                 build_vfs_tree(value, root);
  32.                 continue;
  33.             }
  34.             auto key = convert_to_vfs(value.path(), root);
  35.             vfs_map[key] = value.path();
  36.         }
  37.     }
  38.  
  39.     static auto build_vfs() -> void
  40.     {
  41.         auto path = fs::current_path() / "mod";
  42.         if (!fs::exists(path))
  43.             return;
  44.         for (auto& mod : fs::directory_iterator(path))
  45.         {
  46.             if (!mod.is_directory())
  47.                 continue;
  48.             printf("Found mod directory %s\n", mod.path().string().c_str());
  49.             build_vfs_tree(mod, mod);
  50.             printf("Created %d File Mappings\n", vfs_map.size());
  51.         }
  52.     }
  53.  
  54.     static auto __stdcall create_file(LPCSTR a, DWORD b, DWORD c, LPSECURITY_ATTRIBUTES d, DWORD e, DWORD f, HANDLE g) -> HANDLE
  55.     {
  56.         auto which = convert_to_vfs(a);
  57.         if (vfs_map.contains(which))
  58.             return create_filea.original(vfs_map[which].string().c_str(), b, c, d, e, f, g);
  59.         return create_filea.original(a, b, c, d, e, f, g);
  60.     }
  61. public:
  62.  
  63.     static auto enable() -> void
  64.     {
  65.         build_vfs();
  66.         create_filea.reset(CreateFileA);
  67.         DetourTransactionBegin();
  68.         DetourUpdateThread(GetCurrentThread());
  69.         DetourAttach(create_filea.get(), hook(&create_file));
  70.         DetourTransactionCommit();
  71.     }
  72. };
Advertisement
Add Comment
Please, Sign In to add comment