forked from NtsFranz/Spark
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOverlaysCustom.cs
More file actions
241 lines (214 loc) · 6.55 KB
/
OverlaysCustom.cs
File metadata and controls
241 lines (214 loc) · 6.55 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
using NAudio.SoundFont;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Spark
{
public static class OverlaysCustom
{
private static Dictionary<string, string> overlayData;
public static bool downloading;
public static async Task FetchOverlayData()
{
downloading = true;
try
{
#if DEBUG
await DownloadOverlaysDev();
#else
await DownloadOverlays();
#endif
CopyObsSceneCollection();
}
catch (Exception e)
{
Logger.LogRow(Logger.LogType.Error, e.ToString());
}
downloading = false;
}
private static async Task DownloadOverlaysDev()
{
string route = DiscordOAuth.AccessCode.series_name;
if (route.Contains("vrml"))
{
route = "vrml";
}
string folder = @"S:\git_repo\IgniteVR-Overlays\SparkOverlays\" + route;
if (Directory.Exists(folder))
{
if (Directory.Exists(OverlayServer.StaticOverlayFolder)) Directory.Delete(OverlayServer.StaticOverlayFolder, true);
Directory.CreateDirectory(OverlayServer.StaticOverlayFolder);
CopyDirectory(folder, Path.Combine(OverlayServer.StaticOverlayFolder, route), true);
RemoveHtmlExt(Path.Combine(OverlayServer.StaticOverlayFolder, route));
}
else
{
await DownloadOverlays();
}
}
private static async Task DownloadOverlays()
{
if (DiscordOAuth.Personal) return;
string route = DiscordOAuth.AccessCode.series_name;
if (route.Contains("vrml"))
{
route = "vrml";
}
if (Directory.Exists(OverlayServer.StaticOverlayFolder)) Directory.Delete(OverlayServer.StaticOverlayFolder, true);
Directory.CreateDirectory(OverlayServer.StaticOverlayFolder);
try
{
using HttpClient webClient = new HttpClient();
byte[] result = await webClient.GetByteArrayAsync($"{Program.APIURL}/get_overlays/{DiscordOAuth.AccessCode.series_name}/{DiscordOAuth.oauthToken}", CancellationToken.None);
await using MemoryStream file = new MemoryStream(result);
using ZipArchive zip = new ZipArchive(file, ZipArchiveMode.Read);
zip.ExtractToDirectory(Path.Combine(OverlayServer.StaticOverlayFolder, route));
RemoveHtmlExt(Path.Combine(OverlayServer.StaticOverlayFolder, route));
}
catch (InvalidDataException)
{
// an access code without overlays
}
catch (Exception e)
{
Logger.LogRow(Logger.LogType.Error, e.ToString());
}
}
/// <summary>
/// https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
/// </summary>
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// Get information about the source directory
DirectoryInfo dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
{
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
}
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
private static void CopyObsSceneCollection()
{
string route = DiscordOAuth.AccessCode.series_name;
if (route.Contains("vrml"))
{
route = "vrml";
}
string sceneCollectionFile = Path.Combine(OverlayServer.StaticOverlayFolder, route, route + ".json");
if (route == "personal")
{
route = $"Spark v{Program.AppVersion().Major}.{Program.AppVersion().Minor}";
sceneCollectionFile = Path.Combine(Path.GetDirectoryName(SparkSettings.instance.sparkExeLocation) ?? string.Empty, "resources", "obs_scene_collection.json");
}
string sceneCollectionDestPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "obs-studio", "basic", "scenes", route + ".json");
if (File.Exists(sceneCollectionFile))
{
Directory.CreateDirectory(Path.GetDirectoryName(sceneCollectionDestPath) ?? string.Empty);
File.Copy(sceneCollectionFile, sceneCollectionDestPath, true);
}
}
private static void RemoveHtmlExt(string path)
{
// Get information about the source directory
DirectoryInfo dir = new DirectoryInfo(path);
// Check if the source directory exists
if (!dir.Exists)
{
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
}
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string fileName = file.FullName;
if (file.Name != "index.html" && file.Name.EndsWith(".html"))
{
fileName = fileName.Replace(".html", "");
if (!Directory.Exists(fileName))
{
file.CopyTo(fileName, true);
}
}
}
foreach (DirectoryInfo subDir in dirs)
{
RemoveHtmlExt(subDir.FullName);
}
}
// public static void MapRoutes(IEndpointRouteBuilder endpoints)
// {
// if (overlayData == null) return;
// foreach (string str in overlayData.Keys)
// {
// string[] ignoreEnds = { "index.html", ".html" };
// string route = DiscordOAuth.AccessCode.series_name;
// if (route.Contains("vrml"))
// {
// route = "vrml";
// }
//
// string url = $"/{route}/";
// bool ended = false;
// foreach (string e in ignoreEnds)
// {
// if (str.EndsWith(e))
// {
// url += str[..^e.Length];
// ended = true;
// break;
// }
// }
//
// if (!ended)
// {
// url += str;
// }
//
// url = url.Replace("\\", "/");
//
// endpoints.MapGet(url, async context =>
// {
// #if DEBUG
// await FetchOverlayData();
// #endif
//
// string contentType = str.Split('.').Last() switch
// {
// "js" => "application/javascript",
// "css" => "text/css",
// "png" => "image/png",
// "jpg" => "image/jpeg",
// _ => ""
// };
//
// context.Response.Headers.Add("content-type", contentType);
// await context.Response.WriteAsync(overlayData[str]);
// });
// }
// }
}
}