forked from overextended/ox_core
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathclassInterface.ts
More file actions
113 lines (83 loc) · 3.1 KB
/
classInterface.ts
File metadata and controls
113 lines (83 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type { Dict } from 'types';
export class ClassInterface {
protected static members: Dict<any>;
protected static keys?: Dict<Dict<any>>;
protected static callableMethods: Dict<true>;
static isCallValid(method: string, id: string | number, member: any) {
if (!member) return console.error(`cannot call method ${method} on ${this.name}<${id}> (invalid id)`);
if (!member[method])
return console.error(`cannot call method ${method} on ${this.name}<${id}> (method does not exist)`);
if (!this.callableMethods[method])
return console.error(`cannot call method ${method} on ${this.name}<${id}> (method is not exported)`);
return true;
}
/** Exports several class methods and makes non-private methods callable from external resources. */
static init() {
const classMethods = Object.getOwnPropertyNames(this.prototype);
if (classMethods) {
this.callableMethods = {};
classMethods.forEach((method) => {
if (method !== 'constructor') this.callableMethods[method] = true;
});
}
const name = this.name;
const expName = this.name.replace('Ox', '');
// e.g. exports.ox_core.GetPlayer
exports(`Get${expName}`, (id: string | number) => this.get(id));
// e.g. exports.ox_core.GetPlayerCalls
exports(`Get${expName}Calls`, () => this.callableMethods);
// e.g. exports.ox_core.CallPlayer
exports(`Call${expName}`, (id: string | number, method: string, ...args: any[]) => {
// Maintain backwards compatibility with OxVehicle indexed by entityId..
const member = expName === 'Vehicle' && typeof id === 'number' ? this.keys?.entity[id] : this.get(id);
if (member instanceof Promise) {
return member.then((resolvedMember) => {
if (!this.isCallValid(method, id, resolvedMember)) return;
return resolvedMember.call(method, ...args);
});
}
if (!this.isCallValid(method, id, member)) return;
return member.call(method, ...args);
});
DEV: console.info(`Instantiated ClassInterface<${name}> and exports`);
return this;
}
call(method: string, ...args: any) {
return (this as any)[method](...args);
}
/** Get a member of the class by its id. */
static get(id: string | number) {
return this.members[id];
}
/** Get all members of the class. */
static getAll() {
return this.members;
}
/** Adds a new member of the class to its registries. */
static add(id: string | number, member: any) {
if (this.members[id]) return false;
this.members[id] = member;
if (this.keys) {
Object.entries(this.keys).forEach(([key, obj]) => {
if (member[key]) {
obj[member[key]] = member;
}
});
}
return true;
}
/** Removes a member of the class from its registries. */
static remove(id: string | number) {
const member = this.members[id];
if (!member) return false;
if (this.keys) {
Object.entries(this.keys).forEach(([key, obj]) => {
if (member[key]) {
delete obj[member[key]];
}
});
}
delete this.members[id];
return true;
}
}