Elin Decompiled Documentation EA 23.343.5 Nightly
Loading...
Searching...
No Matches
SpriteData.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Text;
4using IniParser;
5using IniParser.Model;
6using UnityEngine;
7
8public class SpriteData
9{
10 public const float DefaultPPU = 100f;
11
12 public string id;
13
14 public string path;
15
16 public Texture2D tex;
17
18 public Sprite[] sprites;
19
20 public DateTime dateTex;
21
22 public DateTime dateIni;
23
24 public DateTime datePref;
25
26 public int frame = 1;
27
28 public int scale = 50;
29
30 public float time = 0.2f;
31
33
35
36 public static float BaseWidth(Sprite s)
37 {
38 return s.rect.width * 100f / s.pixelsPerUnit;
39 }
40
41 public static float BaseHeight(Sprite s)
42 {
43 return s.rect.height * 100f / s.pixelsPerUnit;
44 }
45
46 public void Init()
47 {
48 try
49 {
50 id = Path.GetFileNameWithoutExtension(path);
52 LoadPref();
53 }
54 catch (Exception exception)
55 {
56 Debug.LogException(exception);
57 Debug.LogError("#sprite failed " + id + "/" + path);
58 }
59 }
60
61 public void LoadPref()
62 {
63 pref = null;
64 if (File.Exists(path + ".pref"))
65 {
66 pref = SourcePref.ReadFromIni(path + ".pref");
67 }
68 }
69
71 {
72 float a = pref?.ScaleTex ?? 1f;
73 LoadPref();
74 if (sprites != null && !Mathf.Approximately(a, pref?.ScaleTex ?? 1f))
75 {
77 }
78 }
79
80 public void LoadAnimationIni()
81 {
82 if (!File.Exists(path + ".ini"))
83 {
84 frame = 1;
85 scale = 50;
86 time = 0.2f;
87 return;
88 }
89 try
90 {
91 IniData iniData = new FileIniDataParser().ReadFile(path + ".ini", Encoding.UTF8);
92 frame = Mathf.Max(1, (!int.TryParse(iniData.GetKey("frame"), out var result)) ? 1 : result);
93 scale = (int.TryParse(iniData.GetKey("scale"), out var result2) ? result2 : 50);
94 time = (float.TryParse(iniData.GetKey("time"), out var result3) ? Mathf.Max(0.016f, result3) : 0.2f);
95 }
96 catch (Exception exception)
97 {
98 Debug.LogException(exception);
99 }
100 }
101
102 public void LoadSprites()
103 {
104 Texture2D texture2D = IO.LoadPNG(path + ".png");
105 Debug.Log(texture2D.width + "/" + texture2D.height + "/" + path);
106 if ((bool)tex)
107 {
108 if (texture2D.width != tex.width || texture2D.height != tex.height)
109 {
110 tex.Reinitialize(texture2D.width, texture2D.height);
111 }
112 tex.SetPixels32(texture2D.GetPixels32());
113 tex.Apply();
114 UnityEngine.Object.Destroy(texture2D);
115 }
116 else
117 {
118 tex = texture2D;
119 }
120 int num = tex.width / frame;
121 int height = tex.height;
122 float num2 = 100f * (pref?.ScaleTex ?? 1f);
123 if (sprites == null || sprites.Length != frame || !sprites[0] || !Mathf.Approximately(sprites[0].rect.width, num) || !Mathf.Approximately(sprites[0].rect.height, height) || !Mathf.Approximately(sprites[0].pixelsPerUnit, num2))
124 {
125 sprites = new Sprite[frame];
126 Vector2 pivot = new Vector2(0.5f, 0.5f);
127 for (int i = 0; i < frame; i++)
128 {
129 sprites[i] = Sprite.Create(tex, new Rect(i * num, 0f, num, height), pivot, num2, 0u, SpriteMeshType.FullRect);
130 }
131 }
132 }
133
134 public Sprite[] GetSprites()
135 {
136 if (sprites == null)
137 {
138 Load();
139 }
140 return sprites;
141 }
142
143 public Sprite GetSprite()
144 {
145 if (sprites == null)
146 {
147 Load();
148 }
149 return sprites.TryGet(0, returnNull: true);
150 }
151
152 public void Validate()
153 {
154 if (IsDirty(".png", ref dateTex) || IsDirty(".ini", ref dateIni) || IsDirty(".pref", ref datePref))
155 {
156 Load();
157 }
158 bool IsDirty(string p, ref DateTime lastChecked)
159 {
160 string text = path + p;
161 if (!File.Exists(text))
162 {
163 return false;
164 }
165 DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(text);
166 if (lastWriteTimeUtc == lastChecked)
167 {
168 return false;
169 }
170 lastChecked = lastWriteTimeUtc;
171 return true;
172 }
173 }
174
175 public void Load()
176 {
178 LoadPref();
179 LoadSprites();
180 }
181}
float ScaleTex
Definition: SourcePref.cs:341
static SourcePref ReadFromIni(string path)
Definition: SourcePref.cs:431
void Init()
Definition: SpriteData.cs:46
Sprite[] sprites
Definition: SpriteData.cs:18
Texture2D tex
Definition: SpriteData.cs:16
bool tryFixPrefNotLoadedAtStart
Definition: SpriteData.cs:34
static float BaseHeight(Sprite s)
Definition: SpriteData.cs:41
const float DefaultPPU
Definition: SpriteData.cs:10
void LoadAnimationIni()
Definition: SpriteData.cs:80
void Load()
Definition: SpriteData.cs:175
DateTime datePref
Definition: SpriteData.cs:24
void LoadPrefAndFixDensity()
Definition: SpriteData.cs:70
void LoadSprites()
Definition: SpriteData.cs:102
string path
Definition: SpriteData.cs:14
void LoadPref()
Definition: SpriteData.cs:61
DateTime dateTex
Definition: SpriteData.cs:20
void Validate()
Definition: SpriteData.cs:152
SourcePref pref
Definition: SpriteData.cs:32
static float BaseWidth(Sprite s)
Definition: SpriteData.cs:36
string id
Definition: SpriteData.cs:12
float time
Definition: SpriteData.cs:30
DateTime dateIni
Definition: SpriteData.cs:22
Sprite GetSprite()
Definition: SpriteData.cs:143
Sprite[] GetSprites()
Definition: SpriteData.cs:134