Exemplo n.º 1
0
        public static string ConvertToString(Type type, object value, string errorString)
        {
            if (typeof(EntityType).IsAssignableFrom(type))
            {
                EntityType entityType = (EntityType)value;
                return(entityType.Name);
            }

            if (typeof(Entity).IsAssignableFrom(type))
            {
                Entity entity = (Entity)value;
                return(entity.UIN.ToString());
            }

            if (typeof(Type) == type)
            {
                return(((Type)value).FullName);
            }

            if (SimpleTypesUtils.IsSimpleType(type))
            {
                return(value.ToString());
            }

            if (errorString != null)
            {
                Log.Fatal("Entity System: Serialization for type \"{0}\" are not supported ({1}).", type.ToString(), errorString);
            }

            return(null);
        }
Exemplo n.º 2
0
        static void ConsoleCommand_ShowDebugInformationWindow(string arguments)
        {
            bool show = DebugInformationWindow.Instance == null;

            try
            {
                show = (bool)SimpleTypesUtils.GetSimpleTypeValue(typeof(bool), arguments);
            }
            catch { }

            ShowDebugInformationWindow(show);
        }
Exemplo n.º 3
0
 protected override bool OnLoad(TextBlock block)
 {
     if (!base.OnLoad(block))
     {
         return(false);
     }
     if (block.IsAttributeExist("value"))
     {
         this.aBX = SimpleTypesUtils.GetSimpleTypeValue(this.aBw, block.GetAttribute("value"));
     }
     return(true);
 }
Exemplo n.º 4
0
        private void OnConsoleConfigCommand(string arguments, object userData)
        {
            Config.Parameter parameter = (Config.Parameter)userData;

            if (string.IsNullOrEmpty(arguments))
            {
                object value = parameter.GetValue();
                Print(string.Format("Value: \"{0}\", Default value: \"{1}\"",
                                    value != null ? value : "(null)", parameter.DefaultValue));
                return;
            }

            try
            {
                if (parameter.Field != null)
                {
                    object value = SimpleTypesUtils.GetSimpleTypeValue(parameter.Field.FieldType,
                                                                       arguments);
                    if (value == null)
                    {
                        throw new Exception("Not simple type");
                    }
                    parameter.Field.SetValue(null, value);
                }
                else if (parameter.Property != null)
                {
                    object value = SimpleTypesUtils.GetSimpleTypeValue(parameter.Property.PropertyType,
                                                                       arguments);
                    if (value == null)
                    {
                        throw new Exception("Not simple type");
                    }
                    parameter.Property.SetValue(null, value, null);
                }
            }
            catch (FormatException e)
            {
                string s = "";
                if (parameter.Field != null)
                {
                    s = parameter.Field.FieldType.ToString();
                }
                else if (parameter.Property != null)
                {
                    s = parameter.Property.PropertyType.ToString();
                }
                Print(string.Format("Config : Invalid parameter format \"{0}\" {1}", s,
                                    e.Message), new ColorValue(1, 0, 0));
            }
        }
Exemplo n.º 5
0
 public override bool OnLinkedTextClick(string linkName)
 {
     if (linkName == "variableType")
     {
         Type variableType = this.VariableType;
         if (!LogicEditorFunctionality.Instance.ShowTypeDialog(null, ref variableType))
         {
             return(false);
         }
         if (variableType != this.VariableType)
         {
             if (this.abD != null)
             {
                 this.abD.SetForDeletion(false);
                 Entities.Instance.DeleteEntitiesQueuedForDeletion();
             }
             this.abC = variableType.Name;
             this.abd = null;
             if (SimpleTypesUtils.IsSimpleType(this.VariableType))
             {
                 this.abD = (LogicAction)Entities.Instance.Create(EntityTypes.Instance.GetByName("LogicGetConstantValueAction"), this);
                 ((LogicGetConstantValueAction)this.abD).ValueType = this.VariableType;
                 this.abD.PostCreate();
             }
         }
         return(true);
     }
     else
     {
         if (!(linkName == "valueAction"))
         {
             return(base.OnLinkedTextClick(linkName));
         }
         LogicAction logicAction = this.abD;
         if (!LogicEditorFunctionality.Instance.ShowActionDialog(this, this.abd, false, ref logicAction))
         {
             return(false);
         }
         this.abD = logicAction;
         return(true);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// 数据转换: 将<paramref name="strValue"/>转化成<paramref name="type"/>,输出<paramref name="outValue"/>
        /// </summary>
        /// <param name="type"></param>
        /// <param name="strValue"></param>
        /// <param name="errorString"></param>
        /// <param name="outValue"></param>
        /// <returns></returns>
        public static bool ConvertFromString(Type type, string strValue, string errorString, out object outValue)
        {
            outValue = null;
            try
            {
                #region Simple Type
                if (SimpleTypesUtils.IsSimpleType(type))
                {
                    if (strValue == "")
                    {
                        if (type == typeof(string))
                        {
                            outValue = "";
                            return(true);
                        }
                        if (errorString != null)
                        {
                            Log.Error("Entity System: Serialization error. The invalid value \"{0}\" for type \"{1}\" ({2}).", strValue, type, errorString);
                        }
                        return(false);
                    }

                    outValue = SimpleTypesUtils.GetSimpleTypeValue(type, strValue);
                    return(outValue != null);
                }
                #endregion

                #region Entity
                if (typeof(Entity).IsAssignableFrom(type))
                {
                    if (strValue == "null" || strValue == "")
                    {
                        return(true);
                    }

                    Entity loadingEntityBySerializedUIN = Entities.Instance.GetLoadingEntityBySerializedUIN(uint.Parse(strValue));
                    bool   isEntityExists = (EntitySystemWorld.Instance.IsSingle() || EntitySystemWorld.Instance.IsEditor()) && loadingEntityBySerializedUIN == null;
                    if (isEntityExists)
                    {
                        if (errorString != null)
                        {
                            Log.Error("Entity System: Serialization error. The entity with UIN \"{0}\" is not exists ({1}).", strValue, errorString);
                        }
                        return(false);
                    }

                    outValue = loadingEntityBySerializedUIN;
                    return(true);
                }
                #endregion

                #region EntityType
                if (typeof(EntityType).IsAssignableFrom(type))
                {
                    if (strValue == "null" || strValue == "")
                    {
                        return(true);
                    }

                    EntityType byName = EntityTypes.Instance.GetByName(strValue);
                    if (byName == null)
                    {
                        if (errorString != null)
                        {
                            Log.Error("Entity System: Serialization error. The entity type is not defined \"{0}\" ({1}).", strValue, errorString);
                        }

                        return(false);
                    }
                    outValue = byName;
                    return(true);
                }
                #endregion

                #region typeof(Type) == type
                if (typeof(Type) == type)
                {
                    if (strValue == "null" || strValue == "")
                    {
                        return(true);
                    }

                    if (strValue.Contains("Engine.UISystem.E"))
                    {
                        string[] rControl = new string[]
                        {
                            "VideoBox",
                            "Button",
                            "CheckBox",
                            "ComboBox",
                            "Control",
                            "EditBox",
                            "ListBox",
                            "ScrollBar",
                            "TabControl",
                            "TextBox"
                        };
                        for (int i = 0; i < rControl.Length; i++)
                        {
                            string str    = rControl[i];
                            bool   flag17 = strValue == "Engine.UISystem.E" + str;
                            if (flag17)
                            {
                                strValue = "Engine.UISystem." + str;
                                break;
                            }
                        }
                    }
                    Type       typeControl = null;
                    Assembly[] assemblies  = AppDomain.CurrentDomain.GetAssemblies();
                    for (int j = 0; j < assemblies.Length; j++)
                    {
                        Assembly assembly = assemblies[j];
                        typeControl = assembly.GetType(strValue);
                        if (typeControl != null)
                        {
                            break;
                        }
                    }
                    if (typeControl == null)
                    {
                        if (errorString != null)
                        {
                            Log.Error("Entity System: Serialization error. The entity type is not found \"{0}\" ({1}).", strValue, errorString);
                        }

                        return(false);
                    }

                    outValue = typeControl;
                    return(true);
                }
                #endregion

                if (errorString != null)
                {
                    Log.Fatal("Entity System: Serialization for type \"{0}\" are not supported ({1}).", type.ToString(), errorString);
                }
                return(false);
            }
            catch (FormatException ex)
            {
                if (errorString != null)
                {
                    Log.Error("Entity System: Serialization error: \"{0}\" ({1}).", ex.Message, errorString);
                }
                return(false);
            }
        }
Exemplo n.º 7
0
        public static bool SaveFieldValue(bool entitySerialize, object owner, FieldInfo field, TextBlock block, object defaultValue, string errorString)
        {
            if (field == null)
            {
                return(false);
            }

            object value = field.GetValue(owner);

            if (value == null)
            {
                return(true);
            }

            string serializeName = GetFieldSerializeName(entitySerialize, field);

            errorString += string.Format(", property: \"{0}\"", serializeName);

            bool IsList            = field.FieldType.IsGenericType && field.FieldType.Name == typeof(List <>).Name;
            bool isArray           = field.FieldType.IsArray;
            bool FieldIsCollection = IsList | isArray;

            if (FieldIsCollection)
            {
                if (isArray && field.FieldType.GetArrayRank() != 1)
                {
                    Log.Fatal("Entity System: Serialization of arrays are supported only for one dimensions arrays ({0}).", errorString);
                    return(false);
                }
                bool saveResult = SaveCollectionFieldValue(entitySerialize, owner, field, block, defaultValue, errorString);
                return(saveResult);
            }

            bool isEntityType        = typeof(EntityType).IsAssignableFrom(field.FieldType);
            bool isEntity            = typeof(Entity).IsAssignableFrom(field.FieldType);
            bool simpleSerialization = (SimpleTypesUtils.IsSimpleType(field.FieldType) | isEntityType | isEntity) || typeof(Type) == field.FieldType;

            if (simpleSerialization)
            {
                if (!entitySerialize & isEntity)
                {
                    Log.Fatal("Entity System: Serialization Entity classes in entity types is forbidden ({0}).", errorString);
                    return(false);
                }

                if (isEntity)
                {
                    Entity entity = (Entity)value;
                    if (!entity.AllowSaveHerit())
                    {
                        Log.Fatal("Entity System: Serialization error. The reference to entity which does not allow serialization ({0}). Field to serialize: \"{1}\".", entity.ToString(), errorString);
                        return(false);
                    }
                }
                string saveValueString        = ConvertToString(field.FieldType, value, errorString);
                string defaultSaveValueString = null;
                if (defaultValue != null)
                {
                    bool isEnum = field.FieldType.IsEnum;
                    if (isEnum)
                    {
                        defaultSaveValueString = Enum.GetName(field.FieldType, defaultValue);
                    }
                    else
                    {
                        defaultSaveValueString = defaultValue.ToString();
                    }
                }

                bool shouldSave = (defaultSaveValueString != null && defaultSaveValueString != saveValueString) || (defaultValue == null && saveValueString != "");
                if (shouldSave)
                {
                    block.SetAttribute(serializeName, saveValueString);
                }
                return(true);
            }

            TextBlock blockSerialize = block.AddChild(serializeName);
            Type      type           = field.FieldType;

            if (field.FieldType == typeof(LogicEntityObject))
            {
                type = field.GetValue(owner).GetType();
            }

            while (type != null)
            {
                FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                for (int m = 0; m < fields.Length; m++)
                {
                    FieldInfo fieldInfo = fields[m];
                    if (!IsFieldSerializable(fieldInfo, entitySerialize))
                    {
                        continue;
                    }
                    bool saveResult = SaveFieldValue(entitySerialize, value, fieldInfo, blockSerialize, errorString);
                    if (!saveResult)
                    {
                        return(false);
                    }
                }
                type = type.BaseType;
            }
            return(true);
        }
Exemplo n.º 8
0
        private static bool SaveCollectionFieldValue(
            bool entitySerialize, object obj, FieldInfo fieldInfo, TextBlock textBlock,
            object defaultObj, string errorMessage)
        {
            object value   = fieldInfo.GetValue(obj);
            bool   isArray = fieldInfo.FieldType.IsArray;

            List <object> items = new List <object>();

            #region 获得Collection中的元素 -> items
            if (isArray)
            {
                PropertyInfo propertyLength = fieldInfo.FieldType.GetProperty("Length");
                MethodInfo   methodGetValue = fieldInfo.FieldType.GetMethod("GetValue", new Type[]
                {
                    typeof(int)
                });
                int arrayLength = (int)propertyLength.GetValue(value, null);
                for (int i = 0; i < arrayLength; i++)
                {
                    object o = methodGetValue.Invoke(value, new object[] { i });
                    items.Add(o);
                }
            }
            else
            {
                PropertyInfo propertyCount = fieldInfo.FieldType.GetProperty("Count");
                PropertyInfo propertyItem  = fieldInfo.FieldType.GetProperty("Item");
                int          listCount     = (int)propertyCount.GetValue(value, null);
                for (int j = 0; j < listCount; j++)
                {
                    object o1 = propertyItem.GetValue(value, new object[] { j });
                    items.Add(o1);
                }
            }
            #endregion

            bool entityTypeDefaultSerialize = !entitySerialize && obj is EntityType && defaultObj != null;

            #region EntityType 如果是缺省值,就不保存
            if (entityTypeDefaultSerialize)
            {
                int itemsCount = 0;
                if (isArray)
                {
                    PropertyInfo propertyLength = fieldInfo.FieldType.GetProperty("Length");
                    itemsCount = (int)propertyLength.GetValue(defaultObj, null);
                }
                else
                {
                    PropertyInfo propertyCount = fieldInfo.FieldType.GetProperty("Count");
                    itemsCount = (int)propertyCount.GetValue(defaultObj, null);
                }
                if (items.Count == 0 & itemsCount == 0)
                {
                    return(true);
                }
            }
            #endregion

            Type typeElement = typeof(object);
            if (isArray)
            {
                typeElement = fieldInfo.FieldType.GetElementType();
            }
            else
            {
                typeElement = fieldInfo.FieldType.GetGenericArguments()[0];
            }

            string fieldSerializeName = GetFieldSerializeName(entitySerialize, fieldInfo);
            if (typeElement == typeof(string))
            {
                TextBlock stringsBlock = textBlock.AddChild(fieldSerializeName);
                for (int k = 0; k < items.Count; k++)
                {
                    object    item       = items[k];
                    TextBlock textBlock3 = stringsBlock.AddChild("item");
                    if (item != null)
                    {
                        textBlock3.SetAttribute("value", (string)item);
                    }
                    else
                    {
                        textBlock3.SetAttribute(NULL_ITEM, true.ToString());
                    }
                }
                return(true);
            }

            bool isEntityType          = typeof(EntityType).IsAssignableFrom(typeElement);
            bool isEntity              = typeof(Entity).IsAssignableFrom(typeElement);
            bool simpleTypeSerializing = (SimpleTypesUtils.IsSimpleType(typeElement) | isEntityType | isEntity) || typeof(Type) == typeElement;
            if (simpleTypeSerializing)
            {
                if (!entitySerialize & isEntity)
                {
                    Log.Fatal("Entity System: Serialization Entity classes in entity types is forbidden ({0}).", errorMessage);
                    return(false);
                }
                char itemSeperator = typeElement.IsPrimitive | isEntity? ' ' :  ';';

                StringBuilder stringBuilder = new StringBuilder();
                for (int l = 0; l < items.Count; l++)
                {
                    object item = items[l];
                    if (isEntity && item != null)
                    {
                        Entity entity = (Entity)item;
                        if (!entity.AllowSaveHerit())
                        {
                            Log.Fatal("Entity System: Serialization error. The reference to entity which does not allow serialization ({0}). Field to serialize: \"{1}\".", entity.ToString(), errorMessage);
                            return(false);
                        }
                    }

                    if (l != 0)
                    {
                        stringBuilder.Append(itemSeperator);
                    }

                    if (item != null)
                    {
                        stringBuilder.Append(ConvertToString(typeElement, item, errorMessage + ": " + fieldSerializeName));
                    }
                    else
                    {
                        stringBuilder.Append("null");
                    }
                }
                textBlock.SetAttribute(fieldSerializeName, stringBuilder.ToString());
                return(true);
            }

            TextBlock        textBlockField = textBlock.AddChild(fieldSerializeName);
            List <FieldInfo> list           = GetTypeSerializableFields(entitySerialize, typeElement);
            for (int m = 0; m < items.Count; m++)
            {
                object item = items[m];

                TextBlock textBlockItem = textBlockField.AddChild("item");
                if (item == null)
                {
                    textBlockItem.SetAttribute(NULL_ITEM, true.ToString());
                    continue;
                }
                for (int k = 0; k < list.Count; k++)
                {
                    FieldInfo elementField = list[k];
                    bool      _saveResult  = SaveFieldValue(entitySerialize, item, elementField, textBlockItem, errorMessage);
                    if (!_saveResult)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 9
0
        public static bool LoadFieldValue(bool entitySerialize, object owner, FieldInfo field, TextBlock block, string errorString)
        {
            string fieldName = GetFieldSerializeName(entitySerialize, field);

            errorString += string.Format(", property: \"{0}\"", fieldName);
            bool isList  = field.FieldType.IsGenericType && field.FieldType.Name == typeof(List <>).Name;
            bool isArray = field.FieldType.IsArray;

            if (isList || isArray)
            {
                if (isArray && field.FieldType.GetArrayRank() != 1)
                {
                    Log.Fatal("Entity System: Serialization of arrays are supported only for one dimensions arrays ({0}).", errorString);
                    return(false);
                }
                bool loadResult = LoadArrayFieldValue(entitySerialize, owner, field, block, errorString);
                return(loadResult);
            }

            bool isEntityType = typeof(EntityType).IsAssignableFrom(field.FieldType);
            bool isEntity     = typeof(Entity).IsAssignableFrom(field.FieldType);
            bool typeSupport  = (SimpleTypesUtils.IsSimpleType(field.FieldType) | isEntityType | isEntity) || typeof(Type) == field.FieldType;

            if (typeSupport)
            {
                if (!entitySerialize & isEntity)
                {
                    Log.Fatal("Entity System: Serialization Entity classes in entity types is forbidden ({0}).", errorString);
                    return(false);
                }
                bool fieldExists = block.IsAttributeExist(fieldName);
                if (fieldExists)
                {
                    string fieldValue = block.GetAttribute(fieldName);
                    object value;
                    bool   convertResult = ConvertFromString(field.FieldType, fieldValue, errorString, out value);
                    if (!convertResult)
                    {
                        return(false);
                    }
                    field.SetValue(owner, value);
                }
                return(true);
            }

            TextBlock textBlock = block.FindChild(fieldName);

            if (textBlock == null)
            {
                return(true);
            }

            object obj = field.GetValue(owner);

            if (obj == null)
            {
                obj = field.FieldType.InvokeMember("", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
                field.SetValue(owner, obj);
            }
            Type type = field.FieldType;

            if (type == typeof(LogicEntityObject))
            {
                type = field.GetValue(owner).GetType();
            }

            while (type != null)
            {
                FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                for (int j = 0; j < fields.Length; j++)
                {
                    FieldInfo fieldInfo = fields[j];
                    if (!IsFieldSerializable(fieldInfo, entitySerialize))
                    {
                        continue;
                    }

                    bool loadFailure = !LoadFieldValue(entitySerialize, obj, fieldInfo, textBlock, errorString);
                    if (loadFailure)
                    {
                        return(false);
                    }
                }
                type = type.BaseType;
            }

            if (obj.GetType().IsValueType)
            {
                field.SetValue(owner, obj);
            }

            return(true);
        }
Exemplo n.º 10
0
        private static bool LoadArrayFieldValue(bool entitySerialize, object targetObj, FieldInfo fieldInfo, TextBlock textBlock, string text)
        {
            bool isArray = fieldInfo.FieldType.IsArray;
            Type typeElement;

            if (isArray)
            {
                typeElement = fieldInfo.FieldType.GetElementType();
            }
            else
            {
                typeElement = fieldInfo.FieldType.GetGenericArguments()[0];
            }

            string    name       = GetFieldSerializeName(entitySerialize, fieldInfo);
            TextBlock blockChild = textBlock.FindChild(name);

            #region typeElement == typeof(string)
            if (typeElement == typeof(string))
            {
                if (blockChild != null)
                {
                    List <object> list = new List <object>(blockChild.Children.Count);
                    foreach (TextBlock current in blockChild.Children)
                    {
                        string currentValue = current.GetAttribute("value");
                        if (string.IsNullOrEmpty(currentValue) && string.Compare(current.GetAttribute(NULL_ITEM), "true", true) == 0)
                        {
                            currentValue = null;
                        }

                        list.Add(currentValue);
                    }
                    SetArrayFieldValue(targetObj, fieldInfo, list);
                }
                return(true);
            }
            #endregion

            #region Simple Type
            bool typeIsEntityType = typeof(EntityType).IsAssignableFrom(typeElement);
            bool typeIsEntity     = typeof(Entity).IsAssignableFrom(typeElement);
            if ((SimpleTypesUtils.IsSimpleType(typeElement) | typeIsEntityType | typeIsEntity) || typeof(Type) == typeElement)
            {
                if (!entitySerialize & typeIsEntity)
                {
                    Log.Fatal("Entity System: Serialization Entity classes in entity types is forbidden ({0}).", text);
                    return(false);
                }

                if (textBlock.IsAttributeExist(name))
                {
                    string   nameAttr = textBlock.GetAttribute(name);
                    string[] nameArr;
                    if (nameAttr.Length != 0)
                    {
                        char c      = ';';
                        bool flag11 = typeElement.IsPrimitive | typeIsEntity;
                        if (flag11)
                        {
                            c = ' ';
                        }
                        nameArr = nameAttr.Split(new char[] { c });
                    }
                    else
                    {
                        nameArr = new string[0];
                    }

                    List <object> values = new List <object>(nameArr.Length);
                    for (int i = 0; i < nameArr.Length; i++)
                    {
                        string strValue = nameArr[i];
                        object item;
                        bool   loadFailure = !ConvertFromString(typeElement, strValue, text, out item);
                        if (loadFailure)
                        {
                            return(false);
                        }

                        values.Add(item);
                    }
                    SetArrayFieldValue(targetObj, fieldInfo, values);
                }
                return(true);
            }
            #endregion

            if (blockChild != null)
            {
                List <FieldInfo> typeElementFields = GetTypeSerializableFields(entitySerialize, typeElement);
                List <object>    children          = new List <object>(blockChild.Children.Count);
                foreach (TextBlock textChild in blockChild.Children)
                {
                    object elementObject;
                    if (string.Compare(textChild.GetAttribute(NULL_ITEM), "true", true) != 0)
                    {
                        elementObject = typeElement.InvokeMember("", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, null, null);
                        if (elementObject == null)
                        {
                            Log.Fatal("EntitySystem: Serialization: InvokeMember failed for \"{0}\"", typeElement);
                            return(false);
                        }

                        using (List <FieldInfo> .Enumerator itElementField = typeElementFields.GetEnumerator())
                        {
                            while (itElementField.MoveNext())
                            {
                                FieldInfo currentField = itElementField.Current;
                                bool      loadOk       = LoadFieldValue(entitySerialize, elementObject, currentField, textChild, text);
                                if (!loadOk)
                                {
                                    return(false);
                                }
                            }
                            children.Add(elementObject);
                            continue;
                        }
                    }
                    children.Add(null);
                }
                SetArrayFieldValue(targetObj, fieldInfo, children);
            }
            return(true);
        }