BITFALL/Assets/BITKit/Unity/Scripts/MonoBleedingEdge/MonoBleedingEdgeService.cs

128 lines
3.5 KiB
C#
Raw Normal View History

2024-01-27 04:09:57 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using Unity.SharpZipLib.Utils;
using UnityEngine;
using UnityEngine.Experimental.Audio;
using UnityEngine.Networking;
namespace BITKit.Mod
{
public class MonoBleedingEdgeService
{
private const string _InstalledFilesKey = "MonoBleedingEdge_InstalledFiles";
public static string DownloadUrl =>
"http://server.bitfall.icu:3000/root/MonoBleedingEdge/releases/download/2022.3.14f1/MonoBleedingEdge.zip";
public static bool Installed=>System.IO.File.Exists(MCSPath);
public static string MCSPath=>System.IO.Path.Combine(Environment.CurrentDirectory, "MonoBleedingEdge", "bin", "mcs");
public static event Action OnInstall;
public static event Action OnInstalled;
public static event Action OnUnInstalled;
public static event Action<Exception> OnInstallFailed;
public static event Action<float> OnDownloadProgress;
private static string[] _InstalledFiles
{
get
{
if (!PlayerPrefs.HasKey(_InstalledFilesKey)) return Array.Empty<string>();
var json = PlayerPrefs.GetString(_InstalledFilesKey);
try
{
return JsonConvert.DeserializeObject<string[]>(json);
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
return Array.Empty<string>();
}
set
{
PlayerPrefs.SetString(_InstalledFilesKey, JsonConvert.SerializeObject(value));
PlayerPrefs.Save();
}
}
public static async UniTask Install()
{
await UniTask.SwitchToMainThread();
OnInstall?.Invoke();
var request = UnityWebRequest.Get(DownloadUrl);
try
{
request.SendWebRequest();
while (request.isDone is false)
{
await UniTask.SwitchToMainThread();
OnDownloadProgress?.Invoke(request.downloadProgress);
await UniTask.NextFrame();
}
OnDownloadProgress?.Invoke(1);
using var ms = new System.IO.MemoryStream(request.downloadHandler.data);
request.Dispose();
await UniTask.SwitchToTaskPool();
var zipArchive = new System.IO.Compression.ZipArchive(ms);
var path = System.IO.Path.Combine(Environment.CurrentDirectory, "MonoBleedingEdge");
PathHelper.EnsureDirectoryCreated(path);
List<string> installedFiles = new();
foreach (var VARIABLE in zipArchive.Entries)
{
var entryPath = System.IO.Path.Combine(path, VARIABLE.FullName);
if (VARIABLE.Name is "")
{
PathHelper.EnsureDirectoryCreated(entryPath);
}
else
{
await using var entryStream = VARIABLE.Open();
await using var fs = System.IO.File.Create(entryPath);
await entryStream.CopyToAsync(fs);
installedFiles.Add(entryPath);
}
}
await UniTask.SwitchToMainThread();
_InstalledFiles = installedFiles.ToArray();
installedFiles.Clear();
}
catch (Exception e)
{
await UniTask.SwitchToMainThread();
request.Dispose();
OnInstallFailed?.Invoke(e);
return;
}
OnInstalled?.Invoke();
}
public static async UniTask UnInstall()
{
if (Installed is false)
{
BIT4Log.Warning<MonoBleedingEdgeService>("未安装,无法卸载");
return;
}
await UniTask.SwitchToMainThread();
var files = _InstalledFiles;
await UniTask.SwitchToTaskPool();
foreach (var path in files)
{
File.Delete(path);
}
await UniTask.SwitchToMainThread();
PlayerPrefs.DeleteKey(_InstalledFilesKey);
PlayerPrefs.Save();
OnUnInstalled?.Invoke();
}
}
}