SpookTubeEX/Uploader.cs

109 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using HarmonyLib;
using Photon.Pun;
using Steamworks;
using UnityEngine.UIElements;
using UnityEngine.UIElements.Collections;
using WebSocketSharp;
namespace SpookTubeEX;
class SpookedUploader
{
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;
}
// Get co-conspirators lmao
// string[] coconspirators = new string[PhotonNetwork.CurrentRoom.PlayerCount];
// foreach (KeyValuePair<int, Photon.Realtime.Player> player in PhotonNetwork.CurrentRoom.Players)
// {
// // if (player.Value.IsMasterClient) continue;
// coconspirators.AddItem(player.Value.NickName);
// // if (player.Value.CustomProperties.ContainsKey("SteamID"))
// // {
// // player.Value.CustomProperties["SteamID"]
// // }
// }
// string[] coconspirators = PhotonNetwork.CurrentRoom.Players.Select((player) => player.Value.NickName) as string[];
ws = new WebSocket(Plugin.URL.Value);
string viewableUrl = "";
string uuid = "";
ws.OnMessage += (sender, evt) =>
{
ws.Send(evt.Data);
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();
// Plugin.ModalProxy("Upload Complete", $"Video uploaded to {viewableUrl}! ({uuid})");
System.Diagnostics.Process.Start($"{viewableUrl}/#{uuid}");
onUpload();
}
if (evt.Data.StartsWith("us"))
{
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 SpookedTube and your Discord account banned from the server. DO NOT SHARE THIS UUID WITH ANYONE!");
}
};
ws.OnError += (sender, evt) =>
{
UnityEngine.Debug.LogError(evt.Message);
};
ws.OnOpen += (sender, evt) =>
{
ws.Send("persona:" + SteamFriends.GetPersonaName());
ws.Send(SteamUser.GetSteamID().m_SteamID.ToString());
};
ws.Connect();
}
}
}