Skip to content

Commit

Permalink
feat(shard): Add enum reverse lookup. (TS enum behavior)
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Dec 20, 2023
1 parent accea7a commit d16e5aa
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 15 deletions.
5 changes: 4 additions & 1 deletion shared/js/enums/audioCategories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
alt.Enums.AudioCategories = Object.freeze({
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

alt.Enums.AudioCategories = createReverseLookupObject({
X44E21C90: "0x44E21C90",
XBAD598C7: "0xBAD598C7",
XA4D158B0: "0xA4D158B0",
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/baseObjectFilterType.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
alt.Enums.BaseObjectFilterType = Object.freeze({
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

alt.Enums.BaseObjectFilterType = createReverseLookupObject({
PLAYER: 1,
VEHICLE: 2,
PED: 4,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/blipColor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.BlipColor = Object.freeze({
alt.Enums.BlipColor = createReverseLookupObject({
WHITE: 0,
RED: 1,
GREEN: 2,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/blipSprite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.BlipSprite = Object.freeze({
alt.Enums.BlipSprite = createReverseLookupObject({
HIGHER: 0,
LOWER: 1,
POLICE_PED: 2,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/configflag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.ConfigFlag = Object.freeze({
alt.Enums.ConfigFlag = createReverseLookupObject({
DISABLE_AUTO_WEAPON_SWAP: "DISABLE_AUTO_WEAPON_SWAP",
DISABLE_PED_PROP_KNOCK_OFF: "DISABLE_PED_PROP_KNOCK_OFF",
DISABLE_IDLE_CAMERA: "DISABLE_IDLE_CAMERA",
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/fileEncoding.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
alt.Enums.FileEncoding = Object.freeze({
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

alt.Enums.FileEncoding = createReverseLookupObject({
UTF8: "utf-8",
UTF16: "utf-16",
BINARY: "binary"
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/gameFont.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.GameFont = Object.freeze({
alt.Enums.GameFont = createReverseLookupObject({
CHALET_LONDON: 0,
HOUSE_SCRIPT: 1,
MONOSPACE: 2,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/keycode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.KeyCode = Object.freeze({
alt.Enums.KeyCode = createReverseLookupObject({
"KEY0": 48,
"KEY1": 49,
"KEY2": 50,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/locale.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.Locale = Object.freeze({
alt.Enums.Locale = createReverseLookupObject({
ARABIC: "ar",
BELARUSIAN: "by",
CZECH: "cz",
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/pedConfigFlag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
alt.Enums.PedConfigFlag = Object.freeze({
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

alt.Enums.PedConfigFlag = createReverseLookupObject({
NO_CRITICAL_HITS: 2,
DROWNS_IN_WATER: 3,
DISABLE_RETICULE_FIXED_LOCKON: 4,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/radioStation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
alt.Enums.RadioStation = Object.freeze({
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

alt.Enums.RadioStation = createReverseLookupObject({
LOS_SANTOS_ROCK_RADIO: 0,
NON_STOP_POP_FM: 1,
RADIO_LOS_SANTOS: 2,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/statName.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.StatName = Object.freeze({
alt.Enums.StatName = createReverseLookupObject({
STAMINA: "stamina",
STRENGTH: "strength",
LUNG_CAPACITY: "lung_capacity",
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/taskTypeIndex.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// WARNING: values can change after a game update
// if R* adds in the middle!
// This is up-to-date for b2372

// When changing, update types/client/index.d.ts aswell
alt.Enums.TaskTypeIndex = Object.freeze({
alt.Enums.TaskTypeIndex = createReverseLookupObject({
HANDS_UP: 0,
CLIMB_LADDER: 1,
EXIT_VEHICLE: 2,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/vehicleDoorState.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.VehicleDoorState = Object.freeze({
alt.Enums.VehicleDoorState = createReverseLookupObject({
CLOSED: 0,
OPENED_LEVEL_1: 1,
OPENED_LEVEL_2: 2,
Expand Down
5 changes: 4 additions & 1 deletion shared/js/enums/vehicleIndicatorLights.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @type {typeof import("../helpers/enums.js")} */
const { createReverseLookupObject } = requireBinding("shared/helpers/enums.js");

// When changing, update types/shared/index.d.ts aswell
alt.Enums.VehicleIndicatorLights = Object.freeze({
alt.Enums.VehicleIndicatorLights = createReverseLookupObject({
NONE: 0,
BLINK_LEFT: 1,
BLINK_RIGHT: 2,
Expand Down
15 changes: 15 additions & 0 deletions shared/js/helpers/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@ export function createNumericEnum(values) {

return Object.freeze(temp);
}

/**
*
* @param {Object} obj
*/
export function createReverseLookupObject(obj) {
const newObj = {};

for (const key in obj) {
newObj[obj[key]] = key;
newObj[key] = obj[key];
}

return Object.freeze(newObj);
}
2 changes: 2 additions & 0 deletions shared/src/helpers/Template.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ namespace js
for(auto& [value, key] : values)
{
obj.Set(key.data(), (int)value);
obj.Set((std::string_view)std::to_string((int)value), key.data());
}
ctx.Return(obj);
}
Expand All @@ -217,6 +218,7 @@ namespace js
{
auto& entry = values[i];
obj.Set(entry.second.data(), (int)entry.first);
obj.Set((std::string_view)std::to_string((int)entry.first), entry.second.data());
}
ctx.Return(obj);
}
Expand Down

0 comments on commit d16e5aa

Please sign in to comment.