BaseUnityPlugin.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using BepInEx.Bootstrap;
  3. using BepInEx.Configuration;
  4. using BepInEx.Logging;
  5. using UnityEngine;
  6. namespace BepInEx
  7. {
  8. /// <summary>
  9. /// The base plugin type that is used by the BepInEx plugin loader.
  10. /// </summary>
  11. public abstract class BaseUnityPlugin : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// Information about this plugin as it was loaded.
  15. /// </summary>
  16. public PluginInfo Info { get; }
  17. /// <summary>
  18. /// Logger instance tied to this plugin.
  19. /// </summary>
  20. protected ManualLogSource Logger { get; }
  21. /// <summary>
  22. /// Default config file tied to this plugin. The config file will not be created until
  23. /// any settings are added and changed, or <see cref="ConfigFile.Save"/> is called.
  24. /// </summary>
  25. public ConfigFile Config { get; }
  26. /// <summary>
  27. /// Create a new instance of a plugin and all of its tied in objects.
  28. /// </summary>
  29. /// <exception cref="InvalidOperationException">BepInPlugin attribute is missing.</exception>
  30. protected BaseUnityPlugin()
  31. {
  32. var metadata = MetadataHelper.GetMetadata(this);
  33. if(metadata == null)
  34. throw new InvalidOperationException("Can't create an instance of " + GetType().FullName + " because it inherits from BaseUnityPlugin and the BepInPlugin attribute is missing.");
  35. if (Chainloader.PluginInfos.TryGetValue(metadata.GUID, out var info))
  36. Info = info;
  37. else
  38. {
  39. Info = new PluginInfo
  40. {
  41. Metadata = metadata,
  42. Instance = this,
  43. Dependencies = MetadataHelper.GetDependencies(GetType()),
  44. Processes = MetadataHelper.GetAttributes<BepInProcess>(GetType()),
  45. Location = GetType().Assembly.Location
  46. };
  47. }
  48. Logger = Logging.Logger.CreateLogSource(metadata.Name);
  49. Config = new ConfigFile(Utility.CombinePaths(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata);
  50. }
  51. }
  52. }