@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,101 @@
|
|
|
1
|
+
import { createLogger, type Logger } from "../../shared";
|
|
2
|
+
import { scheduler } from "./scheduler";
|
|
3
|
+
|
|
4
|
+
export interface EveryOptions {
|
|
5
|
+
/** Start at once (default true); `false` creates the job switched off. */
|
|
6
|
+
active?: boolean;
|
|
7
|
+
/** Name in the error log (default the function's name). */
|
|
8
|
+
name?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** A periodic job registered with `every`. */
|
|
12
|
+
export interface Job {
|
|
13
|
+
/** True while the job is switched on. */
|
|
14
|
+
readonly running: boolean;
|
|
15
|
+
/** Switches the job on (first run one interval later; next frame for 0) or off. */
|
|
16
|
+
set(active: boolean): void;
|
|
17
|
+
/** Switches the job off for good (`set(true)` does nothing afterwards). */
|
|
18
|
+
stop(): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A failing job is logged at most once per this many ms (the errors in between are counted into the next line). */
|
|
22
|
+
const ERROR_LOG_MS = 10_000;
|
|
23
|
+
let log: Logger | null = null;
|
|
24
|
+
|
|
25
|
+
class ScheduledJob implements Job {
|
|
26
|
+
/** Bumped on every switch; a queue entry with an older number is dropped when it comes up. */
|
|
27
|
+
gen = 0;
|
|
28
|
+
private active = false;
|
|
29
|
+
private stopped = false;
|
|
30
|
+
private quietUntil = 0;
|
|
31
|
+
private skipped = 0;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
readonly interval: number,
|
|
35
|
+
private readonly fn: () => unknown,
|
|
36
|
+
private readonly name: string,
|
|
37
|
+
) {}
|
|
38
|
+
|
|
39
|
+
get running(): boolean {
|
|
40
|
+
return this.active;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
set(active: boolean): void {
|
|
44
|
+
if (active === this.active || (active && this.stopped)) return;
|
|
45
|
+
this.active = active;
|
|
46
|
+
if (active) scheduler.activate(this);
|
|
47
|
+
else scheduler.deactivate(this);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
stop(): void {
|
|
51
|
+
this.set(false);
|
|
52
|
+
this.stopped = true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
call(): void {
|
|
56
|
+
try {
|
|
57
|
+
const result = this.fn();
|
|
58
|
+
if (result instanceof Promise)
|
|
59
|
+
result.catch((error: unknown) => this.fail(error));
|
|
60
|
+
} catch (error) {
|
|
61
|
+
this.fail(error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private fail(error: unknown): void {
|
|
66
|
+
const now = GetGameTimer();
|
|
67
|
+
if (now < this.quietUntil) {
|
|
68
|
+
this.skipped++;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this.quietUntil = now + ERROR_LOG_MS;
|
|
72
|
+
const more = this.skipped ? ` (+${this.skipped} more)` : "";
|
|
73
|
+
this.skipped = 0;
|
|
74
|
+
log ??= createLogger(GetCurrentResourceName());
|
|
75
|
+
log.error(
|
|
76
|
+
`job "${this.name}" failed${more}`,
|
|
77
|
+
error instanceof Error ? (error.stack ?? error.message) : error,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Runs `fn` every `ms` milliseconds (0 = every frame) from the resource's one scheduler tick; replaces `setInterval`
|
|
85
|
+
* and `setTick` for periodic work. A throwing (or rejecting) job is logged with its name and keeps running; the others
|
|
86
|
+
* are not affected. Jobs due in the same frame run in the order they were switched on, grouped by interval.
|
|
87
|
+
*/
|
|
88
|
+
export function every(
|
|
89
|
+
ms: number,
|
|
90
|
+
fn: () => unknown,
|
|
91
|
+
options: EveryOptions = {},
|
|
92
|
+
): Job {
|
|
93
|
+
const interval = Math.max(0, Math.floor(ms));
|
|
94
|
+
const job = new ScheduledJob(
|
|
95
|
+
interval,
|
|
96
|
+
fn,
|
|
97
|
+
options.name ?? (fn.name || "anonymous"),
|
|
98
|
+
);
|
|
99
|
+
if (options.active !== false) job.set(true);
|
|
100
|
+
return job;
|
|
101
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/** What a queue needs of a job: its switch number, its interval and the call. */
|
|
2
|
+
export interface QueuedJob {
|
|
3
|
+
/** Bumped on every switch; a queue entry with an older number is dropped when it comes up. */
|
|
4
|
+
gen: number;
|
|
5
|
+
readonly interval: number;
|
|
6
|
+
call(): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** The jobs of one interval: a ring buffer ordered by due time (a job that ran goes to the back, one interval later). */
|
|
10
|
+
export class Group {
|
|
11
|
+
private jobs: (QueuedJob | undefined)[] = new Array<undefined>(8);
|
|
12
|
+
private gens: number[] = new Array<number>(8).fill(0);
|
|
13
|
+
private dues: number[] = new Array<number>(8).fill(0);
|
|
14
|
+
private head = 0;
|
|
15
|
+
private mask = 7;
|
|
16
|
+
count = 0;
|
|
17
|
+
|
|
18
|
+
constructor(readonly interval: number) {}
|
|
19
|
+
|
|
20
|
+
/** Due time of the first entry (only valid while `count > 0`). */
|
|
21
|
+
get front(): number {
|
|
22
|
+
return this.dues[this.head] ?? 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Queue size that triggers dropping stale entries (doubles with the live entries kept). */
|
|
26
|
+
private compactAt = 64;
|
|
27
|
+
|
|
28
|
+
push(job: QueuedJob, due: number): void {
|
|
29
|
+
if (this.count >= this.compactAt) this.compact();
|
|
30
|
+
if (this.count > this.mask) this.grow();
|
|
31
|
+
const i = (this.head + this.count) & this.mask;
|
|
32
|
+
this.jobs[i] = job;
|
|
33
|
+
this.gens[i] = job.gen;
|
|
34
|
+
this.dues[i] = due;
|
|
35
|
+
this.count++;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Runs the due entries once each; entries added meanwhile wait for the next frame. */
|
|
39
|
+
run(now: number): void {
|
|
40
|
+
for (let left = this.count; left > 0 && this.count > 0; left--) {
|
|
41
|
+
const i = this.head;
|
|
42
|
+
const due = this.dues[i] ?? 0;
|
|
43
|
+
if (due > now) return;
|
|
44
|
+
const job = this.jobs[i];
|
|
45
|
+
const gen = this.gens[i];
|
|
46
|
+
this.jobs[i] = undefined;
|
|
47
|
+
this.head = (i + 1) & this.mask;
|
|
48
|
+
this.count--;
|
|
49
|
+
if (!job || job.gen !== gen) continue;
|
|
50
|
+
let next = due + this.interval;
|
|
51
|
+
if (next <= now) next = now + this.interval;
|
|
52
|
+
// Queued before the call, so the job may switch itself off (that entry then goes stale).
|
|
53
|
+
this.push(job, next);
|
|
54
|
+
job.call();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Drops entries of jobs switched off (or on again) since they were queued: a job toggled faster than its interval
|
|
60
|
+
* leaves one per toggle. Order and due times of the live entries stay.
|
|
61
|
+
*/
|
|
62
|
+
private compact(): void {
|
|
63
|
+
let kept = 0;
|
|
64
|
+
for (let n = 0; n < this.count; n++) {
|
|
65
|
+
const i = (this.head + n) & this.mask;
|
|
66
|
+
const job = this.jobs[i];
|
|
67
|
+
if (!job || job.gen !== this.gens[i]) continue;
|
|
68
|
+
const k = (this.head + kept) & this.mask;
|
|
69
|
+
this.jobs[k] = job;
|
|
70
|
+
this.gens[k] = this.gens[i] ?? 0;
|
|
71
|
+
this.dues[k] = this.dues[i] ?? 0;
|
|
72
|
+
kept++;
|
|
73
|
+
}
|
|
74
|
+
for (let n = kept; n < this.count; n++)
|
|
75
|
+
this.jobs[(this.head + n) & this.mask] = undefined;
|
|
76
|
+
this.count = kept;
|
|
77
|
+
this.compactAt = Math.max(64, kept * 2);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private grow(): void {
|
|
81
|
+
const size = (this.mask + 1) * 2;
|
|
82
|
+
const jobs = new Array<QueuedJob | undefined>(size);
|
|
83
|
+
const gens = new Array<number>(size).fill(0);
|
|
84
|
+
const dues = new Array<number>(size).fill(0);
|
|
85
|
+
for (let n = 0; n < this.count; n++) {
|
|
86
|
+
const i = (this.head + n) & this.mask;
|
|
87
|
+
jobs[n] = this.jobs[i];
|
|
88
|
+
gens[n] = this.gens[i] ?? 0;
|
|
89
|
+
dues[n] = this.dues[i] ?? 0;
|
|
90
|
+
}
|
|
91
|
+
this.jobs = jobs;
|
|
92
|
+
this.gens = gens;
|
|
93
|
+
this.dues = dues;
|
|
94
|
+
this.head = 0;
|
|
95
|
+
this.mask = size - 1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Group, type QueuedJob } from "./group";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One scheduler per resource: every periodic job (per-frame work and fixed-interval checks) runs from a single
|
|
5
|
+
* `setTick`, which exists only while a job is active (every tick handler or timer costs the resource time each frame).
|
|
6
|
+
* Jobs with the same interval share a queue ordered by due time, so a frame with nothing due costs one timer read and
|
|
7
|
+
* one comparison, and a due frame touches only the jobs that are due.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
class Scheduler {
|
|
11
|
+
private readonly groups: Group[] = [];
|
|
12
|
+
private readonly byInterval = new Map<number, Group>();
|
|
13
|
+
private tick: number | null = null;
|
|
14
|
+
private active = 0;
|
|
15
|
+
private nextDue = Infinity;
|
|
16
|
+
private running = false;
|
|
17
|
+
|
|
18
|
+
activate(job: QueuedJob): void {
|
|
19
|
+
job.gen++;
|
|
20
|
+
this.active++;
|
|
21
|
+
const due = GetGameTimer() + job.interval;
|
|
22
|
+
let group = this.byInterval.get(job.interval);
|
|
23
|
+
if (!group) {
|
|
24
|
+
group = new Group(job.interval);
|
|
25
|
+
this.byInterval.set(job.interval, group);
|
|
26
|
+
this.groups.push(group);
|
|
27
|
+
}
|
|
28
|
+
group.push(job, due);
|
|
29
|
+
if (due < this.nextDue) this.nextDue = due;
|
|
30
|
+
this.tick ??= setTick(this.onTick);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
deactivate(job: QueuedJob): void {
|
|
34
|
+
job.gen++;
|
|
35
|
+
this.active--;
|
|
36
|
+
if (this.active === 0 && !this.running) this.halt();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** No active job: the tick goes away and the queues are emptied of their stale entries. */
|
|
40
|
+
private halt(): void {
|
|
41
|
+
if (this.tick !== null) clearTick(this.tick);
|
|
42
|
+
this.tick = null;
|
|
43
|
+
this.groups.length = 0;
|
|
44
|
+
this.byInterval.clear();
|
|
45
|
+
this.nextDue = Infinity;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private readonly onTick = (): void => {
|
|
49
|
+
const now = GetGameTimer();
|
|
50
|
+
if (now < this.nextDue) return;
|
|
51
|
+
const groups = this.groups;
|
|
52
|
+
this.running = true;
|
|
53
|
+
// A group created by a job during this pass is visited too (its entries are due one interval later).
|
|
54
|
+
for (let i = 0; i < groups.length; i++) {
|
|
55
|
+
const group = groups[i];
|
|
56
|
+
if (group && group.count > 0 && group.front <= now) group.run(now);
|
|
57
|
+
}
|
|
58
|
+
this.running = false;
|
|
59
|
+
if (this.active === 0) return this.halt();
|
|
60
|
+
let soonest = Infinity;
|
|
61
|
+
let kept = 0;
|
|
62
|
+
for (let i = 0; i < groups.length; i++) {
|
|
63
|
+
const group = groups[i];
|
|
64
|
+
if (!group) continue;
|
|
65
|
+
if (group.count === 0) {
|
|
66
|
+
this.byInterval.delete(group.interval);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
groups[kept++] = group;
|
|
70
|
+
if (group.front < soonest) soonest = group.front;
|
|
71
|
+
}
|
|
72
|
+
groups.length = kept;
|
|
73
|
+
this.nextDue = soonest;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const scheduler = new Scheduler();
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { every } from "../scheduler/every";
|
|
2
|
+
import { Mirror, type StateParse } from "./mirror";
|
|
3
|
+
|
|
4
|
+
const PRUNE_MS = 30_000;
|
|
5
|
+
const isServer = IsDuplicityVersion();
|
|
6
|
+
|
|
7
|
+
/** One key of entity state bags (`fuel`, `ai:owned`...), by network id. */
|
|
8
|
+
export class EntityState<T> extends Mirror<number, T> {
|
|
9
|
+
/** The entity handle each value belongs to: a reused network id with another handle is read again. */
|
|
10
|
+
private readonly handles = new Map<number, number>();
|
|
11
|
+
|
|
12
|
+
constructor(key: string, parse: StateParse<T>) {
|
|
13
|
+
super(key, parse);
|
|
14
|
+
AddStateBagChangeHandler(
|
|
15
|
+
key,
|
|
16
|
+
"",
|
|
17
|
+
(bag: string, _key: string, value: unknown) => {
|
|
18
|
+
if (!bag.startsWith("entity:")) return;
|
|
19
|
+
const netId = Number(bag.slice(7));
|
|
20
|
+
this.handles.set(netId, GetEntityFromStateBagName(bag));
|
|
21
|
+
this.store(netId, value);
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
if (isServer)
|
|
25
|
+
on("entityRemoved", (entity: number) =>
|
|
26
|
+
this.forget(NetworkGetNetworkIdFromEntity(entity)),
|
|
27
|
+
);
|
|
28
|
+
else every(PRUNE_MS, () => this.prune(), { name: `state ${key}` });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The value on an entity (undefined: none or not networked). */
|
|
32
|
+
get(entity: number): T | undefined {
|
|
33
|
+
if (!entity) return undefined;
|
|
34
|
+
const netId = NetworkGetNetworkIdFromEntity(entity);
|
|
35
|
+
const handle = this.handles.get(netId);
|
|
36
|
+
if (handle === entity) return this.values.get(netId);
|
|
37
|
+
if (handle === 0 && this.values.has(netId)) {
|
|
38
|
+
// The value arrived before the entity existed here.
|
|
39
|
+
this.handles.set(netId, entity);
|
|
40
|
+
return this.values.get(netId);
|
|
41
|
+
}
|
|
42
|
+
// First read of this entity (resource started after it was set, or a reused network id): read the bag once.
|
|
43
|
+
this.handles.set(netId, entity);
|
|
44
|
+
const value = this.parseRaw(Entity(entity).state[this.key]);
|
|
45
|
+
this.values.set(netId, value);
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
protected put(entity: number, value: T | undefined): void {
|
|
50
|
+
const netId = NetworkGetNetworkIdFromEntity(entity);
|
|
51
|
+
this.handles.set(netId, entity);
|
|
52
|
+
this.apply(netId, value);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The value by network id, as far as this side knows it. */
|
|
56
|
+
getByNetId(netId: number): T | undefined {
|
|
57
|
+
return this.values.get(netId);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
protected forget(netId: number): void {
|
|
61
|
+
this.values.delete(netId);
|
|
62
|
+
this.handles.delete(netId);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private parseRaw(raw: unknown): T | undefined {
|
|
66
|
+
return raw == null ? undefined : this.parse(raw);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Clients never hear about entities leaving their scope: drop the ones that no longer exist here. */
|
|
70
|
+
private prune(): void {
|
|
71
|
+
for (const netId of this.values.keys())
|
|
72
|
+
if (!DoesEntityExist(NetworkGetEntityFromNetworkId(netId)))
|
|
73
|
+
this.forget(netId);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Mirror, type StateParse } from "./mirror";
|
|
2
|
+
|
|
3
|
+
/** One GlobalState key (shared state everyone needs): read once, then kept by its change handler. */
|
|
4
|
+
export class GlobalKey<T> extends Mirror<"global", T> {
|
|
5
|
+
constructor(key: string, parse: StateParse<T>) {
|
|
6
|
+
super(key, parse);
|
|
7
|
+
const raw: unknown = GlobalState[key];
|
|
8
|
+
this.values.set("global", raw == null ? undefined : parse(raw));
|
|
9
|
+
AddStateBagChangeHandler(
|
|
10
|
+
key,
|
|
11
|
+
"global",
|
|
12
|
+
(_bag: string, _key: string, value: unknown) =>
|
|
13
|
+
this.store("global", value),
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get(): T | undefined {
|
|
18
|
+
return this.values.get("global");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
protected put(value: T | undefined): void {
|
|
22
|
+
this.apply("global", value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type PrefixListener = (key: string, value: unknown) => void;
|
|
27
|
+
const prefixListeners = new Map<string, Set<PrefixListener>>();
|
|
28
|
+
let prefixHandler = false;
|
|
29
|
+
|
|
30
|
+
/** One wildcard handler on the global bag per resource, shared by every `GlobalGroup`. */
|
|
31
|
+
function listenPrefix(prefix: string, listener: PrefixListener): void {
|
|
32
|
+
let set = prefixListeners.get(prefix);
|
|
33
|
+
if (!set) prefixListeners.set(prefix, (set = new Set()));
|
|
34
|
+
set.add(listener);
|
|
35
|
+
if (prefixHandler) return;
|
|
36
|
+
prefixHandler = true;
|
|
37
|
+
AddStateBagChangeHandler(
|
|
38
|
+
"",
|
|
39
|
+
"global",
|
|
40
|
+
(_bag: string, key: string, value: unknown) => {
|
|
41
|
+
for (const [p, listeners] of prefixListeners)
|
|
42
|
+
if (key.startsWith(p)) for (const l of listeners) l(key, value);
|
|
43
|
+
},
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Many GlobalState keys with one prefix (`door:<id>`, `rob:<id>`), by the id after the prefix. */
|
|
48
|
+
export class GlobalGroup<T> extends Mirror<string, T> {
|
|
49
|
+
constructor(
|
|
50
|
+
readonly prefix: string,
|
|
51
|
+
parse: StateParse<T>,
|
|
52
|
+
) {
|
|
53
|
+
super(prefix, parse);
|
|
54
|
+
const keys: unknown = GetStateBagKeys("global");
|
|
55
|
+
for (const key of Array.isArray(keys) ? keys : []) {
|
|
56
|
+
if (typeof key !== "string" || !key.startsWith(prefix)) continue;
|
|
57
|
+
const raw: unknown = GlobalState[key];
|
|
58
|
+
this.values.set(
|
|
59
|
+
key.slice(prefix.length),
|
|
60
|
+
raw == null ? undefined : parse(raw),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
listenPrefix(prefix, (key, value) =>
|
|
64
|
+
this.store(key.slice(prefix.length), value),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get(id: string): T | undefined {
|
|
69
|
+
return this.values.get(id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected put(id: string, value: T | undefined): void {
|
|
73
|
+
this.apply(id, value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Every id with a value. */
|
|
77
|
+
ids(): IterableIterator<string> {
|
|
78
|
+
return this.values.keys();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* State bags read from memory. Reading a bag (`Entity(e).state.x`, `GlobalState.x`) builds two proxies, calls up to
|
|
3
|
+
* three natives, copies the value twice under two locks in the engine and decodes msgpack every time; on a busy server
|
|
4
|
+
* it waits on the network thread's lock. These classes keep one change handler per key and resource and a `Map` of the
|
|
5
|
+
* values, so a read is a map lookup (entities: one `NetworkGetNetworkIdFromEntity`). Writing is server only
|
|
6
|
+
* (`sv_stateBagStrictMode`): the server classes in "@aifivem/lib/server" add `set`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Turns a replicated value into `T`; `undefined` = no (valid) value. */
|
|
10
|
+
export type StateParse<T> = (value: unknown) => T | undefined;
|
|
11
|
+
type Listener<I, T> = (id: I, value: T | undefined, old: T | undefined) => void;
|
|
12
|
+
|
|
13
|
+
export const stateNumber: StateParse<number> = (v) =>
|
|
14
|
+
typeof v === "number" && Number.isFinite(v) ? v : undefined;
|
|
15
|
+
export const stateBoolean: StateParse<boolean> = (v) =>
|
|
16
|
+
typeof v === "boolean" ? v : undefined;
|
|
17
|
+
export const stateString: StateParse<string> = (v) =>
|
|
18
|
+
typeof v === "string" ? v : undefined;
|
|
19
|
+
|
|
20
|
+
/** Shared base: values by id, listeners, the parse. */
|
|
21
|
+
export abstract class Mirror<I, T> {
|
|
22
|
+
protected readonly values = new Map<I, T | undefined>();
|
|
23
|
+
private readonly listeners = new Set<Listener<I, T>>();
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
readonly key: string,
|
|
27
|
+
protected readonly parse: StateParse<T>,
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
/** Runs `listener(id, value, old)` after a value changes (the new value is already readable). */
|
|
31
|
+
onChange(listener: Listener<I, T>): () => void {
|
|
32
|
+
this.listeners.add(listener);
|
|
33
|
+
return () => this.listeners.delete(listener);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected store(id: I, raw: unknown): void {
|
|
37
|
+
this.apply(id, raw == null ? undefined : this.parse(raw));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A value the server just set: readable at once, listeners run once (the change handler later finds it equal). */
|
|
41
|
+
protected apply(id: I, value: T | undefined): void {
|
|
42
|
+
const old = this.values.get(id);
|
|
43
|
+
this.values.set(id, value);
|
|
44
|
+
if (old !== value)
|
|
45
|
+
for (const listener of this.listeners) listener(id, value, old);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Mirror, type StateParse } from "./mirror";
|
|
2
|
+
|
|
3
|
+
const isServer = IsDuplicityVersion();
|
|
4
|
+
|
|
5
|
+
/** Every player state of this resource: one `playerDropped` listener forgets the leaver in all of them. */
|
|
6
|
+
const playerStates = new Set<{ dropPlayer(serverId: number): void }>();
|
|
7
|
+
let dropListener = false;
|
|
8
|
+
|
|
9
|
+
/** One key of player state bags (`dead`, `cuffed`, pma-voice's `proximity`...), by server id. */
|
|
10
|
+
export class PlayerState<T> extends Mirror<number, T> {
|
|
11
|
+
private readonly seen = new Set<number>();
|
|
12
|
+
|
|
13
|
+
constructor(key: string, parse: StateParse<T>) {
|
|
14
|
+
super(key, parse);
|
|
15
|
+
AddStateBagChangeHandler(
|
|
16
|
+
key,
|
|
17
|
+
"",
|
|
18
|
+
(bag: string, _key: string, value: unknown) => {
|
|
19
|
+
if (!bag.startsWith("player:")) return;
|
|
20
|
+
const id = Number(bag.slice(7));
|
|
21
|
+
this.seen.add(id);
|
|
22
|
+
this.store(id, value);
|
|
23
|
+
},
|
|
24
|
+
);
|
|
25
|
+
if (!isServer) return;
|
|
26
|
+
playerStates.add(this);
|
|
27
|
+
if (dropListener) return;
|
|
28
|
+
dropListener = true;
|
|
29
|
+
// One listener for every instance: more than 10 on one event print Node's MaxListenersExceededWarning.
|
|
30
|
+
on("playerDropped", () => {
|
|
31
|
+
const id = Number(globalThis.source);
|
|
32
|
+
for (const state of playerStates) state.dropPlayer(id);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A player left (the shared drop listener calls it). */
|
|
37
|
+
dropPlayer(serverId: number): void {
|
|
38
|
+
this.forget(serverId);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The value of a player by server id (the local player: `cache.serverId`). */
|
|
42
|
+
get(serverId: number): T | undefined {
|
|
43
|
+
if (!this.seen.has(serverId)) {
|
|
44
|
+
this.seen.add(serverId);
|
|
45
|
+
const raw: unknown = Player(serverId).state[this.key];
|
|
46
|
+
this.values.set(serverId, raw == null ? undefined : this.parse(raw));
|
|
47
|
+
}
|
|
48
|
+
return this.values.get(serverId);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected put(serverId: number, value: T | undefined): void {
|
|
52
|
+
this.seen.add(serverId);
|
|
53
|
+
this.apply(serverId, value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected forget(serverId: number): void {
|
|
57
|
+
this.values.delete(serverId);
|
|
58
|
+
this.seen.delete(serverId);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { PanelSchema, WidgetSpec } from "@aifivem/shared";
|
|
2
|
+
import { isRecord } from "../../shared";
|
|
3
|
+
|
|
4
|
+
/** Path inside a file (`branding.name`, list items as `rows[].x`) -> "Label: hint". */
|
|
5
|
+
export type Descriptions = Map<string, string>;
|
|
6
|
+
|
|
7
|
+
function langFile(resource: string): Record<string, string> {
|
|
8
|
+
try {
|
|
9
|
+
const raw: unknown = JSON.parse(
|
|
10
|
+
LoadResourceFile(resource, "locales/en.json") ?? "{}",
|
|
11
|
+
);
|
|
12
|
+
return isRecord(raw)
|
|
13
|
+
? Object.fromEntries(
|
|
14
|
+
Object.entries(raw).filter(
|
|
15
|
+
(e): e is [string, string] => typeof e[1] === "string",
|
|
16
|
+
),
|
|
17
|
+
)
|
|
18
|
+
: {};
|
|
19
|
+
} catch {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The script's texts over ai-core's generic panel texts (shared fields such as a place's NPC use those). */
|
|
25
|
+
export function texts(resource: string): Record<string, string> {
|
|
26
|
+
return { ...langFile("ai-core"), ...langFile(resource) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function collect(
|
|
30
|
+
fields: WidgetSpec[],
|
|
31
|
+
prefix: string,
|
|
32
|
+
lang: Record<string, string>,
|
|
33
|
+
out: Descriptions,
|
|
34
|
+
): void {
|
|
35
|
+
for (const field of fields) {
|
|
36
|
+
// An action button names a handler, not a stored value.
|
|
37
|
+
if (field.widget === "action") continue;
|
|
38
|
+
const path = prefix + field.path;
|
|
39
|
+
const label = lang[field.label] ?? field.label;
|
|
40
|
+
const hint = field.hint ? (lang[field.hint] ?? field.hint) : "";
|
|
41
|
+
if (!out.has(path)) out.set(path, hint ? `${label}: ${hint}` : label);
|
|
42
|
+
if (field.widget === "list") collect(field.rows, `${path}[].`, lang, out);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function describe(
|
|
47
|
+
schema: PanelSchema,
|
|
48
|
+
file: string,
|
|
49
|
+
lang: Record<string, string>,
|
|
50
|
+
): Descriptions {
|
|
51
|
+
const out: Descriptions = new Map();
|
|
52
|
+
for (const tab of schema.tabs)
|
|
53
|
+
if (tab.file === file)
|
|
54
|
+
for (const card of tab.cards) collect(card.fields, "", lang, out);
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|