Exemple #1
0
        public void CreateMValue(out MValueConst mValue, object obj)
        {
            if (obj == null)
            {
                mValue = MValueConst.Nil;
                return;
            }

            int i;

            string[]      dictKeys;
            MValueConst[] dictValues;
            MValueWriter2 writer;

            switch (obj)
            {
            case IPlayer player:
                CreateMValuePlayer(out mValue, player);
                return;

            case IVehicle vehicle:
                CreateMValueVehicle(out mValue, vehicle);
                return;

            case IBlip blip:
                CreateMValueBlip(out mValue, blip);
                return;

            case ICheckpoint checkpoint:
                CreateMValueCheckpoint(out mValue, checkpoint);
                return;

            case bool value:
                CreateMValueBool(out mValue, value);
                return;

            case int value:
                CreateMValueInt(out mValue, value);
                return;

            case uint value:
                CreateMValueUInt(out mValue, value);
                return;

            case long value:
                CreateMValueInt(out mValue, value);
                return;

            case ulong value:
                CreateMValueUInt(out mValue, value);
                return;

            case double value:
                CreateMValueDouble(out mValue, value);
                return;

            case float value:
                CreateMValueDouble(out mValue, value);
                return;

            case string value:
                CreateMValueString(out mValue, value);
                return;

            case MValueConst value:
                mValue = value;
                return;

            case MValueConst[] value:
                CreateMValueList(out mValue, value, (ulong)value.Length);
                return;

            case Invoker value:
                CreateMValueFunction(out mValue, value.NativePointer);
                return;

            case MValueFunctionCallback value:
                CreateMValueFunction(out mValue, Alt.Server.Resource.CSharpResourceImpl.CreateInvoker(value));
                return;

            case Function function:
                CreateMValueFunction(out mValue,
                                     Alt.Server.Resource.CSharpResourceImpl.CreateInvoker(function.Call));
                return;

            case byte[] byteArray:
                CreateMValueByteArray(out mValue, byteArray);
                return;

            case IDictionary dictionary:
                dictKeys   = new string[dictionary.Count];
                dictValues = new MValueConst[dictionary.Count];
                i          = 0;
                foreach (var key in dictionary.Keys)
                {
                    if (key is string stringKey)
                    {
                        dictKeys[i++] = stringKey;
                    }
                    else
                    {
                        mValue = MValueConst.Nil;
                        return;
                    }
                }

                i = 0;
                foreach (var value in dictionary.Values)
                {
                    CreateMValue(out var elementMValue, value);
                    dictValues[i++] = elementMValue;
                }

                CreateMValueDict(out mValue, dictKeys, dictValues, (ulong)dictionary.Count);
                for (int j = 0, dictLength = dictionary.Count; j < dictLength; j++)
                {
                    dictValues[j].Dispose();
                }

                return;

            case ICollection collection:
                var length     = (ulong)collection.Count;
                var listValues = new MValueConst[length];
                i = 0;
                foreach (var value in collection)
                {
                    CreateMValue(out var elementMValue, value);
                    listValues[i++] = elementMValue;
                }

                CreateMValueList(out mValue, listValues, length);
                for (ulong j = 0; j < length; j++)
                {
                    listValues[j].Dispose();
                }

                return;

            case IDictionary <string, object> dictionary:
                dictKeys   = new string[dictionary.Count];
                dictValues = new MValueConst[dictionary.Count];
                i          = 0;
                foreach (var key in dictionary.Keys)
                {
                    dictKeys[i++] = key;
                }

                i = 0;
                foreach (var value in dictionary.Values)
                {
                    CreateMValue(out var elementMValue, value);
                    dictValues[i++] = elementMValue;
                }

                CreateMValueDict(out mValue, dictKeys, dictValues, (ulong)dictionary.Count);
                for (int j = 0, dictLength = dictionary.Count; j < dictLength; j++)
                {
                    dictValues[j].Dispose();
                }

                return;

            case IWritable writable:
                writer = new MValueWriter2();
                writable.OnWrite(writer);
                writer.ToMValue(out mValue);
                return;

            case IMValueConvertible convertible:
                writer = new MValueWriter2();
                convertible.GetAdapter().ToMValue(obj, writer);
                writer.ToMValue(out mValue);
                return;

            case Position position:
                CreateMValueVector3(out mValue, position);
                return;

            case Rotation rotation:
                CreateMValueVector3(out mValue, rotation);
                return;

            case Rgba rgba:
                CreateMValueRgba(out mValue, rgba);
                return;

            case short value:
                CreateMValueInt(out mValue, value);
                return;

            case ushort value:
                CreateMValueUInt(out mValue, value);
                return;

            case Vector3 position:
                CreateMValueVector3(out mValue, position);
                return;

            default:
                Alt.Log("can't convert type:" + obj.GetType());
                mValue = MValueConst.Nil;
                return;
            }
        }
 private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
 {
     Alt.Log(
         $"< ==== UNHANDLED EXCEPTION ==== > {Environment.NewLine} Received an unhandled exception from {sender?.GetType()}: " +
         (Exception)e.ExceptionObject);
 }
Exemple #3
0
        public void OnServerEvent(string name, ref MValueArray args)
        {
            if (parserServerEventHandlers.Count != 0 &&
                parserServerEventHandlers.TryGetValue(name, out var parserEventHandlers))
            {
                foreach (var parserEventHandler in parserEventHandlers)
                {
                    parserEventHandler.Call(ref args);
                }
            }

            MValue[] argArray = null;
            if (this.eventHandlers.Count != 0 && this.eventHandlers.TryGetValue(name, out var eventHandlers))
            {
                argArray = args.ToArray();
                foreach (var eventHandler in eventHandlers)
                {
                    try
                    {
                        eventHandler.Call(BaseBaseObjectPool, argArray);
                    }
                    catch (TargetInvocationException exception)
                    {
                        Alt.Log("exception at event:" + name + ":" + exception.InnerException);
                    }
                    catch (Exception exception)
                    {
                        Alt.Log("exception at event:" + name + ":" + exception);
                    }
                }
            }

            object[] argObjects = null;

            if (eventDelegateHandlers.Count != 0 && eventDelegateHandlers.TryGetValue(name, out var eventDelegates))
            {
                if (argArray == null)
                {
                    argArray = args.ToArray();
                }

                var length = argArray.Length;
                argObjects = new object[length];
                for (var i = 0; i < length; i++)
                {
                    argObjects[i] = argArray[i].ToObject(BaseBaseObjectPool);
                }

                foreach (var eventHandler in eventDelegates)
                {
                    eventHandler(argObjects);
                }
            }

            if (ServerEventEventHandler.HasEvents())
            {
                if (argArray == null)
                {
                    argArray = args.ToArray();
                }

                if (argObjects == null)
                {
                    var length = argArray.Length;
                    argObjects = new object[length];
                    for (var i = 0; i < length; i++)
                    {
                        argObjects[i] = argArray[i].ToObject(BaseBaseObjectPool);
                    }
                }

                foreach (var eventHandler in ServerEventEventHandler.GetEvents())
                {
                    eventHandler(name, argObjects);
                }
            }

            if (ServerCustomEventEventHandler.HasEvents())
            {
                foreach (var eventHandler in ServerCustomEventEventHandler.GetEvents())
                {
                    eventHandler(name, ref args);
                }
            }

            OnServerEventEvent(name, ref args, argArray, argObjects);
        }