protected override void EmitObjectMembers(CodeTypeDeclaration type, bool inherited) { base.EmitObjectMembers(type, inherited); type.DeclareMethod("I" + Decorator.Name + "Input", "Create", method => { method.Attributes |= MemberAttributes.Static; method.Statements.Expr("return new {0}().Input", Decorator.Name); }); }
private void EmitEventMethodOverride(bool global, CodeTypeDeclaration type, EventDecorator d) { if (global && d.Definition.GlobalSenders == GlobalEventSenders.None) { return; } if (!global && d.Definition.EntitySenders == EntityEventSenders.None) { return; } type.BaseTypes.Add(d.ListenerInterface); type.DeclareMethod(typeof(void).FullName, "OnEvent", method => { method.DeclareParameter(d.Name, "evnt"); }) .Attributes = MemberAttributes.Public; }
protected override void EmitObjectMembers(CodeTypeDeclaration type, bool inherited) { base.EmitObjectMembers(type, inherited); type.DeclareMethod(typeof(string).FullName, "ToString", method => { method.Attributes = MemberAttributes.Public | MemberAttributes.Override; string name = Decorator.Definition.Name; string properties = Decorator.Properties.Select((p, i) => " " + p.Definition.Name + "={{" + i + "}}").Join(""); string arguments = Decorator.Properties.Select((p, i) => ", this." + p.Definition.Name).Join(""); method.Statements.Expr("return System.String.Format(\"[" + name + properties + "]\"" + arguments + ")"); }); EmitCreate(type, inherited, "Raise"); EmitCreate(type, inherited, "Create"); }
public override void EmitObjectMembers(CodeTypeDeclaration type) { EmitSimplePropertyMembers(type, new CodeSnippetExpression("Storage"), null, false, Decorator.TriggerListener); PropertyStateSettings s = Decorator.Definition.StateAssetSettings; // don't emit this method if we we are pulling data from mecanim if (s.MecanimMode == MecanimMode.Disabled || s.MecanimDirection == MecanimDirection.UsingAscensionProperties) { type.DeclareMethod(typeof(void).FullName, Decorator.TriggerMethod, method => { // make sure this peer is allowed to modify this property EmitAllowedCheck(method.Statements); // update local trigger method.Statements.Expr( "Storage.Values[this.OffsetStorage + {0}].TriggerLocal.Update(Ascension.Networking.Core.Frame, true)", Decorator.OffsetStorage); // flag this property as changed EmitPropertyChanged(method.Statements, new CodeSnippetExpression("Storage")); }); } }
protected virtual void EmitFactory() { CodeTypeReference factoryInterface = new CodeTypeReference("Ascension.Networking.IFactory"); MetaType.DeclareProperty("Ascension.Networking.TypeId", "TypeId", get => { get.Expr("return TypeId"); }) .PrivateImplementationType = factoryInterface; MetaType.DeclareProperty("Ascension.Networking.UniqueId", "TypeKey", get => { get.Expr("return new Ascension.Networking.UniqueId({0})", Decorator.Definition.Guid.ToByteArray().Join(", ")); }) .PrivateImplementationType = factoryInterface; MetaType.DeclareProperty("System.Type", "TypeObject", get => { get.Expr("return typeof({0})", Decorator.EmitAsInterface ? Decorator.NameInterface : Decorator.Name); }).PrivateImplementationType = factoryInterface; MetaType.DeclareMethod(typeof(object).FullName, "Create", methoid => { methoid.Statements.Expr("return new {0}()", Decorator.Name); }).PrivateImplementationType = factoryInterface; }
private void EmitCreate(CodeTypeDeclaration type, bool inherited, string name) { if (Decorator.Definition.EntitySenders != EntityEventSenders.None) { type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.DeclareParameter("Ascension.Networking.AscensionEntity", "entity"); method.Statements.Expr("return Create(entity, Ascension.Networking.EntityTargets.Everyone)"); }); type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.DeclareParameter("Ascension.Networking.AscensionEntity", "entity"); method.DeclareParameter("Ascension.Networking.EntityTargets", "targets"); method.Statements.Expr("if (!entity) throw new System.ArgumentNullException(\"entity\")"); method.Statements.Expr( "if (!entity.IsAttached) throw new Ascension.Networking.AscensionException(\"You can not raise events on entities which are not attached\")"); switch (Decorator.Definition.EntitySenders) { case EntityEventSenders.OnlyOwner: method.Statements.Expr( "if (!entity.IsOwner) throw new Ascension.Networking.AscensionException(\"You are not the owner of {{0}}, you can not raise this event\", entity)"); break; case EntityEventSenders.OnlyController: method.Statements.Expr( "if (!entity.HasControl) throw new Ascension.Networking.AscensionException(\"You are not the controller of {{0}}, you can not raise this event\", entity)"); break; } method.Statements.Expr("{0} evt", Decorator.Definition.Name); method.Statements.Expr("evt = new {0}()", Decorator.Definition.Name); method.Statements.Expr("evt.Targets = (byte) targets", Decorator.Definition.EntityTargets); method.Statements.Expr("evt.TargetEntity = entity.entity"); method.Statements.Expr("evt.Reliability = Ascension.Networking.ReliabilityModes.Unreliable"); method.Statements.Expr("return evt"); }); } if (Decorator.Definition.GlobalSenders != GlobalEventSenders.None) { type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Private | MemberAttributes.Static; method.DeclareParameter(typeof(byte).FullName, "targets"); method.DeclareParameter("Ascension.Networking.Connection", "connection"); method.DeclareParameter("Ascension.Networking.ReliabilityModes", "reliability"); switch (Decorator.Definition.GlobalSenders) { case GlobalEventSenders.OnlyServer: method.Statements.Expr( "if (!Ascension.Networking.Core.IsServer) throw new Ascension.Networking.AscensionException(\"You are not the server of, you can not raise this event\")"); break; case GlobalEventSenders.OnlyClients: method.Statements.Expr( "if (!Ascension.Networking.Core.IsClient) throw new Ascension.Networking.AscensionException(\"You are not the a client, you can not raise this event\")"); break; } method.Statements.Expr("{0} evt", Decorator.Definition.Name); method.Statements.Expr("evt = new {0}()", Decorator.Definition.Name); method.Statements.Expr("evt.Targets = targets"); method.Statements.Expr("evt.TargetConnection = connection"); method.Statements.Expr("evt.Reliability = reliability"); method.Statements.Expr("return evt"); }); type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.DeclareParameter("Ascension.Networking.GlobalTargets", "targets"); method.Statements.Expr("return Create((byte) targets, null, Ascension.Networking.ReliabilityModes.ReliableOrdered)"); }); type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.DeclareParameter("Ascension.Networking.GlobalTargets", "targets"); method.DeclareParameter("Ascension.Networking.ReliabilityModes", "reliability"); method.Statements.Expr("return Create((byte) targets, null, reliability)"); }); type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.DeclareParameter("Ascension.Networking.Connection", "connection"); method.Statements.Expr( "return Create(Ascension.Networking.Event.GLOBAL_SPECIFIC_CONNECTION, connection, Ascension.Networking.ReliabilityModes.ReliableOrdered)"); }); type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.DeclareParameter("Ascension.Networking.Connection", "connection"); method.DeclareParameter("Ascension.Networking.ReliabilityModes", "reliability"); method.Statements.Expr( "return Create(Ascension.Networking.Event.GLOBAL_SPECIFIC_CONNECTION, connection, reliability)"); }); type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.Statements.Expr( "return Create(Ascension.Networking.Event.GLOBAL_EVERYONE, null, Ascension.Networking.ReliabilityModes.ReliableOrdered)"); }); type.DeclareMethod(Decorator.Definition.Name, name, method => { DeclareObsolete(method, name); method.Attributes = MemberAttributes.Public | MemberAttributes.Static; method.DeclareParameter("Ascension.Networking.ReliabilityModes", "reliability"); method.Statements.Expr("return Create(Ascension.Networking.Event.GLOBAL_EVERYONE, null, reliability)"); }); } }
private void EmitListenerInterface() { CodeTypeDeclaration type = Generator.DeclareInterface(Decorator.ListenerInterface); type.DeclareMethod(typeof(void), "OnEvent", method => { method.DeclareParameter(Decorator.Name, "ev"); }); }
protected virtual void EmitMeta() { MetaType = Generator.DeclareClass(Decorator.NameMeta); //MetaType.TypeAttributes = TypeAttributes.NestedAssembly; //Needed to change the emitted code to be 'public' instead of 'internal' MetaType.Attributes = MemberAttributes.Public; MetaType.TypeAttributes = TypeAttributes.Public; MetaType.BaseTypes.Add(Decorator.BaseClassMeta); MetaType.BaseTypes.Add(Decorator.FactoryInterface); //MetaType.DeclareField(Decorator.NameMeta, "Instance").Attributes = MemberAttributes.Static | // MemberAttributes.Assembly; MetaType.DeclareField(Decorator.NameMeta, "Instance").Attributes = MemberAttributes.Static | MemberAttributes.Public; MetaType.DeclareConstructorStatic(ctor => { ctor.Statements.Add("Instance".Expr().Assign(Decorator.NameMeta.New())); ctor.Statements.Add("Instance".Expr().Call("InitMeta")); EmitMetaStaticCtor(ctor); }); // initialize object MetaType.DeclareMethod(typeof(void).FullName, "InitObject", method => { //method.Attributes = MemberAttributes.Assembly | MemberAttributes.Override; method.Attributes = MemberAttributes.Public | MemberAttributes.Override; method.DeclareParameter("Ascension.Networking.NetworkObj", "obj"); method.DeclareParameter("Ascension.Networking.NetworkObj_Meta.Offsets", "offsets"); DomBlock block; block = new DomBlock(method.Statements); for (int i = 0; i < Decorator.Properties.Count; ++i) { block.Stmts.Comment(""); block.Stmts.Comment("Property: " + Decorator.Properties[i].Definition.Name); PropertyCodeEmitter.Create(Decorator.Properties[i]).EmitObjectSetup(block); } EmitObjectInit(method); }); // initialize meta class MetaType.DeclareMethod(typeof(void).FullName, "InitMeta", method => { //method.Attributes = MemberAttributes.Assembly | MemberAttributes.Override; method.Attributes = MemberAttributes.Public | MemberAttributes.Override; DomBlock block; block = new DomBlock(method.Statements); block.Stmts.Comment("Setup fields"); block.Stmts.Expr("this.TypeId = new Ascension.Networking.TypeId({0})", Decorator.TypeId); block.Stmts.Expr("this.CountStorage = {0}", Decorator.CountStorage); block.Stmts.Expr("this.CountObjects = {0}", Decorator.CountObjects); block.Stmts.Expr("this.CountProperties = {0}", Decorator.CountProperties); block.Stmts.Expr("this.Properties = new Ascension.Networking.NetworkPropertyInfo[{0}]", Decorator.CountProperties); EmitMetaInit(method); for (int i = 0; i < Decorator.Properties.Count; ++i) { block.Stmts.Comment(""); block.Stmts.Comment("Property: " + Decorator.Properties[i].Definition.Name); PropertyCodeEmitter.Create(Decorator.Properties[i]).EmitMetaSetup(block); } block.Stmts.Expr("base.InitMeta()"); }); // emit factory interface EmitFactory(); }