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

Add core events #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/Omp.Net.CApi/Events/NativeClassEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private static int OnPlayerRequestClass(EntityId player, uint classId)
{
return 1;
}
return NativeOnPlayerRequestClass(player, classId) ? 1 : 0;
var ret = NativeOnPlayerRequestClass(player, classId);
return Unsafe.As<bool, int>(ref ret);
}
}
3 changes: 2 additions & 1 deletion api/Omp.Net.CApi/Events/NativeConsoleEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ private static unsafe int OnConsoleText(IntPtr command, IntPtr parameters, void*
unsafe
{
var senderType = *((int*)senderDataPtr + 0);
return NativeOnConsoleText(Marshal.PtrToStringAnsi(command) ?? string.Empty, Marshal.PtrToStringAnsi(parameters) ?? string.Empty, senderType) ? 1 : 0;
var ret = NativeOnConsoleText(Marshal.PtrToStringAnsi(command) ?? string.Empty, Marshal.PtrToStringAnsi(parameters) ?? string.Empty, senderType);
return *(int*)&ret;
}
}

Expand Down
3 changes: 2 additions & 1 deletion api/Omp.Net.CApi/Events/NativeCustomModelEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private static int OnPlayerRequestDownload(EntityId player, ModelDownloadType ty
{
return 1;
}
return NativeOnPlayerRequestDownload(player, type, checksum) ? 1 : 0;
var ret = NativeOnPlayerRequestDownload(player, type, checksum);
return Unsafe.As<bool, int>(ref ret);
}
}
24 changes: 16 additions & 8 deletions api/Omp.Net.CApi/Events/NativePlayerEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ private static int OnPlayerText(EntityId player, IntPtr message)
{
return 1;
}
return NativeOnPlayerText(player, Marshal.PtrToStringAnsi(message) ?? string.Empty) ? 1 : 0;
var ret = NativeOnPlayerText(player, Marshal.PtrToStringAnsi(message) ?? string.Empty);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -134,7 +135,8 @@ private static int OnPlayerCommandText(EntityId player, IntPtr message)
{
return 1;
}
return NativeOnPlayerCommandText(player, Marshal.PtrToStringAnsi(message) ?? string.Empty) ? 1 : 0;
var ret = NativeOnPlayerCommandText(player, Marshal.PtrToStringAnsi(message) ?? string.Empty);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -144,7 +146,8 @@ private static int OnPlayerShotMissed(EntityId player, PlayerBulletData bulletDa
{
return 1;
}
return NativeOnPlayerShotMissed(player, bulletData) ? 1 : 0;
var ret = NativeOnPlayerShotMissed(player, bulletData);
return Unsafe.As<bool, int>(ref ret);
}
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
private static int OnPlayerShotPlayer(EntityId player, EntityId target, PlayerBulletData bulletData)
Expand All @@ -153,7 +156,8 @@ private static int OnPlayerShotPlayer(EntityId player, EntityId target, PlayerBu
{
return 1;
}
return NativeOnPlayerShotPlayer(player, target, bulletData) ? 1 : 0;
var ret = NativeOnPlayerShotPlayer(player, target, bulletData);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -163,7 +167,8 @@ private static int OnPlayerShotVehicle(EntityId player, EntityId target, PlayerB
{
return 1;
}
return NativeOnPlayerShotVehicle(player, target, bulletData) ? 1 : 0;
var ret = NativeOnPlayerShotVehicle(player, target, bulletData);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -173,7 +178,8 @@ private static int OnPlayerShotObject(EntityId player, EntityId target, PlayerBu
{
return 1;
}
return NativeOnPlayerShotObject(player, target, bulletData) ? 1 : 0;
var ret = NativeOnPlayerShotObject(player, target, bulletData);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -183,7 +189,8 @@ private static int OnPlayerShotPlayerObject(EntityId player, EntityId target, Pl
{
return 1;
}
return NativeOnPlayerShotPlayerObject(player, target, bulletData) ? 1 : 0;
var ret = NativeOnPlayerShotPlayerObject(player, target, bulletData);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand Down Expand Up @@ -259,6 +266,7 @@ private static int OnPlayerUpdate(EntityId player, long now)
{
return 1;
}
return NativeOnPlayerUpdate(player, now) ? 1 : 0;
var ret = NativeOnPlayerUpdate(player, now);
return Unsafe.As<bool, int>(ref ret);
}
}
1 change: 0 additions & 1 deletion api/Omp.Net.CApi/Events/NativePoolEvent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Omp.Net.Shared;
using Omp.Net.Shared.Data;

namespace Omp.Net.CApi.Events;

Expand Down
6 changes: 4 additions & 2 deletions api/Omp.Net.CApi/Events/NativeTextDrawEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ private static int OnPlayerCancelTextDrawSelection(EntityId player)
{
return 1;
}
return NativeOnPlayerCancelTextDrawSelection(player) ? 1 : 0;
var ret = NativeOnPlayerCancelTextDrawSelection(player);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -45,6 +46,7 @@ private static int OnPlayerCancelPlayerTextDrawSelection(EntityId player)
{
return 1;
}
return NativeOnPlayerCancelPlayerTextDrawSelection(player) ? 1 : 0;
var ret = NativeOnPlayerCancelPlayerTextDrawSelection(player);
return Unsafe.As<bool, int>(ref ret);
}
}
18 changes: 12 additions & 6 deletions api/Omp.Net.CApi/Events/NativeVehicleEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ private static int OnVehiclePaintJob(EntityId player, EntityId vehicle, int pain
{
return 1;
}
return NativeOnVehiclePaintJob(player, vehicle, paintJob) ? 1 : 0;
var ret = NativeOnVehiclePaintJob(player, vehicle, paintJob);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -90,7 +91,8 @@ private static int OnVehicleMod(EntityId player, EntityId vehicle, int component
{
return 1;
}
return NativeOnVehicleMod(player, vehicle, component) ? 1 : 0;
var ret = NativeOnVehicleMod(player, vehicle, component);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -100,7 +102,8 @@ private static int OnVehicleRespray(EntityId player, EntityId vehicle, int color
{
return 1;
}
return NativeOnVehicleRespray(player, vehicle, color1, color2) ? 1 : 0;
var ret = NativeOnVehicleRespray(player, vehicle, color1, color2);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -122,7 +125,8 @@ private static int OnUnoccupiedVehicleUpdate(EntityId vehicle, EntityId player,
{
return 1;
}
return NativeOnUnoccupiedVehicleUpdate(vehicle, player, data) ? 1 : 0;
var ret = NativeOnUnoccupiedVehicleUpdate(vehicle, player, data);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -132,7 +136,8 @@ private static int OnTrailerUpdate(EntityId player, EntityId trailer)
{
return 1;
}
return NativeOnTrailerUpdate(player, trailer) ? 1 : 0;
var ret = NativeOnTrailerUpdate(player, trailer);
return Unsafe.As<bool, int>(ref ret);
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
Expand All @@ -142,6 +147,7 @@ private static int OnVehicleSirenStateChange(EntityId player, EntityId vehicle,
{
return 1;
}
return NativeOnVehicleSirenStateChange(player, vehicle, sirenState) ? 1 : 0;
var ret = NativeOnVehicleSirenStateChange(player, vehicle, sirenState);
return Unsafe.As<bool, int>(ref ret);
}
}
7 changes: 5 additions & 2 deletions api/Omp.Net/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

namespace Omp.Net;

public sealed class Core
public sealed partial class Core
{
public static Core Instance { get; private set; } = null!;

public CoreEvents Events { get; }
public IEntityPool<IPlayer> PlayerPool { get; }
public IEntityPool<IVehicle> VehiclePool { get; }
public ITextDrawPool GlobalTextDrawPool { get; }
Expand All @@ -21,11 +22,13 @@ internal Core(BaseEntry entry)
{
Instance = this;
this.entry = entry;
tickSchedulerFactory = entry.GetTickSchedulerFactory();

Events = new CoreEvents(this);
PlayerPool = new PlayerPool(entry.GetPlayerFactory());
VehiclePool = new VehiclePool(entry.GetVehicleFactory());
GlobalTextDrawPool = new TextDrawPool(entry.GetTextDrawFactory());
textDrawFactory = entry.GetTextDrawFactory();
tickSchedulerFactory = entry.GetTickSchedulerFactory();
tickScheduler = tickSchedulerFactory.Create(Thread.CurrentThread);

tickDelegate = tickScheduler.Tick;
Expand Down
48 changes: 48 additions & 0 deletions api/Omp.Net/CoreEvents.Checkpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Omp.Net.Entities.Player;
using Omp.Net.Shared;
using static Omp.Net.CApi.Events.NativeCheckpointEvent;

namespace Omp.Net;

public delegate void PlayerEnterCheckpointDelegate(IPlayer player);
public delegate void PlayerLeaveCheckpointDelegate(IPlayer player);
public delegate void PlayerEnterRaceCheckpointDelegate(IPlayer player);
public delegate void PlayerLeaveRaceCheckpointDelegate(IPlayer player);

public sealed partial class CoreEvents
{
public event PlayerEnterCheckpointDelegate? PlayerEnterCheckpoint;
public event PlayerLeaveCheckpointDelegate? PlayerLeaveCheckpoint;
public event PlayerEnterRaceCheckpointDelegate? PlayerEnterRaceCheckpoint;
public event PlayerLeaveRaceCheckpointDelegate? PlayerLeaveRaceCheckpoint;

private void RegisterCheckpointEvents()
{
NativeOnPlayerEnterCheckpoint += HandleNativeOnPlayerEnterCheckpoint;
NativeOnPlayerLeaveCheckpoint += HandleNativeOnPlayerLeaveCheckpoint;
NativeOnPlayerEnterRaceCheckpoint += HandleNativeOnPlayerEnterRaceCheckpoint;
NativeOnPlayerLeaveRaceCheckpoint += HandleNativeOnPlayerLeaveRaceCheckpoint;
}

private void HandleNativeOnPlayerEnterCheckpoint(EntityId player)
{
PlayerEnterCheckpoint?.Invoke(playerPool.Get(player.NativeHandle));
}

private void HandleNativeOnPlayerLeaveCheckpoint(EntityId player)
{
PlayerLeaveCheckpoint?.Invoke(playerPool.Get(player.NativeHandle));
}

private void HandleNativeOnPlayerEnterRaceCheckpoint(EntityId player)
{
PlayerEnterRaceCheckpoint?.Invoke(playerPool.Get(player.NativeHandle));
}

private void HandleNativeOnPlayerLeaveRaceCheckpoint(EntityId player)
{
PlayerLeaveRaceCheckpoint?.Invoke(playerPool.Get(player.NativeHandle));
}

}

22 changes: 22 additions & 0 deletions api/Omp.Net/CoreEvents.Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Omp.Net.Entities.Player;
using Omp.Net.Shared;
using static Omp.Net.CApi.Events.NativeClassEvent;

namespace Omp.Net;

public delegate bool PlayerRequestClassDelegate(IPlayer player, uint classId);

public sealed partial class CoreEvents
{
public event PlayerRequestClassDelegate? PlayerRequestClass;

private void RegisterClassEvents()
{
NativeOnPlayerRequestClass += HandleNativeOnPlayerRequestClass;
}

private bool HandleNativeOnPlayerRequestClass(EntityId player, uint classId)
{
return PlayerRequestClass?.Invoke(playerPool.Get(player.NativeHandle), classId) ?? true;
}
}
31 changes: 31 additions & 0 deletions api/Omp.Net/CoreEvents.CustomModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Omp.Net.Entities.Player;
using Omp.Net.Shared;
using Omp.Net.Shared.Enums;
using static Omp.Net.CApi.Events.NativeCustomModelEvent;

namespace Omp.Net;

public delegate void PlayerFinishedDownloadingDelegate(IPlayer player);
public delegate bool PlayerRequestDownloadDelegate(IPlayer player, ModelDownloadType type, uint checksum);

public sealed partial class CoreEvents
{
public event PlayerFinishedDownloadingDelegate? PlayerFinishedDownloading;
public event PlayerRequestDownloadDelegate? PlayerRequestDownload;

private void RegisterCustomModelEvents()
{
NativeOnPlayerFinishedDownloading += HandleNativeOnPlayerFinishedDownloading;
NativeOnPlayerRequestDownload += HandleNativeOnPlayerRequestDownload;
}

private void HandleNativeOnPlayerFinishedDownloading(EntityId player)
{
PlayerFinishedDownloading?.Invoke(playerPool.Get(player.NativeHandle));
}

private bool HandleNativeOnPlayerRequestDownload(EntityId player, ModelDownloadType type, uint checksum)
{
return PlayerRequestDownload?.Invoke(playerPool.Get(player.NativeHandle), type, checksum) ?? true;
}
}
23 changes: 23 additions & 0 deletions api/Omp.Net/CoreEvents.Dialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Omp.Net.Entities.Player;
using Omp.Net.Shared;
using Omp.Net.Shared.Enums;
using static Omp.Net.CApi.Events.NativeDialogEvent;

namespace Omp.Net;

public delegate void DialogResponseDelegate(IPlayer player, int dialogId, DialogResponse response, int listItem, string inputText);

public sealed partial class CoreEvents
{
public event DialogResponseDelegate? DialogResponse;

private void RegisterDialogEvents()
{
NativeOnDialogResponse += HandleNativeOnDialogResponse;
}

public void HandleNativeOnDialogResponse(EntityId player, int dialogId, DialogResponse response, int listItem, string inputText)
{
DialogResponse?.Invoke(playerPool.Get(player.NativeHandle), dialogId, response, listItem, inputText);
}
}
38 changes: 38 additions & 0 deletions api/Omp.Net/CoreEvents.GangZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Omp.Net.Entities.Player;
using Omp.Net.Shared;
using static Omp.Net.CApi.Events.NativeGangZoneEvent;

namespace Omp.Net;

public delegate void PlayerEnterGangZoneDelegate(IPlayer player, int zoneId);
public delegate void PlayerLeaveGangZoneDelegate(IPlayer player, int zoneId);
public delegate void PlayerClickGangZoneDelegate(IPlayer player, int zoneId);

public sealed partial class CoreEvents
{
public event PlayerEnterGangZoneDelegate? PlayerEnterGangZone;
public event PlayerLeaveGangZoneDelegate? PlayerLeaveGangZone;
public event PlayerClickGangZoneDelegate? PlayerClickGangZone;

private void RegisterGangZoneEvents()
{
NativeOnPlayerEnterGangZone += HandleNativeOnPlayerEnterGangZone;
NativeOnPlayerLeaveGangZone += HandleNativeOnPlayerLeaveGangZone;
NativeOnPlayerClickGangZone += HandleNativeOnPlayerClickGangZone;
}

private void HandleNativeOnPlayerEnterGangZone(EntityId player, int zoneId)
{
PlayerEnterGangZone?.Invoke(playerPool.Get(player.NativeHandle), zoneId);
}

private void HandleNativeOnPlayerLeaveGangZone(EntityId player, int zoneId)
{
PlayerLeaveGangZone?.Invoke(playerPool.Get(player.NativeHandle), zoneId);
}

private void HandleNativeOnPlayerClickGangZone(EntityId player, int zoneId)
{
PlayerClickGangZone?.Invoke(playerPool.Get(player.NativeHandle), zoneId);
}
}
Loading