Ejemplo n.º 1
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            // serialize arguments
            var argsSerialized = MsgPackSerializer.Serialize(args);

            // try invoking the actual member
            var resultSerialized = InvokeResourceExport(binder.Name, argsSerialized);

            if (resultSerialized == null)
            {
                result = null;
                return(false);
            }

            var returnData = MsgPackDeserializer.Deserialize(resultSerialized) as List <object>;

            if (returnData == null || returnData.Count == 0)
            {
                result = null;
            }
            else
            {
                result = returnData[0];
            }

            return(true);
        }
Ejemplo n.º 2
0
        private void PrintError(string where, Exception what)
        {
            ScriptHost.SubmitBoundaryEnd(null, 0);

            var stackTrace = new StackTrace(what, true);
            var frames     = stackTrace.GetFrames()
                             .Select(a => new
            {
                Frame  = a,
                Method = a.GetMethod(),
                Type   = a.GetMethod()?.DeclaringType
            })
                             .Where(a => a.Method != null && (!a.Type.Assembly.GetName().Name.Contains("mscorlib") && !a.Type.Assembly.GetName().Name.Contains("CitizenFX.Core") && !a.Type.Assembly.GetName().Name.StartsWith("System")))
                             .Select(a => new
            {
                name       = EnhancedStackTrace.GetMethodDisplayString(a.Method).ToString(),
                sourcefile = a.Frame.GetFileName() ?? "",
                line       = a.Frame.GetFileLineNumber(),
                file       = $"@{m_resourceName}/{a.Type?.Assembly.GetName().Name ?? "UNK"}.dll"
            });

            var serializedFrames    = MsgPackSerializer.Serialize(frames);
            var formattedStackTrace = FormatStackTrace(serializedFrames);

            if (formattedStackTrace != null)
            {
                Debug.WriteLine($"^1SCRIPT ERROR in {where}: {what.GetType().FullName}: {what.Message}^7");
                Debug.WriteLine("{0}", formattedStackTrace);
            }
        }
Ejemplo n.º 3
0
        static object UnpackExt8(byte a, BinaryReader reader)
        {
            var length  = reader.ReadByte();
            var extType = reader.ReadByte();

            if (extType != 10)
            {
                throw new InvalidOperationException("Only extension type 10 (Citizen funcref) is handled by this class.");
            }

            var funcRefData             = reader.ReadBytes(length);
            var remoteFunctionReference = new RemoteFunctionReference(funcRefData);

            return(new CallbackDelegate(delegate(object[] args)
            {
                var byteData = MsgPackSerializer.Serialize(args);

                var returnByteData = remoteFunctionReference.InvokeNative(byteData);

                var returnData = Deserialize(returnByteData) as List <object>;

                if (returnData == null || returnData.Count == 0)
                {
                    return null;
                }

                return (returnData)[0];
            }));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Broadcasts an event to all connected players.
        /// </summary>
        /// <param name="eventName">The name of the event.</param>
        /// <param name="args">Arguments to pass to the event.</param>
        public static void TriggerClientEvent(string eventName, params object[] args)
        {
            var argsSerialized = MsgPackSerializer.Serialize(args);

            unsafe
            {
                fixed(byte *serialized = &argsSerialized[0])
                {
                    Function.Call(Hash.TRIGGER_CLIENT_EVENT_INTERNAL, eventName, "-1", serialized, argsSerialized.Length);
                }
            }
        }
Ejemplo n.º 5
0
        public void TriggerEvent(string eventName, params object[] args)
        {
            var argsSerialized = MsgPackSerializer.Serialize(args);

            unsafe
            {
                fixed(byte *serialized = &argsSerialized[0])
                {
                    Call(TRIGGER_CLIENT_EVENT_INTERNAL, eventName, m_handle, serialized, argsSerialized.Length);
                }
            }
        }
Ejemplo n.º 6
0
        public void TriggerLatentEvent(string eventName, int bytesPerSecond, params object[] args)
        {
            var argsSerialized = MsgPackSerializer.Serialize(args);

            unsafe
            {
                fixed(byte *serialized = &argsSerialized[0])
                {
                    Function.Call(Hash.TRIGGER_LATENT_CLIENT_EVENT_INTERNAL, eventName, m_handle, serialized, argsSerialized.Length, bytesPerSecond);
                }
            }
        }
Ejemplo n.º 7
0
        public static byte[] Invoke(int reference, byte[] arguments)
        {
            if (!ms_references.TryGetValue(reference, out var funcRef))
            {
                Debug.WriteLine("No such reference for {0}.", reference);

                // return nil
                return(new byte[] { 0xC0 });
            }

            var method = funcRef.m_method;

            // deserialize the passed arguments
            var argList  = (List <object>)MsgPackDeserializer.Deserialize(arguments);
            var argArray = argList.ToArray();

            // the Lua runtime expects this to be an array, so it be an array.
            return(MsgPackSerializer.Serialize(new[] { method.DynamicInvoke(argArray) }));
        }
Ejemplo n.º 8
0
        internal static unsafe void PushObject(ContextType *cxt, object arg)
        {
            var ptr = IntPtr.Zero;

            var b = MsgPackSerializer.Serialize(arg);

            ptr = Marshal.AllocHGlobal(b.Length);
            Marshal.Copy(b, 0, ptr, b.Length);

            ms_finalizers.Enqueue(() => Free(ptr));

            unsafe
            {
                *(IntPtr *)(&cxt->functionData[8 * cxt->numArguments])     = ptr;
                *(long *)(&cxt->functionData[8 * (cxt->numArguments + 1)]) = b.Length;
            }

            cxt->numArguments += 2;
        }
Ejemplo n.º 9
0
        static object CreateRemoteFunctionReference(byte[] funcRefData)
        {
            var remoteFunctionReference = new RemoteFunctionReference(funcRefData);

            return(new CallbackDelegate(delegate(object[] args)
            {
                var byteData = MsgPackSerializer.Serialize(args);

                var returnByteData = remoteFunctionReference.InvokeNative(byteData);

                var returnData = Deserialize(returnByteData) as List <object>;

                if (returnData == null || returnData.Count == 0)
                {
                    return null;
                }

                if (returnData[0] is IDictionary <string, object> obj)
                {
                    if (obj.TryGetValue("__cfx_async_retval", out dynamic asyncRef))
                    {
                        var tcs = new TaskCompletionSource <object>();

                        asyncRef(new Action <dynamic, dynamic>((res, err) =>
                        {
                            if (err != null && err != false)
                            {
                                tcs.SetException(new Exception(err.ToString()));
                            }
                            else
                            {
                                tcs.SetResult(res[0]);
                            }
                        }));

                        return tcs.Task;
                    }
                }

                return (returnData)[0];
            }));
        }
Ejemplo n.º 10
0
        static object UnpackExt8(byte a, BinaryReader reader)
        {
            var length  = reader.ReadByte();
            var extType = reader.ReadByte();

            if (extType != 1)
            {
                throw new InvalidOperationException("Only extension type 1 (Citizen callback) is handled by this class.");
            }

            // read the local reference ID
            var tmpBytes  = reader.ReadBytes(4);
            var reference = BitConverter.ToUInt32(new byte[] { tmpBytes[3], tmpBytes[2], tmpBytes[1], tmpBytes[0] }, 0);

            // read the environment instance ID
            tmpBytes = reader.ReadBytes(4);
            var instance = BitConverter.ToUInt32(new byte[] { tmpBytes[3], tmpBytes[2], tmpBytes[1], tmpBytes[0] }, 0);

            // read the resource name
            tmpBytes = reader.ReadBytes(length - 8);

            var resource = Encoding.UTF8.GetString(tmpBytes, 0, tmpBytes.Length);

            var remoteFunctionReference = new RemoteFunctionReference(resource, instance, reference);

            return(new CallbackDelegate(delegate(object[] args)
            {
                var byteData = MsgPackSerializer.Serialize(args);

                var returnByteData = remoteFunctionReference.InvokeNative(byteData);

                var returnData = Deserialize(returnByteData) as List <object>;

                if (returnData == null || returnData.Count == 0)
                {
                    return null;
                }

                return (returnData)[0];
            }));
        }
Ejemplo n.º 11
0
        public void WalkStack(byte[] boundaryStart, int boundaryStartLength, byte[] boundaryEnd, int boundaryEndLength, IScriptStackWalkVisitor visitor)
        {
            var data = m_intManager?.WalkStack(boundaryStart, boundaryEnd);

            if (data == null)
            {
                return;
            }

            var frames = (IEnumerable <object>)MsgPackDeserializer.Deserialize(data);

#if !IS_FXSERVER
            visitor = new DirectVisitor(visitor);
#endif

            foreach (var frame in frames)
            {
                var f = MsgPackSerializer.Serialize(frame);

                visitor.SubmitStackFrame(f, f.Length);
            }
        }
Ejemplo n.º 12
0
        public static byte[] Invoke(int reference, byte[] arguments)
        {
            if (!ms_references.TryGetValue(reference, out var funcRef))
            {
                Debug.WriteLine("No such reference for {0}.", reference);

                // return nil
                return(new byte[] { 0xC0 });
            }

            var method = funcRef.m_method;

            // deserialize the passed arguments
            var argList  = (List <object>)MsgPackDeserializer.Deserialize(arguments);
            var argArray = CallUtilities.GetPassArguments(method.Method, argList.ToArray(), string.Empty);

            // the Lua runtime expects this to be an array, so it be an array.
            var rv = method.DynamicInvoke(argArray);

            // is this actually an asynchronous method?
            if (rv is Task)
            {
                dynamic rt = rv;

                rv = new
                {
                    __cfx_async_retval = new Action <dynamic>(rvcb =>
                    {
                        rt.ContinueWith(new Action <Task>(t =>
                        {
                            rvcb(new object[] { rt.Result }, false);
                        }));
                    })
                };
            }

            return(MsgPackSerializer.Serialize(new[] { rv }));
        }
Ejemplo n.º 13
0
        public static void TriggerServerEvent(string eventName, params object[] args)
        {
            var argsSerialized = MsgPackSerializer.Serialize(args);

            TriggerEventInternal(eventName, argsSerialized, true);
        }
Ejemplo n.º 14
0
        public void Set(string key, object data, bool replicated)
        {
            var dataSerialized = MsgPackSerializer.Serialize(data);

            SetInternal(key, dataSerialized, replicated);
        }
Ejemplo n.º 15
0
        public static void TriggerLatentServerEvent(string eventName, int bytesPerSecond, params object[] args)
        {
            var argsSerialized = MsgPackSerializer.Serialize(args);

            TriggerLatentServerEventInternal(eventName, argsSerialized, bytesPerSecond);
        }