tModLoader v0.11.8.9
A mod to make and play Terraria mods
EntityDefinition.cs
Go to the documentation of this file.
1using Newtonsoft.Json;
2using System;
3using System.ComponentModel;
4using System.Globalization;
5using System.Reflection;
6using Terraria.ID;
8
10{
14 public abstract class EntityDefinition : TagSerializable
15 {
16 [DefaultValue("Terraria")]
17 public string mod;
18 [DefaultValue("None")]
19 public string name;
20
22 mod = "Terraria";
23 name = "None";
24 }
25
26 public EntityDefinition(string mod, string name) {
27 this.mod = mod;
28 this.name = name;
29 }
30
31 public EntityDefinition(string key) {
32 this.mod = "";
33 this.name = "";
34 string[] parts = key.Split(new char[] { ' ' }, 2);
35 if (parts.Length == 2) {
36 mod = parts[0];
37 name = parts[1];
38 }
39 }
40
41 public override bool Equals(object obj) {
43 if (p == null) {
44 return false;
45 }
46 return (mod == p.mod) && (name == p.name);
47 }
48
49 public override string ToString() => $"{mod} {name}";
50
51 public override int GetHashCode() {
52 return new { mod, name }.GetHashCode();
53 }
54
55 public bool IsUnloaded => Type == 0 && !(mod == "Terraria" && name == "None" || mod == "" && name == "");
56
57 [JsonIgnore]
58 public abstract int Type { get; }
59
61 return new TagCompound {
62 ["mod"] = mod,
63 ["name"] = name,
64 };
65 }
66 }
67
71 // JSONItemConverter should allow this to be used as a dictionary key.
72 [TypeConverter(typeof(ToFromStringConverter<ItemDefinition>))]
73 //[CustomModConfigItem(typeof(UIModConfigItemDefinitionItem))]
75 {
76 public ItemDefinition() : base() {
77 }
78 public ItemDefinition(int type) : base(ItemID.GetUniqueKey(type)) {
79 }
80 public ItemDefinition(string key) : base(key) {
81 }
82 public ItemDefinition(string mod, string name) : base(mod, name) {
83 }
84
85 public override int Type => ItemID.TypeFromUniqueKey(base.mod, base.name);
86
87 public static ItemDefinition FromString(string s) => new ItemDefinition(s);
88
89 public static readonly Func<TagCompound, ItemDefinition> DESERIALIZER = Load;
90 public static ItemDefinition Load(TagCompound tag) => new ItemDefinition(tag.GetString("mod"), tag.GetString("name"));
91 }
92
95 {
96 public ProjectileDefinition() : base() { }
97 public ProjectileDefinition(int type) : base(ProjectileID.GetUniqueKey(type)) { }
98 public ProjectileDefinition(string key) : base(key) { }
99 public ProjectileDefinition(string mod, string name) : base(mod, name) { }
100
101 public override int Type => ProjectileID.TypeFromUniqueKey(mod, name);
102
103 public static ProjectileDefinition FromString(string s) => new ProjectileDefinition(s);
104
105 public static readonly Func<TagCompound, ProjectileDefinition> DESERIALIZER = Load;
106 public static ProjectileDefinition Load(TagCompound tag) => new ProjectileDefinition(tag.GetString("mod"), tag.GetString("name"));
107 }
108
111 {
112 public NPCDefinition() : base() { }
113 public NPCDefinition(int type) : base(NPCID.GetUniqueKey(type)) { }
114 public NPCDefinition(string key) : base(key) { }
115 public NPCDefinition(string mod, string name) : base(mod, name) { }
116
117 public override int Type => NPCID.TypeFromUniqueKey(mod, name);
118
119 public static NPCDefinition FromString(string s) => new NPCDefinition(s);
120
121 public static readonly Func<TagCompound, NPCDefinition> DESERIALIZER = Load;
122 public static NPCDefinition Load(TagCompound tag) => new NPCDefinition(tag.GetString("mod"), tag.GetString("name"));
123 }
124
127 {
128 public PrefixDefinition() : base() { }
129 public PrefixDefinition(int type) : base(PrefixID.GetUniqueKey((byte)type)) { }
130 public PrefixDefinition(string key) : base(key) { }
131 public PrefixDefinition(string mod, string name) : base(mod, name) { }
132
133 public override int Type => PrefixID.TypeFromUniqueKey(mod, name);
134
135 public static PrefixDefinition FromString(string s) => new PrefixDefinition(s);
136
137 public static readonly Func<TagCompound, PrefixDefinition> DESERIALIZER = Load;
138 public static PrefixDefinition Load(TagCompound tag) => new PrefixDefinition(tag.GetString("mod"), tag.GetString("name"));
139 }
140
146 {
147 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
148 return destinationType != typeof(string); // critical for populating from json string. Does prevent compact json values though.
149 }
150
151 public override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType) {
152 if (sourceType == typeof(string)) {
153 return true;
154 }
155 return base.CanConvertFrom(context, sourceType);
156 }
157 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
158 if (value is string) {
159 MethodInfo parse = typeof(T).GetMethod("FromString", new Type[] { typeof(string) });
160 if (parse != null && parse.IsStatic && parse.ReturnType == typeof(T)) {
161 return parse.Invoke(null, new object[] { value });
162 }
163
164 throw new JsonException(string.Format(
165 "The {0} type does not have a public static FromString(string) method that returns a {0}.",
166 typeof(T).Name));
167 }
168 return base.ConvertFrom(context, culture, value);
169 }
170 }
171}
Classes implementing EntityDefinition serve to function as a way to save and load the identities of v...
ItemDefinition represents an Item identity. A typical use for this class is usage in ModConfig,...
static readonly Func< TagCompound, ItemDefinition > DESERIALIZER
static ItemDefinition FromString(string s)
static ItemDefinition Load(TagCompound tag)
static NPCDefinition FromString(string s)
static readonly Func< TagCompound, NPCDefinition > DESERIALIZER
static NPCDefinition Load(TagCompound tag)
static PrefixDefinition Load(TagCompound tag)
static readonly Func< TagCompound, PrefixDefinition > DESERIALIZER
static PrefixDefinition FromString(string s)
static ProjectileDefinition Load(TagCompound tag)
static ProjectileDefinition FromString(string s)
static readonly Func< TagCompound, ProjectileDefinition > DESERIALIZER
This TypeConverter facilitates converting to and from the string Type. This is necessary for Objects ...
override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
override bool CanConvertFrom(ITypeDescriptorContext context, System.Type sourceType)