ResourceCache.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. namespace AtomicEngine
  5. {
  6. public partial class ResourceCache : AObject
  7. {
  8. public T GetResource<T>(string path) where T : Resource
  9. {
  10. return (T) GetResource(typeof(T).Name, path);
  11. }
  12. public T Get<T>(string path) where T : Resource
  13. {
  14. return (T)GetResource(typeof(T).Name, path);
  15. }
  16. public System.IO.Stream GetFileStream(string name, bool sendEventOnFailure = true)
  17. {
  18. File file = GetFile(name, sendEventOnFailure);
  19. if (file != null &&
  20. file.IsOpen())
  21. {
  22. return file.ToStream();
  23. }
  24. return null;
  25. }
  26. /// <summary>
  27. /// Gets all resource directories and places it within an array.
  28. /// </summary>
  29. public string[] GetResourceDirs()
  30. {
  31. ResourceCache cache = GetSubsystem<ResourceCache>();
  32. string[] resourceDirs = new string[cache.GetNumResourceDirs()];
  33. for (uint i = 0; i < cache.GetNumResourceDirs(); i++)
  34. {
  35. resourceDirs[i] = cache.GetResourceDir(i);
  36. }
  37. return resourceDirs;
  38. }
  39. /// <summary>
  40. /// Release all resources. When called with the force flag false, releases all currently unused resources.
  41. /// </summary>
  42. public void ReleaseAllResources(bool force = false)
  43. {
  44. // We need to GC before calling native ResourceCache::ReleaseAllResources, to ensure all managed resource references are down
  45. // otherwise, the cache will hold onto the resource
  46. NativeCore.RunGC();
  47. csi_Atomic_ResourceCache_ReleaseAllResources(nativeInstance, force);
  48. }
  49. [DllImport(Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
  50. private static extern void csi_Atomic_ResourceCache_ReleaseAllResources(IntPtr self, bool force);
  51. }
  52. }