using System; using System.Collections.Generic; using System.Data.Common; using System.IO; using System.Linq; using System.Threading; using HarmonyLib; using Photon.Pun; using Steamworks; using UnityEngine; using UnityEngine.UIElements; using UnityEngine.UIElements.Collections; using WebSocketSharp; namespace SpookTubeEX; class SpookedUploader { void UploadThread(MessageEventArgs evt, WebSocket ws, string path, string uuid, Action setViewableLogText) { // Begin Upload byte[] byteArray = File.ReadAllBytes(path); int offset = 0; const int chunkSize = 512000; var expectedChunks = Math.Ceiling((double)byteArray.Length / chunkSize); while (byteArray.Length > offset * chunkSize) { byte[] buffer = byteArray.Skip(offset * chunkSize).Take(chunkSize).ToArray(); offset += 1; ShowStatus(setViewableLogText, uuid, "stream", $"{offset}/{expectedChunks}"); Plugin.Logger.LogInfo($"(UPLOAD {uuid}) - {offset}/{expectedChunks} (~{Math.Ceiling((double)buffer.Length / 1000)} KB)"); List msgList = new List(); msgList.AddRange(new byte[1] { 36 }); msgList.AddRange(buffer); ws.Send(msgList.ToArray()); } } void ShowStatus(Action setViewableLogText, string uuid, string logType, string message = "") { if (uuid.Length == 0) uuid = "..."; string msg = ""; switch (logType) { case "error": msg = $"{message}\nCHECK CONSOLE FOR MORE DETAILS"; break; case "stream": msg = $"STREAMING VIDEO ({message})"; break; case "complete": msg = $"UPLOAD COMPLETE"; break; default: msg = message; break; } setViewableLogText($"{msg}\nUUID: {uuid.ToUpper()}"); } WebSocket ws; public void Upload(IPlayableVideo cameraRecording, Action setViewableLogText, Action onUpload) { if (cameraRecording.TryGetVideoPath(out string path)) { if (!File.Exists(path)) { Modal.ShowError("Path Nonexistent", $"Can't find {path}"); return; } ws = new WebSocket(Plugin.URL.Value); string viewableUrl = ""; string uuid = ""; ws.OnMessage += (sender, evt) => { // Plugin.Logger.LogInfo("WS MESSAGE: " + evt.Data); if (evt.Data.StartsWith("url:")) // Set viewable URL for browser open { viewableUrl = evt.Data.Substring(4); Plugin.Logger.LogInfo($"Server reports origin as {viewableUrl}."); } if (evt.Data.StartsWith("uuid:")) // Set UUID { uuid = evt.Data.Substring(5); ShowStatus(setViewableLogText, uuid, "stream", "0/?"); Plugin.Logger.LogInfo($"Your video has been assigned the UUID {uuid}. Beginning upload..."); // Start upload thread new Thread(new ThreadStart(() => UploadThread(evt, ws, path, uuid, setViewableLogText))).Start(); } if (evt.Data.StartsWith("fin")) // On upload complete { ShowStatus(setViewableLogText, uuid, "complete"); Plugin.Logger.LogInfo($"Upload Complete. Redirecting to {viewableUrl}..."); Application.OpenURL($"{viewableUrl}/#{uuid}"); } if (evt.Data.StartsWith("us")) // On heuristics error { ShowStatus(setViewableLogText, uuid, "error", "HEUR ERROR"); 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!"); } }; ws.OnError += (sender, evt) => { ShowStatus(setViewableLogText, uuid, "error", "WS ERROR"); Plugin.Logger.LogError(evt.Message); }; ws.OnOpen += (sender, evt) => { ws.Send("persona:" + SteamFriends.GetPersonaName()); ws.Send(SteamUser.GetSteamID().m_SteamID.ToString()); }; ws.OnClose += (sender, evt) => { onUpload(); }; Plugin.Logger.LogInfo($"Connecting to {ws.Url}..."); ws.ConnectAsync(); } } }