-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModAPI.cs
More file actions
213 lines (193 loc) · 10 KB
/
Copy pathModAPI.cs
File metadata and controls
213 lines (193 loc) · 10 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
using System;
using System.IO;
using UnityEngine;
using NAudio.Wave;
using System.Linq;
using JetBrains.Annotations;
using Object = UnityEngine.Object;
using UnityEngine.Events;
using UnityEngine.UI;
using Image = UnityEngine.UI.Image;
namespace WorldBoxModLoader
{
namespace ModAPI
{
public static class Utils
{
internal static bool modConstantsIsValid = false;
internal static ModConstants modConstants;
private static readonly Transform tab_entry_container =
CanvasMain.instance.canvas_ui.transform.Find("CanvasBottom/BottomElements/BottomElementsMover/TabsButtons");
private static readonly Transform tab_container = CanvasMain.instance.canvas_ui.transform.Find(
"CanvasBottom/BottomElements/BottomElementsMover/CanvasScrollView/Scroll View/Viewport/Content/buttons");
public static Texture2D LoadTexture(string fullPath, FilterMode filterMode = FilterMode.Point)
{
if (!modConstantsIsValid)
throw new InvalidOperationException("Invalid use of path-dependent function! LoadSprite must be called in the AfterSpawn, Main, or OnLoad function.");
byte[] data;
try
{
data = File.ReadAllBytes(Path.Combine(modConstants.MetaLocation, fullPath));
}
catch (Exception exception)
{
Debug.LogWarning(exception);
return null;
}
Texture2D texture = new Texture2D(0, 0);
if (!texture.LoadImage(data))
throw new InvalidDataException("Cannot load texture");
texture.filterMode = filterMode;
texture.wrapMode = TextureWrapMode.Clamp;
return texture;
}
public static Sprite LoadSprite(string fullPath, FilterMode filterMode = FilterMode.Point, int pixelsPerUnit = 20)
{
if (!modConstantsIsValid)
throw new InvalidOperationException("Invalid use of path-dependent function! LoadSprite must be called in the AfterSpawn, Main, or OnLoad function.");
Texture2D texture = LoadTexture(fullPath, filterMode);
return texture == null ? null : Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), 0.5f * Vector2.one, pixelsPerUnit);
}
public static AudioClip LoadSound(string fullPath)
{
if (!modConstantsIsValid)
throw new InvalidOperationException("Invalid use of path-dependent function! LoadSprite must be called in the AfterSpawn, Main, or OnLoad function.");
string path = Path.Combine(modConstants.MetaLocation, fullPath);
switch (Path.GetExtension(path))
{
case ".mp3":
return DecodeMP3();
case ".wav":
return DecodeWAV();
default:
return DecodeOther();
}
AudioClip DecodeWAV()
{
using (WaveStream stream = new WaveFileReader(path))
return WaveStreamToAudioClip(stream);
}
AudioClip DecodeOther()
{
using (WaveStream stream = new AudioFileReader(path))
return WaveStreamToAudioClip(stream);
}
AudioClip DecodeMP3()
{
using (WaveStream stream = new Mp3FileReader(path))
return WaveStreamToAudioClip(stream);
}
AudioClip WaveStreamToAudioClip(WaveStream stream)
{
int number = stream.WaveFormat.Channels == 1 ? 1 : 0;
StereoToMonoProvider16 toMono16 = number != 0 ? null : new StereoToMonoProvider16(stream);
byte[] numArray1 = new byte[(stream).Length];
int count = number != 0 ? (stream).Read(numArray1, 0, numArray1.Length) : toMono16.Read(numArray1, 0, numArray1.Length);
short[] numArray2 = new short[count / 2];
Buffer.BlockCopy(numArray1, 0, numArray2, 0, count);
AudioClip audioClip = AudioClip.Create(path, numArray2.Length, 1, stream.WaveFormat.SampleRate, false);
audioClip.SetData((numArray2).Select<short, float>((b => b / short.MaxValue)).ToArray<float>(), 0);
return audioClip;
}
}
public static PowerButton CreateSimplePowerButton([NotNull] string buttonId, UnityAction buttonAction, Sprite buttonIcon, string buttonDescription = "")
{
PowerButton prefab = UtilsInternal.FindResource<PowerButton>("worldlaws");
PowerButton button = Object.Instantiate(prefab);
TipButton tipButton = button.gameObject.GetOrAddComponent<TipButton>();
LocalizedTextManager.add(buttonDescription, buttonDescription);
tipButton.textOnClick = buttonId;
tipButton.textOnClickDescription = buttonDescription;
button.name = buttonId;
button.icon.sprite = buttonIcon;
button.type = PowerButtonType.Library;
button.gameObject.SetActive(true);
if (buttonAction != null) button.GetComponent<Button>().onClick.AddListener(buttonAction);
return button;
}
public static PowersTab GetTab(string tabName)
{
if (string.IsNullOrEmpty(tabName)) return null;
Transform tabTransform = CanvasMain.instance.canvas_ui.transform.Find(
$"CanvasBottom/BottomElements/BottomElementsMover/CanvasScrollView/Scroll View/Viewport/Content/buttons/{tabName}");
return tabTransform == null ? null : tabTransform.GetComponent<PowersTab>();
}
public static void AddButtonToTab(PowerButton button, PowersTab tab, Vector2 position)
{
Transform transform = button.transform;
transform.SetParent(tab.transform);
transform.localPosition = position;
transform.localScale = Vector3.one;
tab.powerButtons.Add(button);
}
public static ScrollWindow CreateEmptyWindow(string windowID, string windowTitleKey)
{
ScrollWindow window = Object.Instantiate(Resources.Load<ScrollWindow>("windows/empty"),
CanvasMain.instance.transformWindows);
LocalizedTextManager.add(windowTitleKey, windowTitleKey);
window.titleText.text = windowTitleKey;
window.screen_id = windowID;
window.name = windowID;
return window;
}
public static PowerButton CreateWindowButton([NotNull] string buttonId, [NotNull] string windowId, Sprite buttonIcon, string buttonDescription = "")
{
PowerButton prefab = UtilsInternal.FindResource<PowerButton>("worldlaws");
PowerButton button = Object.Instantiate(prefab);
TipButton tipButton = button.gameObject.GetOrAddComponent<TipButton>();
LocalizedTextManager.add(buttonDescription, buttonDescription);
tipButton.textOnClick = buttonId;
tipButton.textOnClickDescription = buttonDescription;
button.name = buttonId;
button.icon.sprite = buttonIcon;
button.open_window_id = windowId;
button.type = PowerButtonType.Window;
button.gameObject.SetActive(true);
return button;
}
public static PowersTab CreateTab(string tabName, string titleKey, Sprite tabIcon)
{
GameObject tab_entry = Object.Instantiate(UtilsInternal.FindResource<GameObject>("Button_Other"),
tab_entry_container);
tab_entry.name = "Button_" + tabName;
tab_entry.transform.Find("Icon").GetComponent<Image>().sprite = tabIcon;
PowersTab tab = Object.Instantiate(
UtilsInternal.FindResource<GameObject>("Tab_Other").GetComponent<PowersTab>(),
tab_container);
tab.name = "Tab_" + tabName;
Button tab_entry_button = tab_entry.GetComponent<Button>();
tab_entry_button.onClick = new Button.ButtonClickedEvent();
tab_entry_button.onClick.AddListener(() => tab.showTab(tab_entry_button));
TipButton tab_entry_tip = tab_entry.GetComponent<TipButton>();
tab_entry_tip.textOnClick = titleKey;
// Clear tab content
for (int i = 7; i < tab.transform.childCount; i++)
{
GameObject.Destroy(tab.transform.GetChild(i).gameObject);
}
tab.powerButtons.Clear();
tab.gameObject.SetActive(false);
tab.gameObject.SetActive(true);
return tab;
}
public static T GetOrAddComponent<T>(this GameObject go) where T : Component
{
T type = go.GetComponent<T>();
if (type != null)
return type;
type = go.AddComponent<T>();
return type;
}
}
public static class PowersTabNames
{
public const string Main = "Tab_Main";
public const string Drawing = "Tab_Drawing";
public const string Kingdoms = "Tab_Kingdoms";
public const string Creatures = "Tab_Creatures";
public const string Nature = "Tab_Nature";
public const string Bombs = "Tab_Bombs";
public const string Other = "Tab_Other";
}
}
}