UNPKG

@nativewrappers/redm

Version:

Native wrappers and utilities for use with RedM.

209 lines (208 loc) 7.63 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { GlobalData } from "../GlobalData"; var ConVarType = /* @__PURE__ */ ((ConVarType2) => { ConVarType2[ConVarType2["String"] = 0] = "String"; ConVarType2[ConVarType2["Integer"] = 1] = "Integer"; ConVarType2[ConVarType2["Float"] = 2] = "Float"; ConVarType2[ConVarType2["Boolean"] = 3] = "Boolean"; return ConVarType2; })(ConVarType || {}); const DisablePrettyPrint = /* @__PURE__ */ __name(() => GlobalData.EnablePrettyPrint = false, "DisablePrettyPrint"); const AsyncFunction = (async () => { }).constructor; function Exports(exportName) { return /* @__PURE__ */ __name(function actualDecorator(originalMethod, context) { if (context.private) { throw new Error("Exports does not work on private methods, please mark the method as public"); } context.addInitializer(function() { let exportCb; if (originalMethod instanceof AsyncFunction) { exportCb = /* @__PURE__ */ __name(async (...args) => { try { return await originalMethod.call(this, ...args); } catch (err) { REMOVE_EVENT_LOG: { if (!GlobalData.EnablePrettyPrint) return; console.error("------- EXPORT ERROR --------"); console.error(`Call to ${exportName} errored`); console.error(`Data: ${JSON.stringify(args)}`); console.error(`Error: ${err}`); console.error("------- END EXPORT ERROR --------"); } throw err; } }, "exportCb"); } else { exportCb = /* @__PURE__ */ __name((...args) => { try { return originalMethod.call(this, ...args); } catch (err) { REMOVE_EVENT_LOG: { if (!GlobalData.EnablePrettyPrint) return; console.error("------- EXPORT ERROR --------"); console.error(`Call to ${exportName} errored`); console.error(`Data: ${JSON.stringify(args)}`); console.error(`Error: ${err}`); console.error("------- END EXPORT ERROR --------"); } throw err; } }, "exportCb"); } exports(exportName, exportCb); }); }, "actualDecorator"); } __name(Exports, "Exports"); function Event(eventName) { return /* @__PURE__ */ __name(function actualDecorator(originalMethod, context) { if (context.private) { throw new Error("Event does not work on private methods, please mark the method as public"); } context.addInitializer(function() { on(eventName, async (...args) => { try { return await originalMethod.call(this, ...args); } catch (e) { REMOVE_EVENT_LOG: { if (!GlobalData.EnablePrettyPrint) return; console.error("------- EVENT ERROR --------"); console.error(`Call to ${eventName} errored`); console.error(`Data: ${JSON.stringify(args)}`); console.error(`Error: ${e}`); console.error("------- END EVENT ERROR --------"); } } }); }); }, "actualDecorator"); } __name(Event, "Event"); const CfxEvent = Event; function NetEvent(eventName, remoteOnly = true) { return /* @__PURE__ */ __name(function actualDecorator(originalMethod, context) { if (context.private) { throw new Error("NetEvent does not work on private methods, please mark the method as public"); } context.addInitializer(function() { onNet(eventName, async (...args) => { const src = source; try { $CLIENT: { if (GlobalData.IS_CLIENT && remoteOnly && source !== 65535) { return; } } return await originalMethod.call(this, ...args); } catch (e) { REMOVE_NET_EVENT_LOG: { if (!GlobalData.EnablePrettyPrint) return; console.error("------- NET EVENT ERROR --------"); console.error(`Call to ${eventName} errored`); console.error(`Caller: ${src}`); console.error(`Data: ${JSON.stringify(args)}`); console.error(`Error: ${e}`); console.error("------- END NET EVENT ERROR --------"); } } }); }); }, "actualDecorator"); } __name(NetEvent, "NetEvent"); function NuiEvent(eventName) { return /* @__PURE__ */ __name(function actualDecorator(originalMethod, context) { if (context.private) { throw new Error("NuiEvent does not work on private methods, please mark the method as public"); } context.addInitializer(function() { RegisterNuiCallback(eventName, (...args) => { return originalMethod.call(this, ...args); }); }); }, "actualDecorator"); } __name(NuiEvent, "NuiEvent"); const get_convar_fn = /* @__PURE__ */ __name((con_var_type) => { switch (con_var_type) { case 0 /* String */: return GetConvar; case 1 /* Integer */: return GetConvarInt; case 2 /* Float */: return GetConvarFloat; case 3 /* Boolean */: return GetConvarBool; // needed so typescript wont complain about "unreachable code" for the error below default: } throw new Error("Got invalid ConVarType"); }, "get_convar_fn"); function ConVar(name, is_floating_point, deserialize) { return /* @__PURE__ */ __name(function actualDecorator(_initialValue, context, ..._args) { if (context.private) { throw new Error("ConVar does not work on private types, please mark the field as public"); } context.addInitializer(function() { const t = this; const default_value = Reflect.get(t, context.name); const default_type = typeof default_value; let con_var_type = null; if (default_type === "number") { if (is_floating_point || !Number.isInteger(default_value)) { con_var_type = 2 /* Float */; } else { con_var_type = 1 /* Integer */; } } else if (default_type === "boolean") { con_var_type = 3 /* Boolean */; } else if (default_type === "string") { con_var_type = 0 /* String */; } if (!deserialize && con_var_type === null) { throw new Error( `Failed to determine what to use to deserialize '${name}' was for var had type '${default_type}' which can't be deserialized without providing your own deserialize function.` ); } if (con_var_type === null) { con_var_type = 0 /* String */; } const con_var_fn = get_convar_fn(con_var_type); const get_convar_value = /* @__PURE__ */ __name(() => { const data = con_var_fn(name, default_value); return deserialize ? deserialize(data) : data; }, "get_convar_value"); Reflect.set(t, context.name, get_convar_value()); AddConvarChangeListener(name, () => { Reflect.set(t, context.name, get_convar_value()); }); }); }, "actualDecorator"); } __name(ConVar, "ConVar"); function SetTick() { return /* @__PURE__ */ __name(function actualDecorator(originalMethod, context) { if (context.private) { throw new Error("SetTick does not work on private types, please mark the field as public"); } context.addInitializer(function() { setTick(async () => { await originalMethod.call(this); }); }); }, "actualDecorator"); } __name(SetTick, "SetTick"); export { CfxEvent, ConVar, ConVarType, DisablePrettyPrint, Event, Exports, NetEvent, NuiEvent, SetTick };