PluginInfo.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using BepInEx.Bootstrap;
  6. namespace BepInEx
  7. {
  8. /// <summary>
  9. /// Data class that represents information about a loadable BepInEx plugin.
  10. /// Contains all metadata and additional info required for plugin loading by <see cref="Chainloader"/>.
  11. /// </summary>
  12. public class PluginInfo : ICacheable
  13. {
  14. /// <summary>
  15. /// General metadata about a plugin.
  16. /// </summary>
  17. public BepInPlugin Metadata { get; internal set; }
  18. /// <summary>
  19. /// Collection of <see cref="BepInProcess"/> attributes that describe what processes the plugin can run on.
  20. /// </summary>
  21. public IEnumerable<BepInProcess> Processes { get; internal set; }
  22. /// <summary>
  23. /// Collection of <see cref="BepInDependency"/> attributes that describe what plugins this plugin depends on.
  24. /// </summary>
  25. public IEnumerable<BepInDependency> Dependencies { get; internal set; }
  26. /// <summary>
  27. /// Collection of <see cref="BepInIncompatibility"/> attributes that describe what plugins this plugin
  28. /// is incompatible with.
  29. /// </summary>
  30. public IEnumerable<BepInIncompatibility> Incompatibilities { get; internal set; }
  31. /// <summary>
  32. /// File path to the plugin DLL
  33. /// </summary>
  34. public string Location { get; internal set; }
  35. /// <summary>
  36. /// Instance of the plugin that represents this info. NULL if no plugin is instantiated from info (yet)
  37. /// </summary>
  38. public BaseUnityPlugin Instance { get; internal set; }
  39. internal string TypeName { get; set; }
  40. internal Version TargettedBepInExVersion { get; set; }
  41. void ICacheable.Save(BinaryWriter bw)
  42. {
  43. bw.Write(TypeName);
  44. bw.Write(Metadata.GUID);
  45. bw.Write(Metadata.Name);
  46. bw.Write(Metadata.Version.ToString());
  47. var processList = Processes.ToList();
  48. bw.Write(processList.Count);
  49. foreach (var bepInProcess in processList)
  50. bw.Write(bepInProcess.ProcessName);
  51. var depList = Dependencies.ToList();
  52. bw.Write(depList.Count);
  53. foreach (var bepInDependency in depList)
  54. ((ICacheable)bepInDependency).Save(bw);
  55. var incList = Incompatibilities.ToList();
  56. bw.Write(incList.Count);
  57. foreach (var bepInIncompatibility in incList)
  58. ((ICacheable)bepInIncompatibility).Save(bw);
  59. bw.Write(TargettedBepInExVersion.ToString(4));
  60. }
  61. void ICacheable.Load(BinaryReader br)
  62. {
  63. TypeName = br.ReadString();
  64. Metadata = new BepInPlugin(br.ReadString(), br.ReadString(), br.ReadString());
  65. var processListCount = br.ReadInt32();
  66. var processList = new List<BepInProcess>(processListCount);
  67. for (int i = 0; i < processListCount; i++)
  68. processList.Add(new BepInProcess(br.ReadString()));
  69. Processes = processList;
  70. var depCount = br.ReadInt32();
  71. var depList = new List<BepInDependency>(depCount);
  72. for (int i = 0; i < depCount; i++)
  73. {
  74. var dep = new BepInDependency("");
  75. ((ICacheable)dep).Load(br);
  76. depList.Add(dep);
  77. }
  78. Dependencies = depList;
  79. var incCount = br.ReadInt32();
  80. var incList = new List<BepInIncompatibility>(incCount);
  81. for (int i = 0; i < incCount; i++)
  82. {
  83. var inc = new BepInIncompatibility("");
  84. ((ICacheable)inc).Load(br);
  85. incList.Add(inc);
  86. }
  87. Incompatibilities = incList;
  88. TargettedBepInExVersion = new Version(br.ReadString());
  89. }
  90. /// <inheritdoc />
  91. public override string ToString() => $"{Metadata?.Name} {Metadata?.Version}";
  92. }
  93. }