public NetworkedObject(object Obj, int ID, ObjectNetworkedProperties Properties)
 {
     DirtyProps      = new HashSet <PropertyInfo>();
     this.Properties = Properties;
     this.Obj        = Obj;
     this.ID         = ID;
 }
Beispiel #2
0
        public static ObjectNetworkedProperties Hook(object Obj, SetOverrideAction SetOverride)
        {
            PropertyInfo[]            Props          = Obj.GetType().GetProperties().Where((P) => P.GetCustomAttribute(typeof(NetworkedAttribute)) != null).ToArray();
            ObjectNetworkedProperties NetworkedProps = new ObjectNetworkedProperties();

            TypeBuilder TypeBuilder = ModBuilder.DefineType("Overrides" + Rnd.Next().ToString() + Rnd.Next().ToString(), TypeAttributes.Public);

            string[] PropOverrideNames = new string[Props.Length];

            for (int i = 0; i < Props.Length; i++)
            {
                PropOverrideNames[i] = "_" + Rnd.Next().ToString() + Rnd.Next().ToString();

                MethodBuilder MethBuilder = TypeBuilder.DefineMethod(PropOverrideNames[i], MethodAttributes.Public | MethodAttributes.Static,
                                                                     typeof(void), new Type[] { typeof(object), Props[i].PropertyType });

                FieldInfo        OverridesField        = typeof(PropertyHooks).GetField(nameof(Overrides));
                PropertyInfo     OverridesFieldIndexer = OverridesField.FieldType.GetProperties().Where((P) => P.GetIndexParameters().Length > 0).FirstOrDefault();
                MemberExpression OverrideMember        = Expression.MakeMemberAccess(null, OverridesField);
                IndexExpression  OverrideVal           = Expression.MakeIndex(OverrideMember, OverridesFieldIndexer, new Expression[] { Expression.Constant(Overrides.Count + i) });

                ParameterExpression This        = Expression.Parameter(typeof(object), "This");
                ParameterExpression Val         = Expression.Parameter(Props[i].PropertyType, "Val");
                UnaryExpression     ValAsObject = Expression.Convert(Val, typeof(object));

                MethodCallExpression SetOverrideCall = Expression.Call(SetOverride.GetMethodInfo(), This, ValAsObject, OverrideVal);
                Expression.Lambda(SetOverrideCall, This, Val).CompileToMethod(MethBuilder);
            }

            Type OverridesType = TypeBuilder.CreateType();

            for (int i = 0; i < Props.Length; i++)
            {
                MethodInfo SetMethodInfo = Props[i].GetSetMethod(true);
                Overrides.Add(new PropertyOverride(HookHandle.CreateHook(SetMethodInfo, OverridesType.GetMethod(PropOverrideNames[i])).Hook(), NetworkedProps.Add(Props[i]), Obj));
            }

            return(NetworkedProps);
        }