BITFALL/Assets/Artists/Scripts/Industry/UnityWorldComputer.cs

86 lines
2.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BITFALL.Hotkey;
using BITKit;
using BITKit.IData;
using UnityEngine;
namespace BITFALL.Industry
{
public class UnityWorldComputer:UnityNetDevice,IDescription,IHotkeyCollection
{
public class ShowHDDData
{
public ShowHDDData(IDataStorage dataStorage, IBasicItem item = null)
{
DataStorage = dataStorage;
var infoBuilder = new StringBuilder();
if (item is not null)
infoBuilder.AppendLine($"ID:{item.Id}");
infoBuilder.AppendLine($"数据长度:{dataStorage.Data.Length}");
infoBuilder.AppendLine($"只读:{dataStorage.ReadOnly}");
infoBuilder.AppendLine($"加密:{dataStorage.Encrypted}");
Info = infoBuilder.ToString();
}
private readonly IDataStorage DataStorage;
[Export,ReadOnly(HideLabel = true)] private string Info;
[Export(name:"数据内容"),ReadOnly]
private string Data;
[Export(name:"解密数据")]
private void Execute()
{
Data = DataStorage.Data;
}
[Export(name:"复制数据")]
private void Copy()
{
GUIUtility.systemCopyBuffer = Data;
}
}
public new INetConnectionType Connection { get; set; }
public string Name=>Connection is not null?$"Connected,Bandwidth:{Connection.Bandwidth}":"Disconnected";
protected override void OnTick(float obj)
{
base.OnTick(obj);
var nets =new InGameNetServiceSingleton().SearchDevices(transform.position, 8,0);
if (nets is null or {Length:0})
{
Connection = null;
return;
}
var max = nets.OrderBy(x => x.Connection.Bandwidth).Last();
Connection = max.Connection;
}
public IEnumerable<IHotkeyProvider> Hotkeys => _hotkeyProviders.ToArray();
private readonly List<IHotkeyProvider> _hotkeyProviders=new();
public void Register(IHotkeyProvider hotkey)=>_hotkeyProviders.Add(hotkey);
public void UnRegister(IHotkeyProvider hotkey)=>_hotkeyProviders.Remove(hotkey);
protected override void Start()
{
base.Start();
if (worldItemObject is { Item: not null } is false) return;
if (worldItemObject.Item.TryGetProperty<GameComputerModified>(out var computerModified) is false)
return;
if (computerModified.Modified.TryGetValue(new ComputerExternalHDD(), out var disk) is false)
return;
if (disk is not IBasicItem diskItem ||
diskItem.TryGetProperty<IDataStorage>(out var dataStorage) is false)
return;
Register(new HotkeyProvider()
{
Data =new BindableData(new ShowHDDData(dataStorage,diskItem)),
Description = dataStorage.Encrypted ? "已加密" : "未加密",
HoldDuration = 5,
Enabled = true,
Name = $"获取{worldItemObject.Item.Name}中的数据",
});
}
}
}