Exemplo n.º 1
0
 /// <summary>
 /// 观察另外一个对象的指定字段
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="propertyPath"></param>
 public virtual void Watch(BaseDataModelEntity entity, string propertyPath)
 {
     if (entity == null)
     {
         throw new BindingException("The DataEntity is null.");
     }
     watchTable.Watch(entity, propertyPath, OnEvent);
 }
Exemplo n.º 2
0
 /// <summary>
 /// 将所有的Entity存到字典当中
 /// </summary>
 /// <param name="entity"></param>
 public void Save(BaseDataModelEntity entity)
 {
     //保存ID=DataEntity到字典当中
     if (!AllEntity.ContainsKey(entity.objectID))
     {
         AllEntity.Add(entity.objectID, entity);
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 观察另外一个对象
 /// </summary>
 /// <param name="entity"></param>
 public virtual void Watch(BaseDataModelEntity entity)
 {
     if (entity == null)
     {
         throw new BindingException("The DataEntity is null.");
     }
     watchTable.Watch(entity, "*", OnEvent);
 }
Exemplo n.º 4
0
 /// <summary>
 /// 绑定物体DataModelBehaviour的数据实体,如果不能获取,则抛出异常;
 /// </summary>
 /// <param name="observer"></param>
 /// <param name="subject"></param>
 public void Binding(DataModelBehaviour observer, BaseDataModelEntity subject)
 {
     if (observer != null && subject != null)
     {
         observer.Watch(subject);
     }
     else
     {
         throw new BindingException("observer or subject could not be found.");
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 取消绑定物体,本函数中自动获取gameObject的DataModelBehaviour对象,
 /// 如果不能获取,则抛出异常;
 /// </summary>
 /// <param name="goObserver"></param>
 /// <param name="goSubject"></param>
 public void Unbinding(DataModelBehaviour observer, BaseDataModelEntity subject, string propertyPath)
 {
     if (observer != null && subject != null)
     {
         observer.Unwatch(subject, propertyPath);
     }
     else
     {
         throw new BindingException("observer or subject could not be found.");
     }
 }
Exemplo n.º 6
0
        public void UpdateWatchArrayList(BaseDataModelEntity targetEntity, FieldInfo fieldInfo, string operation, object newValue)
        {
            //获取Remove时没有产生歧义
            var method = fieldInfo.FieldType.GetMethod(operation);

            if (method == null && !operation.Equals("[]"))
            {
                return;
            }

            System.Type tType = fieldInfo.FieldType.GetElementType();

            switch (operation)
            {
            case "Add":
                //原值是int32,回放添加进去的默认为int64
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { newValue });
                break;

            case "Remove":
                newValue = _serviceSerializer.DeSerializerObject(newValue.ToString(), tType);
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { newValue });
                break;

            case "RemoveAt":
                //RemoveAt操作,newValue为Index
                int removeIndex = System.Convert.ToInt32(newValue);
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { removeIndex });
                break;

            case "Clear":
                method.Invoke(fieldInfo.GetValue(targetEntity), null);
                break;

            case "Insert":
                JObject insertJObject = (JObject)newValue;
                int     insertIndex   = insertJObject["Key"].ToObject <int>();
                object  insertValue   = insertJObject["Value"]; // _serviceSerializer.DeSerializerObject(insertJObject["Value"].ToString(), tType);
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { insertIndex, insertValue });
                break;

            case "[]":
                JObject keyValue = (JObject)newValue;
                int     index    = keyValue["Key"].ToObject <int>();
                object  value    = _serviceSerializer.DeSerializerObject(keyValue["Value"].ToString(), tType);
                method = fieldInfo.FieldType.GetMethod("set_Item");
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { index, value });
                break;

            default:
                break;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 更新字典
        /// </summary>
        public void UpdateWatchDic(BaseDataModelEntity targetEntity, FieldInfo fieldInfo, string operation, object newValue)
        {
            MemberInfo[] info = fieldInfo.FieldType.GetMethods();
            foreach (var item in info)
            {
                Debug.Log(item);
            }
            MethodInfo method = null;

#if NET_4_6 || NET_STANDARD_2_0
            System.Type keyType   = fieldInfo.FieldType.GenericTypeArguments[0];
            System.Type valueType = fieldInfo.FieldType.GenericTypeArguments[1];
#endif
#if NET_2_0_SUBSET || NET_2_0 || UNITY_5
            System.Type[] tType     = fieldInfo.FieldType.GenericTypeArguments();
            System.Type   keyType   = tType[0];
            System.Type   valueType = tType[1];
#endif
            switch (operation)
            {
            case "Add":
                method = fieldInfo.FieldType.GetMethod(operation);
                JObject addJObject = (JObject)newValue;
                object  addKey     = _serviceSerializer.DeSerializerObject(addJObject["Key"].ToString(), keyType);
                object  addValue   = _serviceSerializer.DeSerializerObject(addJObject["Value"].ToString(), valueType);
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { addKey, addValue });
                break;

            case "Remove":
                //获取Remove方法是产生了歧义,采用遍历获取的方式
                method = fieldInfo.FieldType.GetMethods().Where(p => p.Name == "Remove").Where(p => p.ReturnType == typeof(void)).First();
                object removeKey = _serviceSerializer.DeSerializerObject(newValue.ToString(), keyType);
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { removeKey });
                break;

            case "Clear":
                method = fieldInfo.FieldType.GetMethod(operation);
                method.Invoke(fieldInfo.GetValue(targetEntity), null);
                break;

            case "[]":
                JObject keyValue = (JObject)newValue;
                object  key      = _serviceSerializer.DeSerializerObject(keyValue["Key"].ToString(), keyType);
                object  value    = _serviceSerializer.DeSerializerObject(keyValue["Value"].ToString(), valueType);
                method = fieldInfo.FieldType.GetMethod("set_Item");
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { key, value });
                break;

            default:
                break;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 还原所有的entity
        /// </summary>
        /// <param name="entity"></param>
        public void Load(BaseDataModelEntity entity)
        {
            //如果在字典中查找不到,找不到的一定不是动态创建生成的(动态生成会立刻写入ID)
            if (!AllEntity.ContainsKey(entity.objectID))
            {
                //如果还不包含,则return
                if (!AllEntity.ContainsKey(entity.objectID))
                {
#if UNITY_EDITOR
                    Debug.Log("can't find entity with objectID: [" + entity.objectID + "___" + entity.GetType() + "],is not storagedata");
#endif
                    return;
                }
            }
            //深拷贝,进行数据还原

            string str       = _serviceSerializer.SerializerObject(AllEntity[entity.objectID]);
            object tmpEntity = _serviceSerializer.DeSerializerObject(str);

            PropertyInfo[] propertyInfos = tmpEntity.GetType().GetProperties();
            FieldInfo[]    fieldInfos    = tmpEntity.GetType().GetFields();

            PropertyInfo[] propertyInfos1 = entity.GetType().GetProperties();
            FieldInfo[]    fieldInfos1    = entity.GetType().GetFields();

            for (int i = 0; i < propertyInfos1.Length; i++)
            {
                for (int j = 0; j < propertyInfos1.Length; j++)
                {
                    if (propertyInfos1[i].Name.Equals(propertyInfos[j].Name))
                    {
                        propertyInfos1[i].SetValue(entity, propertyInfos[j].GetValue(tmpEntity));
                    }
                }
            }

            for (int i = 0; i < fieldInfos1.Length; i++)
            {
                for (int j = 0; j < fieldInfos1.Length; j++)
                {
                    if (fieldInfos1[i].Name.Equals(fieldInfos[j].Name))
                    {
                        fieldInfos1[i].SetValue(entity, fieldInfos[j].GetValue(tmpEntity));
                    }
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 更新容器类数值
        /// </summary>
        /// <param name="targetEntity"></param>
        /// <param name="propertyPath"></param>
        /// <param name="newValue"></param>
        public void UpdateWatchable(BaseDataModelEntity targetEntity, string propertyPath, object newValue)
        {
            string[] nameOfValue = propertyPath.Split('#');
            var      fieldInfo   = targetEntity.GetType().GetField(nameOfValue[0]);

            if (fieldInfo.FieldType == typeof(WatchableArrayList))
            {
                return;
                //UpdateWatchArrayList(targetEntity, fieldInfo, nameOfValue[1], newValue);
            }
            else if (fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(WatchableList <>))
            {
                UpdateWatchList(targetEntity, fieldInfo, nameOfValue[1], newValue);
            }
            else if (fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(WatchableDictionary <,>))
            {
                UpdateWatchDic(targetEntity, fieldInfo, nameOfValue[1], newValue);
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// 忽略某个属性的事件响应
 /// </summary>
 /// <param name="target"></param>
 /// <param name="propertyName"></param>
 public void OffFire(BaseDataModelEntity target, string propertyName)
 {
     watchTable.OffFire(target, propertyName);
 }
Exemplo n.º 11
0
 /// <summary>
 /// 取消观察另外一个对象
 /// </summary>
 /// <param name="obj"></param>
 public virtual void Unwatch(BaseDataModelEntity entity)
 {
     watchTable.Unwatch(entity);
 }
Exemplo n.º 12
0
 /// <summary>
 /// 取消观察另外一个对象
 /// </summary>
 /// <param name="obj"></param>
 public virtual void Unwatch(BaseDataModelEntity entity, string propertyName)
 {
     watchTable.Unwatch(entity, propertyName);
 }
Exemplo n.º 13
0
        /// <summary>
        /// 重新绑定实体对象
        /// </summary>
        /// <param name="entity"></param>
        public void Rebinding(BaseDataModelEntity entity)
        {
            //如果绑定对象为空则返回
            if (entity == null)
            {
                return;
            }

            //如果绑定对象为空实体则返回
            if (entity is EmptyEntity)
            {
                return;
            }

            //取消绑定、Watch、初始化状态
            RestoreSystem.RemoveDataModel(DataEntity.objectID);

            _bindingTable.Reset();
            isViewInitialized = false;

            //重新设置实体对象并监听实体对象
            this.DataEntity = entity;

            //当entity的ID为-1时表示对象为新创建对象,
            //此时需要生成一个ID
            if (entity.objectID == -1)
            {
                // this.DataEntity.objectID = ObjectIdentity.GenerateRuntimeId().id;
                //注册动态id,并添加到存储字典中
                DynamicIds.SetId(this);
            }

            //重监听
            ReWatch(this);

            //根据继承类属性和字段的Binding注解添加绑定条目
            PropertyInfo[] pInfos = this.GetType().GetProperties();
            FieldInfo[]    fInfos = this.GetType().GetFields();
            //获取公有属性

            foreach (PropertyInfo info in pInfos)
            {
                Binding attr = info.GetCustomAttribute <Binding>();
                if (attr == null)
                {
                    continue;
                }
                _bindingTable.AddItem(this.DataEntity, attr.EntityPropertyName, info.GetValue(this), "Value");
            }
            //获取公有字段
            foreach (FieldInfo info in fInfos)
            {
                Binding attr = info.GetCustomAttribute <Binding>();
                if (attr == null)
                {
                    continue;
                }
                _bindingTable.AddItem(this.DataEntity, attr.EntityPropertyName, info.GetValue(this), "Value");
            }

            //执行绑定过程
            List <BindingItem> items = _bindingTable.GetItems();

            foreach (BindingItem item in items)
            {
                this.watchTable.Watch(item.BindedView, item.BindedViewProperty, OnViewValueChanged);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// 更新list
        /// </summary>
        /// <param name="targetEntity"></param>
        /// <param name="fieldInfo"></param>
        /// <param name="operation"></param>
        /// <param name="newValue"></param>
        public void UpdateWatchList(BaseDataModelEntity targetEntity, FieldInfo fieldInfo, string operation, object newValue)
        {
            //获取Remove时没有产生歧义
            var method = fieldInfo.FieldType.GetMethod(operation);

            if (method == null && !operation.Equals("[]"))
            {
                return;
            }

#if NET_4_6 || NET_STANDARD_2_0
            System.Type tType = fieldInfo.FieldType.GenericTypeArguments[0];
#endif
#if NET_2_0_SUBSET || NET_2_0 || UNITY_5
            System.Type tType = fieldInfo.FieldType.GenericTypeArguments()[0];
#endif
            switch (operation)
            {
            case "Add":
                if (newValue is System.ValueType)
                {
                    newValue = _serviceSerializer.DeSerializerObject(newValue.ToString(), tType);
                    method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { newValue });
                }
                else
                {
                    method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { newValue });
                }
                break;

            case "Remove":
                if (newValue is System.ValueType)
                {
                    newValue = _serviceSerializer.DeSerializerObject(newValue.ToString(), tType);
                    method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { newValue });
                }
                else
                {
                    method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { newValue });
                }
                break;

            case "RemoveAt":
                //RemoveAt操作,newValue为Index
                int removeIndex = System.Convert.ToInt32(newValue);
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { removeIndex });
                //if (newValue is System.ValueType)
                //{
                //    int removeIndex = System.Convert.ToInt32(newValue);
                //    method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { removeIndex });
                //}
                //else
                //{
                //    method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { newValue });
                //}
                break;

            case "Clear":
                method.Invoke(fieldInfo.GetValue(targetEntity), null);
                break;

            case "Insert":
                JObject insertJObject = (JObject)newValue;
                int     insertIndex   = insertJObject["Key"].ToObject <int>();
                object  insertValue   = _serviceSerializer.DeSerializerObject(insertJObject["Value"].ToString(), tType);
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { insertIndex, insertValue });
                break;

            case "[]":
                JObject keyValue = (JObject)newValue;
                int     index    = keyValue["Key"].ToObject <int>();
                object  value    = _serviceSerializer.DeSerializerObject(keyValue["Value"].ToString(), tType);
                method = fieldInfo.FieldType.GetMethod("set_Item");
                method.Invoke(fieldInfo.GetValue(targetEntity), new object[] { index, value });
                break;

            default:
                break;
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// 回放设置对应的属性值
        /// </summary>
        /// <param name="item"></param>
        public void UpdateEntityValue(RecordItem item)
        {
            //获取实体对象实例
            BaseDataModelEntity targetEntity = GetModelEntityByUuid(item.ObjectId);

            if (targetEntity == null)
            {
                targetEntity = RestoreSystem.GetEntity(item.ObjectId);
            }
            if (targetEntity == null)
            {
                return;
            }
            if (item.PropertyPath.Contains("#"))
            {
                UpdateWatchable(targetEntity, item.PropertyPath, item.NewValue);
            }
            //通过Property方式查找属性并设置属性值
            PropertyInfo propertyInfo = null;

            propertyInfo = targetEntity.GetType().GetProperty(item.PropertyPath);
            if (propertyInfo != null)
            {
                if (propertyInfo.PropertyType == typeof(System.Single))
                {
                    System.Single _value = System.Convert.ToSingle(item.NewValue);
                    propertyInfo.SetValue(targetEntity, _value);
                }
                else if (propertyInfo.PropertyType == typeof(System.Int32))
                {
                    System.Int32 _value = System.Convert.ToInt32(item.NewValue);
                    propertyInfo.SetValue(targetEntity, _value);
                }
                else
                {
                    propertyInfo.SetValue(targetEntity, item.NewValue, null);
                }

                return;
            }

            //通过Field方式查找属性并设置属性值
            FieldInfo fieldInfo = targetEntity.GetType().GetField(item.PropertyPath);

            if (fieldInfo != null)
            {
                if (fieldInfo.FieldType == typeof(System.Single))
                {
                    System.Single _value = System.Convert.ToSingle(item.NewValue);
                    fieldInfo.SetValue(targetEntity, _value);
                }
                else if (fieldInfo.FieldType == typeof(System.Int32))
                {
                    System.Int32 _value = System.Convert.ToInt32(item.NewValue);
                    fieldInfo.SetValue(targetEntity, _value);
                }
                else
                {
                    fieldInfo.SetValue(targetEntity, item.NewValue);
                }
            }
        }