Implement uploading
This commit is contained in:
parent
252e3a5190
commit
c655b9de01
32
Plugin.cs
32
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<IPlayableVideo>();
|
||||
int views = traverse.Field("m_replayViews").GetValue<int>();
|
||||
CommentUI[] comments = traverse.Field("m_replayComments").GetValue<CommentUI[]>();
|
||||
IPlayableVideo video = new Traverse(__instance).Field("m_replayVideo").GetValue() as IPlayableVideo;
|
||||
int views = new Traverse(__instance).Field("m_replayViews").GetValue<int>();
|
||||
CommentUI[] comments = new Traverse(__instance).Field("m_replayComments").GetValue() as CommentUI[];
|
||||
|
||||
SpookedUploader.Upload(video, views, comments);
|
||||
GameObject States = __instance.m_videoPlayer.transform.parent.parent.parent.gameObject;
|
||||
|
||||
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<CloseVideoInteractable>().Interact(null);
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
80
Uploader.cs
80
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<SpookNetMessage>(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<byte> msgList = new List<byte>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user