BaseUnityPlugin.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. Info = new PluginInfo
  36. {
  37. Metadata = metadata,
  38. Instance = this,
  39. Dependencies = MetadataHelper.GetDependencies(GetType()),
  40. Processes = MetadataHelper.GetAttributes<BepInProcess>(GetType()),
  41. Location = GetType().Assembly.Location
  42. };
  43. Logger = Logging.Logger.CreateLogSource(metadata.Name);
  44. Config = new ConfigFile(Utility.CombinePaths(Paths.ConfigPath, metadata.GUID + ".cfg"), false, metadata);
  45. }
  46. }
  47. }