SpookTubeEX/Uploader.cs
2024-06-04 19:53:14 +10:00

109 lines
4.0 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($"https://{viewableUrl}/#{uuid}");
onUpload();
}
if (evt.Data.StartsWith("us"))
{
// Modal.ShowError("Unsafe Upload", "The server responded with an HEUR error. Your file has not been uploaded due to security reasons. If you believe this was an error, contact st.appeals.vid@redbigz.com with this ID:\n4cedcd69-b5e4-4cda-9142-104009841695\n\nYour appeal will be rejected if:\n- Your video uses mods such as Flashcard\n\nYour IP address will be banned if:\n- Your video contains illicit content\n- Your video contains sexual imagery\n\nYour email address WILL be blocked if you troll the email address above. It is an automated service and your address will be blacklisted, along with your IP address if it has connections to your email.");
}
};
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();
}
}
}