Elin Decompiled Documentation EA 23.362.1 Stable
Loading...
Searching...
No Matches
SpriteReplacer.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text.RegularExpressions;
6using UnityEngine;
7
8public class SpriteReplacer
9{
10 public static Dictionary<string, SpriteReplacer> dictSkins = new Dictionary<string, SpriteReplacer>();
11
12 public static Dictionary<string, SpriteReplacer> dictOverrides = new Dictionary<string, SpriteReplacer>();
13
14 public static List<(string suffix, Func<Card, bool> when)> states = new List<(string, Func<Card, bool>)>();
15
16 public static Dictionary<string, string> dictModItems = new Dictionary<string, string>();
17
18 private static List<string> sortedModIds = new List<string>();
19
20 private static bool modIdsDirty = true;
21
22 private static Dictionary<(int, int, bool, string), List<string>> suffixKeys = new Dictionary<(int, int, bool, string), List<string>>();
23
24 private static HashSet<string> failedStates = new HashSet<string>();
25
27
28 public Dictionary<string, SpriteData> suffixes = new Dictionary<string, SpriteData>();
29
30 public Dictionary<string, bool> isChecked = new Dictionary<string, bool>();
31
32 public static Dictionary<string, SpriteReplacer> ListSkins()
33 {
34 List<string> list = new List<string>();
35 foreach (KeyValuePair<string, SpriteReplacer> dictSkin in dictSkins)
36 {
37 if (!File.Exists(dictSkin.Value.data.path + ".png"))
38 {
39 list.Add(dictSkin.Key);
40 }
41 }
42 foreach (string item in list)
43 {
44 dictSkins.Remove(item);
45 }
46 Dictionary<string, string> dictionary = ListSkinItems();
47 List<string> sortedIds = dictionary.Keys.OrderBy((string k) => k).ToList();
48 foreach (KeyValuePair<string, string> item2 in dictionary)
49 {
50 item2.Deconstruct(out var key, out var value);
51 string text = key;
52 string path = value;
53 SpriteReplacer spriteReplacer = new SpriteReplacer
54 {
55 data = new SpriteData
56 {
57 path = path
58 }
59 };
60 spriteReplacer.BuildSuffixData(text, dictionary, sortedIds);
61 if (spriteReplacer.suffixes.TryGetValue("", out var value2))
62 {
63 spriteReplacer.data = value2;
64 }
65 else
66 {
67 spriteReplacer.data.Init();
68 }
69 dictSkins[text] = spriteReplacer;
70 }
71 return dictSkins;
72 }
73
74 public static Dictionary<string, string> ListSkinItems()
75 {
76 List<DirectoryInfo> list = new List<DirectoryInfo>();
77 list.Add(new DirectoryInfo(CorePath.custom + "Skin"));
78 list.AddRange(PackageIterator.GetDirectories("Skin", useCache: false));
79 IEnumerable<FileInfo> enumerable = list.SelectMany((DirectoryInfo d) => from f in d.GetFiles("*.png", SearchOption.TopDirectoryOnly)
80 orderby f.Name
81 select f);
82 Dictionary<string, string> dictionary = new Dictionary<string, string>();
83 foreach (FileInfo item in enumerable)
84 {
85 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(item.Name);
86 string value = Path.ChangeExtension(item.FullName, null);
87 dictionary[fileNameWithoutExtension] = value;
88 }
89 return dictionary;
90 }
91
92 public Sprite GetSprite(string suffix = "")
93 {
94 if (!suffixes.TryGetValue(suffix, out var value))
95 {
96 return null;
97 }
98 return value.GetSprite();
99 }
100
101 public Sprite GetSprite(int dir, int skin, bool snow)
102 {
103 return GetData(dir, skin, snow)?.GetSprite();
104 }
105
106 public SpriteData GetData(int dir, int skin, bool snow, Card card = null)
107 {
108 SpriteData spriteData = FindData(dir, skin, snow, "");
109 if (card == null)
110 {
111 return spriteData;
112 }
113 foreach (var state in states)
114 {
115 string item = state.suffix;
116 Func<Card, bool> item2 = state.when;
117 SpriteData spriteData2 = FindData(dir, skin, snow, item);
118 if (spriteData2 == spriteData)
119 {
120 continue;
121 }
122 try
123 {
124 if (item2(card))
125 {
126 return spriteData2;
127 }
128 }
129 catch (Exception arg)
130 {
131 if (failedStates.Add(item))
132 {
133 Debug.LogError($"#sprite failed to load state {item} on {card.id}\n{arg}");
134 }
135 }
136 }
137 return spriteData;
138 }
139
140 public SpriteData FindData(int dir, int skin, bool snow, string state)
141 {
142 if (!suffixKeys.TryGetValue((dir, skin, snow, state), out var value))
143 {
144 value = new List<string>
145 {
146 $"_skin{skin}_dir{dir}",
147 $"_skin{skin}",
148 $"_dir{dir}",
149 ""
150 };
151 if (snow)
152 {
153 value = value.SelectMany((string k) => new string[2]
154 {
155 k + "_snow",
156 k
157 }).ToList();
158 }
159 if (state != "")
160 {
161 value = value.Select((string k) => state + k).Concat(value).ToList();
162 }
163 suffixKeys[(dir, skin, snow, state)] = value;
164 }
165 foreach (string item in value)
166 {
167 if (suffixes.TryGetValue(item, out var value2))
168 {
169 Sprite[] sprites = value2.sprites;
170 if (sprites == null || sprites.Length != 0)
171 {
172 return value2;
173 }
174 }
175 }
176 return data;
177 }
178
179 public void Validate()
180 {
181 data?.Validate();
182 foreach (SpriteData value in suffixes.Values)
183 {
184 value?.Validate();
185 }
186 }
187
188 public void BuildSuffixData(string id, IReadOnlyDictionary<string, string> dictTexItems, List<string> sortedIds = null)
189 {
190 List<string> list = sortedIds ?? dictTexItems.Keys.OrderBy((string k) => k).ToList();
191 int num = list.BinarySearch(id);
192 if (num < 0)
193 {
194 num = ~num;
195 }
196 for (int num2 = num; num2 < list.Count; num2++)
197 {
198 string text = list[num2];
199 if (!text.StartsWith(id))
200 {
201 break;
202 }
203 string text2 = text[id.Length..];
204 if (text2.StartsWith("_"))
205 {
206 text2 = string.Concat(from t in text2.Split('_').Skip(1)
207 orderby (!(t == "snow")) ? ((!Regex.IsMatch(t, "^dir\\d+$")) ? (Regex.IsMatch(t, "^skin\\d+$") ? 1 : 0) : 2) : 3
208 select "_" + t);
209 }
210 SpriteData spriteData = new SpriteData
211 {
212 path = dictTexItems[text]
213 };
214 spriteData.Init();
215 suffixes[text2] = spriteData;
216 Debug.Log("#sprite replacer " + text2.IsEmpty("<base>") + "/" + dictTexItems[text].ShortPath());
217 }
218 }
220 public void SortModItemIds()
221 {
222 if (sortedModIds.Count != dictModItems.Count || modIdsDirty)
223 {
224 sortedModIds = dictModItems.Keys.OrderBy((string k) => k).ToList();
225 modIdsDirty = false;
226 }
227 }
229 public void Reload(string id, RenderData renderData = null)
230 {
231 data = null;
232 suffixes.Clear();
233 try
234 {
236 if (dictModItems.ContainsKey(id))
237 {
239 }
240 else if (renderData != null && dictModItems.ContainsKey("Item/" + id))
241 {
243 }
244 suffixes.TryGetValue("", out data);
245 if (data != null)
246 {
247 Debug.Log(id + ":" + data.path);
248 }
249 }
250 catch (Exception ex)
251 {
252 Debug.LogError("#sprite error fetching sprite replacer '" + id + "' : " + ex);
253 }
254 isChecked[id] = true;
255 }
257 public bool HasSprite(string id, RenderData renderData = null)
258 {
259 if (!isChecked.GetValueOrDefault(id) || (data != null && data.id != id))
260 {
261 Reload(id, renderData);
262 }
263 return data != null;
264 }
265}
Definition: Card.cs:11
static string custom
Definition: CorePath.cs:159
void Init()
Definition: SpriteData.cs:46
string path
Definition: SpriteData.cs:14
void Validate()
Definition: SpriteData.cs:159
string id
Definition: SpriteData.cs:12
Sprite GetSprite()
Definition: SpriteData.cs:150
Dictionary< string, SpriteData > suffixes
SpriteData data
bool HasSprite(string id, RenderData renderData=null)
Dictionary< string, bool > isChecked
static Dictionary< string, string > ListSkinItems()
static HashSet< string > failedStates
static bool modIdsDirty
static Dictionary<(int, int, bool, string), List< string > > suffixKeys
Sprite GetSprite(string suffix="")
static Dictionary< string, SpriteReplacer > dictSkins
void Reload(string id, RenderData renderData=null)
SpriteData GetData(int dir, int skin, bool snow, Card card=null)
static Dictionary< string, string > dictModItems
static Dictionary< string, SpriteReplacer > dictOverrides
static List< string > sortedModIds
Sprite GetSprite(int dir, int skin, bool snow)
SpriteData FindData(int dir, int skin, bool snow, string state)
static List<(string suffix, Func< Card, bool > when)> states
void BuildSuffixData(string id, IReadOnlyDictionary< string, string > dictTexItems, List< string > sortedIds=null)
static Dictionary< string, SpriteReplacer > ListSkins()