Skip to content

Il2CppEvent properties #261

Description

@ds5678
public static Il2CppEvent<Action> StaticEvent => new(&add_StaticEvent, &remove_StaticEvent, &(() => StaticEvent_BackingField));
public Il2CppEvent<Class, Action> InstanceEvent => new(&UnsafeInvoke_add_StaticEvent, &UnsafeInvoke_remove_StaticEvent, &((instance) => instanceStaticEvent_BackingField), this);

// Lambda functions used for brevity
// If an event doesn't have a backing field, null is used in the constructor.
public readonly unsafe struct Il2CppEvent<TDelegate>
{
    private readonly delegate*<TDelegate, void> _add;
    private readonly delegate*<TDelegate, void> _remove;
    private readonly delegate*<TDelegate> _get;

    public Il2CppEvent(delegate*<TDelegate, void> add, delegate*<TDelegate, void> remove, delegate*<TDelegate> get)
    {
        _add = add;
        _remove = remove;
        _get = get;
    }

    public static object? operator +(Il2CppEvent<TDelegate> eventInfo, TDelegate handler)
    {
        if (eventInfo._add is null)
            throw new ArgumentException("Event doesn't have an add method", nameof(eventInfo));
        eventInfo._add(handler);
        return null; // Have to return something
    }

    public static object? operator -(Il2CppEvent<TDelegate> eventInfo, TDelegate handler)
    {
        if (eventInfo._remove is null)
            throw new ArgumentException("Event doesn't have a remove method", nameof(eventInfo));
        eventInfo._remove(handler);
        return null; // Have to return something
    }

    public TDelegate? Get() => _get is not null ? _get() : default;
}
public readonly unsafe struct Il2CppEvent<TClass, TDelegate>
{
    private readonly TClass _instance;
    private readonly delegate*<TClass, TDelegate, void> _add;
    private readonly delegate*<TClass, TDelegate, void> _remove;
    private readonly delegate*<TClass, TDelegate> _get;
    public Il2CppEvent(delegate*<TClass, TDelegate, void> add, delegate*<TClass, TDelegate, void> remove, delegate*<TClass, TDelegate> get, TClass instance)
    {
        _add = add;
        _remove = remove;
        _get = get;
        _instance = instance;
    }

    public static object? operator +(Il2CppEvent<TClass, TDelegate> eventInfo, TDelegate handler)
    {
        if (eventInfo._add is null)
            throw new ArgumentException("Event doesn't have an add method", nameof(eventInfo));
        eventInfo._add(eventInfo._instance, handler);
        return null; // Have to return something
    }

    public static object? operator -(Il2CppEvent<TClass, TDelegate> eventInfo, TDelegate handler)
    {
        if (eventInfo._remove is null)
            throw new ArgumentException("Event doesn't have a remove method", nameof(eventInfo));
        eventInfo._remove(eventInfo._instance, handler);
        return null; // Have to return something
    }

    public TDelegate? Get() => _get is not null ? _get(_instance) : default;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions