//////////////// protected CustomEntity(CustomEntityConstructor ctor) { var myctor = ctor as CustomEntityConstructor; if (this.OwnerPlayer != null) { this.OwnerPlayerWho = this.OwnerPlayer.whoAmI; this.OwnerPlayerUID = PlayerIdentityHelpers.GetProperUniqueId(this.OwnerPlayer); } else { this.OwnerPlayerWho = -1; this.OwnerPlayerUID = ""; } if (myctor != null) { this.Core = this.CreateCore(myctor); this.Components = this.CreateComponents(myctor); } else { this.Core = this.CreateCoreTemplate(); this.Components = this.CreateComponentsTemplate(); } }
internal SerializableCustomEntity(string typeName, CustomEntityCore core, IList <CustomEntityComponent> components, string playerUid) : base(null) { this.MyTypeName = typeName; this.Core = core; this.Components = components; this.OwnerPlayerUID = playerUid; this.OwnerPlayerWho = PlayerIdentityHelpers.GetPlayerByProperId(playerUid)?.whoAmI ?? -1; }
//////////////// internal CustomEntityCore(CustomEntityCore copy) { this.whoAmI = copy.whoAmI; this.DisplayName = copy.DisplayName; this.position = copy.position; this.velocity = copy.velocity; this.width = copy.width; this.height = copy.height; this.direction = copy.direction; }
protected override void ReadStream(BinaryReader reader) { int typeId = (ushort)reader.ReadUInt16(); byte ownerWho = (byte)reader.ReadByte(); int who = (ushort)reader.ReadUInt16(); string displayName = (string)reader.ReadString(); var pos = new Vector2 { X = (float)reader.ReadSingle(), Y = (float)reader.ReadSingle() }; int dir = (short)reader.ReadInt16(); int wid = (ushort)reader.ReadUInt16(); int hei = (ushort)reader.ReadUInt16(); var vel = new Vector2 { X = (float)reader.ReadSingle(), Y = (float)reader.ReadSingle() }; Type entType = CustomEntityManager.GetTypeById(typeId); if (entType == null) { //throw new HamstarException( "!ModHelpers.CustomEntity.ReadStream - Invalid entity type id " + typeId ); throw new HamstarException("Invalid entity type id " + typeId); } Player plr = ownerWho == (byte)255 ? null : Main.player[ownerWho]; var myentTemplate = (CustomEntity)PacketProtocolData.CreateInstance(entType); //var myentTemplate = (CustomEntity)Activator.CreateInstance( entType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null ); CustomEntityCore core = myentTemplate.CreateCoreTemplate(); IList <CustomEntityComponent> components = myentTemplate.CreateComponentsTemplate(); core.WhoAmI = who; core.DisplayName = displayName; core.Width = wid; core.Height = hei; core.Position = pos; core.Velocity = vel; core.Direction = dir; for (int i = 0; i < components.Count; i++) { components[i].ReadStreamForwarded(reader); components[i].InternalOnClone(); } this.MyTypeName = SerializableCustomEntity.GetTypeName(myentTemplate); this.CopyChangesFrom(core, components, plr); //LogHelpers.Log( "READ "+this ); }
internal static CustomEntity CreateRaw(Type mytype, CustomEntityCore core, IList <CustomEntityComponent> components) { var ent = (CustomEntity)Activator.CreateInstance(mytype, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null); ent.Core = core; ent.Components = components; ent.OwnerPlayerWho = -1; ent.OwnerPlayerUID = ""; ent.OnClone(); return(ent); }
internal void CopyChangesFrom(CustomEntityCore core, IList <CustomEntityComponent> components, Player ownerPlr = null) { this.Core = new CustomEntityCore(core); this.OwnerPlayerWho = ownerPlr != null ? ownerPlr.whoAmI : -1; this.OwnerPlayerUID = ownerPlr != null?PlayerIdentityHelpers.GetProperUniqueId(ownerPlr) : ""; this.Components = components.SafeSelect(c => c.InternalClone()).ToList(); this.ClearComponentCache(); if (!this.IsInitialized) { //throw new HamstarException( "!ModHelpers."+this.GetType().Name+".CopyChangesFrom - Not initialized post-copy." ); throw new HamstarException("Not initialized post-copy."); } }
internal static CustomEntity CreateRaw(Type mytype, CustomEntityCore core, IList <CustomEntityComponent> components, string ownerUid = "") { var ent = (CustomEntity)Activator.CreateInstance(mytype, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null); ent.Core = core; ent.Components = components; ent.OwnerPlayerWho = -1; ent.OwnerPlayerUID = ownerUid; Player plr = PlayerIdentityHelpers.GetPlayerByProperId(ownerUid); if (plr != null) { ent.OwnerPlayerWho = plr.whoAmI; } ent.OnClone(); return(ent); }
protected override void WriteStream(BinaryWriter writer) { if (!this.IsInitialized) { //throw new HamstarException( "!ModHelpers.SerializableCustomEntity.WriteStream - Not initialized." ); throw new HamstarException("Not initialized."); } if (Main.netMode != 1) { this.RefreshOwnerWho(); } CustomEntityCore core = this.Core; byte ownerWho = this.OwnerPlayerWho == -1 ? (byte)255 : (byte)this.OwnerPlayerWho; writer.Write((ushort)CustomEntityManager.GetIdByTypeName(this.MyTypeName)); writer.Write((byte)ownerWho); //LogHelpers.Log( "WRITE id: "+this.ID+", name: "+core.DisplayName+", templates: "+ CustomEntityTemplates.TotalEntityTemplates()); //LogHelpers.Log( "WRITE2 who: "+core.whoAmI+", component count: "+this.Components.Count ); writer.Write((ushort)core.whoAmI); writer.Write((string)core.DisplayName); writer.Write((float)core.position.X); writer.Write((float)core.position.Y); writer.Write((short)core.direction); writer.Write((ushort)core.width); writer.Write((ushort)core.height); writer.Write((float)core.velocity.X); writer.Write((float)core.velocity.Y); for (int i = 0; i < this.Components.Count; i++) { this.Components[i].WriteStreamForwarded(writer); } //LogHelpers.Log( "WRITE "+this.ToString()+" pos:"+ core.position ); }
//// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { IList <CustomEntityComponent> components = new List <CustomEntityComponent>(); IEnumerable <Type> allCompTypes = ReflectionHelpers.GetAllAvailableSubTypesFromMods(typeof(CustomEntityComponent)); IDictionary <string, Type> allCompTypeMap = allCompTypes.ToDictionary(t => t.Name, t => t); JObject jo = JObject.Load(reader); int mark = 0; try { string typeName = jo["MyTypeName"].ToObject <String>(); mark++; //int typeId = jo["TypeID"].ToObject<Int32>(); string playerUid = jo["OwnerPlayerUID"].ToObject <String>(); CustomEntityCore core = jo["Core"].ToObject <CustomEntityCore>(); string[] compNames = jo["ComponentNames"].ToObject <string[]>(); JToken rawComponents = jo["Components"]; mark++; Type[] compTypes = new Type[compNames.Length]; int i; for (i = 0; i < compNames.Length; i++) { if (!allCompTypeMap.ContainsKey(compNames[i])) { return(null); } compTypes[i] = allCompTypeMap[compNames[i]]; mark += 10; } mark++; i = 0; foreach (JObject obj in rawComponents) { Type compType = compTypes[i]; var comp = (CustomEntityComponent)Activator.CreateInstance(compType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null); this.ReadIntoComponentFromJson(comp, obj, serializer); //var comp = obj.ToObject( compType, serializer ); comp.InternalOnClone(); components.Add(comp); i++; mark += 100; } mark++; return(new SerializableCustomEntity(typeName, core, components, playerUid)); //Type entType = CustomEntityManager.GetTypeByName( typeName ); //if( entType == null ) { // return null; //} //return CustomEntity.CreateRaw( entType, core, components, playerUid ); //return (CustomEntity)Activator.CreateInstance( entType, // BindingFlags.NonPublic | BindingFlags.Instance, // null, // new object[] { core, components, playerUid }, // null ); } catch (Exception e) { LogHelpers.Warn("(" + mark + ") " + e.Message); return(null); } }