KyiroK
Denoβ€’3y agoβ€’
22 replies
Kyiro

FFI string corrupted?

Is there anything i'm doing wrong here? I'm confused on why it's crashing on setVerisonUE5 in particular. The code without FFI in C++ works fine
import { Dataminer } from "./mod.ts";

Dataminer.withLogs(true);
Dataminer.setOodle("...");

const core = new Dataminer("...");
console.log(core); // printed
core.setVersionUE5(1008); // crashes here?
console.log("set version"); // not printed

const DLL_PATH = "...";

const library = Deno.dlopen(DLL_PATH, {
    "dataminer_options_with_oodle": {
        parameters: ["buffer"],
        result: "void"
    },
    "dataminer_with_logging": {
        parameters: ["bool"],
        result: "void"
    },
    "dataminer_construct": {
        parameters: ["buffer"],
        result: "pointer"
    },
    "dataminer_set_version_ue5": {
        parameters: ["pointer", "i32"],
        result: "void"
    }
});

function encode(text: string) {
    return new TextEncoder().encode(text);
}

export class Dataminer {
    pointer: Deno.PointerValue;
    
    constructor(paksDir: string) {
        this.pointer = library.symbols.dataminer_construct(encode(paksDir));
    }
    
    checkPointer() {
        if (!this.pointer) throw new Error("pointer is null");
    }
    
    static setOodle(oodlePath: string) {
        library.symbols.dataminer_options_with_oodle(encode(oodlePath));
    }
    
    static withLogs(shouldLog: boolean) {
        library.symbols.dataminer_with_logging(shouldLog);
    }
    
    setVersionUE5(version: number) {
        this.checkPointer();
        library.symbols.dataminer_set_version_ue5(this.pointer, version);
    }
}
Was this page helpful?