Elin Decompiled Documentation EA 23.295 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 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 public static Dictionary<string, string> dictTextureItems = new Dictionary<string, string>();
14
16
17 public Dictionary<string, SpriteData> suffixes = new Dictionary<string, SpriteData>();
18
19 public Dictionary<string, bool> isChecked = new Dictionary<string, bool>();
20
21 public static Dictionary<string, SpriteReplacer> ListSkins()
22 {
23 List<string> list = new List<string>();
24 foreach (KeyValuePair<string, SpriteReplacer> dictSkin in dictSkins)
25 {
26 if (!File.Exists(dictSkin.Value.data.path + ".png"))
27 {
28 list.Add(dictSkin.Key);
29 }
30 }
31 foreach (string item in list)
32 {
33 dictSkins.Remove(item);
34 }
35 Dictionary<string, string> dictionary = new DirectoryInfo(CorePath.custom + "Skin").GetFiles("*.png").ToDictionary((FileInfo f) => Path.GetFileNameWithoutExtension(f.Name), (FileInfo f) => Path.ChangeExtension(f.FullName, null));
36 string key2;
37 foreach (KeyValuePair<string, string> item2 in dictionary)
38 {
39 item2.Deconstruct(out var key, out key2);
40 string key3 = key;
41 string path = key2;
42 if (!dictSkins.ContainsKey(key3))
43 {
44 dictSkins[key3] = new SpriteReplacer
45 {
46 data = new SpriteData
47 {
48 path = path
49 }
50 };
51 }
52 }
53 foreach (KeyValuePair<string, SpriteReplacer> dictSkin2 in dictSkins)
54 {
55 dictSkin2.Deconstruct(out key2, out var value);
56 string id = key2;
57 value.BuildSuffixData(id, dictionary);
58 }
59 return dictSkins;
60 }
61
62 public Sprite GetSprite(string suffix = "")
63 {
64 if (!suffixes.TryGetValue(suffix, out var value))
65 {
66 return null;
67 }
68 return value.GetSprite();
69 }
70
71 public Sprite GetSprite(int dir, int skin, bool snow)
72 {
73 foreach (string item in new List<string>
74 {
75 $"_skin{skin}_dir{dir}",
76 $"_skin{skin}",
77 $"_dir{dir}",
78 ""
79 })
80 {
81 Sprite sprite = null;
82 if (snow)
83 {
84 sprite = GetSprite(item + "_snow");
85 }
86 if ((object)sprite == null)
87 {
88 sprite = GetSprite(item);
89 }
90 if ((bool)sprite)
91 {
92 return sprite;
93 }
94 }
95 return data?.GetSprite();
96 }
97
98 public void Validate()
99 {
100 data?.Validate();
101 foreach (SpriteData value in suffixes.Values)
102 {
103 value?.Validate();
104 }
105 }
106
108 {
109 dictTextureItems = new DirectoryInfo(CorePath.packageCore + "Texture/Item").GetFiles("*.png").ToDictionary((FileInfo f) => Path.GetFileNameWithoutExtension(f.Name), (FileInfo f) => Path.ChangeExtension(f.FullName, null));
110 }
111
112 public void BuildSuffixData(string id, Dictionary<string, string> dictTexItems)
113 {
114 foreach (var (text3, path) in dictTexItems)
115 {
116 if (text3.StartsWith(id))
117 {
118 string text4 = text3[id.Length..];
119 SpriteData spriteData = new SpriteData
120 {
121 path = path
122 };
123 spriteData.Init();
124 suffixes[text4] = spriteData;
125 Debug.Log("#sprite replacer init '" + text4 + "' at " + path.ShortPath());
126 }
127 }
128 }
129
130 public void Reload(string id, RenderData renderData = null)
131 {
132 data = null;
133 suffixes.Clear();
134 try
135 {
136 if (dictModItems.ContainsKey(id))
137 {
139 }
140 else
141 {
142 if (dictTextureItems.Count == 0)
143 {
145 }
146 string text = dictTextureItems.TryGetValue(id);
147 if (text == null && renderData != null)
148 {
149 text = dictTextureItems.TryGetValue(renderData.name);
150 }
151 if (text != null)
152 {
154 }
155 }
156 data = suffixes.TryGetValue("");
157 if (data != null)
158 {
159 Debug.Log(id + ":" + data.path);
160 }
161 }
162 catch (Exception ex)
163 {
164 Debug.LogError("#sprite error fetching sprite replacer:" + ex);
165 }
166 isChecked[id] = true;
167 }
168
169 public bool HasSprite(string id, RenderData renderData = null)
170 {
171 if (!isChecked.GetValueOrDefault(id) || (data != null && data.id != id))
172 {
173 Reload(id, renderData);
174 }
175 return data != null;
176 }
177}
$
Definition: ModManager.cs:86
static string custom
Definition: CorePath.cs:159
static string packageCore
Definition: CorePath.cs:153
void Init()
Definition: SpriteData.cs:28
string path
Definition: SpriteData.cs:12
void Validate()
Definition: SpriteData.cs:95
string id
Definition: SpriteData.cs:10
Sprite GetSprite()
Definition: SpriteData.cs:89
Dictionary< string, SpriteData > suffixes
static Dictionary< string, string > dictTextureItems
void ReloadBuiltInTextures()
SpriteData data
bool HasSprite(string id, RenderData renderData=null)
Dictionary< string, bool > isChecked
void BuildSuffixData(string id, Dictionary< string, string > dictTexItems)
Sprite GetSprite(string suffix="")
static Dictionary< string, SpriteReplacer > dictSkins
void Reload(string id, RenderData renderData=null)
static Dictionary< string, string > dictModItems
Sprite GetSprite(int dir, int skin, bool snow)
static Dictionary< string, SpriteReplacer > ListSkins()