public static void Weave(ModuleDefinition module, List <TypeDefinition> syncs, out Dictionary <FieldDefinition, MethodDefinition> gets, out Dictionary <FieldDefinition, MethodDefinition> sets) { sets = new Dictionary <FieldDefinition, MethodDefinition>(); gets = new Dictionary <FieldDefinition, MethodDefinition>(); foreach (TypeDefinition type in syncs) { List <FieldDefinition> fields = new List <FieldDefinition>(); foreach (FieldDefinition field in type.Fields) { if (field.CustomAttributes.Count > 0 && field.CustomAttributes[0].AttributeType.FullName == WeaverProgram.SyncFieldType.FullName) { fields.Add(field); } } if (fields.Count == 0) { return; } FieldDefinition dirty = FieldFactory.CreateField(module, type, "m_Dirty", FieldAttributes.Private, module.ImportReference(typeof(long))); //public bool IsDirty(); ModifyIsDirtyMethod(module, type, dirty); //get set method for (int i = 0; i < fields.Count; ++i) { FieldDefinition field = fields[i]; if (!CheckHelper.CheckSyncFiled(WeaverProgram.Server, type, field.FieldType.Resolve())) { continue; } string name = field.Name; MethodDefinition get = CreateGetMethod(module, type, field, name); MethodDefinition set = CreateSetMethod(module, type, field, name, dirty, i); PropertyDefinition propertyDefinition = new PropertyDefinition("Sync" + name, PropertyAttributes.None, field.FieldType) { GetMethod = get, SetMethod = set }; type.Properties.Add(propertyDefinition); gets.Add(field, get); sets.Add(field, set); } //Serialize MethodDefinition serialize = ResolveHelper.ResolveMethod(type, "Serialize"); ILProcessor processor = serialize.Body.GetILProcessor(); processor.Body.Instructions.Clear(); Instruction ins1 = processor.Create(OpCodes.Nop); Instruction ins2 = processor.Create(OpCodes.Ldarg_0); Instruction ins3 = processor.Create(OpCodes.Ldc_I8, 0L); Instruction ins4 = processor.Create(OpCodes.Stfld, dirty); Instruction ins5 = processor.Create(OpCodes.Nop); Instruction ins6 = processor.Create(OpCodes.Ret); processor.Append(processor.Create(OpCodes.Nop)); processor.Append(processor.Create(OpCodes.Ldarg_0)); processor.Append(processor.Create(OpCodes.Ldfld, dirty)); processor.Append(processor.Create(OpCodes.Ldc_I8, 0L)); processor.Append(processor.Create(OpCodes.Cgt)); processor.Append(processor.Create(OpCodes.Stloc_0)); processor.Append(processor.Create(OpCodes.Ldloc_0)); processor.Append(processor.Create(OpCodes.Brfalse, ins6)); processor.Append(processor.Create(OpCodes.Nop)); processor.Append(processor.Create(OpCodes.Ldarg_1)); processor.Append(processor.Create(OpCodes.Ldarg_0)); processor.Append(processor.Create(OpCodes.Ldfld, dirty)); processor.Append(BaseTypeFactory.CreateWriteInstruction(module, processor, dirty.FieldType.Resolve())); serialize.Body.Variables.Add(new VariableDefinition(module.ImportReference(typeof(bool)))); int offset = serialize.Body.Variables.Count; for (int i = 0; i < fields.Count; ++i) { WriteSerializeInstruction(module, serialize, processor, fields[i], dirty, i, offset); } processor.Append(ins1); processor.Append(ins2); processor.Append(ins3); processor.Append(ins4); processor.Append(ins5); processor.Append(ins6); } }
public static void Weave(ModuleDefinition module, List <TypeDefinition> syncs) { if (module == null || syncs.Count == 0) { return; } foreach (TypeDefinition type in syncs) { List <FieldDefinition> fields = new List <FieldDefinition>(); foreach (FieldDefinition field in type.Fields) { if (field.CustomAttributes.Count > 0 && field.CustomAttributes[0].AttributeType.FullName == WeaverProgram.SyncFieldType.FullName) { fields.Add(field); } } if (fields.Count == 0) { return; } //Deserialize MethodDefinition deserialize = ResolveHelper.ResolveMethod(type, "Deserialize"); ILProcessor processor = deserialize.Body.GetILProcessor(); processor.Body.Instructions.Clear(); Instruction ret = processor.Create(OpCodes.Ret); processor.Append(processor.Create(OpCodes.Nop)); processor.Append(processor.Create(OpCodes.Ldarg_1)); processor.Append(BaseTypeFactory.CreateReadInstruction(module, processor, typeof(long).ToString())); processor.Append(processor.Create(OpCodes.Stloc_0)); processor.Append(processor.Create(OpCodes.Ldloc_0)); processor.Append(processor.Create(OpCodes.Ldc_I8, 0L)); processor.Append(processor.Create(OpCodes.Cgt)); processor.Append(processor.Create(OpCodes.Stloc_1)); processor.Append(processor.Create(OpCodes.Ldloc_1)); processor.Append(processor.Create(OpCodes.Brfalse, ret)); deserialize.Body.Variables.Add(new VariableDefinition(module.ImportReference(typeof(long)))); deserialize.Body.Variables.Add(new VariableDefinition(module.ImportReference(typeof(bool)))); int offset = deserialize.Body.Variables.Count; for (int i = 0; i < fields.Count; ++i) { FieldDefinition field = fields[i]; if (!CheckHelper.CheckSyncFiled(WeaverProgram.Client, type, field.FieldType.Resolve())) { continue; } WriteDeserializeInstruction(module, deserialize, processor, field, i, offset); } processor.Append(processor.Create(OpCodes.Nop)); processor.Append(ret); } }