Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Internal Calls #14

Merged
merged 15 commits into from
Jan 13, 2024
Merged

Custom Internal Calls #14

merged 15 commits into from
Jan 13, 2024

Conversation

Fexty12573
Copy link
Owner

Current API (Still up for debate)

Managed API

using SharpPluginLoader.Core;
using SharpPluginLoader.InternalCallGenerator;

[InternalCallManager]
public partial class InternalCalls2 // Name doesn't matter, but partial is required.
{
    [InternalCall]
    public static int Sum(int[] nums); // public static required.

    [InternalCall(InternalCallOptions
        .Unsafe)] // Will suppress the GC transition when invoked. Don't use on long methods.
    public static bool ProcessData(byte[] data, out int result);

    // Non-internal call methods are allowed
    public static int ProcessDatasAndSum(byte[][] datas)
    {
        List<int> results = [];
        foreach (var data in datas)
        {
            if (ProcessData(data, out var result))
            {
                results.Add(result);
            }
        }

        return Sum([..results]);
    }
}

Native API

extern "C" int Sum(int* nums) { ... }

extern "C" bool ProcessData(u8* data, int* result) { ... }

The function name needs to match exactly on the managed and native ends.

@Fexty12573 Fexty12573 added feature New feature to be implemented interop This issue is related to managed-native interop labels Jan 6, 2024
@Fexty12573
Copy link
Owner Author

Actually, I will probably change the extern "C" based API to one that requires the native component to only export 2 functions:

struct InternalCall {
    const char* Name;
    void* FuncPtr;
};

// Tells the PluginMananager how many InternalCalls to allocate
extern "C" int get_internal_call_count() { return 2; }

// Retrieves the actual call information
extern "C" void get_internal_calls(InternalCall* icalls) { 
    icalls[0] = { "Sum", &sum };
    icalls[1] = { "ProcessData", &process_data };
}

This also gives you the option to directly bind game functions to internal calls for convenience.

@Fexty12573 Fexty12573 marked this pull request as ready for review January 10, 2024 07:36
@Fexty12573 Fexty12573 merged commit 8ab9a3d into master Jan 13, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature to be implemented interop This issue is related to managed-native interop
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant