Elin Decompiled Documentation EA 23.339.0 Nightly
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 UnityEngine;
6
7public class SpriteReplacer
8{
9 public static Dictionary<string, SpriteReplacer> dictSkins = new Dictionary<string, SpriteReplacer>();
10
11 public static Dictionary<string, string> dictModItems = new Dictionary<string, string>();
12
13 private static List<string> sortedModIds = new List<string>();
14
15 private static bool modIdsDirty = true;
16
18
19 public Dictionary<string, SpriteData> suffixes = new Dictionary<string, SpriteData>();
20
21 public Dictionary<string, bool> isChecked = new Dictionary<string, bool>();
22
23 public static Dictionary<string, SpriteReplacer> ListSkins()
24 {
25 List<string> list = new List<string>();
26 foreach (KeyValuePair<string, SpriteReplacer> dictSkin in dictSkins)
27 {
28 if (!File.Exists(dictSkin.Value.data.path + ".png"))
29 {
30 list.Add(dictSkin.Key);
31 }
32 }
33 foreach (string item in list)
34 {
35 dictSkins.Remove(item);
36 }
37 Dictionary<string, string> dictionary = ListSkinItems();
38 List<string> sortedIds = dictionary.Keys.OrderBy((string k) => k).ToList();
39 foreach (KeyValuePair<string, string> item2 in dictionary)
40 {
41 item2.Deconstruct(out var key, out var value);
42 string text = key;
43 string path = value;
44 SpriteReplacer spriteReplacer = new SpriteReplacer
45 {
46 data = new SpriteData
47 {
48 path = path
49 }
50 };
51 spriteReplacer.BuildSuffixData(text, dictionary, sortedIds);
52 if (spriteReplacer.suffixes.TryGetValue("", out var value2))
53 {
54 spriteReplacer.data = value2;
55 }
56 else
57 {
58 spriteReplacer.data.Init();
59 }
60 dictSkins[text] = spriteReplacer;
61 }
62 return dictSkins;
63 }
64
65 public static Dictionary<string, string> ListSkinItems()
66 {
67 List<DirectoryInfo> list = new List<DirectoryInfo>();
68 list.Add(new DirectoryInfo(CorePath.custom + "Skin"));
69 list.AddRange(PackageIterator.GetDirectories("Skin", useCache: false));
70 IEnumerable<FileInfo> enumerable = list.SelectMany((DirectoryInfo d) => from f in d.GetFiles("*.png", SearchOption.TopDirectoryOnly)
71 orderby f.Name
72 select f);
73 Dictionary<string, string> dictionary = new Dictionary<string, string>();
74 foreach (FileInfo item in enumerable)
75 {
76 string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(item.Name);
77 string value = Path.ChangeExtension(item.FullName, null);
78 dictionary[fileNameWithoutExtension] = value;
79 }
80 return dictionary;
81 }
82
83 public Sprite GetSprite(string suffix = "")
84 {
85 if (!suffixes.TryGetValue(suffix, out var value))
86 {
87 return null;
88 }
89 return value.GetSprite();
90 }
91
92 public Sprite GetSprite(int dir, int skin, bool snow)
93 {
94 foreach (string item in new List<string>
95 {
96 $"_skin{skin}_dir{dir}",
97 $"_skin{skin}",
98 $"_dir{dir}",
99 ""
100 })
101 {
102 Sprite sprite = null;
103 if (snow)
104 {
105 sprite = GetSprite(item + "_snow");
106 }
107 if ((object)sprite == null)
108 {
109 sprite = GetSprite(item);
110 }
111 if ((bool)sprite)
112 {
113 return sprite;
114 }
115 }
116 return data?.GetSprite();
117 }
118
119 public void Validate()
120 {
121 data?.Validate();
122 foreach (SpriteData value in suffixes.Values)
123 {
124 value?.Validate();
125 }
126 }
127
128 public void BuildSuffixData(string id, IReadOnlyDictionary<string, string> dictTexItems, List<string> sortedIds = null)
129 {
130 List<string> list = sortedIds ?? dictTexItems.Keys.OrderBy((string k) => k).ToList();
131 int num = list.BinarySearch(id);
132 if (num < 0)
133 {
134 num = ~num;
135 }
136 for (int num2 = num; num2 < list.Count; num2++)
137 {
138 string text = list[num2];
139 if (text.StartsWith(id))
140 {
141 string text2 = text[id.Length..];
142 SpriteData spriteData = new SpriteData
143 {
144 path = dictTexItems[text]
145 };
146 spriteData.Init();
147 suffixes[text2] = spriteData;
148 Debug.Log("#sprite replacer " + text2.IsEmpty("<base>") + "/" + dictTexItems[text].ShortPath());
149 continue;
150 }
151 break;
152 }
153 }
154
155 public void SortModItemIds()
156 {
157 if (sortedModIds.Count != dictModItems.Count || modIdsDirty)
158 {
159 sortedModIds = dictModItems.Keys.OrderBy((string k) => k).ToList();
160 modIdsDirty = false;
161 }
162 }
163
164 public void Reload(string id, RenderData renderData = null)
165 {
166 data = null;
167 suffixes.Clear();
168 try
169 {
171 if (dictModItems.ContainsKey(id))
172 {
174 }
175 else if (renderData != null && dictModItems.ContainsKey("Item/" + id))
176 {
178 }
179 suffixes.TryGetValue("", out data);
180 if (data != null)
181 {
182 Debug.Log(id + ":" + data.path);
183 }
184 }
185 catch (Exception ex)
186 {
187 Debug.LogError("#sprite error fetching sprite replacer '" + id + "' : " + ex);
188 }
189 isChecked[id] = true;
190 }
191
192 public bool HasSprite(string id, RenderData renderData = null)
193 {
194 if (!isChecked.GetValueOrDefault(id) || (data != null && data.id != id))
195 {
196 Reload(id, renderData);
197 }
198 return data != null;
199 }
200}
static string custom
Definition: CorePath.cs:159
void Init()
Definition: SpriteData.cs:34
string path
Definition: SpriteData.cs:12
void Validate()
Definition: SpriteData.cs:129
string id
Definition: SpriteData.cs:10
Sprite GetSprite()
Definition: SpriteData.cs:120
Dictionary< string, SpriteData > suffixes
SpriteData data
bool HasSprite(string id, RenderData renderData=null)
Dictionary< string, bool > isChecked
static Dictionary< string, string > ListSkinItems()
static bool modIdsDirty
Sprite GetSprite(string suffix="")
static Dictionary< string, SpriteReplacer > dictSkins
void Reload(string id, RenderData renderData=null)
static Dictionary< string, string > dictModItems
static List< string > sortedModIds
Sprite GetSprite(int dir, int skin, bool snow)
void BuildSuffixData(string id, IReadOnlyDictionary< string, string > dictTexItems, List< string > sortedIds=null)
static Dictionary< string, SpriteReplacer > ListSkins()