2024-09-15 20:55:08 +00:00
|
|
|
import { RPC_LISTENER } from './events'
|
2024-08-15 19:47:22 +00:00
|
|
|
import { Environment, utils } from './utils'
|
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
import { client } from './client'
|
|
|
|
import { server } from './server'
|
|
|
|
import type { RPCData } from './types.ts'
|
2024-08-15 19:47:22 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
class FrameworkRpc {
|
|
|
|
private readonly _environment: Environment
|
|
|
|
private readonly _environmentGlobal: Record<string, Function>
|
2024-08-15 19:47:22 +00:00
|
|
|
|
|
|
|
constructor() {
|
2024-09-15 20:55:08 +00:00
|
|
|
this._environment = utils.getEnvironment()
|
|
|
|
this._environmentGlobal =
|
|
|
|
utils.getEnvironment() === utils.environment.CEF
|
|
|
|
? window.rpcEvents
|
|
|
|
: global.rpcEvents
|
2024-08-15 19:47:22 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
mp.events.add(RPC_LISTENER, async (player: any, data: string) => {
|
|
|
|
switch (this._environment) {
|
|
|
|
case utils.environment.UNKNOWN:
|
|
|
|
return
|
2024-08-15 19:47:22 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
case utils.environment.CLIENT:
|
|
|
|
player = data
|
|
|
|
return client.listen(player)
|
2024-09-15 18:32:27 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
case utils.environment.SERVER:
|
|
|
|
return server.listen(player, data)
|
2024-09-15 18:32:27 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
case utils.environment.CEF:
|
2024-08-15 19:47:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
public register<Args extends any[] = unknown[], Return = unknown>(
|
2024-08-15 19:47:22 +00:00
|
|
|
eventName: string,
|
2024-09-15 20:55:08 +00:00
|
|
|
cb: (...args: Args) => Return,
|
2024-08-15 19:47:22 +00:00
|
|
|
) {
|
2024-09-15 20:55:08 +00:00
|
|
|
if (this._environment === utils.environment.UNKNOWN) return
|
|
|
|
this._environmentGlobal[eventName] = cb
|
2024-08-15 19:47:22 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
public callClient<Args extends any[] = unknown[], Return = unknown>(
|
2024-08-15 19:47:22 +00:00
|
|
|
player: any,
|
|
|
|
eventName: string,
|
|
|
|
...args: Args
|
|
|
|
): Promise<Return | unknown> {
|
2024-09-15 20:55:08 +00:00
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
const uuid = utils.generateUUID()
|
|
|
|
|
|
|
|
const data: RPCData = {
|
|
|
|
uuid,
|
|
|
|
eventName,
|
|
|
|
from: this._environment,
|
|
|
|
to: utils.environment.CLIENT,
|
|
|
|
data: args,
|
|
|
|
}
|
|
|
|
|
|
|
|
player.call(RPC_LISTENER, [utils.stringifyData(data)])
|
|
|
|
|
|
|
|
const responseEventName = utils.generateResponseEventName(uuid)
|
|
|
|
|
|
|
|
const handler = (_player: any, data: string) => {
|
|
|
|
resolve(utils.parseData(data).data)
|
|
|
|
mp.events.remove(responseEventName)
|
|
|
|
}
|
|
|
|
|
|
|
|
mp.events.add(responseEventName, handler)
|
|
|
|
})
|
2024-08-15 19:47:22 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
public callServer<Args extends any[] = unknown[], Return = unknown>(
|
2024-08-15 19:47:22 +00:00
|
|
|
eventName: string,
|
|
|
|
...args: Args
|
|
|
|
): Promise<Return | unknown> {
|
2024-09-15 20:55:08 +00:00
|
|
|
return new Promise((resolve, _reject) => {
|
|
|
|
const uuid = utils.generateUUID()
|
|
|
|
|
|
|
|
const data: RPCData = {
|
|
|
|
uuid,
|
|
|
|
eventName,
|
|
|
|
from: this._environment,
|
|
|
|
to: utils.environment.CLIENT,
|
|
|
|
data: args,
|
|
|
|
}
|
2024-09-15 18:32:27 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
mp.events.callRemote(RPC_LISTENER, utils.stringifyData(data))
|
2024-09-15 18:32:27 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
const responseEventName = utils.generateResponseEventName(uuid)
|
2024-09-15 18:32:27 +00:00
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
const handler = (_player: any, data: string) => {
|
|
|
|
resolve(utils.parseData(data).data)
|
|
|
|
mp.events.remove(responseEventName)
|
|
|
|
}
|
|
|
|
|
|
|
|
mp.events.add(responseEventName, handler)
|
|
|
|
})
|
2024-08-15 19:47:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 20:55:08 +00:00
|
|
|
const rpc = new FrameworkRpc()
|
|
|
|
|
|
|
|
export { rpc }
|