예제 #1
0
        public void TriggerEvent(string eventName, byte[] argsSerialized, string sourceString)
        {
            try
            {
                var obj = MsgPackDeserializer.Deserialize(argsSerialized) as List <object> ?? (IEnumerable <object>) new object[0];

                var scripts = ms_definedScripts.ToArray();

                // TODO: store source someplace
                var objArray = obj.ToArray();

                foreach (var script in scripts)
                {
                    Task.Factory.StartNew(() => script.EventHandlers.Invoke(eventName, sourceString, objArray)).Unwrap().ContinueWith(a =>
                    {
                        if (a.IsFaulted)
                        {
                            Debug.WriteLine($"Error invoking event handlers for {eventName}: {a.Exception?.InnerExceptions.Aggregate("", (b, s) => s + b.ToString() + "\n")}");
                        }
                    });
                }

                ExportDictionary.Invoke(eventName, objArray);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
예제 #2
0
        public void TriggerEvent(string eventName, byte[] argsSerialized, string sourceString)
        {
            try
            {
                var obj = MsgPackDeserializer.Deserialize(argsSerialized, netSource: (sourceString.StartsWith("net") ? sourceString : null)) as List <object> ?? (IEnumerable <object>) new object[0];

                var scripts = ms_definedScripts.ToArray();

                var objArray = obj.ToArray();

                NetworkFunctionManager.HandleEventTrigger(eventName, objArray, sourceString);

                foreach (var script in scripts)
                {
                    Task.Factory.StartNew(() => script.EventHandlers.Invoke(eventName, sourceString, objArray)).Unwrap().ContinueWith(a =>
                    {
                        if (a.IsFaulted)
                        {
                            Debug.WriteLine($"Error invoking event handlers for {eventName}: {a.Exception?.InnerExceptions.Aggregate("", (b, s) => s + b.ToString() + "\n")}");
                        }
                    });
                }

                ExportDictionary.Invoke(eventName, objArray);

                // invoke a single task tick
                CitizenTaskScheduler.Instance.Tick();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
예제 #3
0
        public void TriggerEvent(string eventName, byte[] argsSerialized, string sourceString)
        {
            if (GameInterface.SnapshotStackBoundary(out var bo))
            {
                ScriptHost.SubmitBoundaryStart(bo, bo.Length);
            }

            try
            {
#if IS_FXSERVER
                var netSource = (sourceString.StartsWith("net") || sourceString.StartsWith("internal-net")) ? sourceString : null;
#else
                var netSource = sourceString.StartsWith("net") ? sourceString : null;
#endif

                var obj = MsgPackDeserializer.Deserialize(argsSerialized, netSource) as List <object> ?? (IEnumerable <object>) new object[0];

                var scripts = ms_definedScripts.ToArray();

                var objArray = obj.ToArray();

                NetworkFunctionManager.HandleEventTrigger(eventName, objArray, sourceString);

                foreach (var script in scripts)
                {
                    Task.Factory.StartNew(() =>
                    {
                        BaseScript.CurrentName = $"eventHandler {script.GetType().Name} -> {eventName}";
                        var t = script.EventHandlers.Invoke(eventName, sourceString, objArray);
                        BaseScript.CurrentName = null;

                        return(t);
                    }, CancellationToken.None, TaskCreationOptions.None, CitizenTaskScheduler.Instance).Unwrap().ContinueWith(a =>
                    {
                        if (a.IsFaulted)
                        {
                            Debug.WriteLine($"Error invoking event handlers for {eventName}: {a.Exception?.InnerExceptions.Aggregate("", (b, s) => s + b.ToString() + "\n")}");
                        }
                    });
                }

                ExportDictionary.Invoke(eventName, objArray);

                // invoke a single task tick
                CitizenTaskScheduler.Instance.Tick();
            }
            catch (Exception e)
            {
                PrintError($"event ({eventName})", e);
            }
            finally
            {
                ScriptHost.SubmitBoundaryStart(null, 0);
            }
        }
예제 #4
0
        public static object Deserialize(byte[] data, string netSource = null)
        {
            var memoryStream = new MemoryStream(data);
            var reader       = new BinaryReader(memoryStream);

            var deserializer = new MsgPackDeserializer();

            deserializer.NetSource = netSource;

            return(deserializer.UnpackAny(reader));
        }
예제 #5
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) }));
        }
예제 #6
0
        public void TriggerEvent(string eventName, byte[] argsSerialized, string sourceString)
        {
            try
            {
                var obj = MsgPackDeserializer.Deserialize(argsSerialized) as List <object> ?? (IEnumerable <object>) new object[0];

                var scripts  = ms_definedScripts.ToArray();
                var objArray = obj.ToArray();

                foreach (var script in scripts)
                {
                    script.EventHandlers.Invoke(eventName, objArray);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
예제 #7
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);
            }
        }
예제 #8
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 }));
        }
예제 #9
0
        public static void TriggerEvent(string eventName, byte[] eventArgs, int eventSource)
        {
            try
            {
                IEnumerable <object> obj = MsgPackDeserializer.Deserialize(eventArgs) as List <object>;

                if (obj == null)
                {
                    obj = new object[0];
                }

                var scripts = ms_definedScripts.ToArray();

                foreach (var script in scripts)
                {
                    script.EventHandlers.Invoke(eventName, obj.ToArray());
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
예제 #10
0
        internal unsafe static object GetResult(Type type, byte *ptr)
        {
            if (type == typeof(string))
            {
                var nativeUtf8 = *(IntPtr *)&ptr[0];

                if (nativeUtf8 == IntPtr.Zero)
                {
                    return(null);
                }

                var len = 0;
                while (Marshal.ReadByte(nativeUtf8, len) != 0)
                {
                    ++len;
                }

                var buffer = new byte[len];
                Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length);
                return(Encoding.UTF8.GetString(buffer));
            }

            if (type == typeof(object))
            {
                var dataPtr    = *(IntPtr *)&ptr[0];
                var dataLength = *(long *)&ptr[8];

                byte[] data = new byte[dataLength];
                Marshal.Copy(dataPtr, data, 0, (int)dataLength);

                return(MsgPackDeserializer.Deserialize(data));
            }

            if (type.IsEnum)
            {
                return(Enum.ToObject(type, (int)GetResult(typeof(int), ptr)));
            }

            if (type.IsAssignableFrom(typeof(INativeValue)))
            {
                var a = (int)GetResultInternal(typeof(int), ptr);

                return(Activator.CreateInstance(type, a));
            }

            if (type == typeof(Vector3))
            {
                var x = *(float *)(&ptr[0]);
                var y = *(float *)(&ptr[8]);
                var z = *(float *)(&ptr[16]);

                return(new Vector3(x, y, z));
            }

            if (Marshal.SizeOf(type) <= 8)
            {
                return(GetResultInternal(type, ptr));
            }

            return(null);
        }
예제 #11
0
        internal static object GetResult(Type type, byte[] ptr)
        {
            if (type == typeof(string))
            {
                var nativeUtf8 = new IntPtr(BitConverter.ToInt64(ptr, 0));

                if (nativeUtf8 == IntPtr.Zero)
                {
                    return(null);
                }

                var len = 0;
                while (Marshal.ReadByte(nativeUtf8, len) != 0)
                {
                    ++len;
                }

                var buffer = new byte[len];
                Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length);
                return(Encoding.UTF8.GetString(buffer));
            }

            if (type == typeof(object))
            {
                var dataPtr    = new IntPtr(BitConverter.ToInt64(ptr, 0));
                var dataLength = BitConverter.ToInt64(ptr, 8);

                byte[] data = new byte[dataLength];
                Marshal.Copy(dataPtr, data, 0, (int)dataLength);

                return(MsgPackDeserializer.Deserialize(data));
            }

            if (type.IsEnum)
            {
                return(Enum.ToObject(type, (int)GetResult(typeof(int), ptr)));
            }

            if (type.IsAssignableFrom(typeof(INativeValue)))
            {
                var a = (int)GetResultInternal(typeof(int), ptr);

                return(Activator.CreateInstance(type, a));
            }

            if (type == typeof(Vector3))
            {
                var x = BitConverter.ToSingle(ptr, 0);
                var y = BitConverter.ToSingle(ptr, 8);
                var z = BitConverter.ToSingle(ptr, 16);

                return(new Vector3(x, y, z));
            }

            if (Marshal.SizeOf(type) <= 8)
            {
                return(GetResultInternal(type, ptr));
            }

            return(null);
        }