using System; using System.Collections.Generic; using Svelto.DataStructures; namespace Svelto.ECS { class EngineNodeDB : IEngineNodeDB { internal EngineNodeDB( Dictionary> nodesDB, Dictionary> nodesDBdic, Dictionary> nodesDBgroups) { _nodesDB = new DataStructures.WeakReference>>(nodesDB); _nodesDBdic = new DataStructures.WeakReference>>(nodesDBdic); _nodesDBgroups = new DataStructures.WeakReference>>(nodesDBgroups); } public FasterReadOnlyListCast QueryNodes() where T:INode { var type = typeof(T); if (_nodesDB.IsValid == false || _nodesDB.Target.ContainsKey(type) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyListCast(_nodesDB.Target[type]); } /* public FasterReadOnlyList QueryStructNodes() where T:struct { var type = typeof(T); if (_nodesDBStructs.ContainsKey(type) == false) return RetrieveEmptyStructNodeList(); return new FasterReadOnlyList(((StructNodeList)(_nodesDBStructs[type])).list); }*/ public ReadOnlyDictionary QueryIndexableNodes() where T:INode { var type = typeof(T); if (_nodesDB.IsValid == false || _nodesDBdic.Target.ContainsKey(type) == false) return _defaultEmptyNodeDict; return new ReadOnlyDictionary(_nodesDBdic.Target[type]); } public T QueryNodeFromGroup(int groupID) where T : INode { return QueryNode(groupID); } public bool QueryNodeFromGroup(int groupID, out T node) where T : INode { return QueryNode(groupID, out node); } public FasterReadOnlyListCast QueryNodesFromGroups() where T : INode { var type = typeof(T); if (_nodesDBgroups.IsValid == false || _nodesDBgroups.Target.ContainsKey(type) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyListCast(_nodesDBgroups.Target[type]); } public bool QueryNode(int ID, out T node) where T:INode { var type = typeof(T); INode internalNode; if (_nodesDBdic.IsValid && _nodesDBdic.Target.ContainsKey(type) && _nodesDBdic.Target[type].TryGetValue(ID, out internalNode)) { node = (T)internalNode; return true; } node = default(T); return false; } public T QueryNode(int ID) where T:INode { var type = typeof(T); INode internalNode; if (_nodesDBdic.IsValid && _nodesDBdic.Target.ContainsKey(type) && _nodesDBdic.Target[type].TryGetValue(ID, out internalNode)) return (T)internalNode; throw new Exception("Node Not Found"); } static FasterReadOnlyListCast RetrieveEmptyNodeList() where T : INode { return new FasterReadOnlyListCast(FasterList.DefaultList); } static FasterReadOnlyList RetrieveEmptyStructNodeList() where T : struct { return new FasterReadOnlyList(FasterList.DefaultList); } Svelto.DataStructures.WeakReference>> _nodesDB; Svelto.DataStructures.WeakReference>> _nodesDBdic; Svelto.DataStructures.WeakReference>> _nodesDBgroups; // Dictionary _nodesDBStructs; //Dictionary> _nodesDB; //Dictionary> _nodesDBdic; // Dictionary> _nodesDBgroups; ReadOnlyDictionary _defaultEmptyNodeDict = new ReadOnlyDictionary(new Dictionary()); class StructNodeList { } class StructNodeList : StructNodeList where T : struct { public FasterList list = new FasterList(); } } }