SpookTubeEX/Uploader.cs

84 lines
2.4 KiB
C#
Raw Normal View History

2024-06-01 22:14:28 +10:00
using System;
using System.Collections.Generic;
2024-05-29 08:18:35 +10:00
using System.IO;
2024-06-01 22:14:28 +10:00
using System.Linq;
using HarmonyLib;
using Steamworks;
2024-05-29 08:18:35 +10:00
using WebSocketSharp;
namespace SpookedTube;
class SpookedUploader
{
2024-06-01 22:14:28 +10:00
WebSocket ws;
public void Upload(IPlayableVideo cameraRecording, int views, CommentUI[] comments, Action onUpload)
2024-05-29 08:18:35 +10:00
{
if (cameraRecording.TryGetVideoPath(out string path))
{
if (!File.Exists(path))
{
2024-06-01 09:56:59 +10:00
Modal.ShowError("Path Nonexistent", $"Can't find {path}");
2024-06-01 22:14:28 +10:00
return;
2024-05-29 08:18:35 +10:00
}
2024-06-01 22:14:28 +10:00
ws = new WebSocket(Plugin.URL.Value);
string viewableUrl = "";
string uuid = "";
ws.OnMessage += (sender, evt) =>
2024-05-29 08:18:35 +10:00
{
2024-06-01 22:14:28 +10:00
ws.Send(evt.Data);
if (evt.Data.StartsWith("url:")) viewableUrl = evt.Data.Substring(4);
if (evt.Data.StartsWith("uuid:"))
2024-05-29 08:18:35 +10:00
{
2024-06-01 22:14:28 +10:00
uuid = evt.Data.Substring(5);
2024-05-29 08:18:35 +10:00
2024-06-01 22:14:28 +10:00
// 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();
2024-05-29 08:18:35 +10:00
}
}
}