BaseUnityPlugin.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using BepInEx.Bootstrap;
  2. using BepInEx.Configuration;
  3. using BepInEx.Contract;
  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. protected ConfigFile Config { get; }
  26. /// <summary>
  27. /// Create a new instance of a plugin and all of its tied in objects.
  28. /// </summary>
  29. protected BaseUnityPlugin()
  30. {
  31. var metadata = MetadataHelper.GetMetadata(this);
  32. if (Chainloader.PluginInfos.TryGetValue(metadata.GUID, out var info))
  33. Info = info;
  34. else
  35. {
  36. Info = new PluginInfo
  37. {
  38. Metadata = metadata,
  39. Instance = this,
  40. Dependencies = MetadataHelper.GetDependencies(GetType()),
  41. Processes = MetadataHelper.GetAttributes<BepInProcess>(GetType()),
  42. Location = GetType().Assembly.Location
  43. };
  44. }
  45. Logger = Logging.Logger.CreateLogSource(metadata.Name);
  46. Config = new ConfigFile(Utility.CombinePaths(Paths.ConfigPath, metadata.GUID + ".cfg"), false, this);
  47. }
  48. }
  49. }