@aifivem/lib 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +34 -4
- package/src/client/action.ts +38 -0
- package/src/client/adminWorld.ts +95 -0
- package/src/client/appearance.ts +29 -0
- package/src/client/areaFollower.ts +41 -0
- package/src/client/blip.ts +79 -0
- package/src/client/cache.ts +66 -0
- package/src/client/callback.ts +70 -0
- package/src/client/controls.ts +73 -0
- package/src/client/entities.ts +83 -0
- package/src/client/entity.ts +26 -0
- package/src/client/fuel.ts +18 -0
- package/src/client/gestures.ts +18 -0
- package/src/client/headshot.ts +26 -0
- package/src/client/hud.ts +24 -0
- package/src/client/index.ts +131 -0
- package/src/client/inventory.ts +117 -0
- package/src/client/keyHints.ts +13 -0
- package/src/client/keybind.ts +163 -0
- package/src/client/localPed.ts +169 -0
- package/src/client/location.ts +54 -0
- package/src/client/mdt.ts +10 -0
- package/src/client/medical.ts +28 -0
- package/src/client/membership.ts +41 -0
- package/src/client/menu/dialogs.ts +77 -0
- package/src/client/menu/minigames.ts +79 -0
- package/src/client/menu/requests.ts +65 -0
- package/src/client/modelScan/def.ts +29 -0
- package/src/client/modelScan/objectIndex.ts +41 -0
- package/src/client/modelScan/scan.ts +69 -0
- package/src/client/modelScan/scanner.ts +164 -0
- package/src/client/netEvent.ts +40 -0
- package/src/client/notify.ts +14 -0
- package/src/client/nuiScreen.ts +87 -0
- package/src/client/player/anim.ts +98 -0
- package/src/client/player/fade.ts +17 -0
- package/src/client/player/player.ts +80 -0
- package/src/client/player/teleport.ts +87 -0
- package/src/client/playerData.ts +83 -0
- package/src/client/players.ts +44 -0
- package/src/client/points/interact.ts +144 -0
- package/src/client/points/modelHost.ts +117 -0
- package/src/client/points/modelPoint.ts +181 -0
- package/src/client/points/point.ts +159 -0
- package/src/client/points/points.ts +170 -0
- package/src/client/points/targets/adapter.ts +40 -0
- package/src/client/points/targets/interactTarget.ts +45 -0
- package/src/client/points/targets/optionShape.ts +22 -0
- package/src/client/points/targets/oxTarget.ts +42 -0
- package/src/client/points/targets/qbTarget.ts +71 -0
- package/src/client/points/targets/targets.ts +20 -0
- package/src/client/preview.ts +51 -0
- package/src/client/progress.ts +132 -0
- package/src/client/prompt.ts +26 -0
- package/src/client/prop.ts +79 -0
- package/src/client/radial.ts +16 -0
- package/src/client/raycast.ts +48 -0
- package/src/client/scriptCamera.ts +167 -0
- package/src/client/seatbelt.ts +18 -0
- package/src/client/shot.ts +29 -0
- package/src/client/streaming.ts +91 -0
- package/src/client/tickLoop.ts +18 -0
- package/src/client/vehicleKeys.ts +14 -0
- package/src/client/vehicleProps.ts +107 -0
- package/src/client/vehicles.ts +46 -0
- package/src/client/waypoint.ts +10 -0
- package/src/client/world.ts +10 -0
- package/src/client/zoneEngine.ts +174 -0
- package/src/client/zones.ts +161 -0
- package/src/fivem/byResource.ts +146 -0
- package/src/fivem/callbackNames.ts +12 -0
- package/src/fivem/exports.ts +70 -0
- package/src/fivem/index.ts +13 -0
- package/src/fivem/locale.ts +57 -0
- package/src/fivem/playerData.ts +13 -0
- package/src/fivem/scheduler/every.ts +101 -0
- package/src/fivem/scheduler/group.ts +97 -0
- package/src/fivem/scheduler/scheduler.ts +77 -0
- package/src/fivem/state/entityState.ts +75 -0
- package/src/fivem/state/globalState.ts +80 -0
- package/src/fivem/state/mirror.ts +47 -0
- package/src/fivem/state/playerState.ts +60 -0
- package/src/server/admin/descriptions.ts +57 -0
- package/src/server/admin/edit.ts +127 -0
- package/src/server/admin/jsonSchema.ts +156 -0
- package/src/server/admin/panel.ts +167 -0
- package/src/server/admin/panelFiles.ts +128 -0
- package/src/server/admin/patch.ts +46 -0
- package/src/server/admin/validate.ts +147 -0
- package/src/server/appearance.ts +28 -0
- package/src/server/areaStream/areaStream.ts +141 -0
- package/src/server/areaStream/followers.ts +154 -0
- package/src/server/areaStream/grid.ts +74 -0
- package/src/server/areaStream/options.ts +16 -0
- package/src/server/banking.ts +15 -0
- package/src/server/bans.ts +47 -0
- package/src/server/billing.ts +20 -0
- package/src/server/buckets.ts +12 -0
- package/src/server/callback.ts +112 -0
- package/src/server/characters.ts +129 -0
- package/src/server/command/addCommand.ts +83 -0
- package/src/server/command/command.ts +116 -0
- package/src/server/command/commandSet.ts +74 -0
- package/src/server/dataFile.ts +81 -0
- package/src/server/database.ts +117 -0
- package/src/server/definitions.ts +65 -0
- package/src/server/dispatch.ts +13 -0
- package/src/server/doors.ts +15 -0
- package/src/server/ems.ts +15 -0
- package/src/server/fuel.ts +15 -0
- package/src/server/garages.ts +34 -0
- package/src/server/gate.ts +148 -0
- package/src/server/index.ts +165 -0
- package/src/server/inventory.ts +106 -0
- package/src/server/jobs.ts +42 -0
- package/src/server/log.ts +21 -0
- package/src/server/mdt.ts +12 -0
- package/src/server/medical.ts +16 -0
- package/src/server/minigameFor.ts +65 -0
- package/src/server/money.ts +70 -0
- package/src/server/netEntity.ts +19 -0
- package/src/server/netEvent.ts +58 -0
- package/src/server/notify.ts +17 -0
- package/src/server/online/mirror.ts +176 -0
- package/src/server/online/online.ts +190 -0
- package/src/server/pending.ts +70 -0
- package/src/server/phone.ts +22 -0
- package/src/server/players.ts +197 -0
- package/src/server/position.ts +31 -0
- package/src/server/primeTime.ts +7 -0
- package/src/server/prison.ts +15 -0
- package/src/server/progressFor.ts +62 -0
- package/src/server/proximity.ts +98 -0
- package/src/server/rateLimit.ts +53 -0
- package/src/server/state.ts +45 -0
- package/src/server/stress.ts +25 -0
- package/src/server/vehicle/nearby.ts +66 -0
- package/src/server/vehicle/plate.ts +14 -0
- package/src/server/vehicle/spawn.ts +125 -0
- package/src/server/vehicle/vehicleType.ts +50 -0
- package/src/server/vehicleKeys.ts +23 -0
- package/src/server/watchers.ts +46 -0
- package/src/server/world.ts +16 -0
- package/src/shared/access.ts +94 -0
- package/src/shared/action.ts +28 -0
- package/src/shared/blipStyle.ts +26 -0
- package/src/shared/clock.ts +22 -0
- package/src/shared/format.ts +36 -0
- package/src/shared/grid.ts +20 -0
- package/src/shared/index.ts +179 -0
- package/src/shared/lang.ts +39 -0
- package/src/shared/placePed.ts +96 -0
- package/src/shared/random.ts +11 -0
- package/src/shared/read.ts +60 -0
- package/src/shared/records.ts +148 -0
- package/src/shared/spec.ts +20 -0
- package/src/shared/strict.ts +70 -0
- package/src/shared/vec.ts +48 -0
- package/src/shared/vehicleProps.ts +138 -0
- package/src/shared/wait.ts +27 -0
- package/src/shared/zone.ts +144 -0
- package/README.md +0 -3
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { mysql } from "@aifivem/mysql";
|
|
2
|
+
import { createLogger } from "../shared";
|
|
3
|
+
|
|
4
|
+
const log = createLogger("aicore-lib");
|
|
5
|
+
|
|
6
|
+
interface TableStatement {
|
|
7
|
+
table: string;
|
|
8
|
+
sql: string;
|
|
9
|
+
columns: Map<string, string>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const CREATE_TABLE =
|
|
13
|
+
/^CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+`?([A-Za-z0-9_]+)`?\s*\(/i;
|
|
14
|
+
const NON_COLUMN_LINE =
|
|
15
|
+
/^(PRIMARY|UNIQUE|KEY|INDEX|CONSTRAINT|FOREIGN|FULLTEXT|SPATIAL|CHECK)\b/i;
|
|
16
|
+
|
|
17
|
+
function stripComments(sql: string): string {
|
|
18
|
+
return sql.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*--.*$/gm, "");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Splits a table body on commas that are not inside parentheses or quotes. */
|
|
22
|
+
function splitTopLevel(body: string): string[] {
|
|
23
|
+
const parts: string[] = [];
|
|
24
|
+
let depth = 0;
|
|
25
|
+
let quote: string | null = null;
|
|
26
|
+
let current = "";
|
|
27
|
+
|
|
28
|
+
for (const char of body) {
|
|
29
|
+
if (quote) {
|
|
30
|
+
if (char === quote) quote = null;
|
|
31
|
+
} else if (char === "'" || char === '"' || char === "`") {
|
|
32
|
+
quote = char;
|
|
33
|
+
} else if (char === "(") {
|
|
34
|
+
depth++;
|
|
35
|
+
} else if (char === ")") {
|
|
36
|
+
depth--;
|
|
37
|
+
} else if (char === "," && depth === 0) {
|
|
38
|
+
parts.push(current.trim());
|
|
39
|
+
current = "";
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
current += char;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (current.trim()) parts.push(current.trim());
|
|
46
|
+
return parts;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function parseStatement(sql: string): TableStatement | null {
|
|
50
|
+
const match = CREATE_TABLE.exec(sql);
|
|
51
|
+
if (!match?.[1]) return null;
|
|
52
|
+
|
|
53
|
+
const open = sql.indexOf("(");
|
|
54
|
+
const close = sql.lastIndexOf(")");
|
|
55
|
+
const columns = new Map<string, string>();
|
|
56
|
+
|
|
57
|
+
for (const part of splitTopLevel(sql.slice(open + 1, close))) {
|
|
58
|
+
if (NON_COLUMN_LINE.test(part)) continue;
|
|
59
|
+
const name = /^`?([A-Za-z0-9_]+)`?/.exec(part)?.[1];
|
|
60
|
+
if (name) columns.set(name.toLowerCase(), part);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return { table: match[1], sql, columns };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function existingColumns(table: string): Promise<Set<string> | null> {
|
|
67
|
+
const rows = await mysql.query<{ COLUMN_NAME: string }[]>(
|
|
68
|
+
"SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?",
|
|
69
|
+
[table],
|
|
70
|
+
);
|
|
71
|
+
if (!rows || rows.length === 0) return null;
|
|
72
|
+
return new Set(rows.map((row) => row.COLUMN_NAME.toLowerCase()));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Creates missing tables and adds missing columns from a resource's sql file (`file` is a path inside
|
|
77
|
+
* `resource`). Never drops, renames or modifies existing columns and never adds keys to existing tables.
|
|
78
|
+
* Waits for the database connection first.
|
|
79
|
+
*/
|
|
80
|
+
export async function createDatabaseTables(
|
|
81
|
+
resource: string,
|
|
82
|
+
file: string,
|
|
83
|
+
): Promise<void> {
|
|
84
|
+
await mysql.awaitConnection();
|
|
85
|
+
const content = LoadResourceFile(resource, file);
|
|
86
|
+
if (!content) throw new Error(`${resource}/${file} not found`);
|
|
87
|
+
|
|
88
|
+
const statements = stripComments(content)
|
|
89
|
+
.split(/;\s*$/m)
|
|
90
|
+
.map((sql) => sql.trim())
|
|
91
|
+
.filter(Boolean);
|
|
92
|
+
|
|
93
|
+
for (const sql of statements) {
|
|
94
|
+
const statement = parseStatement(sql);
|
|
95
|
+
if (!statement) {
|
|
96
|
+
log.warn(
|
|
97
|
+
`${resource}/${file}: skipped statement (only CREATE TABLE IF NOT EXISTS is applied)`,
|
|
98
|
+
);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const columns = await existingColumns(statement.table);
|
|
103
|
+
if (!columns) {
|
|
104
|
+
await mysql.query(statement.sql);
|
|
105
|
+
log.info(`^2created table ${statement.table}^7 (${resource})`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
for (const [name, definition] of statement.columns) {
|
|
110
|
+
if (columns.has(name)) continue;
|
|
111
|
+
await mysql.query(
|
|
112
|
+
`ALTER TABLE \`${statement.table}\` ADD COLUMN ${definition}`,
|
|
113
|
+
);
|
|
114
|
+
log.info(`^2added column ${statement.table}.${name}^7 (${resource})`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { resourceExport } from "../fivem/exports";
|
|
2
|
+
import {
|
|
3
|
+
CORE,
|
|
4
|
+
type CharacterResult,
|
|
5
|
+
type GangDefinition,
|
|
6
|
+
type JobDefinition,
|
|
7
|
+
} from "@aifivem/shared";
|
|
8
|
+
|
|
9
|
+
const core = resourceExport("ai-core");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* ai-core's job / gang definitions for any resource: each fetched once from ai-core, forgotten when the panel
|
|
13
|
+
* changes it (`CORE.definitionsChangedEvent`), so a read is a Map lookup, not an export copy.
|
|
14
|
+
*/
|
|
15
|
+
class Definitions {
|
|
16
|
+
private readonly jobs = new Map<string, JobDefinition | null>();
|
|
17
|
+
private readonly gangs = new Map<string, GangDefinition | null>();
|
|
18
|
+
|
|
19
|
+
constructor() {
|
|
20
|
+
on(CORE.definitionsChangedEvent, (file: unknown, name: unknown) => {
|
|
21
|
+
if (typeof name !== "string") return;
|
|
22
|
+
if (file === "jobs") this.jobs.delete(name);
|
|
23
|
+
else if (file === "gangs") this.gangs.delete(name);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
job(name: string): JobDefinition | null {
|
|
28
|
+
let def = this.jobs.get(name);
|
|
29
|
+
if (def === undefined)
|
|
30
|
+
this.jobs.set(name, (def = core.GetJobDefinition(name) ?? null));
|
|
31
|
+
return def;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Registers a job of the calling resource (gone when it stops, never saved to the panel); refused with an ai-core
|
|
36
|
+
* lang key when the panel has a job of that name or another resource registered it.
|
|
37
|
+
*/
|
|
38
|
+
registerJob(name: string, definition: JobDefinition): CharacterResult {
|
|
39
|
+
return core.RegisterJobDefinition(name, definition);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Removes a job this resource registered; false otherwise. */
|
|
43
|
+
removeJob(name: string): boolean {
|
|
44
|
+
return core.RemoveJobDefinition(name);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Registers a gang of the calling resource, like `registerJob`. */
|
|
48
|
+
registerGang(name: string, definition: GangDefinition): CharacterResult {
|
|
49
|
+
return core.RegisterGangDefinition(name, definition);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Removes a gang this resource registered; false otherwise. */
|
|
53
|
+
removeGang(name: string): boolean {
|
|
54
|
+
return core.RemoveGangDefinition(name);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
gang(name: string): GangDefinition | null {
|
|
58
|
+
let def = this.gangs.get(name);
|
|
59
|
+
if (def === undefined)
|
|
60
|
+
this.gangs.set(name, (def = core.GetGangDefinition(name) ?? null));
|
|
61
|
+
return def;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const definitions = new Definitions();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CORE, type DispatchAlert } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Police (and other job) alerts through ai-core's dispatch layer: the active alert system (ai-policealert's alert cards,
|
|
8
|
+
* tgiann-policealert, or another the panel picks) shows them to on-duty members of `jobs` (default the police jobs).
|
|
9
|
+
*/
|
|
10
|
+
export const dispatch = {
|
|
11
|
+
/** Sends an alert; false when no alert system runs or the alert is invalid. */
|
|
12
|
+
alert: (alert: DispatchAlert): boolean => core.SendDispatchAlert(alert),
|
|
13
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CORE } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/** Door locks through ai-core's door lock layer, by the active door lock script's own door id (ai-doorlock's panel id...). */
|
|
7
|
+
export const doors = {
|
|
8
|
+
/** Locks or unlocks a door; `by` = the player acting (scripts without a server API need one). False when nothing took it. */
|
|
9
|
+
setLocked: (id: string, locked: boolean, by?: number): boolean =>
|
|
10
|
+
core.SetDoorLocked(id, locked, by),
|
|
11
|
+
/** Whether a door is locked; null when unknown (no door lock, no such door, the script cannot tell on the server). */
|
|
12
|
+
isLocked: (id: string): boolean | null => core.IsDoorLocked(id),
|
|
13
|
+
/** The door lock script that answers, or null when none runs. */
|
|
14
|
+
provider: (): string | null => core.GetDoorlockSystem(),
|
|
15
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CORE, type DistressCall } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/** Medics through ai-core's EMS layer: an EMS script's own count and distress calls, else ai-core's duty counts and alerts. */
|
|
7
|
+
export const ems = {
|
|
8
|
+
/** Medics free to take a call in `jobs` (busy ones left out when a dispatch script knows them). */
|
|
9
|
+
available: (jobs: string[]): number => core.GetAvailableMedics(jobs),
|
|
10
|
+
/** Sends a distress call for a player to the medics of `call.jobs`; false when nothing took it. */
|
|
11
|
+
distress: (source: number, call: DistressCall): boolean =>
|
|
12
|
+
core.SendDistress(source, call),
|
|
13
|
+
/** The EMS source the layer uses ("ai-core" for its own duty counts). */
|
|
14
|
+
provider: (): string | null => core.GetEmsSystem(),
|
|
15
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CORE } from "@aifivem/shared";
|
|
2
|
+
import { resourceExport } from "../fivem/exports";
|
|
3
|
+
|
|
4
|
+
const core = resourceExport(CORE.resource);
|
|
5
|
+
|
|
6
|
+
/** A vehicle's fuel (0-100) through ai-core's fuel layer, whatever fuel script runs (ai-fuel, ox_fuel, LegacyFuel...). */
|
|
7
|
+
export const fuel = {
|
|
8
|
+
/** The level of a vehicle (server entity), null when it has none. */
|
|
9
|
+
get: (vehicle: number): number | null => core.GetVehicleFuel(vehicle),
|
|
10
|
+
/** Sets the level (clamped to 0-100); false for a wrong vehicle. */
|
|
11
|
+
set: (vehicle: number, level: number): boolean =>
|
|
12
|
+
core.SetVehicleFuel(vehicle, level),
|
|
13
|
+
/** The fuel script that answers, or null when none runs. */
|
|
14
|
+
provider: (): string | null => core.GetFuelSystem(),
|
|
15
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CORE,
|
|
3
|
+
toGaragedVehicles,
|
|
4
|
+
type GaragedVehicle,
|
|
5
|
+
} from "@aifivem/shared";
|
|
6
|
+
import { resourceExport } from "../fivem/exports";
|
|
7
|
+
|
|
8
|
+
const core = resourceExport(CORE.resource);
|
|
9
|
+
|
|
10
|
+
/** Garages through ai-core's garage layer, whatever garage script runs; each script answers only what it exposes. */
|
|
11
|
+
export const garages = {
|
|
12
|
+
/** Sends a vehicle (server entity) to the impound, or opens the script's impound form for `by`; false when nothing took it (delete it yourself). */
|
|
13
|
+
impound: (vehicle: number, by?: number): boolean =>
|
|
14
|
+
core.ImpoundVehicle(vehicle, by),
|
|
15
|
+
/** Stores a vehicle in a garage (`garage` = the script's garage id); false when nothing took it. */
|
|
16
|
+
store: (vehicle: number, garage?: string | null, by?: number): boolean =>
|
|
17
|
+
core.StoreVehicleInGarage(vehicle, garage ?? null, by),
|
|
18
|
+
/** Opens the garage menu of the active script for a player; false when it has none. */
|
|
19
|
+
open: (source: number, garage?: string | null): boolean =>
|
|
20
|
+
core.OpenGarage(source, garage ?? null),
|
|
21
|
+
/** A player's vehicles in garages or the impound; null when the script cannot tell or ai-core is stopped. */
|
|
22
|
+
stored: (source: number): Promise<GaragedVehicle[] | null> =>
|
|
23
|
+
new Promise((resolve) => {
|
|
24
|
+
try {
|
|
25
|
+
core.GetGaragedVehicles(source, (list) =>
|
|
26
|
+
resolve(toGaragedVehicles(list)),
|
|
27
|
+
);
|
|
28
|
+
} catch {
|
|
29
|
+
resolve(null);
|
|
30
|
+
}
|
|
31
|
+
}),
|
|
32
|
+
/** The garage script that answers, or null when none runs. */
|
|
33
|
+
provider: (): string | null => core.GetGarageSystem(),
|
|
34
|
+
};
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { ADMIN, CORE } from "@aifivem/shared";
|
|
2
|
+
import { createLogger, logSafe, previewJson } from "../shared";
|
|
3
|
+
import type { Vec3 } from "../shared/vec";
|
|
4
|
+
import { resourceExport } from "../fivem/exports";
|
|
5
|
+
import { isNear } from "./position";
|
|
6
|
+
import { RateLimit, SourceLock } from "./rateLimit";
|
|
7
|
+
|
|
8
|
+
const core = resourceExport(CORE.resource);
|
|
9
|
+
|
|
10
|
+
const log = createLogger(GetCurrentResourceName());
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A check a player must pass before a net event or callback runs: `true` passes, `false` refuses quietly,
|
|
14
|
+
* a string refuses and is logged as the reason.
|
|
15
|
+
*/
|
|
16
|
+
export type Guard = (source: number) => boolean | string;
|
|
17
|
+
|
|
18
|
+
/** Options of every client input (net event or callback). */
|
|
19
|
+
export interface ClientInputOptions {
|
|
20
|
+
/** Run in order; the first that does not pass refuses the call. */
|
|
21
|
+
guards?: Guard[];
|
|
22
|
+
/** At most `count` calls per player inside `windowMs` (events default 10 per second, callbacks 20). */
|
|
23
|
+
rate?: { count: number; windowMs: number };
|
|
24
|
+
/** One call at a time per player: a second one while the first still runs is dropped (async writes). */
|
|
25
|
+
lock?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Options of an input whose handler takes arguments: `args` turns the raw ones into checked values (`null` refuses). */
|
|
29
|
+
export interface ParsedInputOptions<
|
|
30
|
+
A extends readonly unknown[],
|
|
31
|
+
> extends ClientInputOptions {
|
|
32
|
+
args: (raw: unknown[]) => A | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The player is in ai-core's admin group (ACE `ai.admin` unless another ace is named). */
|
|
36
|
+
export const isAdmin =
|
|
37
|
+
(ace: string = ADMIN.ace): Guard =>
|
|
38
|
+
(source) =>
|
|
39
|
+
Boolean(IsPlayerAceAllowed(String(source), ace)) || `not allowed (${ace})`;
|
|
40
|
+
|
|
41
|
+
/** The player has a character in the world (not on the character screen). */
|
|
42
|
+
export const hasCharacter = (): Guard => (source) =>
|
|
43
|
+
core.GetCharacter(source) !== null || "no character loaded";
|
|
44
|
+
|
|
45
|
+
/** The player's ped is within `metres` of a point (or of the point a function returns for this player). */
|
|
46
|
+
export const near =
|
|
47
|
+
(point: Vec3 | ((source: number) => Vec3 | null), metres: number): Guard =>
|
|
48
|
+
(source) => {
|
|
49
|
+
const target = typeof point === "function" ? point(source) : point;
|
|
50
|
+
if (!target) return "no target point";
|
|
51
|
+
return isNear(source, target, metres) || `too far (${metres} m)`;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** One refusal line per player and input in this window; the refusals in between are counted into the next line. */
|
|
55
|
+
const REFUSAL_LOG_MS = 10_000;
|
|
56
|
+
|
|
57
|
+
/** Every gate of this resource; one playerDropped listener frees them all (one listener per gate trips Node's limit). */
|
|
58
|
+
const gates = new Set<{ release(source: number): void }>();
|
|
59
|
+
on("playerDropped", () => {
|
|
60
|
+
const source = Number(globalThis.source);
|
|
61
|
+
for (const gate of gates) gate.release(source);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The checks every client input passes, in this order: a real player sent it (not the resource itself), rate,
|
|
66
|
+
* guards, arguments. Refusals with a reason (and rate-limit refusals) are logged with the player and the input's name,
|
|
67
|
+
* at most one line per player per 10 s.
|
|
68
|
+
*/
|
|
69
|
+
export class ClientGate<A extends readonly unknown[]> {
|
|
70
|
+
private readonly limit: RateLimit;
|
|
71
|
+
private readonly lock: SourceLock | null;
|
|
72
|
+
/** Per player: when the last refusal was logged and how many were skipped since. */
|
|
73
|
+
private readonly refusals = new Map<
|
|
74
|
+
number,
|
|
75
|
+
{ at: number; skipped: number }
|
|
76
|
+
>();
|
|
77
|
+
|
|
78
|
+
constructor(
|
|
79
|
+
private readonly name: string,
|
|
80
|
+
private readonly options: ClientInputOptions & {
|
|
81
|
+
args?: (raw: unknown[]) => A | null;
|
|
82
|
+
},
|
|
83
|
+
defaultRate: number,
|
|
84
|
+
) {
|
|
85
|
+
this.limit = new RateLimit(
|
|
86
|
+
options.rate?.count ?? defaultRate,
|
|
87
|
+
options.rate?.windowMs ?? 1_000,
|
|
88
|
+
);
|
|
89
|
+
this.lock = options.lock ? new SourceLock() : null;
|
|
90
|
+
gates.add(this);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The checked arguments, or null when the call must not run. */
|
|
94
|
+
check(source: number, raw: unknown[]): A | null {
|
|
95
|
+
// A local emit of the same name has no player source.
|
|
96
|
+
if (!(source > 0) || !GetPlayerName(String(source))) return null;
|
|
97
|
+
if (!this.limit.take(source)) {
|
|
98
|
+
this.refuse(source, "rate limit");
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
for (const guard of this.options.guards ?? []) {
|
|
102
|
+
const verdict = guard(source);
|
|
103
|
+
if (verdict === true) continue;
|
|
104
|
+
if (typeof verdict === "string") this.refuse(source, verdict);
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
if (!this.options.args) return [] as unknown as A;
|
|
108
|
+
const args = this.options.args(raw);
|
|
109
|
+
if (args === null)
|
|
110
|
+
this.refuse(
|
|
111
|
+
source,
|
|
112
|
+
() => `invalid arguments ${logSafe(previewJson(raw, 200), 200)}`,
|
|
113
|
+
);
|
|
114
|
+
return args;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Runs `task` under the per-player lock when the input has one; `busy` when another call still runs. */
|
|
118
|
+
run<T, B>(
|
|
119
|
+
source: number,
|
|
120
|
+
busy: B,
|
|
121
|
+
task: () => Promise<T> | T,
|
|
122
|
+
): Promise<T | B> {
|
|
123
|
+
return this.lock
|
|
124
|
+
? this.lock.run(source, busy, async () => task())
|
|
125
|
+
: Promise.resolve(task());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
release(source: number): void {
|
|
129
|
+
this.limit.release(source);
|
|
130
|
+
this.lock?.release(source);
|
|
131
|
+
this.refusals.delete(source);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Logs a refusal, at most one line per player per window (a hostile client firing every input stays readable). */
|
|
135
|
+
private refuse(source: number, reason: string | (() => string)): void {
|
|
136
|
+
const now = GetGameTimer();
|
|
137
|
+
const last = this.refusals.get(source);
|
|
138
|
+
if (last && now - last.at < REFUSAL_LOG_MS) {
|
|
139
|
+
last.skipped++;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const more = last?.skipped ? ` (+${last.skipped} more refused)` : "";
|
|
143
|
+
this.refusals.set(source, { at: now, skipped: 0 });
|
|
144
|
+
log.warn(
|
|
145
|
+
`${this.name} refused for ${GetPlayerName(String(source))} (${source}): ${typeof reason === "string" ? reason : reason()}${more}`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/** Server-only helpers. Import from "@aifivem/lib/server". */
|
|
2
|
+
|
|
3
|
+
import type { AiPlayerData } from "@aifivem/shared";
|
|
4
|
+
import {
|
|
5
|
+
PLAYER_DATA_EVENT,
|
|
6
|
+
PLAYER_DATA_PATCH_EVENT,
|
|
7
|
+
type PlayerDataKey,
|
|
8
|
+
} from "../fivem/playerData";
|
|
9
|
+
|
|
10
|
+
/** The player who sent the net event being handled. Read it before the first `await`: FiveM resets it. */
|
|
11
|
+
export function eventSource(): number {
|
|
12
|
+
return Number(globalThis.source);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Whether the player is still connected. */
|
|
16
|
+
export function isOnline(source: number): boolean {
|
|
17
|
+
return Boolean(GetPlayerName(String(source)));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Runs database writes one after another (memory is the truth, the table follows in order). A failed write is
|
|
22
|
+
* logged with `label` and does not stop the ones after it. `idle()` resolves when all queued writes are done.
|
|
23
|
+
*/
|
|
24
|
+
export class WriteQueue {
|
|
25
|
+
private tail: Promise<void> = Promise.resolve();
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
private readonly onError: (label: string, error: unknown) => void,
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
push(label: string, task: () => Promise<unknown>): void {
|
|
32
|
+
this.tail = this.tail.then(task).then(
|
|
33
|
+
() => undefined,
|
|
34
|
+
(error: unknown) => this.onError(label, error),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
idle(): Promise<void> {
|
|
39
|
+
return this.tail;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Pushes one value of `AiPlayerData` to its owner's client (`onPlayerData` there). Only ai-core calls this. */
|
|
44
|
+
export function sendPlayerData<K extends PlayerDataKey>(
|
|
45
|
+
source: number,
|
|
46
|
+
key: K,
|
|
47
|
+
value: AiPlayerData[K],
|
|
48
|
+
): void {
|
|
49
|
+
emitNet(PLAYER_DATA_EVENT, source, key, value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Pushes only the changed entries of a map value (money); the client merges them into what it has. */
|
|
53
|
+
export function patchPlayerData<K extends PlayerDataKey>(
|
|
54
|
+
source: number,
|
|
55
|
+
key: K,
|
|
56
|
+
patch: Partial<AiPlayerData[K]>,
|
|
57
|
+
): void {
|
|
58
|
+
emitNet(PLAYER_DATA_PATCH_EVENT, source, key, patch);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export { RateLimit, SourceLock } from "./rateLimit";
|
|
62
|
+
export { DataFile } from "./dataFile";
|
|
63
|
+
export { AdminPanel } from "./admin/panel";
|
|
64
|
+
export { aiLog, aiLogChannels } from "./log";
|
|
65
|
+
export { notify } from "./notify";
|
|
66
|
+
export { inventory } from "./inventory";
|
|
67
|
+
export { online, type JobStateChange } from "./online/online";
|
|
68
|
+
export { vehicleKeys } from "./vehicleKeys";
|
|
69
|
+
export { vehicleType } from "./vehicle/vehicleType";
|
|
70
|
+
export { spawnVehicle, type SpawnVehicleOptions } from "./vehicle/spawn";
|
|
71
|
+
export { randomPlate } from "./vehicle/plate";
|
|
72
|
+
export {
|
|
73
|
+
nearestVehicleAt,
|
|
74
|
+
nearestVehicleTo,
|
|
75
|
+
vehicleNearPlayer,
|
|
76
|
+
} from "./vehicle/nearby";
|
|
77
|
+
export { entityFromNet, pedFromNet, vehicleFromNet } from "./netEntity";
|
|
78
|
+
export {
|
|
79
|
+
nearestPlayerTo,
|
|
80
|
+
playersNear,
|
|
81
|
+
playersNearCoords,
|
|
82
|
+
playersNearEntity,
|
|
83
|
+
} from "./proximity";
|
|
84
|
+
export { coordsOf, entityCoords, isNear, vehicleOf } from "./position";
|
|
85
|
+
export { AreaStream } from "./areaStream/areaStream";
|
|
86
|
+
export type { AreaItem } from "./areaStream/grid";
|
|
87
|
+
export type { AreaStreamOptions } from "./areaStream/options";
|
|
88
|
+
export {
|
|
89
|
+
Cooldowns,
|
|
90
|
+
KeyedCooldowns,
|
|
91
|
+
onCharacter,
|
|
92
|
+
PlayerMap,
|
|
93
|
+
type CharacterHandlers,
|
|
94
|
+
} from "./players";
|
|
95
|
+
export { PendingActions } from "./pending";
|
|
96
|
+
export { KeyWatchers } from "./watchers";
|
|
97
|
+
export { progressFor, type ProgressForOptions } from "./progressFor";
|
|
98
|
+
export { definitions } from "./definitions";
|
|
99
|
+
export {
|
|
100
|
+
minigameFor,
|
|
101
|
+
type MinigameForOptions,
|
|
102
|
+
type MinigameOutcome,
|
|
103
|
+
} from "./minigameFor";
|
|
104
|
+
export {
|
|
105
|
+
addCommand,
|
|
106
|
+
type CommandArgs,
|
|
107
|
+
type CommandHandler,
|
|
108
|
+
type CommandOptions,
|
|
109
|
+
} from "./command/addCommand";
|
|
110
|
+
export { onClientCallback, triggerClientCallback } from "./callback";
|
|
111
|
+
export { onClientEvent } from "./netEvent";
|
|
112
|
+
export {
|
|
113
|
+
ClientGate,
|
|
114
|
+
hasCharacter,
|
|
115
|
+
isAdmin,
|
|
116
|
+
near,
|
|
117
|
+
type ClientInputOptions,
|
|
118
|
+
type Guard,
|
|
119
|
+
type ParsedInputOptions,
|
|
120
|
+
} from "./gate";
|
|
121
|
+
export { createDatabaseTables } from "./database";
|
|
122
|
+
export {
|
|
123
|
+
callExport,
|
|
124
|
+
isResourceStarted,
|
|
125
|
+
optionalExport,
|
|
126
|
+
provideExport,
|
|
127
|
+
registerExport,
|
|
128
|
+
resourceExport,
|
|
129
|
+
} from "../fivem/exports";
|
|
130
|
+
export { createLocale, loadLocaleTexts, LOCALE_CONVAR } from "../fivem/locale";
|
|
131
|
+
export {
|
|
132
|
+
ByResource,
|
|
133
|
+
caller,
|
|
134
|
+
onStop,
|
|
135
|
+
plateOf,
|
|
136
|
+
Reasons,
|
|
137
|
+
} from "../fivem/byResource";
|
|
138
|
+
export { every, type EveryOptions, type Job } from "../fivem/scheduler/every";
|
|
139
|
+
export { EntityState, GlobalGroup, GlobalKey, PlayerState } from "./state";
|
|
140
|
+
export {
|
|
141
|
+
stateBoolean,
|
|
142
|
+
stateNumber,
|
|
143
|
+
stateString,
|
|
144
|
+
type StateParse,
|
|
145
|
+
} from "../fivem/state/mirror";
|
|
146
|
+
export { world } from "./world";
|
|
147
|
+
export { prison } from "./prison";
|
|
148
|
+
export { phone } from "./phone";
|
|
149
|
+
export { dispatch } from "./dispatch";
|
|
150
|
+
export { money } from "./money";
|
|
151
|
+
export { jobs } from "./jobs";
|
|
152
|
+
export { buckets } from "./buckets";
|
|
153
|
+
export { medical } from "./medical";
|
|
154
|
+
export { billing } from "./billing";
|
|
155
|
+
export { banking } from "./banking";
|
|
156
|
+
export { mdt } from "./mdt";
|
|
157
|
+
export { ems } from "./ems";
|
|
158
|
+
export { appearance } from "./appearance";
|
|
159
|
+
export { needs, stress } from "./stress";
|
|
160
|
+
export { fuel } from "./fuel";
|
|
161
|
+
export { doors } from "./doors";
|
|
162
|
+
export { garages } from "./garages";
|
|
163
|
+
export { characters } from "./characters";
|
|
164
|
+
export { bans } from "./bans";
|
|
165
|
+
export { isPrimeTime } from "./primeTime";
|