# ribshark/safediscshim architecture

> Architecture diagram of the GitHub repository ribshark/safediscshim, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ribshark/safediscshim
- Repository: https://github.com/ribshark/safediscshim

## Overview

##### Project type and purpose
- SafeDiscShim is a native Windows compatibility shim—a small DLL that injects into SafeDisc-protected game processes and intercepts calls that would normally go to the blacklisted secdrv.sys driver. It simulates the driver’s responses in user-mode so the game can start without requiring the real driver.

##### File structure
- installer/
– installer.iss (Inno Setup script)
– license.rtf
- src/
– CMakeLists.txt (build orchestration, vcpkg integration via vcpkg.json)
– dllmain.cpp (DLL entry point, initialization)
– hooks.cpp/.h (API-hooking logic)
– secdrv_ioctl.cpp/.h (mapping of IOCTL codes to emulated responses)
– process.cpp/.h (process-related helpers, injection points)
– logging.cpp/.h (log subsystem, governed by SAFEDISCSHIM_LOGLEVEL)
– pch.cpp/.h (precompiled headers)
– version.h.in (versioning stub)

##### Extract insights from the README
- No kernel drivers are installed; all “driver” behavior is emulated in user-mode.
- The tool leverages Windows API hooking to catch CreateFile/DeviceIoControl requests destined for “\\.\SecDrv”.
- Logs are emitted next to the game executable, toggleable via an environment variable.

a. Main components:
- Game Executable (process)
- SafeDiscShim DLL
– Hook Engine (initializes and installs detours)
– IOCTL Handler (secdrv_ioctl module)
– Process Helper (process module)
– Logging Subsystem
- Windows API / OS Kernel Interfaces
- Installer (Inno Setup)

b. Relationships & data flows:
1. Installer installs the DLL and any manifest/AppInit settings into the game folder.
2. Windows Loader starts the Game Executable; AppInit or similar mechanism loads SafeDiscShim.dll at process startup.
3. DllMain invokes the Hook Engine, which attaches detours to:
– CreateFileW targeting “\\.\SecDrv”
– DeviceIoControl for secdrv IOCTL codes
4. When the game issues a DeviceIoControl to “\\.\SecDrv”:
– Control flow is intercepted by the IOCTL Handler
– Handler returns a crafted response buffer that mimics the real driver
5. Logging subsystem captures each intercepted call/result and writes to disk if enabled

c. Architectural patterns & principles:
– Modular separation: hooks vs. IOCTL vs. logging vs. process logic
– Dependency management via vcpkg and CMake
– Inno Setup for packaging, keeping installer code separate from shim code

d. Key technologies/frameworks:
– C++17 (or later) on Windows
– Windows API Detours/inline hooking
– CMake + vcpkg for build & dependencies
– Inno Setup (installer.iss)

## Diagram (Mermaid)

```mermaid
graph TB
    %% Subgraphs
    subgraph "Installer" 
        direction TB
        Installer["Installer"]:::installer
        License["License File"]:::installer
    end

    subgraph "Build System" 
        direction TB
        CMake["CMakeLists.txt"]:::build
        Vcpkg["vcpkg.json"]:::build
    end

    subgraph "Game Process" 
        direction TB
        GameExe["Game Executable"]:::process
    end

    subgraph "SafeDiscShim.dll" 
        direction TB
        DLLMain["dllmain.cpp (Entry Point)"]:::shim
        HookEngine["Hook Engine<br/>(hooks.cpp/.h)"]:::shim
        IOCTLHandler["IOCTL Handler<br/>(secdrv_ioctl.cpp/.h)"]:::shim
        ProcessHelper["Process Helper<br/>(process.cpp/.h)"]:::shim
        Logging["Logging Subsystem<br/>(logging.cpp/.h)"]:::shim
        PCH["Precompiled Headers<br/>(pch.cpp/.h)"]:::shim
        VersionStub["Version Stub<br/>(version.h.in)"]:::shim
    end

    subgraph "OS Layer" 
        direction TB
        WinLoader["Windows Loader"]:::os
        WinAPI["Windows API / Kernel Interfaces"]:::os
    end

    Documentation["Project Documentation (README.md)"]:::doc

    %% Connections
    Installer -->|"deploy DLL & settings"| GameExe
    Installer --> License
    CMake -->|build orchestration| DLLMain
    Vcpkg -->|dependency management| DLLMain

    GameExe -->|loaded by| WinLoader
    WinLoader -->|injects DLL at startup| SafeDiscShim.dll
    DLLMain -->|initializes| HookEngine
    HookEngine -->|installs detours on<br/>CreateFileW & DeviceIoControl| IOCTLHandler
    GameExe -->|calls CreateFileW/DeviceIoControl| WinAPI
    WinAPI -->|intercepted| IOCTLHandler
    IOCTLHandler -->|returns emulated response| GameExe
    IOCTLHandler -->|logs event| Logging
    HookEngine -->|uses| ProcessHelper
    Logging -->|writes logs next to exe| GameExe

    Documentation --> CMake

    %% Click Events
    click Installer "https://github.com/ribshark/safediscshim/blob/main/installer/installer.iss"
    click License "https://github.com/ribshark/safediscshim/blob/main/installer/license.rtf"
    click CMake "https://github.com/ribshark/safediscshim/blob/main/src/CMakeLists.txt"
    click Vcpkg "https://github.com/ribshark/safediscshim/blob/main/src/vcpkg.json"
    click DLLMain "https://github.com/ribshark/safediscshim/blob/main/src/dllmain.cpp"
    click HookEngine "https://github.com/ribshark/safediscshim/blob/main/src/hooks.cpp"
    click HookEngine "https://github.com/ribshark/safediscshim/blob/main/src/hooks.h"
    click IOCTLHandler "https://github.com/ribshark/safediscshim/blob/main/src/secdrv_ioctl.cpp"
    click IOCTLHandler "https://github.com/ribshark/safediscshim/blob/main/src/secdrv_ioctl.h"
    click ProcessHelper "https://github.com/ribshark/safediscshim/blob/main/src/process.cpp"
    click ProcessHelper "https://github.com/ribshark/safediscshim/blob/main/src/process.h"
    click Logging "https://github.com/ribshark/safediscshim/blob/main/src/logging.cpp"
    click Logging "https://github.com/ribshark/safediscshim/blob/main/src/logging.h"
    click PCH "https://github.com/ribshark/safediscshim/blob/main/src/pch.cpp"
    click PCH "https://github.com/ribshark/safediscshim/blob/main/src/pch.h"
    click VersionStub "https://github.com/ribshark/safediscshim/blob/main/src/version.h.in"
    click Documentation "https://github.com/ribshark/safediscshim/blob/main/README.md"

    %% Styles
    classDef installer fill:#ffe699,stroke:#b58900,color:#000;
    classDef build fill:#cde5ff,stroke:#3b6ea5,color:#000;
    classDef process fill:#cce5ff,stroke:#2a5d9f,color:#000;
    classDef shim fill:#d4f8d4,stroke:#2f8f2f,color:#000;
    classDef os fill:#e0e0e0,stroke:#888888,color:#000;
    classDef doc fill:#fff2cc,stroke:#b58900,color:#000;
```
