From c655b9de013aca38094487a4c40b1ba1a3b8571d Mon Sep 17 00:00:00 2001 From: RedBigz Date: Sat, 1 Jun 2024 22:14:28 +1000 Subject: [PATCH] Implement uploading --- Plugin.cs | 32 +++++++++++++++++---- Uploader.cs | 80 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 89 insertions(+), 23 deletions(-) diff --git a/Plugin.cs b/Plugin.cs index bd5c6b8..c6b12cd 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -4,6 +4,7 @@ using HarmonyLib; using UnityEngine; using UnityEngine.UI; using TMPro; +using Photon.Pun; namespace SpookedTube; @@ -19,7 +20,7 @@ public class Plugin : BaseUnityPlugin private void Awake() { // Config - URL = Config.Bind("Networking", "WebsocketURL", "ws://127.0.0.1:6969/upload.ws", "The ws[s]:// URL."); + URL = Config.Bind("Networking", "WebsocketURL", "ws://127.0.0.1:3000/", "The ws[s]:// URL."); Username = Config.Bind("Networking", "Username", "Guest", "Your display name."); // Plugin startup logic @@ -44,13 +45,32 @@ class ReplayPatch [HarmonyPrefix] static bool Prefix(UploadCompleteUI __instance) { - Traverse traverse = new(__instance); + if (!PhotonNetwork.IsMasterClient) + { + Modal.ShowError("Upload Error", "Only the host can upload videos to SpookedTube."); + return false; + } - IPlayableVideo video = traverse.Field("m_replayVideo").GetValue(); - int views = traverse.Field("m_replayViews").GetValue(); - CommentUI[] comments = traverse.Field("m_replayComments").GetValue(); + IPlayableVideo video = new Traverse(__instance).Field("m_replayVideo").GetValue() as IPlayableVideo; + int views = new Traverse(__instance).Field("m_replayViews").GetValue(); + CommentUI[] comments = new Traverse(__instance).Field("m_replayComments").GetValue() as CommentUI[]; + + GameObject States = __instance.m_videoPlayer.transform.parent.parent.parent.gameObject; - SpookedUploader.Upload(video, views, comments); + GameObject ShowVideoState = Util.FindObject(States, "ShowVideoState"); + GameObject UploadingState = Util.FindObject(States, "UploadingState"); + + // Show Uploading Screen + UploadingState.SetActive(true); + ShowVideoState.SetActive(false); + + SpookedUploader uploader = new(); + + uploader.Upload(video, views, comments, () => + { + UploadingState.SetActive(false); + Util.FindObject(Util.FindObject(__instance.m_videoPlayer.transform.parent.parent.parent.parent.parent.gameObject, "SaveVideoToDesktopInteractable"), "CloseInt").GetComponent().Interact(null); + }); return false; } diff --git a/Uploader.cs b/Uploader.cs index eb596a7..206ec0b 100644 --- a/Uploader.cs +++ b/Uploader.cs @@ -1,38 +1,84 @@ +using System; +using System.Collections.Generic; using System.IO; +using System.Linq; +using HarmonyLib; +using Steamworks; using WebSocketSharp; -using Newtonsoft.Json; namespace SpookedTube; -public class SpookNetMessage -{ - public int statusID { get; set; } - public string data { get; set; } // Base64 -} - class SpookedUploader { - public static int Upload(IPlayableVideo cameraRecording, int views, CommentUI[] comments) + WebSocket ws; + public void Upload(IPlayableVideo cameraRecording, int views, CommentUI[] comments, Action onUpload) { if (cameraRecording.TryGetVideoPath(out string path)) { if (!File.Exists(path)) { Modal.ShowError("Path Nonexistent", $"Can't find {path}"); - return -1; + return; } - using (WebSocket ws = new(Plugin.URL.Value)) + ws = new WebSocket(Plugin.URL.Value); + + string viewableUrl = ""; + string uuid = ""; + + ws.OnMessage += (sender, evt) => { - ws.OnMessage += (sender, evt) => - { - SpookNetMessage message = JsonConvert.DeserializeObject(evt.Data); - }; + ws.Send(evt.Data); - ws.Connect(); - } + if (evt.Data.StartsWith("url:")) viewableUrl = evt.Data.Substring(4); + if (evt.Data.StartsWith("uuid:")) + { + uuid = evt.Data.Substring(5); + + // Begin Upload + byte[] byteArray = File.ReadAllBytes(path); + + int offset = 0; + const int chunkSize = 512000; + + while (byteArray.Length > offset * chunkSize) + { + byte[] buffer = byteArray.Skip(offset * chunkSize).Take(chunkSize).ToArray(); + offset += 1; + + ws.Send($"Bytes: {buffer.Length}"); + + List msgList = new List(); + + msgList.AddRange(new byte[1] { 36 }); + msgList.AddRange(buffer); + + ws.Send(msgList.ToArray()); + } + } + + if (evt.Data.StartsWith("fin")) + { + ws.Close(); + System.Diagnostics.Process.Start($"https://{viewableUrl}/#{uuid}"); + // Modal.ShowError("Upload Complete", $"Video uploaded to {viewableUrl}! ({uuid})"); + onUpload(); + } + }; + + ws.OnError += (sender, evt) => + { + UnityEngine.Debug.LogError(evt.Message); + }; + + ws.OnOpen += (sender, evt) => + { + UnityEngine.Debug.Log(SteamUser.GetSteamID().m_SteamID.ToString()); + ws.Send(SteamUser.GetSteamID().m_SteamID.ToString()); + }; + + ws.Connect(); } - return 0; } } \ No newline at end of file