Manifest.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace VEngine
  6. {
  7. [Serializable]
  8. public class ManifestBundle
  9. {
  10. public static ManifestBundle Empty = new ManifestBundle();
  11. public int id;
  12. public string name;
  13. public List<string> assets;
  14. public long size;
  15. public uint crc;
  16. public string nameWithAppendHash;
  17. public int[] dependencies;
  18. }
  19. public class Manifest : ScriptableObject
  20. {
  21. public static readonly ManifestBundle[] EmptyBundles = new ManifestBundle[0];
  22. public int version;
  23. public string appVersion;
  24. public List<ManifestBundle> bundles = new List<ManifestBundle>();
  25. private Dictionary<string, ManifestBundle> nameWithBundles = new Dictionary<string, ManifestBundle>();
  26. public Action<string> onReadAsset;
  27. public Dictionary<string, ManifestBundle> GetBundles()
  28. {
  29. var dictionary = new Dictionary<string, ManifestBundle>();
  30. foreach (var bundle in bundles) dictionary[bundle.name] = bundle;
  31. return dictionary;
  32. }
  33. public bool Contains(string assetPath)
  34. {
  35. return nameWithBundles.ContainsKey(assetPath);
  36. }
  37. public ManifestBundle GetBundle(string assetPath)
  38. {
  39. ManifestBundle manifestBundle;
  40. return nameWithBundles.TryGetValue(assetPath, out manifestBundle) ? manifestBundle : null;
  41. }
  42. public ManifestBundle[] GetDependencies(ManifestBundle bundle)
  43. {
  44. if (bundle == null) return EmptyBundles;
  45. return Array.ConvertAll(bundle.dependencies, input => bundles[input]);
  46. }
  47. public void Override(Manifest manifest)
  48. {
  49. version = manifest.version;
  50. bundles = manifest.bundles;
  51. nameWithBundles = manifest.nameWithBundles;
  52. }
  53. public static string GetVersionFile(string file)
  54. {
  55. return $"{file}.version";
  56. }
  57. public void Load(string path)
  58. {
  59. var json = File.ReadAllText(path);
  60. JsonUtility.FromJsonOverwrite(json, this);
  61. nameWithBundles.Clear();
  62. if (onReadAsset != null)
  63. foreach (var bundle in bundles)
  64. {
  65. nameWithBundles[bundle.nameWithAppendHash] = bundle;
  66. foreach (var asset in bundle.assets)
  67. {
  68. nameWithBundles[asset] = bundle;
  69. onReadAsset.Invoke(asset);
  70. }
  71. }
  72. else
  73. foreach (var bundle in bundles)
  74. {
  75. nameWithBundles[bundle.nameWithAppendHash] = bundle;
  76. foreach (var asset in bundle.assets) nameWithBundles[asset] = bundle;
  77. }
  78. }
  79. public void AddAsset(string assetPath)
  80. {
  81. nameWithBundles[assetPath] = ManifestBundle.Empty;
  82. if (onReadAsset != null) onReadAsset(assetPath);
  83. }
  84. }
  85. }