-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSSSLoadingHelper.cs
More file actions
308 lines (276 loc) · 11.2 KB
/
Copy pathSSSLoadingHelper.cs
File metadata and controls
308 lines (276 loc) · 11.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using UnityEngine;
using Sunless.Game.ApplicationProviders;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Sunless.Game.Audio;
namespace SSSoftcoded
{
public static class SSSLoadingHelper
{
private static readonly string[] supportedWallpaperFormats = { ".png" };
private static readonly string[] supportedSoundFormats = { ".wav", ".mp3", ".ogg" };
private static string wallpaperFilePath = GameProvider.Instance.GetApplicationPath("images/sn/wallpapers/");
private static string musicFilePath = GameProvider.Instance.GetApplicationPath("audio/music/");
private static string ambientSFXFilePath = GameProvider.Instance.GetApplicationPath("audio/ambient sfx/");
private static string regularSFXFilePath = GameProvider.Instance.GetApplicationPath("audio/sfx/");
private static bool ignoreVanillaWallpapers = false;
private static SSSLoadableResource[] customWallpapers;
private static SSSLoadableResource[] customMusicTracks;
private static SSSLoadableResource[] customAmbientSFX;
private static SSSLoadableResource[] customRegularSFX;
private static List<AudioClip> loadedAmbientSFX = new List<AudioClip>();
private static List<AudioClip> loadedRegularSFX = new List<AudioClip>();
public static void Initialise()
{
if (File.Exists("SSSConfig.ini"))
{
string[] lines = File.ReadAllLines("SSSConfig.ini");
var optionsDict = new Dictionary<string, string>();
foreach (var s in lines)
{
s.Trim();
if (s.StartsWith("#") || s.StartsWith("["))
{
continue;
}
else if (s.Contains("="))
{
string[] split = s.Split('=');
optionsDict.Add(split[0], split[1]);
}
}
if (optionsDict["customWallpapers"] != "")
{
wallpaperFilePath = CorrectFilePath(optionsDict["customWallpapers"]);
}
if (optionsDict["customMusic"] != "")
{
musicFilePath = CorrectFilePath(optionsDict["customMusic"]);
}
if (optionsDict["customAmbientSFX"] != "")
{
ambientSFXFilePath = CorrectFilePath(optionsDict["customAmbientSFX"]);
}
if (optionsDict["customRegularSFX"] != "")
{
regularSFXFilePath = CorrectFilePath(optionsDict["customRegularSFX"]);
}
if (optionsDict["ignoreVanillaWallpapers"] == "true")
{
ignoreVanillaWallpapers = true;
}
} else
{
System.Console.WriteLine("Couldn't find SSSConfig.ini in the Sunless Sea root folder. Only default settings will be available");
}
}
public static void DocumentAllCustomContent()
{
//load string arrays of all custom loading screens, custom music tracks, and custom ambientfx, to be referred to at runtime
customWallpapers = GetCustomWallpapers();
customMusicTracks = GetCustomAssetArray(GetMusicFilePath(), supportedSoundFormats);
customAmbientSFX = GetCustomAssetArray(GetAmbientSFXFilePath(), supportedSoundFormats);
customRegularSFX = GetCustomAssetArray(GetRegularSFXFilePath(), supportedSoundFormats);
}
public static SSSLoadableResource[] GetCustomWallpapers()
{
List<SSSLoadableResource> wallpapers = GetCustomAssetArray(wallpaperFilePath, supportedWallpaperFormats).ToList<SSSLoadableResource>();
//Now we want to add asset references to any of the 14 (or 10) original wallpapers that haven't been replaced
//Unless ignoreVanillaWallpapers is true; UNLESS UNLESS there are no custom wallpapers.
if (wallpapers.Count == 0 || !ignoreVanillaWallpapers)
{
for (int i = 1; i < (GameProvider.Instance.IsZubmariner ? 14 : 10); i++)
{
if (!wallpapers.Any(w => w.GetName() == i.ToString()))
{
wallpapers.Add(new SSSLoadableResource(i.ToString(), ""));
}
}
}
return wallpapers.ToArray();
}
public static SSSLoadableResource[] GetCustomAssetArray(string directoryPath, string[] extensions)
{
if (!Directory.Exists(directoryPath))
{
return new SSSLoadableResource[] { };
}
string[] allstrings = Directory.GetFiles(directoryPath, "*.*", SearchOption.TopDirectoryOnly);
List<string> uniqueNames = new List<string>();
List<SSSLoadableResource> returnResources = new List<SSSLoadableResource>();
// If there are multiple files with the same name with different extensions, load the first one and then complain about the rest
foreach (string s in allstrings)
{
string name = IsolateFileName(s);
string ext = IsolateExtension(s);
if (extensions.Contains(ext) && !uniqueNames.Contains(name))
{
if (uniqueNames.Contains(name))
{
System.Console.WriteLine("Tried to document a file named " + name + ext + " for mod loading, but there is already a file in" + directoryPath + " named " + name + " with a different extension so the new file will be ignored.");
}
else
{
uniqueNames.Add(name);
returnResources.Add(new SSSLoadableResource(name, ext));
}
}
}
return returnResources.ToArray();
}
public static Texture2D LoadWallpaperTexture(SSSLoadableResource texture)
{
string text = GetWallpaperFilePath() + texture.GetAddress();
Texture2D result;
byte[] data = File.ReadAllBytes(text);
Texture2D texture2D = new Texture2D(2048, 1024);
texture2D.LoadImage(data);
result = texture2D;
return result;
}
public static AudioClip GetMusic(string clipName)
{
foreach (SSSLoadableResource r in customMusicTracks)
{
if (r.GetName().ToLower() == clipName.ToLower())
{
AudioClip clip = SSSAudioLoader.LoadSound(GetMusicFilePath() + r.GetAddress());
loadedAmbientSFX.Add(clip);
return clip;
}
}
return null;
}
public static AudioClip GetAmbientSFX(string clipName)
{
//First check if we've already got this sound effect loaded, which could happen if it's played very frequently
foreach (AudioClip a in loadedAmbientSFX)
{
if (a.name.ToLower() == clipName.ToLower())
{
return a;
}
}
//Otherwise, find it in the array and load it
foreach (SSSLoadableResource r in customAmbientSFX)
{
if (r.GetName().ToLower() == clipName.ToLower())
{
AudioClip clip = SSSAudioLoader.LoadSound(GetAmbientSFXFilePath() + r.GetAddress());
loadedAmbientSFX.Add(clip);
return clip;
}
}
return null;
}
public static AudioClip GetRegularSFX(string clipName)
{
//First check if we've already got this sound effect loaded, which could happen if it's played very frequently
foreach (AudioClip a in loadedRegularSFX)
{
if (a.name.ToLower() == clipName.ToLower())
{
return a;
}
}
//Otherwise, find it in the array and load it
foreach (SSSLoadableResource r in customRegularSFX)
{
if (r.GetName().ToLower() == clipName.ToLower())
{
AudioClip clip = SSSAudioLoader.LoadSound(GetRegularSFXFilePath() + r.GetAddress());
loadedRegularSFX.Add(clip);
return clip;
}
}
return null;
}
public static bool CustomAmbientSFXExists(string name)
{
if (customAmbientSFX.Any(s => s.GetName().ToLower() == name.ToLower()))
{
return true;
}
return false;
}
public static bool CustomRegularSFXExists(string name)
{
if (customRegularSFX.Any(s => s.GetName().ToLower() == name.ToLower()))
{
return true;
}
return false;
}
public static bool UnloadAmbientSFX(string clipName)
{
for (int i = 0; i < loadedAmbientSFX.Count; i++)
{
if (loadedAmbientSFX[i].name == clipName)
{
loadedAmbientSFX.RemoveAt(i);
return true;
}
}
return false;
}
public static bool UnloadRegularSFX(string clipName)
{
for (int i = 0; i < loadedRegularSFX.Count; i++)
{
if (loadedRegularSFX[i].name == clipName)
{
loadedRegularSFX.RemoveAt(i);
return true;
}
}
return false;
}
public static SSSLoadableResource[] GetCustomWallPapers()
{
return customWallpapers;
}
public static SSSLoadableResource[] GetCustomMusicTracks()
{
return customMusicTracks;
}
public static SSSLoadableResource[] GetCustomAmbientSFX()
{
return customAmbientSFX;
}
public static string GetWallpaperFilePath()
{
return wallpaperFilePath;
}
public static string GetMusicFilePath()
{
return musicFilePath;
}
public static string GetAmbientSFXFilePath()
{
return ambientSFXFilePath;
}
public static string GetRegularSFXFilePath()
{
return regularSFXFilePath;
}
public static string IsolateFileName(string filePath)
{
string name = filePath.Substring(filePath.LastIndexOf('/') + 1);
name = name.Substring(0, name.LastIndexOf('.'));
return name;
}
public static string IsolateExtension(string filePath)
{
return filePath.Substring(filePath.LastIndexOf('.'));
}
public static string CorrectFilePath(string filePath)
{
if (!filePath.EndsWith("/") && filePath.Length > 0)
{
filePath = filePath + "/";
}
return filePath;
}
}
}