Exemplo n.º 1
0
        /// <summary>
        /// Copy value
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="copyField"></param>
        /// <returns></returns>
        public static bool CopyValueTo <TSource, TTarget>(this TSource source, TTarget target, bool copyField = false)
            where TSource : class
            where TTarget : TSource
        {
            if (source == null || target == null)
            {
                ZDebug.LogError("argument NULL");
                return(false);
            }

            foreach (PropertyInfo pi in source.GetType().GetProperties())
            {
                if (pi.CanRead && pi.CanWrite)
                {
                    pi.SetValue(target, pi.GetValue(source, null), null);
                }
            }

            if (copyField)
            {
                foreach (FieldInfo fi in source.GetType().GetFields())
                {
                    fi.SetValue(target, fi.GetValue(source));
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        private void Dispatch()
        {
            if (_events.Count > 0)
            {
                IEvent evt = _events.Dequeue();
                if (!_listeners.ContainsKey(evt.Name))
                {
                    ZDebug.LogError("Event " + evt.Name + " has no listeners");
                    return;
                }

                for (int i = 0; i < _listeners[evt.Name].Count; i++)
                {
                    IEventListener listener = _listeners[evt.Name][i];
                    if (listener == null)
                    {
                        _listeners[evt.Name].RemoveAt(i);
                    }
                    else
                    {
                        listener.HandleEvent(evt);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void Notify <T>(T sender, string propName) where T : class
        {
            if (sender == null)
            {
                ZDebug.LogError(string.Format("Notify property sender is null! type: {0}, propertyName: {1}", typeof(T),
                                              propName));
                return;
            }
            if (string.IsNullOrEmpty(propName))
            {
                ZDebug.LogError(string.Format("Notify property Name cannot be null or empty! sender: {0}, propertyName: {1}", sender,
                                              propName));
                return;
            }

            PropertyInfo prop = sender.GetType().GetProperty(propName);

            if (prop == null)
            {
                ZDebug.LogError(string.Format("Notify property not found! sender: {0}, propertyName: {1}", sender,
                                              propName));
                return;
            }
            Notify(prop);
        }
Exemplo n.º 4
0
        public void RemoveCommand <T>(string name) where T : ICommand
        {
            if (!HasCommand <T>(name))
            {
                ZDebug.LogError(string.Format("Error in {0} Command '{1}' not registered.", this, name));
                return;
            }

            lock (_commandList)
            {
                _commandList[name].Remove(typeof(T));
            }
        }
Exemplo n.º 5
0
        public void RemoveWire(string name)
        {
            if (!HasWire(name))
            {
                ZDebug.LogError(string.Format("Error in {0} Wire '{1}' not registered.", this, name));
                return;
            }

            lock (_wires)
            {
                _wires[name].Dispose();
                _wires.Remove(name);
            }
        }
Exemplo n.º 6
0
    public void StateChange(IGameState newState)
    {
        if (newState == null)
        {
            ZDebug.LogError("State error!");
            return;
        }
        if (CurrentState != null)
        {
            CurrentState.OnExit();
        }

        CurrentState = newState;

        CurrentState.OnEnter();
    }
Exemplo n.º 7
0
 /// <summary>
 /// Get raw value by CSVColumnAttribute or name
 /// </summary>
 /// <param name="attribute"></param>
 /// <param name="fields"></param>
 /// <param name="keys"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 private string GetRawValue(CSVColumnAttribute attribute, List <string> fields, List <string> keys, string name)
 {
     if (attribute.Column >= 0 && fields.Count > attribute.Column)
     {
         return(fields[attribute.Column]);
     }
     if (!string.IsNullOrEmpty(attribute.Key) && keys.Contains(attribute.Key))
     {
         return(fields[keys.IndexOf(attribute.Key)]);
     }
     if (keys.Contains(name))
     {
         return(fields[keys.IndexOf(name)]);
     }
     ZDebug.LogError(string.Format("Mapping Error! Column: {0}, Key: {1}, Name:{2}", attribute.Column,
                                   attribute.Key ?? "NULL", name));
     return(name);
 }
Exemplo n.º 8
0
        public void AddWire(IWire wire)
        {
            if (wire == null)
            {
                ZDebug.LogError(string.Format("Error in {0} Wire can't be null.", this));
                return;
            }

            if (HasWire(wire.Name))
            {
                ZDebug.LogError(string.Format("Error in {0} Wire '{1}' already registered.", this, wire.Name));
                return;
            }

            lock (_wires)
            {
                _wires.Add(wire.Name, wire);
                wire.Dispatcher = _dispatcher;
                wire.Init();
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Parse and set raw value
 /// </summary>
 /// <param name="member"></param>
 /// <param name="obj"></param>
 /// <param name="value"></param>
 /// <param name="defaultValue"></param>
 /// <param name="arraySeparator"></param>
 /// <param name="trueValues"></param>
 private void SetValue(MemberInfo member, object obj, string value, object defaultValue, char arraySeparator,
                       string[] trueValues)
 {
     if (member.MemberType == MemberTypes.Property)
     {
         MethodInfo set = ((PropertyInfo)member).GetSetMethod(true);
         if (set == null)
         {
             ZDebug.LogError(string.Format("CSV property must be writable! CSVData type: {0}, Property: {1}",
                                           member.DeclaringType, member.Name));
             return;
         }
         ((PropertyInfo)member).SetValue(obj,
                                         ParseRawValue(value, ((PropertyInfo)member).PropertyType, defaultValue, arraySeparator, trueValues),
                                         null);
     }
     else
     {
         ((FieldInfo)member).SetValue(obj,
                                      ParseRawValue(value, ((FieldInfo)member).FieldType, defaultValue, arraySeparator, trueValues));
     }
 }
Exemplo n.º 10
0
        public void AddView(IView view)
        {
            if (view == null)
            {
                ZDebug.LogError(string.Format("Error in {0} View can't be null.", this));
                return;
            }

            if (HasView(view.Name))
            {
                ZDebug.LogError(string.Format("Error in {0} View '{1}' already registered.", this, view.Name));
                return;
            }

            lock (_views)
            {
                _views.Add(view.Name, view);
                view.Dispatcher = _dispatcher;

                if (view.EventList != null)
                {
                    foreach (string name in view.EventList)
                    {
                        if (_viewsToNotify.ContainsKey(name))
                        {
                            _viewsToNotify[name].Add(view);
                        }
                        else
                        {
                            _viewsToNotify.Add(name, new List <IView> {
                                view
                            });
                        }
                    }
                }

                view.Init();
            }
        }
Exemplo n.º 11
0
    // int[] Arr=[3](1,2,3);
    void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            C.Add(i);
        }
        Debuger.EnableLog = true;
        string b = string.Format("<Color=cyan>雨松MOMO{0}</Color>", a);

        Debuger.Log(b);
        EGDebug.enable = true;
        EGDebug.LogBold("zgh");
        EGDebug.LogTitle("zz", 5, EGDebug.Color.blue);
        Debug.Log("1");
        Debug.LogError("2");
        Debug.LogWarning("2");
        Debug.LogErrorFormat("3", a);

        EGDebug.LogList(C);
        EGDebug.Log("hh", EGDebug.Color.black);
        ZDebug.Log("GG", ZDebug.Color.yellow);
        ZDebug.LogError("zh", ZDebug.Color.cyan);
    }
Exemplo n.º 12
0
        public void Register <T>(T bindable) where T : class
        {
            if (Attribute.IsDefined(bindable.GetType(), typeof(ViewModelAttribute)))
            {
                if (!_viewModels.TryAdd(bindable.GetType(), bindable))
                {
                    ZDebug.LogError(string.Format("Register mvvm binding failed. type: {0}, object: {1}", bindable.GetType(), bindable));
                }
            }
            if (Attribute.IsDefined(bindable.GetType(), typeof(ViewAttribute)))
            {
                if (!_views.SafeAdd(bindable))
                {
                    ZDebug.LogError(string.Format("Register mvvm binding failed. type: {0}, object: {1}", bindable.GetType(), bindable));
                }
            }

            _groups.Clear();

            foreach (object view in _views)
            {
                Type viewModelType =
                    ((ViewAttribute)Attribute.GetCustomAttribute(view.GetType(), typeof(ViewAttribute)))
                    .SourceType;
                if (viewModelType == null)
                {
                    ZDebug.LogError(string.Format("View class has no viewModel class! view type: {0}", view.GetType()));
                    continue;
                }
                object viewModel = _viewModels.TryGet(viewModelType);
                if (viewModel == null)
                {
                    ZDebug.LogWarning(
                        string.Format(
                            "It may happend when view has been registered but view model has not yet. View: {0}, ViewModel: {1}", view,
                            viewModelType));
                    continue;
                }

                foreach (
                    MemberInfo member in
                    view.GetType().GetMembers().Where(p => Attribute.IsDefined(p, typeof(BindingMemberAttribute))))
                {
                    BindingGroup group = new BindingGroup();
                    group.ViewMember = member;
                    group.View       = view;
                    group.ViewModel  = viewModel;

                    string bindingKey =
                        ((BindingMemberAttribute)Attribute.GetCustomAttribute(member, typeof(BindingMemberAttribute)))
                        .BindingKey;
                    switch (member.MemberType)
                    {
                    case MemberTypes.Event:
                    {
                        Delegate del = Delegate.CreateDelegate(((EventInfo)member).EventHandlerType, viewModel, bindingKey, false,
                                                               false);
                        if (del == null)
                        {
                            ZDebug.LogError(string.Format("Binding method to event failed! event: {0}, methodName: {1}", member, bindingKey));
                        }
                        else
                        {
                            ((EventInfo)member).AddEventHandler(view, del);
                        }
                    }
                    break;

                    case MemberTypes.Field:
                        break;

                    case MemberTypes.Method:
                    {
                        MethodInfo method = viewModelType.GetMethod(bindingKey);

                        if (method.ReturnType == ((MethodInfo)member).ReturnType)
                        {
                            if (method.GetParameters().Length == ((MethodInfo)member).GetParameters().Length)
                            {
                                bool ok = true;
                                for (int i = 0; i < method.GetParameters().Length; i++)
                                {
                                    if (method.GetParameters()[i].ParameterType != ((MethodInfo)member).GetParameters()[i].ParameterType)
                                    {
                                        ok = false;
                                    }
                                }
                                if (ok)
                                {
                                    group.ViewModelMember = method;
                                    break;
                                }
                            }
                        }

                        ZDebug.LogError(string.Format("View method does not match view model method! {0}, {1}",
                                                      member, method));
                    }
                    break;

                    case MemberTypes.Property:
                    {
                        if (!((PropertyInfo)member).CanRead || !((PropertyInfo)member).CanWrite)
                        {
                            ZDebug.LogError(string.Format("View property is not accessable! {2} Read: {0}, Write: {1}",
                                                          ((PropertyInfo)member).CanRead, ((PropertyInfo)member).CanWrite, member));
                            break;
                        }
                        PropertyInfo prop = viewModelType.GetProperty(bindingKey);
                        if (!prop.CanRead || !prop.CanWrite)
                        {
                            ZDebug.LogError(string.Format("View model property is not accessable! {2} Read: {0}, Write: {1}",
                                                          prop.CanRead, prop.CanWrite, prop));
                            break;
                        }
                        if (((PropertyInfo)member).PropertyType != prop.PropertyType)
                        {
                            ZDebug.LogError(string.Format("View property does not match view model property! {0}, {1}",
                                                          member, prop));
                            break;
                        }
                        group.ViewModelMember = prop;
                    }
                    break;

                    default:
                        ZDebug.LogError("Member type not surported for binding! type: " + member.MemberType);
                        break;
                    }

                    if (group.CanUpdate)
                    {
                        _groups.Add(group);
                    }
                }
            }

            foreach (object viewModel in _viewModels.Values)
            {
                if (viewModel is INotifyPropertyChanged)
                {
                    ((INotifyPropertyChanged)viewModel).PropertyChanged += (sender, args) => Notify(sender, args.PropertyName);
                }
            }
        }