UNPKG

@nativewrappers/common

Version:

Native wrappers and utilities for use with Cfx scripting runtimes.

164 lines (163 loc) 5.91 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { GlobalData } from "../GlobalData"; const DisablePrettyPrint = /* @__PURE__ */ __name(() => GlobalData.EnablePrettyPrint = false, "DisablePrettyPrint"); const AsyncFunction = (async () => { }).constructor; var Binding = /* @__PURE__ */ ((Binding2) => { Binding2[Binding2["None"] = 0] = "None"; Binding2[Binding2["Local"] = 1] = "Local"; Binding2[Binding2["Remote"] = 2] = "Remote"; Binding2[Binding2["All"] = 3] = "All"; return Binding2; })(Binding || {}); function CfxEvent(eventName, binding = 1 /* Local */) { 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() { const fn = (binding & 2 /* Remote */) !== 0 ? onNet : on; const _t = this; fn(eventName, async (...args) => { const src = source; if (_t.__permissionMap && binding & 2 /* Remote */) { const permissions = _t.__permissionMap.get(context.name); if (permissions) { let hasPermission = false; for (const perm of permissions) { if (IsPlayerAceAllowed(src, perm)) { hasPermission = true; break; } } if (!hasPermission) { emit("@nativewrappers:no_permission", { eventName, method: context.name }); return; } } } 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(CfxEvent, "CfxEvent"); 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"); 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() { const _t = this; onNet(eventName, async (...args) => { const src = source; if (_t.__permissionMap) { const permissions = _t.__permissionMap.get(context.name); if (permissions) { let hasPermission = false; for (const perm of permissions) { if (IsPlayerAceAllowed(src, perm)) { hasPermission = true; break; } } if (!hasPermission) { emit("@nativewrappers:no_permission", { eventName, method: context.name }); return; } } } 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, dontErrorWhenCbIsntInvoked = false) { 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, async (data, cb) => { let wasInvoked = false; const cbWrapper = /* @__PURE__ */ __name((args) => { wasInvoked = true; cb(args); }, "cbWrapper"); const retData = await originalMethod.call(this, data, cbWrapper); if (!wasInvoked && !retData) { if (dontErrorWhenCbIsntInvoked) return; throw new Error( `Error in NuiEvent ${eventName} '@NuiEvent' expects you to return data in your callback, or to return from the function call` ); } if (!wasInvoked) { cb(retData); } }); }); }, "actualDecorator"); } __name(NuiEvent, "NuiEvent"); export { Binding, CfxEvent, DisablePrettyPrint, Event, NetEvent, NuiEvent };