Elin Decompiled Documentation EA 23.264 Nightly
Loading...
Searching...
No Matches
GalleryFlattenAndRename.cs
Go to the documentation of this file.
1using System.IO;
2using System.Linq;
3using UnityEngine;
4
5public static class GalleryFlattenAndRename
6{
7 public static string root = CorePath.CorePackage.ETC + "/Gallery";
8
9 public static UD_Int_String dict;
10
11 public const int ThumbMaxW = 360;
12
13 public const int ThumbMaxH = 240;
14
15 public static UD_Int_String Run()
16 {
17 dict = new UD_Int_String();
19 string[] array = Directory.GetDirectories(root, "*", SearchOption.AllDirectories).ToArray();
20 for (int i = 0; i < array.Length; i++)
21 {
22 Directory.Delete(array[i], recursive: true);
23 }
24 array = Directory.GetFiles(root, "*", SearchOption.TopDirectoryOnly);
25 foreach (string text in array)
26 {
27 if (text.EndsWith("meta"))
28 {
29 File.Delete(text);
30 }
31 }
33 array = Directory.GetFiles(root, "*", SearchOption.TopDirectoryOnly);
34 foreach (string text2 in array)
35 {
36 if (text2.EndsWith("meta"))
37 {
38 File.Delete(text2);
39 }
40 }
42 Debug.Log("[Gallery] Done. Total:" + dict.Count);
43 return dict;
44 }
45
46 private static void MoveAllFilesUnderSubfoldersToRoot(string root)
47 {
48 string[] directories = Directory.GetDirectories(root, "*", SearchOption.AllDirectories);
49 for (int i = 0; i < directories.Length; i++)
50 {
51 string[] files = Directory.GetFiles(directories[i], "*", SearchOption.TopDirectoryOnly);
52 foreach (string text in files)
53 {
54 string text2 = Path.Combine(root, Path.GetFileName(text));
55 if (!File.Exists(text2))
56 {
57 File.Move(text, text2);
58 }
59 }
60 }
61 }
62
63 private static void RenameAllFilesToLeadingNumber(string root)
64 {
65 string[] files = Directory.GetFiles(root, "*", SearchOption.TopDirectoryOnly);
66 foreach (string text in files)
67 {
68 string extension = Path.GetExtension(text);
69 if (IsImageExt(extension))
70 {
71 string text2 = Path.GetFileNameWithoutExtension(text).Split('_')[0];
72 string text3 = Path.Combine(root, text2 + extension);
73 if (text != text3)
74 {
75 File.Move(text, text3);
76 }
77 dict[text2.ToInt()] = text2 + extension;
78 }
79 }
80 }
81
82 private static void CreateThumbnailsSameFolder(string root, int maxW, int maxH)
83 {
84 string[] files = Directory.GetFiles(root, "*", SearchOption.TopDirectoryOnly);
85 foreach (string text in files)
86 {
87 if (text.EndsWith(".meta"))
88 {
89 continue;
90 }
91 string path = text + "_t";
92 if (File.Exists(path))
93 {
94 continue;
95 }
96 string text2 = Path.GetExtension(text).ToLowerInvariant();
97 if (!IsImageExt(text2) || Path.GetFileName(text).Contains(text2 + "_t"))
98 {
99 continue;
100 }
101 byte[] data = File.ReadAllBytes(text);
102 Texture2D texture2D = new Texture2D(2, 2, TextureFormat.RGBA32, mipChain: false, linear: false);
103 if (!texture2D.LoadImage(data))
104 {
105 Object.DestroyImmediate(texture2D);
106 continue;
107 }
108 int width = texture2D.width;
109 int height = texture2D.height;
110 float num = Mathf.Min((float)maxW / (float)width, (float)maxH / (float)height);
111 if (num > 1f)
112 {
113 num = 1f;
114 }
115 int w = Mathf.Max(1, Mathf.RoundToInt((float)width * num));
116 int h = Mathf.Max(1, Mathf.RoundToInt((float)height * num));
117 Texture2D texture2D2 = ResizeTo(texture2D, w, h);
118 byte[] bytes = ((!(text2 == ".png")) ? texture2D2.EncodeToJPG(90) : texture2D2.EncodeToPNG());
119 File.WriteAllBytes(path, bytes);
120 Object.DestroyImmediate(texture2D);
121 Object.DestroyImmediate(texture2D2);
122 }
123 }
124
125 private static bool IsImageExt(string ext)
126 {
127 if (!(ext == ".png") && !(ext == ".jpg"))
128 {
129 return ext == ".jpeg";
130 }
131 return true;
132 }
133
134 private static Texture2D ResizeTo(Texture2D src, int w, int h)
135 {
136 RenderTexture temporary = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);
137 RenderTexture active = RenderTexture.active;
138 Graphics.Blit(src, temporary);
139 RenderTexture.active = temporary;
140 Texture2D texture2D = new Texture2D(w, h, TextureFormat.RGBA32, mipChain: false, linear: false);
141 texture2D.ReadPixels(new Rect(0f, 0f, w, h), 0, 0);
142 texture2D.Apply(updateMipmaps: false, makeNoLongerReadable: false);
143 RenderTexture.active = active;
144 RenderTexture.ReleaseTemporary(temporary);
145 return texture2D;
146 }
147}
static Texture2D ResizeTo(Texture2D src, int w, int h)
static UD_Int_String Run()
static void MoveAllFilesUnderSubfoldersToRoot(string root)
static void CreateThumbnailsSameFolder(string root, int maxW, int maxH)
static bool IsImageExt(string ext)
static void RenameAllFilesToLeadingNumber(string root)