BITKit/Src/Core/WorldNode/WorldInfoNodeService.cs

61 lines
1.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
#if UNITY_5_3_OR_NEWER
using UnityEngine;
#endif
namespace BITKit.WorldNode
{
[Serializable]
public struct WorldInfoNode : IWorldNode
{
// ReSharper disable once InconsistentNaming
#if UNITY_5_3_OR_NEWER
[SerializeReference,SubclassSelector]
#endif
private IReference name;
// ReSharper disable once InconsistentNaming
#if UNITY_5_3_OR_NEWER
[SerializeReference,SubclassSelector]
#endif
private IReference description;
#if UNITY_5_3_OR_NEWER
public void Initialize()
{
Name = name?.Value;
Description = description?.Value;
}
#endif
public int Id { get; set; }
public object WorldObject { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public sealed class WorldInfoNodeService : IDisposable
{
public IReadOnlyDictionary<int, WorldInfoNode> WorldInfoNodes => InfoNodes;
private readonly IWorldNodeService _worldNodeService;
private static readonly ConcurrentDictionary<int, WorldInfoNode> InfoNodes = new();
public WorldInfoNodeService(IWorldNodeService worldNodeService)
{
_worldNodeService = worldNodeService;
_worldNodeService.OnNodeRegistered += OnNodeRegistered;
}
private void OnNodeRegistered(IWorldNode obj)
{
if (obj is not WorldInfoNode infoNode) return;
InfoNodes.TryAdd(obj.Id, infoNode);
}
public void Dispose()
{
InfoNodes.Clear();
}
}
}