Elin Decompiled Documentation EA 23.355 Stable
Loading...
Searching...
No Matches
TextureData.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using UnityEngine;
5
6public class TextureData : EScriptable
7{
8 [Serializable]
9 public class Date
10 {
11 public long ticks;
12
14 {
15 get
16 {
17 return new DateTime(ticks);
18 }
19 set
20 {
21 ticks = value.Ticks;
22 }
23 }
24 }
25
26 public enum Type
27 {
28 Pass,
29 World,
31 }
32
33 public string id;
34
35 public Type type;
36
37 public Date date;
38
39 public Texture2D tex;
40
41 public bool forceRefresh;
42
43 public int tileW;
44
45 public int tileH;
46
47 [NonSerialized]
48 public int extraRows;
49
50 [NonSerialized]
51 public string path;
52
53 [NonSerialized]
54 public string texName;
55
56 [NonSerialized]
57 public List<MeshPass> listPass = new List<MeshPass>();
58
59 [NonSerialized]
60 public Dictionary<int, TextureReplace> dictReplace = new Dictionary<int, TextureReplace>();
61
62 [NonSerialized]
63 public List<TextureReplace> listReplaceLocal = new List<TextureReplace>();
64
65 public void TryRefresh()
66 {
67 DateTime lastWriteTime = File.GetLastWriteTime(path);
68 bool flag = date.DateTime.Year != 1 && date.DateTime.Equals(lastWriteTime);
69 if (dictReplace.Count > 0)
70 {
71 List<int> list = new List<int>();
72 foreach (TextureReplace value in dictReplace.Values)
73 {
74 if (!File.Exists(value.file.FullName))
75 {
76 forceRefresh = true;
77 list.Add(value.index);
78 }
79 }
80 foreach (int item in list)
81 {
83 }
84 }
85 if (forceRefresh)
86 {
87 forceRefresh = false;
88 flag = false;
89 }
90 if (!flag || !IsValid())
91 {
92 Load(flag);
93 }
94 foreach (TextureReplace value2 in dictReplace.Values)
95 {
96 value2.TryRefresh(!flag);
97 }
98 date.DateTime = lastWriteTime;
99 }
100
101 public void ForceRefresh()
102 {
103 forceRefresh = true;
104 TryRefresh();
105 }
106
107 public static void RefreshAll()
108 {
110 {
111 return;
112 }
113 foreach (TextureData value in EClass.core.textures.texMap.Values)
114 {
115 value.ForceRefresh();
116 }
117 }
118
119 public virtual bool IsValid()
120 {
121 switch (type)
122 {
123 case Type.Pass:
124 foreach (MeshPass item in listPass)
125 {
126 if (!item.mat.GetTexture(texName))
127 {
128 return false;
129 }
130 }
131 return true;
132 case Type.World:
133 return EClass.scene.tileset.AtlasTexture;
134 default:
135 return true;
136 }
137 }
138
139 public void Load(bool dateMatched)
140 {
141 if (!dateMatched)
142 {
143 Texture2D texture2D = IO.LoadPNG(path);
144 if (!texture2D)
145 {
146 Debug.Log(path);
147 }
148 int num = extraRows * tileH;
149 if (tex.width != texture2D.width || tex.height != texture2D.height + num)
150 {
151 Debug.Log(id + "/" + texture2D.width + "/" + texture2D.height + "/" + path);
152 tex.Reinitialize(texture2D.width, texture2D.height + num, tex.format, tex.mipmapCount > 1);
153 }
154 if (num > 0)
155 {
156 tex.SetPixels32(0, 0, tex.width, num, new Color32[tex.width * num]);
157 }
158 tex.SetPixels32(0, num, texture2D.width, texture2D.height, texture2D.GetPixels32());
159 tex.Apply();
160 UnityEngine.Object.Destroy(texture2D);
161 Debug.Log("Reloaded Texture:" + path);
162 }
163 switch (type)
164 {
165 case Type.Pass:
166 {
167 foreach (MeshPass item in listPass)
168 {
169 item.mat.SetTexture(texName, tex);
170 if (item.haveShadowPass)
171 {
172 item.shadowPass.mat.SetTexture(texName, tex);
173 }
174 }
175 break;
176 }
177 case Type.World:
178 EClass.scene.tileset.AtlasTexture = tex;
179 break;
180 }
181 }
182
183 public void CreateReplace(int index, string path, TextureReplace.Source source, int sizeX, int sizeY)
184 {
185 string text = id + "_" + index + ".png";
186 Texture2D texture2D = new Texture2D(tileW * sizeX, tileH * sizeY);
187 int srcX = index % 100 * tileW;
188 int srcY = tex.height - index / 100 * tileH - texture2D.height;
189 Graphics.CopyTexture(tex, 0, 0, srcX, srcY, tileW * sizeX, tileH * sizeY, texture2D, 0, 0, 0, 0);
190 byte[] bytes = texture2D.EncodeToPNG();
191 try
192 {
193 File.WriteAllBytes(path + text, bytes);
194 EClass.core.textures.TryAddReplace(new FileInfo(path + text), source, add: true, refresh: true);
195 }
196 catch
197 {
198 }
199 UnityEngine.Object.Destroy(texture2D);
200 }
201
203 {
204 TextureReplace textureReplace = dictReplace.TryGetValue(r.index);
205 if (textureReplace != null)
206 {
207 if (r.source == TextureReplace.Source.Local && textureReplace.source != TextureReplace.Source.Local)
208 {
209 r.original = textureReplace;
210 }
211 else
212 {
213 textureReplace.DestoryTex();
214 }
215 }
216 if (r.source == TextureReplace.Source.Local)
217 {
218 listReplaceLocal.Add(r);
219 }
220 dictReplace[r.index] = r;
221 }
222
224 {
225 r.file.Delete();
227 }
228
229 public void RemoveDeletedReplace(int index)
230 {
231 TextureReplace textureReplace = dictReplace.TryGetValue(index);
232 if (textureReplace != null)
233 {
234 textureReplace.DestoryTex();
235 if (textureReplace.original != null && textureReplace.original.file.Exists)
236 {
237 dictReplace[index] = textureReplace.original;
238 }
239 else
240 {
241 dictReplace.Remove(index);
242 }
243 }
244 }
245}
TextureManager textures
Definition: Core.cs:45
bool IsGameStarted
Definition: Core.cs:87
Definition: EClass.cs:6
static Scene scene
Definition: EClass.cs:31
static Core core
Definition: EClass.cs:7
Tileset tileset
Definition: Scene.cs:105
DateTime DateTime
Definition: TextureData.cs:14
Texture2D tex
Definition: TextureData.cs:39
bool forceRefresh
Definition: TextureData.cs:41
Dictionary< int, TextureReplace > dictReplace
Definition: TextureData.cs:60
static void RefreshAll()
Definition: TextureData.cs:107
void TryRefresh()
Definition: TextureData.cs:65
virtual bool IsValid()
Definition: TextureData.cs:119
string texName
Definition: TextureData.cs:54
void ForceRefresh()
Definition: TextureData.cs:101
void CreateReplace(int index, string path, TextureReplace.Source source, int sizeX, int sizeY)
Definition: TextureData.cs:183
List< MeshPass > listPass
Definition: TextureData.cs:57
void DeleteReplace(TextureReplace r)
Definition: TextureData.cs:223
void AddReplace(TextureReplace r)
Definition: TextureData.cs:202
void Load(bool dateMatched)
Definition: TextureData.cs:139
string id
Definition: TextureData.cs:33
List< TextureReplace > listReplaceLocal
Definition: TextureData.cs:63
string path
Definition: TextureData.cs:51
void RemoveDeletedReplace(int index)
Definition: TextureData.cs:229
bool TryAddReplace(FileInfo file, TextureReplace.Source source=TextureReplace.Source.Mod, bool add=true, bool refresh=false)
Dictionary< string, TextureData > texMap
void TryRefresh(bool force)
TextureReplace original