-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
167 lines (127 loc) · 5.74 KB
/
Main.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
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using MewsToolbox;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace SpeenPhone
{
[BepInPlugin(PluginInfo.GUID, PluginInfo.NAME, PluginInfo.VERSION)]
public class Main : BaseUnityPlugin
{
private static ManualLogSource logger;
public static string ConfigPath { get; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Speen Mods");
public static string ConfigFileName { get; } = Path.Combine(ConfigPath, "SpeenPhoneConfig.ini");
public new static IniFile Config { get; private set; }
private static Dictionary<string, string> mappings;
private static Dictionary<string, AudioClip[]> newClips;
private void Awake()
{
logger = Logger;
if (!Directory.Exists(ConfigPath))
Directory.CreateDirectory(ConfigPath);
if (!File.Exists(ConfigFileName))
File.Create(ConfigFileName).Close();
Config = new IniFile(ConfigFileName);
if (GetMappings())
Harmony.CreateAndPatchAll(typeof(AudioPatches));
//Harmony harmony = new Harmony(PluginInfo.GUID);
//harmony.PatchAll<AudioPatches>();
}
public static bool TryGetClips(string name, out AudioClip[] clips) {
if (mappings.TryGetValue(name, out string mapping) && newClips.TryGetValue(mapping, out clips))
return true;
clips = null;
return false;
}
private static bool GetMappings()
{
string soundsPath = Config.GetValueOrDefaultTo("SFX", "SoundsPath", string.Empty);
if (string.IsNullOrWhiteSpace(soundsPath))
{
LogWarning("This is your first time running the mod (or update 1.1.0). Go to Documents/SpeenMods/SpeenPhoneConfig.ini to change the sounds folder path");
LogWarning("More informations on the GitHub page: https://github.com/SRXDModdingGroup/SpeenPhone#hitsound-folder-info");
return false;
}
if (!Directory.Exists(soundsPath))
{
LogError("The folder specified in the configuration file does not exist or is inaccessible.");
return false;
}
if (!File.Exists(Path.Combine(soundsPath, "Mappings.txt")))
{
LogError("The folder specified in the configuration file does not contain a Mappings.txt file");
return false;
}
mappings = new Dictionary<string, string>();
using (var reader = new StreamReader(Path.Combine(soundsPath, "Mappings.txt"))) {
while (!reader.EndOfStream) {
string line = reader.ReadLine();
if (string.IsNullOrWhiteSpace(line))
continue;
string[] split = line.Split('=');
if (split.Length < 2)
continue;
mappings.Add(split[0].Trim(), split[1].Trim());
}
}
newClips = new Dictionary<string, AudioClip[]>();
foreach (string directory in Directory.EnumerateDirectories(soundsPath)) {
string name = Path.GetFileName(directory);
if (string.IsNullOrWhiteSpace(name) || !mappings.ContainsValue(name) || !TryLoadClips(directory, out var clips)) {
LogWarning($"Could not get clips for {name}");
continue;
}
newClips.Add(name, clips);
}
return true;
}
private static bool TryLoadClips(string directory, out AudioClip[] clips) {
if (!Directory.Exists(directory)) {
clips = null;
return false;
}
var clipsList = new List<AudioClip>();
foreach (string path in Directory.EnumerateFiles(directory)) {
if (TryLoadClip(path, out var clip))
clipsList.Add(clip);
}
if (clipsList.Count == 0) {
clips = null;
return false;
}
clips = clipsList.ToArray();
return true;
}
private static bool TryLoadClip(string path, out AudioClip clip) {
clip = null;
if (!File.Exists(path))
return false;
#pragma warning disable 0618
using (WWW www = new WWW(BepInEx.Utility.ConvertToWWWFormat(path)))
#pragma warning restore 0618
{
try
{
clip = www.GetAudioClip();
while (clip.loadState == AudioDataLoadState.Loading) { }
LogInfo($"Loaded audio file at {path}");
}
catch
{
LogError($"Error while loading {path}");
LogError(www.error);
}
return clip != null;
}
}
public static void Log(LogLevel level, object msg) => logger.Log(level, msg);
public static void LogDebug(object msg) => Log(LogLevel.Debug, msg);
public static void LogInfo(object msg) => Log(LogLevel.Info, msg);
public static void LogMessage(object msg) => Log(LogLevel.Message, msg);
public static void LogWarning(object msg) => Log(LogLevel.Warning, msg);
public static void LogError(object msg) => Log(LogLevel.Error, msg);
}
}