SpookTubeEX/Uploader.cs

145 lines
4.9 KiB
C#
Raw Normal View History

2024-06-01 22:14:28 +10:00
using System;
using System.Collections.Generic;
2024-06-04 17:11:41 +10:00
using System.Data.Common;
2024-05-29 08:18:35 +10:00
using System.IO;
2024-06-01 22:14:28 +10:00
using System.Linq;
2024-06-10 08:32:16 +10:00
using System.Threading;
2024-06-01 22:14:28 +10:00
using HarmonyLib;
2024-06-04 17:11:41 +10:00
using Photon.Pun;
2024-06-01 22:14:28 +10:00
using Steamworks;
using UnityEngine;
2024-06-04 17:11:41 +10:00
using UnityEngine.UIElements;
using UnityEngine.UIElements.Collections;
2024-05-29 08:18:35 +10:00
using WebSocketSharp;
2024-06-04 19:53:14 +10:00
namespace SpookTubeEX;
2024-05-29 08:18:35 +10:00
class SpookedUploader
{
2024-06-11 18:56:41 +10:00
void UploadThread(MessageEventArgs evt, WebSocket ws, string path, string uuid, Action<string> setViewableLogText)
2024-06-10 08:32:16 +10:00
{
// Begin Upload
byte[] byteArray = File.ReadAllBytes(path);
int offset = 0;
const int chunkSize = 512000;
2024-06-10 15:58:30 +10:00
var expectedChunks = Math.Ceiling((double)byteArray.Length / chunkSize);
2024-06-10 08:32:16 +10:00
while (byteArray.Length > offset * chunkSize)
{
byte[] buffer = byteArray.Skip(offset * chunkSize).Take(chunkSize).ToArray();
offset += 1;
2024-06-11 18:56:41 +10:00
ShowStatus(setViewableLogText, uuid, "stream", $"{offset}/{expectedChunks}");
2024-06-10 15:58:30 +10:00
Plugin.Logger.LogInfo($"(UPLOAD {uuid}) - {offset}/{expectedChunks} (~{Math.Ceiling((double)buffer.Length / 1000)} KB)");
2024-06-10 08:32:16 +10:00
List<byte> msgList = new List<byte>();
msgList.AddRange(new byte[1] { 36 });
msgList.AddRange(buffer);
ws.Send(msgList.ToArray());
}
}
2024-06-11 18:56:41 +10:00
void ShowStatus(Action<string> setViewableLogText, string uuid, string logType, string message = "")
{
if (uuid.Length == 0) uuid = "...";
string msg = "";
switch (logType) {
case "error":
msg = $"<color=red>{message}</color>\n<color=red><size=50%>CHECK CONSOLE FOR MORE DETAILS</size></color>";
break;
case "stream":
msg = $"STREAMING VIDEO ({message})";
break;
case "complete":
msg = $"<color=#2E8B57>UPLOAD COMPLETE</color>";
break;
default:
msg = message;
break;
}
setViewableLogText($"{msg}\n<mspace=0.13em><size=20%>UUID: {uuid.ToUpper()}</size></mspace>");
}
2024-06-01 22:14:28 +10:00
WebSocket ws;
2024-06-11 18:56:41 +10:00
public void Upload(IPlayableVideo cameraRecording, Action<string> setViewableLogText, Action onUpload)
2024-05-29 08:18:35 +10:00
{
if (cameraRecording.TryGetVideoPath(out string path))
{
if (!File.Exists(path))
{
2024-06-01 09:56:59 +10:00
Modal.ShowError("Path Nonexistent", $"Can't find {path}");
2024-06-01 22:14:28 +10:00
return;
2024-05-29 08:18:35 +10:00
}
2024-06-01 22:14:28 +10:00
ws = new WebSocket(Plugin.URL.Value);
string viewableUrl = "";
string uuid = "";
ws.OnMessage += (sender, evt) =>
2024-05-29 08:18:35 +10:00
{
2024-06-10 15:58:30 +10:00
// Plugin.Logger.LogInfo("WS MESSAGE: " + evt.Data);
if (evt.Data.StartsWith("url:")) // Set viewable URL for browser open
{
viewableUrl = evt.Data.Substring(4);
2024-06-01 22:14:28 +10:00
2024-06-10 15:58:30 +10:00
Plugin.Logger.LogInfo($"Server reports origin as {viewableUrl}.");
}
2024-06-10 08:32:16 +10:00
2024-06-10 15:58:30 +10:00
if (evt.Data.StartsWith("uuid:")) // Set UUID
2024-05-29 08:18:35 +10:00
{
2024-06-01 22:14:28 +10:00
uuid = evt.Data.Substring(5);
2024-06-10 15:58:30 +10:00
2024-06-11 18:56:41 +10:00
ShowStatus(setViewableLogText, uuid, "stream", "0/?");
2024-06-10 15:58:30 +10:00
Plugin.Logger.LogInfo($"Your video has been assigned the UUID {uuid}. Beginning upload...");
// Start upload thread
2024-06-11 18:56:41 +10:00
new Thread(new ThreadStart(() => UploadThread(evt, ws, path, uuid, setViewableLogText))).Start();
2024-06-01 22:14:28 +10:00
}
2024-06-10 15:58:30 +10:00
if (evt.Data.StartsWith("fin")) // On upload complete
2024-06-01 22:14:28 +10:00
{
2024-06-11 18:56:41 +10:00
ShowStatus(setViewableLogText, uuid, "complete");
2024-06-10 15:58:30 +10:00
Plugin.Logger.LogInfo($"Upload Complete. Redirecting to {viewableUrl}...");
Application.OpenURL($"{viewableUrl}/#{uuid}");
2024-06-01 22:14:28 +10:00
}
2024-06-04 17:11:41 +10:00
2024-06-10 15:58:30 +10:00
if (evt.Data.StartsWith("us")) // On heuristics error
2024-06-04 17:11:41 +10:00
{
2024-06-11 18:56:41 +10:00
ShowStatus(setViewableLogText, uuid, "error", "HEUR ERROR");
2024-06-10 15:58:30 +10:00
Plugin.Logger.LogError($"SpookTubeEX has experienced a heuristics error. Please contact ModMail at https://discord.gg/Gw2f86B2vC with this UUID in your ticket request: {uuid}\nThis is an automated service, so trolling will get your IP blocked from accessing anything with SpookTubeEX and your Discord account banned from the server. DO NOT SHARE THIS UUID WITH ANYONE!");
2024-06-04 17:11:41 +10:00
}
2024-06-01 22:14:28 +10:00
};
ws.OnError += (sender, evt) =>
{
ShowStatus(setViewableLogText, uuid, "error", "WS ERROR");
2024-06-10 08:32:16 +10:00
Plugin.Logger.LogError(evt.Message);
2024-06-01 22:14:28 +10:00
};
ws.OnOpen += (sender, evt) =>
{
2024-06-04 17:11:41 +10:00
ws.Send("persona:" + SteamFriends.GetPersonaName());
2024-06-01 22:14:28 +10:00
ws.Send(SteamUser.GetSteamID().m_SteamID.ToString());
};
2024-06-11 18:56:41 +10:00
ws.OnClose += (sender, evt) => {
onUpload();
};
2024-06-10 15:58:30 +10:00
Plugin.Logger.LogInfo($"Connecting to {ws.Url}...");
2024-06-11 18:56:41 +10:00
ws.ConnectAsync();
2024-05-29 08:18:35 +10:00
}
}
}