forked from headassbtw/CustomMenuText
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTools.cs
More file actions
executable file
·259 lines (224 loc) · 8.08 KB
/
Tools.cs
File metadata and controls
executable file
·259 lines (224 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using System.Data;
using System.IO;
using CustomMenuText.CustomTypes;
using System.Reflection;
using IPA.Utilities;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace CustomMenuText
{
public static class Tools
{
public static string ColorToHex(Color color)
{
float r = color.r;
float g = color.g;
float b = color.b;
int ir = (int)(r * 255);
int ig = (int)(g * 255);
int ib = (int)(b * 255);
string hr = string.Format("{0:x}", ir);
if (hr.Length == 1) hr = "0" + hr;
string hg = string.Format("{0:x}", ig);
if (hg.Length == 1) hg = "0" + hg;
string hb = string.Format("{0:x}", ib);
if (hb.Length == 1) hb = "0" + hb;
return "<#" + hr + hg + hb + ">";
}
public static GameObject FindLogo(logo? l)
{
switch (l)
{
case logo.bat:
return GameObject.Find("Logo/BatLogo");
case logo.a:
return GameObject.Find("Logo/EFlickering/LogoE");
case logo.saber:
return GameObject.Find("Logo/SaberLogo");
}
if (l == null)
return null;
else
return null;
}
public static void ReplaceTexture(logo logoType, Sprite texture)
{
FindLogo(logoType).GetComponent<SpriteRenderer>().sprite = texture;
}
public static Sprite GetTexture(logo logoType)
{
Plugin.Log.Info("Finding Logo");
return (Sprite)FindLogo(logoType).GetComponentInChildren<SpriteRenderer>().sprite;
}
public static Texture2D ClearTexture2D(int width, int height)
{
Texture2D tex = new Texture2D(1, 1);
tex.SetPixel(0, 0, Color.clear);
tex.Reinitialize(width, height);
return tex;
}
public static void ReplaceLogos(CustomTypes.LogoImages imgs)
{
ReplaceTexture(logo.bat, imgs.BatLogo);
ReplaceTexture(logo.a, imgs.ELogo);
ReplaceTexture(logo.saber, imgs.SaberLogo);
}
}
public static class FileUtils
{
public static List<string[]> readFromFile(string relPath)
{
List<string[]> entriesInFile = new List<string[]>();
// Look for the custom text file
string gameDirectory = Environment.CurrentDirectory;
string customTag1 = "<diColor1>";
string customTag2 = "<diColor2>";
string maincol = Tools.ColorToHex(Plugin.diMainColor);
string botcol = Tools.ColorToHex(Plugin.diBottomColor);
gameDirectory = gameDirectory.Replace('\\', '/');
if (File.Exists(gameDirectory + relPath))
{
var linesInFile = File.ReadLines(gameDirectory + relPath);
// Strip comments (all lines beginning with #)
linesInFile = linesInFile.Where(s => s == "" || s[0] != '#');
// Collect entries, splitting on empty lines
List<string> currentEntry = new List<string>();
foreach (string line in linesInFile)
{
if (line == "")
{
entriesInFile.Add(currentEntry.ToArray());
currentEntry.Clear();
}
else
{
string _line = line.Replace(customTag1, maincol);
_line = _line.Replace(customTag2, botcol);
currentEntry.Add(_line);
}
}
if (currentEntry.Count != 0)
{
// in case the last entry doesn't end in a newline
entriesInFile.Add(currentEntry.ToArray());
}
}
else
{
// No custom text file found!
// Create the file and populate it with the default config
try
{
FileUtils.WriteDefaultConfig();
}
catch (Exception ex)
{
Console.WriteLine("[CustomMenuText] No custom text file found, and an error was encountered trying to generate a default one!");
Console.WriteLine("[CustomMenuText] Error:");
Console.WriteLine(ex);
Console.WriteLine("[CustomMenuText] To use this plugin, manually create the file " + relPath + " in your Beat Saber install directory.");
return entriesInFile;
}
// File was successfully created; load from it with a recursive call.
return readFromFile(relPath);
}
return entriesInFile;
}
public static List<String> GetFileChunks(string path)
{
return Directory.GetDirectories(path).ToList();
}
public static Sprite LoadPNG(string filePath)
{
Texture2D tex;
byte[] fileData;
Sprite spr = null;
if (File.Exists(filePath))
{
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadRawTextureData(fileData); //..this will auto-resize the texture dimensions.
spr = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
}
return spr;
}
public static LogoImages LoadImagesFromChunk(string chunkPath)
{
//Sprite s;
//Sprite e;
//Sprite b;
CustomTypes.LogoImages li = new CustomTypes.LogoImages();
if(File.Exists(chunkPath + "\\LogoSaber.png"))
li.SaberLogo = LoadPNG(chunkPath + "\\LogoSaber.png");
if (File.Exists(chunkPath + "\\LogoE.png"))
li.ELogo = LoadPNG(chunkPath + "\\LogoE.png");
else if (!File.Exists(chunkPath + "\\LogoE.png"))
li.ELogo = null;
if (File.Exists(chunkPath + "\\LogoBat.png"))
li.BatLogo = LoadPNG(chunkPath + "\\LogoBat.png");
return li;
}
public static void WriteDefaultConfig()
{
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("CustomMenuText.DefaultTextFile.txt"))
{
using (var file = new FileStream(Path.Combine(UnityGame.UserDataPath, "CustomMenuText.txt"), FileMode.Create, FileAccess.Write))
{
resource.CopyTo(file);
}
}
}
}
}
namespace CustomMenuText.CustomTypes
{
public enum logo
{
bat,
a,
saber
}
public struct OldFont
{
public GameObject prefab;
public string name;
public bool builtin;
public OldFont(GameObject prefab, string name, bool builtIn = false)
{
this.prefab = prefab;
this.name = name;
this.builtin = builtIn;
}
}
public struct LogoImages
{
public Sprite BatLogo;
public Sprite SaberLogo;
public Sprite ELogo;
public bool E;
public string name;
public LogoImages(Sprite BatLogo, Sprite SaberLogo, string name, Sprite ELogo = null)
{
this.BatLogo = BatLogo;
this.SaberLogo = SaberLogo;
this.name = name;
if (ELogo)
{
this.E = true;
this.ELogo = ELogo;
}
else
{
this.E = false;
Sprite c = Sprite.Create(Tools.ClearTexture2D(256, 256), new Rect(0.0f, 0.0f, 256.0f, 256.0f), new Vector2(0.5f, 0.5f));
this.ELogo = c;
}
}
}
}