Elin Decompiled Documentation EA 23.342.1 Nightly
Loading...
Searching...
No Matches
CustomCharaContent.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Newtonsoft.Json;
6using UnityEngine;
7
8[JsonObject(MemberSerialization.OptOut)]
10{
11 public enum SpawnType
12 {
13 Common,
16 Unique
17 }
18
19 private static readonly Dictionary<string, SpawnType> _cachedSpawnTypes = new Dictionary<string, SpawnType>();
20
21 public List<string> equipments = new List<string>();
22
23 public Dictionary<string, int> mapInt = new Dictionary<string, int>();
24
25 public Dictionary<string, string> mapStr = new Dictionary<string, string>();
26
28
29 public List<string> spawnZones = new List<string>();
30
31 public List<string> stocks = new List<string>();
32
33 public List<string> things = new List<string>();
34
35 public override string SourceType => "SourceChara";
36
37 public bool IsAdventurer => spawnType == SpawnType.Adventurer;
38
40 {
41 string trait = r.trait.TryGet(0, returnNull: true) ?? "Chara";
42 if (owner == null)
43 {
44 owner = ModUtil.FindSourceRowPackage(r);
45 }
46 CustomCharaContent customCharaContent = new CustomCharaContent
47 {
48 ContentId = "Chara/" + r.id,
49 SourceId = r.id,
51 Owner = owner
52 };
53 string[] tag = r.tag;
54 for (int i = 0; i < tag.Length; i++)
55 {
56 var (text, text2, array) = CustomSourceContent.GetParams(tag[i]);
57 switch (text)
58 {
59 case "addZone":
60 case "addAdvZone":
61 if (!text2.IsEmpty())
62 {
63 customCharaContent.spawnZones.Add(text2);
64 }
65 break;
66 case "addEq":
67 case "addAdvEq":
68 case "addEquipment":
69 if (!text2.IsEmpty())
70 {
71 customCharaContent.equipments.Add(text2);
72 }
73 break;
74 case "addThing":
75 if (!text2.IsEmpty())
76 {
77 customCharaContent.things.Add(text2);
78 }
79 break;
80 case "addStock":
81 {
82 string text3 = text2.IsEmpty(r.id);
83 if (!ModUtil.HasContent("MerchantStock/" + text3))
84 {
85 CustomMerchantStock customMerchantStock = CustomMerchantStock.CreateFromId(text3, owner);
86 if (customMerchantStock == null)
87 {
88 break;
89 }
90 ModUtil.AddContent(customMerchantStock);
91 }
92 if (!customCharaContent.stocks.Contains(text3))
93 {
94 customCharaContent.stocks.Add(text3);
95 customCharaContent.mapStr["merchant_override"] = string.Join('|', customCharaContent.stocks);
96 }
97 break;
98 }
99 case "addDrama":
100 if (!text2.IsEmpty())
101 {
102 customCharaContent.mapStr["drama_override"] = text2;
103 }
104 break;
105 case "addBio":
106 case "addBiography":
107 {
108 string text4 = text2.IsEmpty(r.id);
109 customCharaContent.mapStr["biography_override"] = text4;
110 if (!ModUtil.HasContent("Biography/" + r.id))
111 {
112 CustomBiographyContent customBiographyContent = CustomBiographyContent.CreateFromId(text4, owner, r.id);
113 if (customBiographyContent != null)
114 {
115 ModUtil.AddContent(customBiographyContent);
116 }
117 }
118 break;
119 }
120 case "addFlag":
121 case "addInt":
122 customCharaContent.mapInt[array[0]] = array.TryGet(1, returnNull: true)?.ToInt() ?? 1;
123 break;
124 case "addFlagValue":
125 case "addStr":
126 if (!array.TryGet(1, returnNull: true).IsEmpty())
127 {
128 customCharaContent.mapStr[array[0]] = array[1];
129 }
130 break;
131 }
132 }
133 return customCharaContent;
134 }
135
136 public static SpawnType GetSpawnTypeFromTrait(string trait)
137 {
138 if (_cachedSpawnTypes.TryGetValue(trait, out var value))
139 {
140 return value;
141 }
142 Trait trait2 = ClassCache.Create<Trait>("Trait" + trait, "Elin");
143 SpawnType spawnType = ((trait2 is TraitAdventurer) ? SpawnType.Adventurer : ((trait2 is TraitMerchant) ? SpawnType.Merchant : ((trait2 is TraitUniqueChara) ? SpawnType.Unique : SpawnType.Common)));
144 return _cachedSpawnTypes[trait] = spawnType;
145 }
146
147 public void OnCharaCreated(Chara chara)
148 {
149 if (chara.GetBool("custom_content") || chara.id != base.SourceId)
150 {
151 return;
152 }
153 chara.SetBool("custom_content", enable: true);
154 chara.SetStr("custom_content_id", base.ContentId);
155 chara.SetStr("custom_content_package", base.Owner.id);
156 string key2;
157 foreach (KeyValuePair<string, string> item in mapStr)
158 {
159 item.Deconstruct(out var key, out key2);
160 string id = key;
161 string value = key2;
162 chara.SetStr(id, value);
163 }
164 foreach (KeyValuePair<string, int> item2 in mapInt)
165 {
166 item2.Deconstruct(out key2, out var value2);
167 string id2 = key2;
168 int value3 = value2;
169 chara.SetInt(id2, value3);
170 }
171 if (chara.GetBool("cwl_tags_applied"))
172 {
173 return;
174 }
175 if (things.Count > 0 || equipments.Count > 0)
176 {
177 chara.RemoveThings();
178 }
179 foreach (string[] item3 in equipments.Select((string t) => t.Split('#')))
180 {
181 if (!Enum.TryParse<Rarity>(item3.TryGet(1, returnNull: true), out var result))
182 {
183 result = Rarity.Random;
184 }
185 if (HasValidContentId(item3[0]))
186 {
187 chara.EQ_ID(item3[0], -1, result);
188 }
189 }
190 foreach (string[] item4 in things.Select((string t) => t.Split('#')))
191 {
192 if (!int.TryParse(item4.TryGet(1, returnNull: true), out var result2))
193 {
194 result2 = 1;
195 }
196 if (HasValidContentId(item4[0]))
197 {
198 chara.AddThing(item4[0]).SetNum(result2);
199 }
200 }
201 bool HasValidContentId(string text)
202 {
203 if (EClass.sources.cards.map.ContainsKey(text))
204 {
205 return true;
206 }
207 ModUtil.LogModError("source chara row '" + base.ContentId + "' has invalid addEq/addThing spec '" + text + "'", base.Owner);
208 return false;
209 }
210 }
211
212 public override void OnGameLoad(GameIOContext context)
213 {
214 if (spawnZones.Count == 0)
215 {
216 return;
217 }
218 Debug.Log($"#mod-content loading {this}");
219 ILookup<string, Chara> lookup = EClass.game.cards.globalCharas.Values.ToLookup((Chara c) => c.id);
220 List<Zone> list = new List<Zone>();
221 foreach (string spawnZone in spawnZones)
222 {
223 Zone zone = ModUtil.FindZoneByFullName(spawnZone);
224 if (zone != null)
225 {
226 list.Add(zone);
227 continue;
228 }
229 ModUtil.LogModError("source chara row '" + base.SourceId + "' has invalid addZone spec '" + spawnZone + "'", base.Owner);
230 }
231 List<Chara> list2 = lookup[base.SourceId].ToList();
232 int count = list2.Count;
233 if (list.Count == 0 && count == 0)
234 {
235 ModUtil.LogModError("source chara row '" + base.SourceId + "' has invalid addZone spec", base.Owner);
236 return;
237 }
238 int num = Math.Max(0, list.Count - count);
239 int num2 = count;
240 if (num2 <= 1)
241 {
242 if (num2 != 1)
243 {
244 goto IL_01d6;
245 }
246 if (IsAdventurer)
247 {
248 goto IL_017f;
249 }
250 }
251 else if (IsAdventurer)
252 {
253 Debug.LogWarning("#mod-content adventurer '" + base.SourceId + "' has spawned more than once");
254 return;
255 }
256 if (num == 0)
257 {
258 goto IL_017f;
259 }
260 goto IL_01d6;
261 IL_017f:
262 Debug.Log("#mod-content skipping existing character '" + base.SourceId + "', " + $"{count} at {string.Join(',', list2.Select((Chara c) => c.currentZone?.ZoneFullName))}");
263 return;
264 IL_01d6:
265 HashSet<Zone> occupied = list2.Select((Chara c) => c.homeZone).ToHashSet();
266 foreach (Zone item in list.Where((Zone z) => !occupied.Contains(z)).Take(num))
267 {
268 Chara chara = SpawnToZone(item);
269 if (chara == null)
270 {
271 ModUtil.LogModError("can't spawn character '" + base.SourceId + "'", base.Owner);
272 break;
273 }
274 if (IsAdventurer)
275 {
276 EClass.game.cards.listAdv.Add(chara);
277 break;
278 }
279 }
280 Chara SpawnToZone(Zone z)
281 {
282 Chara chara2 = CharaGen.Create(base.SourceId);
283 if (chara2.id == "chicken")
284 {
285 return null;
286 }
287 chara2.SetHomeZone(z);
288 chara2.MoveZone(z, ZoneTransition.EnterState.RandomVisit);
289 Debug.Log("#mod-content spawned character '" + base.SourceId + "' to " + z.ZoneFullName);
290 return chara2;
291 }
292 }
293
294 public override string ToString()
295 {
296 StringBuilder stringBuilder = new StringBuilder();
297 stringBuilder.AppendLine($"{base.ContentId}/{spawnType}/{base.Owner.id}");
298 stringBuilder.AppendLine($" - zones({spawnZones.Count})/{string.Join(';', spawnZones)}");
299 if (things.Count > 0)
300 {
301 stringBuilder.AppendLine($" - things({things.Count})/{string.Join(';', things)}");
302 }
303 if (equipments.Count > 0)
304 {
305 stringBuilder.AppendLine($" - equipments({equipments.Count})/{string.Join(';', equipments)}");
306 }
307 if (stocks.Count > 0)
308 {
309 stringBuilder.AppendLine(" - stocks/" + string.Join(';', stocks));
310 }
311 if (mapStr.Count > 0)
312 {
313 stringBuilder.AppendLine(" - str/" + string.Join(';', mapStr.Select((KeyValuePair<string, string> kv) => kv.Key + "=" + kv.Value)));
314 }
315 if (mapInt.Count > 0)
316 {
317 stringBuilder.AppendLine(" - int/" + string.Join(';', mapInt.Select((KeyValuePair<string, int> kv) => $"{kv.Key}={kv.Value}")));
318 }
319 return stringBuilder.ToString();
320 }
321}
Rarity
Definition: Rarity.cs:2
List< Chara > listAdv
Definition: CardManager.cs:64
GlobalCharaList globalCharas
Definition: CardManager.cs:46
string id
Definition: Card.cs:36
void SetStr(string id, string value=null)
Definition: Card.cs:2600
bool GetBool(string id)
Definition: Card.cs:2557
void SetBool(string id, bool enable)
Definition: Card.cs:2562
static Chara Create(string id, int lv=-1)
Definition: CharaGen.cs:17
Definition: Chara.cs:10
Chara SetHomeZone(Zone zone)
Definition: Chara.cs:1498
Zone homeZone
Definition: Chara.cs:271
void MoveZone(string alias)
Definition: Chara.cs:3548
static CustomBiographyContent CreateFromId(string biographyId, ModPackage owner=null, string charaId=null)
Dictionary< string, string > mapStr
Dictionary< string, int > mapInt
static SpawnType GetSpawnTypeFromTrait(string trait)
List< string > things
override void OnGameLoad(GameIOContext context)
List< string > spawnZones
override string ToString()
static CustomCharaContent CreateFromRow(SourceChara.Row r, ModPackage owner=null)
void OnCharaCreated(Chara chara)
override string SourceType
List< string > equipments
static readonly Dictionary< string, SpawnType > _cachedSpawnTypes
List< string > stocks
string ContentId
Definition: CustomContent.cs:6
static CustomMerchantStock CreateFromId(string stockId, ModPackage owner=null)
static string string string[] kv GetParams(string tag)
Definition: EClass.cs:6
static Game game
Definition: EClass.cs:9
static SourceManager sources
Definition: EClass.cs:43
CardManager cards
Definition: Game.cs:156
Dictionary< string, CardRow > map
Definition: SourceCard.cs:9
SourceCard cards
Definition: Trait.cs:7
Definition: Zone.cs:12
string ZoneFullName
Definition: Zone.cs:88