GameResourceBase.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameResourceBase.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using RobotGameData.GameInterface;
  15. #endregion
  16. namespace RobotGameData.Resource
  17. {
  18. /// <summary>
  19. /// the base class from which all resource structures must inherit.
  20. /// </summary>
  21. public class GameResourceBase : IDisposable, IIdentity
  22. {
  23. #region Fields
  24. int id = -1;
  25. string keyName = String.Empty;
  26. string assetName = String.Empty;
  27. bool isDisposed;
  28. protected object resource = null;
  29. #endregion
  30. #region Properties
  31. public int Id
  32. {
  33. get
  34. {
  35. if (id == 0)
  36. {
  37. id = GetHashCode();
  38. }
  39. return id;
  40. }
  41. }
  42. public string Key
  43. {
  44. get { return keyName; }
  45. }
  46. public string AssetName
  47. {
  48. get { return assetName; }
  49. }
  50. public object Resource
  51. {
  52. get { return resource; }
  53. }
  54. public bool IsDisposed
  55. {
  56. get { return isDisposed; }
  57. }
  58. #endregion
  59. /// <summary>
  60. /// Constructor.
  61. /// </summary>
  62. /// <param name="key">key name</param>
  63. /// <param name="assetName">asset name</param>
  64. public GameResourceBase(string key, string assetName)
  65. {
  66. this.keyName = key;
  67. this.assetName = assetName;
  68. }
  69. public void Dispose()
  70. {
  71. Dispose(true);
  72. GC.SuppressFinalize(this);
  73. }
  74. protected virtual void Dispose(bool disposing)
  75. {
  76. if (!isDisposed)
  77. {
  78. if (disposing)
  79. {
  80. //if we're manually disposing,
  81. //then managed content should be unloaded
  82. resource = null;
  83. }
  84. isDisposed = true;
  85. }
  86. }
  87. }
  88. }