BITKit/Src/Unity/Scripts/UX/ModService/UXModService.cs

227 lines
5.9 KiB
C#
Raw Normal View History

2024-03-31 23:31:00 +08:00
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
2024-07-05 15:07:38 +08:00
#if UNITY_5_3_OR_NEWER && UNITY_WINDOW
2024-03-31 23:31:00 +08:00
using AnotherFileBrowser.Windows;
2024-06-14 14:11:28 +08:00
#endif
2024-03-31 23:31:00 +08:00
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
2024-11-03 16:38:17 +08:00
public class UXModService:UIToolKitPanel,IDisposable
2024-03-31 23:31:00 +08:00
{
2024-11-03 16:38:17 +08:00
protected override string DocumentPath => "ux_mod_Service";
private const string TemplatePath = "ux_mod_service_template";
public override bool AllowCursor => true;
public UXModService(IUXService uxService) : base(uxService)
{
ModService.OnModInstalled+=OnModInstalled;
ModService.OnModUnInstalled+=OnModUnInstalled;
ModService.OnModLoaded+=OnModLoaded;
ModService.OnModUnLoaded+=OnModUnLoaded;
ModService.OnLocked+=OnLocked;
}
private VisualTreeAsset _modTemplate;
2024-03-31 23:31:00 +08:00
[UXBindPath("open-mod-button")]
private Button _openModButton;
[UXBindPath("mods-container")]
private VisualElement _modsContainer;
[UXBindPath("return-button")]
private Button _returnButton;
[UXBindPath("mod-name-label")]
private Label _modNameLabel;
[UXBindPath("mod-description-label")]
private Label _modDescriptionLabel;
[UXBindPath("reload-mod-button",true)]
2024-11-03 16:38:17 +08:00
private Button _reloadModButton;
2024-03-31 23:31:00 +08:00
2024-08-13 18:42:51 +08:00
private readonly ConcurrentDictionary<string,VisualElement> _modContainers=new();
2024-11-03 16:38:17 +08:00
2024-03-31 23:31:00 +08:00
private void OnLocked(bool obj)
{
2024-11-03 16:38:17 +08:00
RootVisualElement?.SetEnabled(!obj);
2024-03-31 23:31:00 +08:00
}
2024-11-03 16:38:17 +08:00
public override async UniTask EntryAsync()
2024-03-31 23:31:00 +08:00
{
2024-11-03 16:38:17 +08:00
await base.EntryAsync();
_modTemplate =await ModService.LoadAsset<VisualTreeAsset>(TemplatePath);
2024-03-31 23:31:00 +08:00
UXUtils.Inject(this);
if (_openModButton is not null)
{
_openModButton.clicked += OpenMod;
}
if (_returnButton is not null)
{
2024-11-03 16:38:17 +08:00
_returnButton.clicked += UXService.Return;
2024-03-31 23:31:00 +08:00
}
_modsContainer.Clear();
foreach (var x in ModService.Mods)
{
OnModInstalled(x);
}
2024-11-03 16:38:17 +08:00
if (_reloadModButton is not null)
_reloadModButton.clicked += async () =>
2024-03-31 23:31:00 +08:00
{
2024-11-03 16:38:17 +08:00
_reloadModButton.SetEnabled(false);
2024-03-31 23:31:00 +08:00
await ModService.Reload();
await UniTask.SwitchToMainThread();
2024-11-03 16:38:17 +08:00
_reloadModButton.SetEnabled(true);
2024-03-31 23:31:00 +08:00
};
}
private async void OnModUnInstalled(IMod obj)
{
await UniTask.SwitchToMainThread();
_modContainers.TryRemove(obj.Name, out var container);
2024-08-13 18:42:51 +08:00
container.RemoveFromHierarchy();
2024-03-31 23:31:00 +08:00
}
private async void OnModInstalled(IMod obj)
{
await UniTask.SwitchToMainThread();
var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
2024-08-13 18:42:51 +08:00
container.RegisterCallback<MouseDownEvent>(x =>
2024-03-31 23:31:00 +08:00
{
if (x.button != 1) return;
ContextMenuBuilder.Create().BuildAction("卸载", () =>
{
ModService.UnLoad(obj);
ModService.UnInstall(obj);
}).Build();
});
}
private async void OnModUnLoaded(IMod obj)
{
await UniTask.SwitchToMainThread();
//var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
if(_modContainers.TryGetValue(obj.Name,out var container))
{
2024-08-13 18:42:51 +08:00
container.Get<Toggle>().SetValueWithoutNotify(false);
2024-03-31 23:31:00 +08:00
}
}
private async void OnModLoaded(IMod obj)
{
await UniTask.SwitchToMainThread();
var container = _modContainers.GetOrAdd(obj.Name,_=> Create(obj));
2024-08-13 18:42:51 +08:00
container.Get<Toggle>().SetValueWithoutNotify(true);
2024-03-31 23:31:00 +08:00
}
private static void OpenMod()
{
BIT4Log.Log("正在打开选择文件对话框");
#if UNITY_EDITOR
new Thread(OpenModInternal).Start();
#else
OpenModInternal();
#endif
return;
void OpenModInternal()
{
2024-07-05 15:07:38 +08:00
#if UNITY_5_3_OR_NEWER && UNITY_WINDOW
2024-03-31 23:31:00 +08:00
BIT4Log.Log<UXModService>("已进入文件对话框线程");
new FileBrowser().OpenFileBrowser(new BrowserProperties()
{
//filterIndex = 0,
//filter = "C Sharp files (*.cs)|*.cs |Dll files (*.dll)|*.dll",
}, Filepath);
2024-06-14 14:11:28 +08:00
#else
throw new NotSupportedException($"{Application.platform} 不支持打开文件对话框");
#endif
2024-03-31 23:31:00 +08:00
return;
async void Filepath(string path)
{
BIT4Log.Log<UXModService>("已选择文件:"+path);
await BITApp.SwitchToMainThread();
try
{
var file = new FileInfo(path);
switch (file.Extension)
{
case ".cs":
2024-06-14 14:12:02 +08:00
#if UNITY_5_3_OR_NEWER
2024-03-31 23:31:00 +08:00
var code = await File.ReadAllTextAsync(path);
var assembly = BITSharp.Compile(code);
await ModService.Load(assembly,Path.GetDirectoryName(path));
2024-06-14 14:11:28 +08:00
#else
throw new NotSupportedException($"{Application.platform} 不支持编译C#代码");
#endif
2024-03-31 23:31:00 +08:00
break;
case ".dll":
var bytes = await File.ReadAllBytesAsync(path);
await ModService.Load(Assembly.Load(bytes),Path.GetDirectoryName(path));
break;
case ".json" when file.Name is ModPackage.DefaultFileName:
await ModService.LoadFromPackage(path);
break;
default:
Alert.Print(new AlertMessage()
{
title = "加载失败",
message = "不支持的文件类型"
});
break;
}
}
catch (Exception e)
{
Alert.Print(new AlertMessage()
{
title = "加载失败",
message = e.Message
});
BIT4Log.LogException(e);
}
}
}
}
2024-08-13 18:42:51 +08:00
private VisualElement Create(IMod mod)
2024-03-31 23:31:00 +08:00
{
2024-11-03 16:38:17 +08:00
var container =_modsContainer.Create(_modTemplate);
2024-08-13 18:42:51 +08:00
container.Get<Toggle>().RegisterValueChangedCallback(evt =>
2024-03-31 23:31:00 +08:00
{
if (evt.newValue)
{
ModService.Load(mod);
}
else
{
ModService.UnLoad(mod);
}
});
2024-08-13 18:42:51 +08:00
container.tooltip = mod.Name+"\n"+mod.Description;
2024-03-31 23:31:00 +08:00
2024-08-13 18:42:51 +08:00
container.Get<Button>().clicked += () =>
2024-03-31 23:31:00 +08:00
{
_modNameLabel.text = mod.Name;
_modDescriptionLabel.text = mod.Description;
};
return container;
}
2024-11-03 16:38:17 +08:00
public void Dispose()
{
ModService.OnModInstalled-=OnModInstalled;
ModService.OnModUnInstalled-=OnModUnInstalled;
ModService.OnModLoaded-=OnModLoaded;
ModService.OnModUnLoaded-=OnModUnLoaded;
ModService.OnLocked-=OnLocked;
}
2024-03-31 23:31:00 +08:00
}
}