Skip to content

Commit

Permalink
shared: more compat stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Oct 7, 2023
1 parent 9ab2baa commit 78eb47b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 49 deletions.
7 changes: 6 additions & 1 deletion server/js/compatibility/classes/voiceChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

requireBinding("shared/entity.js");

const { BaseObject } = requireBinding("server/compatibility/classes/baseObject.js");

const { extendAltEntityClass } = requireBinding("shared/compatibility/utils/classes.js");

class VoiceChannel extends alt.VoiceChannel {
constructor(...args) {
// NOTE (xLuxy): This prevents the infinite loop caused by alt.*.create
if (!args.length) return super();

const [spatial, maxDistance] = args;
return alt.VoiceChannel.create({ spatial, maxDistance });
const instance = alt.VoiceChannel.create({ spatial, maxDistance });
return extendAltEntityClass(instance, BaseObject);
}

isPlayerInChannel(player) {
Expand Down
67 changes: 67 additions & 0 deletions server/js/compatibility/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/// <reference path="../../../types/shared/index.d.ts" />
/// <reference path="../../../types/server/index.d.ts" />
/// <reference path="../../../types/client/index.d.ts" />
// import * as alt from "@altv/server";

function setSyncedMeta(key, value) {
alt.syncedMeta[key] = value;
}

function deleteSyncedMeta(key) {
delete alt.syncedMeta[key];
}

cppBindings.registerCompatibilityExport("setSyncedMeta", alt.setSyncedMeta);
cppBindings.registerCompatibilityExport("deleteSyncedMeta", alt.deleteSyncedMeta);

// Resource
cppBindings.registerCompatibilityExport("startResource", alt.Resource.start);
cppBindings.registerCompatibilityExport("stopResource", alt.Resource.stop);
cppBindings.registerCompatibilityExport("restartResource", alt.Resource.restart);

// Model
cppBindings.registerCompatibilityExport("getVehicleModelInfoByHash", alt.VehicleModelInfo.get);
cppBindings.registerCompatibilityExport("getPedModelInfoByHash", alt.PedModelInfo.get);
cppBindings.registerCompatibilityExport("getWeaponModelInfoByHash", alt.WeaponModelInfo.get);

// TODO (xLuxy): Missing from v2
// cppBindings.registerCompatibilityExport("getAmmoHashForWeaponHash", alt.we);

cppBindings.registerCompatibilityExport("getServerConfig", () => alt.serverConfig);

// Streaming
cppBindings.registerCompatibilityExport("getMaxStreamingPeds", () => alt.Streaming.maxStreamingPeds);
cppBindings.registerCompatibilityExport("setMaxStreamingPeds", (val) => (alt.Streaming.maxStreamingPeds = val));

cppBindings.registerCompatibilityExport("getMaxStreamingObjects", () => alt.Streaming.maxStreamingObjects);
cppBindings.registerCompatibilityExport("getMaxStreamingObjects", (val) => (alt.Streaming.maxStreamingObjects = val));

cppBindings.registerCompatibilityExport("getMaxStreamingVehicles", () => alt.Streaming.maxStreamingVehicles);
cppBindings.registerCompatibilityExport("setMaxStreamingVehicles", (val) => (alt.Streaming.maxStreamingVehicles = val));

cppBindings.registerCompatibilityExport("getStreamerThreadCount", () => alt.Streaming.streamerThreadCount);
cppBindings.registerCompatibilityExport("setStreamerThreadCount", (val) => (alt.Streaming.streamerThreadCount = val));

cppBindings.registerCompatibilityExport("getStreamingTickRate", () => alt.Streaming.streamingTickRate);
cppBindings.registerCompatibilityExport("setStreamingTickRate", (val) => (alt.Streaming.streamingTickRate = val));

cppBindings.registerCompatibilityExport("getStreamingDistance", () => alt.Streaming.streamingDistance);
cppBindings.registerCompatibilityExport("setStreamingDistance", (val) => (alt.Streaming.streamingDistance = val));

cppBindings.registerCompatibilityExport("getMigrationThreadCount", () => alt.Streaming.migrationThreadCount);
cppBindings.registerCompatibilityExport("setMigrationThreadCount", (val) => (alt.Streaming.migrationThreadCount = val));

cppBindings.registerCompatibilityExport("getSyncSendThreadCount", () => alt.Streaming.syncSendThreadCount);
cppBindings.registerCompatibilityExport("setSyncSendThreadCount", (val) => (alt.Streaming.syncSendThreadCount = val));

cppBindings.registerCompatibilityExport("getSyncReceiveThreadCount", () => alt.Streaming.syncReceiveThreadCount);
cppBindings.registerCompatibilityExport("setSyncReceiveThreadCount", (val) => (alt.Streaming.syncReceiveThreadCount = val));

cppBindings.registerCompatibilityExport("getMigrationTickRate", () => alt.Streaming.migrationTickRate);
cppBindings.registerCompatibilityExport("setMigrationTickRate", (val) => (alt.Streaming.migrationTickRate = val));

cppBindings.registerCompatibilityExport("getColShapeTickRate", () => alt.Streaming.colShapeTickRate);
cppBindings.registerCompatibilityExport("setColShapeTickRate", (val) => (alt.Streaming.colShapeTickRate = val));

cppBindings.registerCompatibilityExport("getMigrationDistance", () => alt.Streaming.migrationDistance);
cppBindings.registerCompatibilityExport("setMigrationDistance", (val) => (alt.Streaming.migrationDistance = val));
54 changes: 54 additions & 0 deletions server/js/compatibility/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/// <reference path="../../../types/shared/index.d.ts" />
/// <reference path="../../../types/server/index.d.ts" />
/// <reference path="../../../types/client/index.d.ts" />
// import * as alt from "@altv/shared";

/** @type {typeof import("../../../shared/js/compatibility/utils/events.js")} */
const { getEventTypeFromName, getEventArgumentConverter, isCustomEvent } = requireBinding("shared/compatibility/utils/events.js");

function emitClient(player, eventName, ...args) {
if (!(player instanceof alt.Player) || !Array.isArray(player)) return;

if (Array.isArray(player)) {
alt.Events.emitPlayers(player, eventName, ...args);
return;
}

player.emit(eventName, ...args);
}

function emitClientRaw(player, eventName, ...args) {
if (!(player instanceof alt.Player) || !Array.isArray(player)) return;

if (Array.isArray(player)) {
alt.Events.emitPlayersRaw(player, eventName, ...args);
return;
}

player.emitRaw(eventName, ...args);
}

function emitClientUnreliable(player, eventName, ...args) {
if (!(player instanceof alt.Player) || !Array.isArray(player)) return;

if (Array.isArray(player)) {
alt.Events.emitPlayersUnreliable(player, eventName, ...args);
return;
}

player.emitUnreliable(eventName, ...args);
}

cppBindings.registerCompatibilityExport("emitClient", emitClient);
cppBindings.registerCompatibilityExport("emitClientRaw", emitClientRaw);
cppBindings.registerCompatibilityExport("emitClientUnreliable", emitClientUnreliable);

cppBindings.registerCompatibilityExport("emitAllClients", alt.Events.emitAllPlayers);
cppBindings.registerCompatibilityExport("emitAllClientsRaw", alt.Events.emitAllPlayersRaw);
cppBindings.registerCompatibilityExport("emitAllClientsUnreliable", alt.Events.emitAllPlayersUnreliableRaw);

// TODO (xLuxy)
// export function offClient(eventName: string, listener: (player: Player, ...args: any[]) => void): void;
// export function offClient(listener: (eventName: string, ...args: any[]) => void): void;
// export function onClient(listener: (eventName: string, player: Player, ...args: any[]) => void): void;
// export function onceClient(listener: (eventName: string, player: Player, ...args: any[]) => void): void;
49 changes: 1 addition & 48 deletions shared/js/compatibility/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/** @type {typeof import("./utils/events.js")} */
const { getEventTypeFromName, getEventArgumentConverter, isCustomEvent } = requireBinding("shared/compatibility/utils/events.js");

const eventMap = new Map();
export const eventMap = new Map();

/**
*
Expand Down Expand Up @@ -113,54 +113,7 @@ alt.Events.onEvent(async (ctx) => {
}
});

function emitClient(player, eventName, ...args) {
if (!(player instanceof alt.Player) || !Array.isArray(player)) return;

if (Array.isArray(player)) {
alt.Events.emitPlayers(player, eventName, ...args);
return;
}

player.emit(eventName, ...args);
}

function emitClientRaw(player, eventName, ...args) {
if (!(player instanceof alt.Player) || !Array.isArray(player)) return;

if (Array.isArray(player)) {
alt.Events.emitPlayersRaw(player, eventName, ...args);
return;
}

player.emitRaw(eventName, ...args);
}

function emitClientUnreliable(player, eventName, ...args) {
if (!(player instanceof alt.Player) || !Array.isArray(player)) return;

if (Array.isArray(player)) {
alt.Events.emitPlayersUnreliable(player, eventName, ...args);
return;
}

player.emitUnreliable(eventName, ...args);
}

cppBindings.registerCompatibilityExport("on", on);
cppBindings.registerCompatibilityExport("once", once);
cppBindings.registerCompatibilityExport("off", off);
cppBindings.registerCompatibilityExport("getEventListeners", getEventListeners);

cppBindings.registerCompatibilityExport("emitClient", emitClient);
cppBindings.registerCompatibilityExport("emitClientRaw", emitClientRaw);
cppBindings.registerCompatibilityExport("emitClientUnreliable", emitClientUnreliable);

cppBindings.registerCompatibilityExport("emitAllClients", alt.Events.emitAllPlayers);
cppBindings.registerCompatibilityExport("emitAllClientsRaw", alt.Events.emitAllPlayersRaw);
cppBindings.registerCompatibilityExport("emitAllClientsUnreliable", alt.Events.emitAllPlayersUnreliableRaw);

// TODO (xLuxy)
// export function offClient(eventName: string, listener: (player: Player, ...args: any[]) => void): void;
// export function offClient(listener: (eventName: string, ...args: any[]) => void): void;
// export function onClient(listener: (eventName: string, player: Player, ...args: any[]) => void): void;
// export function onceClient(listener: (eventName: string, player: Player, ...args: any[]) => void): void;

0 comments on commit 78eb47b

Please sign in to comment.