PluginInfo.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 object Instance { get; internal set; }
  39. public string TypeName { get; internal set; }
  40. internal Version TargettedBepInExVersion { get; set; }
  41. void ICacheable.Save(BinaryWriter bw)
  42. {
  43. bw.Write(TypeName);
  44. bw.Write(Location);
  45. bw.Write(Metadata.GUID);
  46. bw.Write(Metadata.Name);
  47. bw.Write(Metadata.Version.ToString());
  48. var processList = Processes.ToList();
  49. bw.Write(processList.Count);
  50. foreach (var bepInProcess in processList)
  51. bw.Write(bepInProcess.ProcessName);
  52. var depList = Dependencies.ToList();
  53. bw.Write(depList.Count);
  54. foreach (var bepInDependency in depList)
  55. ((ICacheable)bepInDependency).Save(bw);
  56. var incList = Incompatibilities.ToList();
  57. bw.Write(incList.Count);
  58. foreach (var bepInIncompatibility in incList)
  59. ((ICacheable)bepInIncompatibility).Save(bw);
  60. bw.Write(TargettedBepInExVersion.ToString(4));
  61. }
  62. void ICacheable.Load(BinaryReader br)
  63. {
  64. TypeName = br.ReadString();
  65. Location = br.ReadString();
  66. Metadata = new BepInPlugin(br.ReadString(), br.ReadString(), br.ReadString());
  67. var processListCount = br.ReadInt32();
  68. var processList = new List<BepInProcess>(processListCount);
  69. for (int i = 0; i < processListCount; i++)
  70. processList.Add(new BepInProcess(br.ReadString()));
  71. Processes = processList;
  72. var depCount = br.ReadInt32();
  73. var depList = new List<BepInDependency>(depCount);
  74. for (int i = 0; i < depCount; i++)
  75. {
  76. var dep = new BepInDependency("");
  77. ((ICacheable)dep).Load(br);
  78. depList.Add(dep);
  79. }
  80. Dependencies = depList;
  81. var incCount = br.ReadInt32();
  82. var incList = new List<BepInIncompatibility>(incCount);
  83. for (int i = 0; i < incCount; i++)
  84. {
  85. var inc = new BepInIncompatibility("");
  86. ((ICacheable)inc).Load(br);
  87. incList.Add(inc);
  88. }
  89. Incompatibilities = incList;
  90. TargettedBepInExVersion = new Version(br.ReadString());
  91. }
  92. /// <inheritdoc />
  93. public override string ToString() => $"{Metadata?.Name} {Metadata?.Version}";
  94. }
  95. }