Elin Decompiled Documentation EA 23.312 Nightly Patch 4
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/" + text4))
111 {
112 CustomBiographyContent customBiographyContent = CustomBiographyContent.CreateFromId(text4, owner);
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 if (!(trait2 is TraitChara traitChara))
144 {
145 goto IL_005f;
146 }
148 if (traitChara.AdvType == TraitChara.Adv_Type.None)
149 {
150 if (!(trait2 is TraitMerchant))
151 {
152 if (!(trait2 is TraitUniqueChara))
153 {
154 goto IL_005f;
155 }
156 spawnType = SpawnType.Unique;
157 }
158 else
159 {
160 spawnType = SpawnType.Merchant;
161 }
162 }
163 else
164 {
165 spawnType = SpawnType.Adventurer;
166 }
167 goto IL_0061;
168 IL_0061:
169 return _cachedSpawnTypes[trait] = spawnType;
170 IL_005f:
171 spawnType = SpawnType.Common;
172 goto IL_0061;
173 }
174
175 public void OnCharaCreated(Chara chara)
176 {
177 if (chara.GetBool("custom_content") || chara.GetBool("cwl_tags_applied") || chara.id != base.SourceId)
178 {
179 return;
180 }
181 chara.SetBool("custom_content", enable: true);
182 chara.SetStr("custom_content_id", base.ContentId);
183 chara.SetStr("custom_content_package", base.Owner.id);
184 string key2;
185 foreach (KeyValuePair<string, string> item in mapStr)
186 {
187 item.Deconstruct(out var key, out key2);
188 string id2 = key;
189 string value = key2;
190 chara.SetStr(id2, value);
191 }
192 foreach (KeyValuePair<string, int> item2 in mapInt)
193 {
194 item2.Deconstruct(out key2, out var value2);
195 string id3 = key2;
196 int value3 = value2;
197 chara.SetInt(id3, value3);
198 }
199 if (things.Count > 0 || equipments.Count > 0)
200 {
201 chara.RemoveThings();
202 }
203 foreach (string[] item3 in equipments.Select((string t) => t.Split('#')))
204 {
205 if (!Enum.TryParse<Rarity>(item3.TryGet(1, returnNull: true), out var result))
206 {
207 result = Rarity.Random;
208 }
209 if (HasValidContentId(item3[0]))
210 {
211 chara.EQ_ID(item3[0], -1, result);
212 }
213 }
214 foreach (string[] item4 in things.Select((string t) => t.Split('#')))
215 {
216 if (!int.TryParse(item4.TryGet(1, returnNull: true), out var result2))
217 {
218 result2 = 1;
219 }
220 if (HasValidContentId(item4[0]))
221 {
222 chara.AddThing(item4[0]).SetNum(result2);
223 }
224 }
225 bool HasValidContentId(string id)
226 {
227 if (EClass.sources.cards.map.ContainsKey(id))
228 {
229 return true;
230 }
231 ModUtil.LogModError("source chara row '" + base.ContentId + "' has invalid addEq/addThing spec '" + id + "'", base.Owner);
232 return false;
233 }
234 }
235
236 public override void OnGameLoad(GameIOContext context)
237 {
238 if (spawnZones.Count == 0)
239 {
240 return;
241 }
242 Debug.Log($"#mod-content loading {this}");
243 ILookup<string, Chara> lookup = EClass.game.cards.globalCharas.Values.ToLookup((Chara c) => c.id);
244 List<Zone> list = new List<Zone>();
245 foreach (string spawnZone in spawnZones)
246 {
247 Zone zone = ModUtil.FindZoneByFullName(spawnZone);
248 if (zone != null)
249 {
250 list.Add(zone);
251 continue;
252 }
253 ModUtil.LogModError("source chara row '" + base.SourceId + "' has invalid addZone spec '" + spawnZone + "'", base.Owner);
254 }
255 List<Chara> list2 = lookup[base.SourceId].ToList();
256 int count = list2.Count;
257 if (list.Count == 0 && count == 0)
258 {
259 ModUtil.LogModError("source chara row '" + base.SourceId + "' has invalid addZone spec", base.Owner);
260 return;
261 }
262 int num = Math.Max(0, list.Count - count);
263 int num2 = count;
264 if (num2 <= 1)
265 {
266 if (num2 != 1)
267 {
268 goto IL_01d5;
269 }
270 if (IsAdventurer)
271 {
272 goto IL_017e;
273 }
274 }
275 else if (IsAdventurer)
276 {
277 ModUtil.LogModError("adventurer '" + base.SourceId + "' has spawned more than once", base.Owner);
278 return;
279 }
280 if (num == 0)
281 {
282 goto IL_017e;
283 }
284 goto IL_01d5;
285 IL_017e:
286 Debug.Log("#mod-content skipping existing character '" + base.SourceId + "', " + $"{count} at {string.Join('/', list2.Select((Chara c) => c.currentZone.ZoneFullName))}");
287 return;
288 IL_01d5:
289 for (int i = 0; i < num; i++)
290 {
291 Zone z2 = list[i];
292 Chara chara = SpawnToZone(z2);
293 if (chara == null)
294 {
295 ModUtil.LogModError("can't spawn character '" + base.SourceId + "'", base.Owner);
296 break;
297 }
298 if (IsAdventurer)
299 {
300 EClass.game.cards.listAdv.Add(chara);
301 break;
302 }
303 }
304 Chara SpawnToZone(Zone z)
305 {
306 Chara chara2 = CharaGen.Create(base.SourceId);
307 if (chara2.id == "chicken")
308 {
309 return null;
310 }
311 chara2.SetHomeZone(z);
312 chara2.MoveZone(z, ZoneTransition.EnterState.RandomVisit);
313 Debug.Log("#mod-content spawned character '" + base.SourceId + "' to " + z.ZoneFullName);
314 return chara2;
315 }
316 }
317
318 public override string ToString()
319 {
320 StringBuilder stringBuilder = new StringBuilder();
321 stringBuilder.AppendLine($"{base.ContentId}/{spawnType}/{base.Owner.id}");
322 stringBuilder.AppendLine($" - zones({spawnZones.Count})/{string.Join(';', spawnZones)}");
323 if (things.Count > 0)
324 {
325 stringBuilder.AppendLine($" - things({things.Count})/{string.Join(';', things)}");
326 }
327 if (equipments.Count > 0)
328 {
329 stringBuilder.AppendLine($" - equipments({equipments.Count})/{string.Join(';', equipments)}");
330 }
331 if (stocks.Count > 0)
332 {
333 stringBuilder.AppendLine(" - stocks/" + string.Join(';', stocks));
334 }
335 if (mapStr.Count > 0)
336 {
337 stringBuilder.AppendLine(" - str/" + string.Join(';', mapStr.Select((KeyValuePair<string, string> kv) => kv.Key + "=" + kv.Value)));
338 }
339 if (mapInt.Count > 0)
340 {
341 stringBuilder.AppendLine(" - int/" + string.Join(';', mapInt.Select((KeyValuePair<string, int> kv) => $"{kv.Key}={kv.Value}")));
342 }
343 return stringBuilder.ToString();
344 }
345}
$
Definition: ModManager.cs:76
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:2571
bool GetBool(string id)
Definition: Card.cs:2541
void SetBool(string id, bool enable)
Definition: Card.cs:2546
static Chara Create(string id, int lv=-1)
Definition: CharaGen.cs:17
Definition: Chara.cs:10
Chara SetHomeZone(Zone zone)
Definition: Chara.cs:1474
void MoveZone(string alias)
Definition: Chara.cs:3412
static CustomBiographyContent CreateFromId(string biographyId, ModPackage owner=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)
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:85