Skip to content

nitanmarcel/ScriptHookVZig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ScriptHookVZig

Develop GTA V mods with the help of Zig

Requirements

Usage

Adding to build.zig.zon

zig fetch git+https://github.com/nitanmarcel/ScriptHookVZig#{LastCommitHash} -- save

Adding to build.zig

    // Shared/Dynamic library is required
    const lib = b.addSharedLibrary(.{
        .name = "{name}",
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
    });

    // Add ScriptHookVZig as a dependency
    const shvz_dep = b.dependency("shvz", .{});

    // Import is as a module
    lib.root_module.addImport("shvz", shvz_dep.module("shvz"));

    // Save the dll
    b.installArtifact(lib);
    // Copy bin/{name}.dll (zig-0.13.0 and above) / lib/{name}.dll (zig-0.12.0) to the GTA root folder where ScriptHookV.dll is located and rename the library extension to .asi: {name}.dll -> {name}.asi

Your first script

// import the module
const shvz = @import("shvz");

// Create the script main function
pub export fn scriptMain() void {}

pub const DLL_PROCESS_ATTACH: std.os.windows.DWORD = 1;
pub const DLL_THREAD_ATTACH: std.os.windows.DWORD = 2;
pub const DLL_THREAD_DETACH: std.os.windows.DWORD = 3;
pub const DLL_PROCESS_DETACH: std.os.windows.DWORD = 4;

/// Main entry point. Will be loaded by ScriptHookV.dll
pub fn DllMain(hInstance: std.os.windows.HINSTANCE, reason: std.os.windows.DWORD, lpReserved: std.os.windows.LPVOID) std.os.windows.BOOL {
    _ = lpReserved;

    switch (reason) {
        DLL_PROCESS_ATTACH => {
            // shvz.init() REQUIRED
            // It handles opening the ScriptHookV.dll library and read symbols from it
            shvz.init() catch |e| { };
            // register the script's entry point
            shvz.main.scriptRegister(hInstance, scriptMain);
        },
        DLL_PROCESS_DETACH => {
            // Unregister the script's entry point
            // Call this before shvz.deInit() to ensure taht we didn't unloaded ScriptHookV.dll library.
            shvz.main.scriptUnregister(scriptMain);
            // call shvz.deInit() REQUIRED
            // Will close the loaded ScriptHookV.dll library freeing memory
            shvz.deinit();
        },
        else => {},
    }

    return std.os.windows.TRUE;
}

Documentation

  • You can find a set of examples (C) and a readme in the ScriptHookV SDK archive and help on gtaforums.
  • Natives are documented on Github Pages
  • The example project in here only showcases the setup for this respective zig library.

Thanks

LICENSE

  • MIT