This commit is contained in:
CortexCore
2025-01-24 18:11:03 +08:00
parent 4b72602bfa
commit 41715e4413
12 changed files with 545 additions and 1391 deletions

View File

@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: bf1c155248e26c446b9f5bd892f84eef
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,137 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
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 DotNetSdkRoslynService
{
private const string _InstalledFilesKey = "DotNetSdkRoslyn_InstalledFiles";
public static string DownloadUrl =>
"http://server.bitfall.icu:3000/root/BITKit.DotNetSdkRoslyn.Unity/archive/main.zip";
public static bool Installed=>File.Exists(CSCPath);
public static string CSCPath =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(BITKit),"DotnetSdkRoslyn","csc.dll");
public static string FolderPath =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(BITKit),"DotnetSdkRoslyn");
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
{
await request.SendWebRequest();
while (request.isDone is false)
{
await UniTask.SwitchToMainThread();
OnDownloadProgress?.Invoke(request.downloadProgress);
await UniTask.NextFrame();
}
OnDownloadProgress?.Invoke(1);
using var ms = new MemoryStream(request.downloadHandler.data);
request.Dispose();
await UniTask.SwitchToTaskPool();
var zipArchive = new ZipArchive(ms);
PathHelper.EnsureDirectoryCreated(FolderPath);
List<string> installedFiles = new();
var reportBuilder =new StringBuilder();
reportBuilder.AppendLine($"正在安装文件到:{FolderPath}");
var entryFolder = zipArchive.Entries[0].FullName;
foreach (var zipArchiveEntry in zipArchive.Entries)
{
var entryPath = Path.Combine(FolderPath, zipArchiveEntry.FullName.Replace(entryFolder,string.Empty));
if (zipArchiveEntry.Name is "")
{
reportBuilder.AppendLine($"正在创建目录:{entryPath}");
PathHelper.EnsureDirectoryCreated(entryPath);
}
else
{
reportBuilder.AppendLine($"正在解压文件:{entryPath}");
await using var entryStream = zipArchiveEntry.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<DotNetSdkRoslynService>("未安装,无法卸载");
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();
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 56389bdbeb2065541a8e62a4ae5bd746
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,98 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
using static BITKit.Mod.DotNetSdkRoslynService;
namespace BITKit.UX
{
public class UXDotNetSdkRoslynService : MonoBehaviour
{
[UXBindPath("install-roslyn-button")]
private Button _installButton;
[UXBindPath("uninstall-roslyn-button")]
private Button _uninstallButton;
[UXBindPath("install-roslyn-fill")]
private VisualElement _installFill;
private void OnEnable()
{
OnInstall+=_OnInstall;
OnInstalled+=_OnInstalled;
OnUnInstalled+=_OnUnInstalled;
OnDownloadProgress+=_OnDownloadProgress;
OnInstallFailed += _OnInstallFailed;
}
private void OnDisable()
{
OnInstall-=_OnInstall;
OnInstalled-=_OnInstalled;
OnUnInstalled-=_OnUnInstalled;
OnDownloadProgress-=_OnDownloadProgress;
OnInstallFailed -= _OnInstallFailed;
}
private void Start()
{
UXUtils.Inject(this);
if (Installed)
{
_OnInstalled();
}
else
{
_OnUnInstalled();
}
_installButton.clicked +=()=>Install().Forget();
_uninstallButton.clicked +=()=>
{
_uninstallButton.SetEnabled(false);
UnInstall().Forget();
};
}
private void _OnInstall()
{
_installButton.text = "准备中...";
_installButton.SetEnabled(false);
_uninstallButton.SetEnabled(false);
}
private void _OnUnInstalled()
{
_installButton.text = "安装";
_uninstallButton.SetEnabled(false);
_installButton.SetEnabled(true);
_installFill.style.width = new StyleLength(Length.Percent(0));
}
private void _OnInstalled()
{
_installButton.text = "重新安装";
_installButton.SetEnabled(true);
_uninstallButton.SetEnabled(true);
_installFill.style.width = new StyleLength(Length.Percent(0));
}
private void _OnDownloadProgress(float progress)
{
if (progress >= 1)
{
_installButton.text = "安装中...";
}
else
{
_installButton.text = $"下载中...{progress:P}";
}
_installFill.style.width = new StyleLength(Length.Percent((int)(progress * 100)));
}
private void _OnInstallFailed(Exception e)
{
_installButton.tooltip = e.Message;
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 8c8c71e08d0a2544aae8e62cd2b86af2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: