-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-KaraokeProviderAdapterLifeCycle.cs
204 lines (170 loc) · 7.61 KB
/
2-KaraokeProviderAdapterLifeCycle.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
using UnityEngine;
using UniInject;
using UnityEngine.UIElements;
using UniRx;
using System;
using System.Collections.Generic;
using UnityEngine.Networking;
using System.Linq;
using System.IO;
using System.Text;
// Open the mod folder with Visual Studio Code and installed C# Dev Kit for IDE features such as
// code completion, error markers, parameter hints, go to definition, etc.
// ---
// Mods must implement subtypes of special mod interfaces.
// Available interfaces can be found by executing 'mod.interfaces' in the game's console.
public class KaraokeProviderAdapterLifeCycle : IOnLoadMod, IOnDisableMod, ISongRepository
{
// Get common objects from the app environment via Inject attribute.
[Inject]
private AudioManager audioManager;
[Inject]
private KaraokeProviderAdapterModSettings kpAdapterSettings;
private static bool isKaraokeProviderRequestDone;
private static readonly Dictionary<string, SongRepositorySearchResultEntry> songIdToSearchResultCache = new Dictionary<string, SongRepositorySearchResultEntry>();
private static List<KaraokeProviderSongMetaDto> webSongMetas;
[Serializable]
class KaraokeProviderSongMetaDto
{
public string songId { get; set; }
public string audioLink { get; set; }
public string videoLink { get; set; }
public string coverLink { get; set; }
public string backgroundLink { get; set; }
public string textLink { get; set; }
public string artist { get; set; }
public string title { get; set; }
}
public void OnLoadMod()
{
// You can do anything here, for example ...
// ... change audio clips
// audioManager.defaultButtonSound = AudioManager.LoadAudioClipFromUriImmediately($"{modContext.ModFolder}/sounds/cartoon-jump-6462.mp3");
Debug.Log($"{nameof(KaraokeProviderAdapterLifeCycle)}.OnLoadMod");
Debug.Log($"Getting KaraokeProvider songs from {kpAdapterSettings.KaraokeProviderApiUrl}");
UnityWebRequest request = UnityWebRequest.Get(kpAdapterSettings.KaraokeProviderApiUrl);
request.SendWebRequest()
.AsAsyncOperationObservable()
.Subscribe(_ => LoadKaraokeProviderSongs(request),
exception => Debug.LogException(exception),
() => LoadKaraokeProviderSongs(request));
}
private void LoadKaraokeProviderSongs(UnityWebRequest webRequest)
{
if (isKaraokeProviderRequestDone
|| !webRequest.isDone)
{
return;
}
isKaraokeProviderRequestDone = true;
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError($"Getting song metas from '{webRequest.url}'"
+ $" has result {webRequest.result}.\n{webRequest.error}");
return;
}
string body = webRequest.downloadHandler.text;
webSongMetas = JsonConverter.FromJson<List<KaraokeProviderSongMetaDto>>(body);
Debug.Log($"{webSongMetas.Count} songs found");
}
public void OnDisableMod()
{
Debug.Log($"{nameof(KaraokeProviderAdapterLifeCycle)}.OnDisableMod");
}
public IObservable<SongRepositorySearchResultEntry> SearchSongs(SongRepositorySearchParameters searchParameters)
{
if(!isKaraokeProviderRequestDone)
{
Debug.Log("songs not loaded yet");
return Observable.Empty<SongRepositorySearchResultEntry>();
}
if (searchParameters == null
|| searchParameters.SearchText.IsNullOrEmpty())
{
Debug.Log("no search text given");
return Observable.Empty<SongRepositorySearchResultEntry>();
}
Debug.Log($"searching song with search text: '{searchParameters.SearchText}'");
return ObservableUtils.RunOnNewTaskAsObservableElements(() => SearchSongList(searchParameters), Disposable.Empty);
}
public List<SongRepositorySearchResultEntry> SearchSongList(SongRepositorySearchParameters searchParameters)
{
string searchText = searchParameters.SearchText;
if (searchText.IsNullOrEmpty())
{
return new List<SongRepositorySearchResultEntry>();
}
string searchTextLower = searchText.ToLower();
List<SongRepositorySearchResultEntry> resultEntries = webSongMetas
.Where(songMeta =>
{
return songMeta.title.Contains(searchTextLower) || songMeta.artist.Contains(searchTextLower);
})
.Select(LoadUltraStarSongFromProvider)
.Where(it => it != null)
.ToList();
Debug.Log($"{nameof(KaraokeProviderAdapterLifeCycle)} - Found {resultEntries.Count} songs matching search '{searchText}'");
return resultEntries;
}
private SongRepositorySearchResultEntry LoadUltraStarSongFromProvider(KaraokeProviderSongMetaDto kpSongMeta)
{
if (songIdToSearchResultCache.TryGetValue(kpSongMeta.songId, out SongRepositorySearchResultEntry cachedResultEntry))
{
return cachedResultEntry;
}
}
private void fetchTextFileAndAddToSongMeta(KaraokeProviderSongMetaDto source)
{
UnityWebRequest request = UnityWebRequest.Get(kpAdapterSettings.KaraokeProviderApiUrl + source.textLink);
request.SendWebRequest()
.AsAsyncOperationObservable()
.Subscribe(_ => SaveKaraokeProviderSongAndParse(request, source),
exception => Debug.LogException(exception));
}
private SongRepositorySearchResultEntry SaveKaraokeProviderSongAndParse(UnityWebRequest webRequest, KaraokeProviderSongMetaDto source)
{
if (!webRequest.isDone)
{
Log.Debug($"WebRequest for song '{source.artist} - {source.title}' not completed yet");
return null;
}
if (webRequest.result != UnityWebRequest.Result.Success)
{
Debug.LogError($"Getting song text from '{webRequest.url}' for song '{source.artist} - {source.title}'"
+ $" has result {webRequest.result}.\n{webRequest.error}");
return null;
}
string folderName = Application.persistentDataPath + "/KaraokeProviderSongs/" + source.artist + "/" + source.title + "/";
string fileName = source.artist + " - " + source.title + ".txt";
Directory.CreateDirectory(folderName);
string fullPath = folderName + fileName;
File.WriteAllText(fullPath, webRequest.downloadHandler.text);
try
{
SongMeta newSongMeta = UltraStarSongParser.ParseFile(fullPath, out List<SongIssue> newSongIssues, Encoding.UTF8);
SongRepositorySearchResultEntry resultEntry = new SongRepositorySearchResultEntry(newSongMeta, newSongIssues);
newSongMeta.RemoteSource = nameof(KaraokeProviderAdapterLifeCycle);
newSongMeta.Audio = appendUrlPrefix(source.audioLink);
newSongMeta.Video = appendUrlPrefix(source.videoLink);
newSongMeta.Background = appendUrlPrefix(source.backgroundLink);
newSongMeta.Cover = appendUrlPrefix(source.coverLink);
songIdToSearchResultCache[source.songId] = resultEntry;
Debug.Log($"'{newSongMeta.Artist} - {newSongMeta.Title}' added");
return resultEntry;
}
catch (Exception ex)
{
Debug.LogException(ex);
Debug.LogError($"Failed to load {fullPath}");
return null;
}
}
private string appendUrlPrefix(string value)
{
if (!String.IsNullOrEmpty(value))
{
return kpAdapterSettings.KaraokeProviderApiUrl + value;
}
return value;
}
}