1
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.SqlServer.Server;
|
||||
|
||||
namespace BITKit
|
||||
@@ -67,4 +71,83 @@ namespace BITKit
|
||||
/// </summary>
|
||||
event Action<bool> OnRelease;
|
||||
}
|
||||
|
||||
public class RuntimeItemContainer : IRuntimeItemContainer
|
||||
{
|
||||
private readonly ConcurrentDictionary<int, IRuntimeItem> _items = new();
|
||||
public void Read(BinaryReader r)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Write(BinaryWriter w)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public int Id { get; set; }
|
||||
public IRuntimeItem[] GetItems()=>_items.Values.ToArray();
|
||||
|
||||
public bool Add(IRuntimeItem item)
|
||||
{
|
||||
foreach (var func in AddFactory.CastAsFunc())
|
||||
{
|
||||
if (func.Invoke(item) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_items.Set(item.Id,item);
|
||||
|
||||
OnAdd?.Invoke(item);
|
||||
//BIT4Log.Log<RuntimeItemContainer>($"添加了了:{item.Id}");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(int id)
|
||||
{
|
||||
if (_items.TryGetValue(id, out var item) is false) return false;
|
||||
|
||||
foreach (var func in RemoveFactory.CastAsFunc())
|
||||
{
|
||||
if (func.Invoke(item) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_items.TryRemove(item.Id);
|
||||
OnRemove?.Invoke(item);
|
||||
//BIT4Log.Log<RuntimeItemContainer>($"移除了:{id}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Drop(int id)
|
||||
{
|
||||
|
||||
if (_items.TryGetValue(id, out var item) is false) return false;
|
||||
foreach (var func in DropFactory.CastAsFunc())
|
||||
{
|
||||
if (func.Invoke(item) is false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_items.TryRemove(item.Id);
|
||||
OnDrop?.Invoke(item);
|
||||
//BIT4Log.Log<RuntimeItemContainer>($"丢下了:{id}");
|
||||
return true;
|
||||
}
|
||||
|
||||
public event Func<IRuntimeItem, bool> AddFactory;
|
||||
public event Func<IRuntimeItem, bool> RemoveFactory;
|
||||
public event Func<IRuntimeItem, bool> DropFactory;
|
||||
public event Action<IRuntimeItem> OnAdd;
|
||||
public event Action<IRuntimeItem> OnRemove;
|
||||
public event Action<IRuntimeItem> OnSet;
|
||||
public event Action<IRuntimeItem> OnDrop;
|
||||
public event Action<IRuntimeItemContainer> OnRebuild;
|
||||
public event Action<bool> OnRelease;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user