Skip to content

Commit

Permalink
Add cgo support for DesyncUntar function
Browse files Browse the repository at this point in the history
  • Loading branch information
fire committed Oct 29, 2023
1 parent 5f57174 commit 1e8a039
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions godot_cgo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Godot cgo enhancement proposal

The `cgo` feature in Go programming allows you to call C code from Go and vice versa. This can be used to provide a C interface for the `desync` Go library.

## Updating the C Interface

To expose the output directory and the cache directory, you can modify the `DesyncUntar` function to accept these as parameters. Here's how you could do it:

```go
package main

/*
#include <stdlib.h>
*/
import "C"
import (
"os/exec"
"unsafe"
"errors"
"context"
)

// DesyncUntar is a C-exported function
func DesyncUntar(storeUrl *C.char, indexUrl *C.char, outputDir *C.char, cacheDir *C.char) error {
store := C.GoString(storeUrl)
index := C.GoString(indexUrl)
output := C.GoString(outputDir)
cache := C.GoString(cacheDir)

if store == "" || index == "" || output == "" {
return errors.New("storeUrl, indexUrl, and outputDir are required")
}

args := []string{"--no-same-owner", "--store", store, "--index", index, output}
if cache != "" {
args = append(args, "--cache", cache)
}

cmd := exec.CommandContext(context.Background(), "desync", args...)
err := cmd.Run()
if err != nil {
return err
}
return nil
}

func main() {}
```

## Using the Updated Static Library in C Program

You can then call this function from your C program like this:

```c
#include <stdio.h>
#include "desync_c_interface.h"

int main() {
int result = DesyncUntar("https://v-sekai.github.io/casync-v-sekai-game/store",
"https://github.com/V-Sekai/casync-v-sekai-game/raw/main/vsekai_game_windows_x86_64.caidx",
"/path/to/output/directory",
"/path/to/cache/directory");
if (result != 0) {
printf("Error: storeUrl, indexUrl, and outputDir are required\n");
}
return 0;
}
```

In this C code, the `DesyncUntar` function is called with four parameters: the store URL, the index URL, the output directory, and the cache directory.

0 comments on commit 1e8a039

Please sign in to comment.