Esempio n. 1
0
        public override void Handle(ByteReader data)
        {
            object target = Activator.CreateInstance(delegateType);

            for (int i = 0; i < fieldPaths.Length; i++)
            {
                string path       = fieldPaths[i];
                string noTypePath = MpReflection.RemoveType(path);
                Type   fieldType  = fieldTypes[i];
                object value;

                if (fieldType.IsCompilerGenerated())
                {
                    value = Activator.CreateInstance(fieldType);
                }
                else
                {
                    value = Sync.ReadSyncObject(data, fieldType);
                }

                if (value == null)
                {
                    if (cancelIfAnyNullBlacklist != null && !cancelIfAnyNullBlacklist.Contains(noTypePath))
                    {
                        return;
                    }

                    if (path.EndsWith("$this"))
                    {
                        return;
                    }

                    if (cancelIfNull != null && cancelIfNull.Contains(noTypePath))
                    {
                        return;
                    }
                }

                if (removeNullsFromLists != null && removeNullsFromLists.Contains(noTypePath) && value is IList list)
                {
                    list.RemoveNulls();
                }

                MpReflection.SetValue(target, path, value);
            }

            if (context.HasFlag(SyncContext.MapSelected) && cancelIfNoSelectedObjects && Find.Selector.selected.Count == 0)
            {
                return;
            }

            object[] parameters = Sync.ReadSyncObjects(data, argTypes);

            MpLog.Log("Invoked delegate method " + method + " " + delegateType);
            method.Invoke(target, parameters);
        }
Esempio n. 2
0
        public SyncDelegate(Type delegateType, MethodInfo method, string[] inPaths) :
            base(delegateType, null, method, null)
        {
            if (inPaths == null)
            {
                List <string> fieldList = new List <string>();
                AllDelegateFieldsRecursive(delegateType, path => { fieldList.Add(path); return(false); });
                fieldPaths = fieldList.ToArray();
            }
            else
            {
                var temp = new UniqueList <string>();
                foreach (string path in inPaths.Select(p => MpReflection.AppendType(p, delegateType)))
                {
                    string[] parts     = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    string   increment = parts[0] + "/" + parts[1];
                    for (int i = 2; i < parts.Length; i++)
                    {
                        if (!MpReflection.PathType(increment).IsCompilerGenerated())
                        {
                            break;
                        }
                        temp.Add(increment);
                        increment += "/" + parts[i];
                    }

                    temp.Add(path);
                }

                fieldPaths = temp.ToArray();
            }

            fieldTypes        = fieldPaths.Select(path => MpReflection.PathType(path)).ToArray();
            fieldPathsNoTypes = fieldPaths.Select(path => MpReflection.RemoveType(path)).ToArray();
            fieldTransformers = new SyncTransformer[fieldTypes.Length];
        }