-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
279 lines (261 loc) · 12 KB
/
Plugin.cs
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using BepInEx;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
namespace UltraAchievement
{
[BepInPlugin("zeddev.ultraachievement.core", "ultraAchievement Core", "1.1.0")]
public class Core : BaseUnityPlugin
{
public static Core current;
private static string path;
private static List<string> obtainedAchievements;
private GameObject overlay;
private GameObject achievementTemplate;
public string[][] achivementList;
private void Awake()
{
current = this;
path = $"{Application.persistentDataPath}\\achievements.uaf";
if(File.Exists(path))
{
obtainedAchievements = GetAchievements();
}
else File.Create(path);
UnityEngine.SceneManagement.SceneManager.sceneLoaded += Scene;
Logger.LogInfo($"Loaded UltraAchievement core");
overlay = CreateOverlay();
achievementTemplate = CreateTemplate();
}
public static void ShowAchievement(string iconPath, string name = "Achievement", string desc = "Example description", string mod = "unknown")
{
if(!HasAchievement(name, desc, mod))
{
AddAchievement(name,desc,mod);
GameObject achievement = CreateTemplate();
achievement.GetComponent<RectTransform>().SetParent(current.overlay.GetComponent<RectTransform>());
achievement.GetComponent<Achievement>().InitAchievement(iconPath,name,desc);
}
}
public static void ShowAchievement(Sprite icon, string name = "Achievement", string desc = "Example description", string mod = "unknown")
{
if(!HasAchievement(name,desc,mod))
{
AddAchievement(name, desc, mod);
GameObject achievement = CreateTemplate();
achievement.GetComponent<RectTransform>().SetParent(current.overlay.GetComponent<RectTransform>());
achievement.GetComponent<Achievement>().InitAchievementI(icon,name,desc);
}
}
public static void ShowAchievementI(string iconPath, string name = "Achievement", string desc = "Example description", string sprite = "", string mod = "unknown")
{
if (!HasAchievement(name,desc,mod))
{
AddAchievement(name, desc, mod);
GameObject achievement = CreateTemplate(sprite);
achievement.GetComponent<RectTransform>().SetParent(current.overlay.GetComponent<RectTransform>());
achievement.GetComponent<Achievement>().InitAchievement(iconPath, name, desc);
}
}
public static void ShowAchievementI(Sprite icon, string name = "Achievement", string desc = "Example description", string sprite = "", string mod = "unknown")
{
if (!HasAchievement(name, desc, mod))
{
AddAchievement(name,desc,mod);
GameObject achievement = CreateTemplate(sprite);
achievement.GetComponent<RectTransform>().SetParent(current.overlay.GetComponent<RectTransform>());
achievement.GetComponent<Achievement>().InitAchievementI(icon, name, desc);
}
}
void Scene(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode loadSceneMode)
{
if(scene.name.Contains("Menu"))
{
obtainedAchievements = GetAchievements();
}
}
public static GameObject CreateTemplate()
{
GameObject blank = CreatePanel();
blank.name = "Achievement";
blank.AddComponent<Achievement>();
blank.transform.position = new Vector3(1000,1000);
GameObject title = CreateText();
title.name = "Title";
RectTransform rect = title.GetComponent<RectTransform>();
rect.SetParent(blank.GetComponent<RectTransform>());
rect.SetAnchor(AnchorPresets.TopLeft);
rect.SetPivot(PivotPresets.TopLeft);
rect.anchoredPosition = new Vector2(102,0);
rect.sizeDelta = new Vector2(300,25);
title.GetComponent<Text>().text = "Title";
title.GetComponent<Text>().fontSize = 20;
title.GetComponent<Text>().fontStyle = FontStyle.Bold;
GameObject desc = CreateText();
desc.name = "Description";
rect = desc.GetComponent<RectTransform>();
rect.SetParent(blank.GetComponent<RectTransform>());
rect.SetAsLastSibling();
rect.SetAnchor(AnchorPresets.TopLeft);
rect.SetPivot(PivotPresets.TopLeft);
rect.anchoredPosition = new Vector2(102,-25);
rect.sizeDelta = new Vector2(300,75);
desc.GetComponent<Text>().text = "Description";
desc.GetComponent<Text>().fontSize = 18;
GameObject icon = CreateImage();
icon.name = "Icon";
rect = icon.GetComponent<RectTransform>();
rect.SetParent(blank.GetComponent<RectTransform>());
rect.SetAsLastSibling();
rect.anchoredPosition = new Vector2(0,0);
return blank;
}
public static GameObject CreateTemplate(string sprite)
{
GameObject blank = CreatePanel(sprite);
blank.name = "Achievement";
blank.AddComponent<Achievement>();
blank.transform.position = new Vector3(1000, 1000);
GameObject title = CreateText();
title.name = "Title";
RectTransform rect = title.GetComponent<RectTransform>();
rect.SetParent(blank.GetComponent<RectTransform>());
rect.SetAnchor(AnchorPresets.TopLeft);
rect.SetPivot(PivotPresets.TopLeft);
rect.anchoredPosition = new Vector2(102, 0);
rect.sizeDelta = new Vector2(300, 25);
title.GetComponent<Text>().text = "Title";
title.GetComponent<Text>().fontSize = 20;
title.GetComponent<Text>().fontStyle = FontStyle.Bold;
GameObject desc = CreateText();
desc.name = "Description";
rect = desc.GetComponent<RectTransform>();
rect.SetParent(blank.GetComponent<RectTransform>());
rect.SetAsLastSibling();
rect.SetAnchor(AnchorPresets.TopLeft);
rect.SetPivot(PivotPresets.TopLeft);
rect.anchoredPosition = new Vector2(102, -25);
rect.sizeDelta = new Vector2(300, 75);
desc.GetComponent<Text>().text = "Description";
desc.GetComponent<Text>().fontSize = 18;
GameObject icon = CreateImage();
icon.name = "Icon";
rect = icon.GetComponent<RectTransform>();
rect.SetParent(blank.GetComponent<RectTransform>());
rect.SetAsLastSibling();
rect.anchoredPosition = new Vector2(0, 0);
return blank;
}
private static bool HasAchievement(string name, string desc, string mod)
{
string achievement = $"{name}.{desc}.{mod}";
if(obtainedAchievements.Contains(achievement))
{
return true;
}
else return false;
}
private static void AddAchievement(string name, string desc, string mod)
{
obtainedAchievements.Add($"{name}.{desc}.{mod}");
File.WriteAllLines(path, obtainedAchievements);
}
private List<string> GetAchievements()
{
List<string> achievements = File.ReadAllLines(path).ToList<string>();
int i = 0;
string[][] achivement = new string[achievements.Count()][];
foreach (var ach in achievements)
{
achivement[i] = ach.Split('.');
i++;
}
achivementList = achivement;
return achievements;
}
// Please ignore all the shit below this comment :D
private static GameObject CreateOverlay()
{
GameObject blank = new GameObject();
blank.name = "Achievement Overlay";
blank.AddComponent<Canvas>();
blank.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
blank.GetComponent<Canvas>().sortingOrder = 1000;
blank.AddComponent<CanvasScaler>();
blank.AddComponent<GraphicRaycaster>();
blank.GetComponent<CanvasScaler>().screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
blank.GetComponent<CanvasScaler>().matchWidthOrHeight = 0f;
blank.GetComponent<CanvasScaler>().referenceResolution = new Vector2(1920,1080);
DontDestroyOnLoad(blank);
return blank;
}
private static GameObject CreatePanel()
{
GameObject blank = CreateImage();
blank.name = "Panel";
RectTransform rect = blank.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(350,100);
rect.SetAnchor(AnchorPresets.BottomRight);
rect.SetPivot(PivotPresets.BottomRight);
blank.GetComponent<Image>().color = new Color(0.2745f,0.2745f,0.3529f,1f);
return blank;
}
private static GameObject CreatePanel(string sprite)
{
GameObject blank = CreateImage();
blank.name = "Panel";
RectTransform rect = blank.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(350, 100);
rect.SetAnchor(AnchorPresets.BottomRight);
rect.SetPivot(PivotPresets.BottomRight);
if (sprite != "") {
blank.GetComponent<Image>().sprite = Core.LoadSprite(sprite, Vector4.zero,100);
return blank;
}
blank.GetComponent<Image>().color = new Color(0.2745f, 0.2745f, 0.3529f, 1f);
return blank;
}
private static GameObject CreateImage()
{
GameObject blank = new GameObject();
blank.name = "Image";
blank.AddComponent<RectTransform>();
RectTransform rect = blank.GetComponent<RectTransform>();
blank.AddComponent<CanvasRenderer>();
rect.sizeDelta = new Vector2(100,100);
rect.SetAnchor(AnchorPresets.TopLeft);
rect.SetPivot(PivotPresets.TopLeft);
blank.AddComponent<Image>();
return blank;
}
private static GameObject CreateText()
{
GameObject blank = new GameObject();
blank.name = "Text";
blank.AddComponent<RectTransform>();
blank.AddComponent<CanvasRenderer>();
RectTransform rect = blank.GetComponent<RectTransform>();
rect.sizeDelta = new Vector2(500, 50);
rect.SetAnchor(AnchorPresets.MiddleCenter);
rect.SetPivot(PivotPresets.MiddleCenter);
blank.AddComponent<Text>();
blank.GetComponent<Text>().text = "Text";
blank.GetComponent<Text>().font = Font.GetDefault();
blank.GetComponent<Text>().fontSize = 30;
blank.GetComponent<Text>().alignment = TextAnchor.UpperLeft;
blank.GetComponent<Text>().color = Color.white;
return blank;
}
public static Sprite LoadSprite(string path, Vector4 border, float pixelsPerUnit)
{
Texture2D texture = new Texture2D(200, 200);
texture.LoadImage(File.ReadAllBytes(path));
return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), pixelsPerUnit, 0, SpriteMeshType.Tight, border);
}
}
}