Elin Decompiled Documentation EA 23.102 Nightly
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 string path;
49
50 [NonSerialized]
51 public string texName;
52
53 [NonSerialized]
54 public List<MeshPass> listPass = new List<MeshPass>();
55
56 [NonSerialized]
57 public Dictionary<int, TextureReplace> dictReplace = new Dictionary<int, TextureReplace>();
58
59 [NonSerialized]
60 public List<TextureReplace> listReplaceLocal = new List<TextureReplace>();
61
62 public void TryRefresh()
63 {
64 DateTime lastWriteTime = File.GetLastWriteTime(path);
65 bool flag = date.DateTime.Year != 1 && date.DateTime.Equals(lastWriteTime);
66 if (dictReplace.Count > 0)
67 {
68 List<int> list = new List<int>();
69 foreach (TextureReplace value in dictReplace.Values)
70 {
71 if (!File.Exists(value.file.FullName))
72 {
73 forceRefresh = true;
74 list.Add(value.index);
75 }
76 }
77 foreach (int item in list)
78 {
80 }
81 }
82 if (forceRefresh)
83 {
84 forceRefresh = false;
85 flag = false;
86 }
87 if (!flag || !IsValid())
88 {
89 Load(flag);
90 }
91 foreach (TextureReplace value2 in dictReplace.Values)
92 {
93 value2.TryRefresh(!flag);
94 }
95 date.DateTime = lastWriteTime;
96 }
97
98 public void ForceRefresh()
99 {
100 forceRefresh = true;
101 TryRefresh();
102 }
103
104 public static void RefreshAll()
105 {
107 {
108 return;
109 }
110 foreach (TextureData value in EClass.core.textures.texMap.Values)
111 {
112 value.ForceRefresh();
113 }
114 }
115
116 public virtual bool IsValid()
117 {
118 switch (type)
119 {
120 case Type.Pass:
121 foreach (MeshPass item in listPass)
122 {
123 if (!item.mat.GetTexture(texName))
124 {
125 return false;
126 }
127 }
128 return true;
129 case Type.World:
130 return EClass.scene.tileset.AtlasTexture;
131 default:
132 return true;
133 }
134 }
135
136 public void Load(bool dateMatched)
137 {
138 if (!dateMatched)
139 {
140 Texture2D texture2D = IO.LoadPNG(path);
141 if (!texture2D)
142 {
143 Debug.Log(path);
144 }
145 if (tex.width != texture2D.width || tex.height != texture2D.height)
146 {
147 Debug.Log(id + "/" + texture2D.width + "/" + texture2D.height + "/" + path);
148 }
149 tex.SetPixels32(texture2D.GetPixels32());
150 tex.Apply();
151 UnityEngine.Object.Destroy(texture2D);
152 Debug.Log("Reloaded Texture:" + path);
153 }
154 switch (type)
155 {
156 case Type.Pass:
157 {
158 foreach (MeshPass item in listPass)
159 {
160 item.mat.SetTexture(texName, tex);
161 if (item.haveShadowPass)
162 {
163 item.shadowPass.mat.SetTexture(texName, tex);
164 }
165 }
166 break;
167 }
168 case Type.World:
169 EClass.scene.tileset.AtlasTexture = tex;
170 break;
171 }
172 }
173
174 public void CreateReplace(int index, string path, TextureReplace.Source source, int sizeX, int sizeY)
175 {
176 string text = id + "_" + index + ".png";
177 Texture2D texture2D = new Texture2D(tileW * sizeX, tileH * sizeY);
178 int srcX = index % 100 * tileW;
179 int srcY = tex.height - index / 100 * tileH - texture2D.height;
180 Graphics.CopyTexture(tex, 0, 0, srcX, srcY, tileW * sizeX, tileH * sizeY, texture2D, 0, 0, 0, 0);
181 byte[] bytes = texture2D.EncodeToPNG();
182 try
183 {
184 File.WriteAllBytes(path + text, bytes);
185 EClass.core.textures.TryAddReplace(new FileInfo(path + text), source, add: true, refresh: true);
186 }
187 catch
188 {
189 }
190 UnityEngine.Object.Destroy(texture2D);
191 }
192
194 {
195 TextureReplace textureReplace = dictReplace.TryGetValue(r.index);
196 if (textureReplace != null)
197 {
198 if (r.source == TextureReplace.Source.Local && textureReplace.source != TextureReplace.Source.Local)
199 {
200 r.original = textureReplace;
201 }
202 else
203 {
204 textureReplace.DestoryTex();
205 }
206 }
207 if (r.source == TextureReplace.Source.Local)
208 {
209 listReplaceLocal.Add(r);
210 }
211 dictReplace[r.index] = r;
212 }
213
215 {
216 r.file.Delete();
218 }
219
220 public void RemoveDeletedReplace(int index)
221 {
222 TextureReplace textureReplace = dictReplace.TryGetValue(index);
223 if (textureReplace != null)
224 {
225 textureReplace.DestoryTex();
226 if (textureReplace.original != null && textureReplace.original.file.Exists)
227 {
228 dictReplace[index] = textureReplace.original;
229 }
230 else
231 {
232 dictReplace.Remove(index);
233 }
234 }
235 }
236}
TextureManager textures
Definition: Core.cs:45
bool IsGameStarted
Definition: Core.cs:84
Definition: EClass.cs:5
static Scene scene
Definition: EClass.cs:30
static Core core
Definition: EClass.cs:6
Tileset tileset
Definition: Scene.cs:101
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:57
static void RefreshAll()
Definition: TextureData.cs:104
void TryRefresh()
Definition: TextureData.cs:62
virtual bool IsValid()
Definition: TextureData.cs:116
string texName
Definition: TextureData.cs:51
void ForceRefresh()
Definition: TextureData.cs:98
void CreateReplace(int index, string path, TextureReplace.Source source, int sizeX, int sizeY)
Definition: TextureData.cs:174
List< MeshPass > listPass
Definition: TextureData.cs:54
void DeleteReplace(TextureReplace r)
Definition: TextureData.cs:214
void AddReplace(TextureReplace r)
Definition: TextureData.cs:193
void Load(bool dateMatched)
Definition: TextureData.cs:136
string id
Definition: TextureData.cs:33
List< TextureReplace > listReplaceLocal
Definition: TextureData.cs:60
string path
Definition: TextureData.cs:48
void RemoveDeletedReplace(int index)
Definition: TextureData.cs:220
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