AssetInstance.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections.Generic;
  2. namespace XGame.Framework.Asset
  3. {
  4. /// <summary>
  5. /// 加载过的资源对象,只用于内部记录引用
  6. /// </summary>
  7. internal class AssetInstance
  8. {
  9. public object Asset { private set; get; }
  10. public int Reference { private set; get; }
  11. public long Hashcode { private set; get; }
  12. public string Name { private set; get; }
  13. private List<long> parents;
  14. public AssetInstance(object asset, string name)
  15. {
  16. Asset = asset;
  17. Reference = 1;
  18. Hashcode = asset.GetLongHashCode();
  19. Name = name;
  20. }
  21. public void Retain()
  22. {
  23. Reference++;
  24. }
  25. public bool Release()
  26. {
  27. return --Reference <= 0;
  28. }
  29. public bool AddParent(long parentHash)
  30. {
  31. if (parents == null)
  32. {
  33. parents = new List<long>();
  34. }
  35. if (parents.Contains(parentHash))
  36. {
  37. return false;
  38. }
  39. parents.Add(parentHash);
  40. return true;
  41. }
  42. public bool RemoveParent(long parentHash)
  43. {
  44. return parents?.Remove(parentHash) ?? false;
  45. }
  46. public bool IsContainParent(long parentHash)
  47. {
  48. return parents?.Contains(parentHash) ?? false;
  49. }
  50. public override string ToString()
  51. {
  52. return $"LoadedAsset type:{Asset?.GetType()} name:{Name} hash:{Hashcode} ref:{Reference}";
  53. }
  54. public void Clear()
  55. {
  56. Asset = null;
  57. Reference = 0;
  58. Hashcode = 0;
  59. Name = string.Empty;
  60. parents?.Clear();
  61. }
  62. internal AssetInstance Clone()
  63. {
  64. var clone = new AssetInstance(Asset, Name)
  65. {
  66. Reference = Reference,
  67. };
  68. return clone;
  69. }
  70. }
  71. }