You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.7 KiB

  1. using IllusionPlugin;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using Logger = IllusionPlugin.Logger;
  8. namespace IllusionInjector {
  9. public class CompositePlugin : IPlugin {
  10. IEnumerable<IPlugin> plugins;
  11. private delegate void CompositeCall(IPlugin plugin);
  12. private Logger debugLogger => PluginManager.debugLogger;
  13. public CompositePlugin(IEnumerable<IPlugin> plugins) {
  14. this.plugins = plugins;
  15. }
  16. public void OnApplicationStart() {
  17. Invoke(plugin => plugin.OnApplicationStart());
  18. }
  19. public void OnApplicationQuit() {
  20. Invoke(plugin => plugin.OnApplicationQuit());
  21. }
  22. public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) {
  23. foreach (var plugin in plugins) {
  24. try {
  25. plugin.OnSceneLoaded(scene, sceneMode);
  26. }
  27. catch (Exception ex) {
  28. debugLogger.Exception($"{plugin.Name}: {ex}");
  29. }
  30. }
  31. }
  32. public void OnSceneUnloaded(Scene scene) {
  33. foreach (var plugin in plugins) {
  34. try {
  35. plugin.OnSceneUnloaded(scene);
  36. }
  37. catch (Exception ex) {
  38. debugLogger.Exception($"{plugin.Name}: {ex}");
  39. }
  40. }
  41. }
  42. public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  43. foreach (var plugin in plugins) {
  44. try {
  45. plugin.OnActiveSceneChanged(prevScene, nextScene);
  46. }
  47. catch (Exception ex) {
  48. debugLogger.Exception($"{plugin.Name}: {ex}");
  49. }
  50. }
  51. }
  52. private void Invoke(CompositeCall callback) {
  53. foreach (var plugin in plugins) {
  54. try {
  55. callback(plugin);
  56. }
  57. catch (Exception ex) {
  58. debugLogger.Exception($"{plugin.Name}: {ex}");
  59. }
  60. }
  61. }
  62. public void OnUpdate() {
  63. Invoke(plugin => plugin.OnUpdate());
  64. }
  65. public void OnFixedUpdate() {
  66. Invoke(plugin => plugin.OnFixedUpdate());
  67. }
  68. [Obsolete("Use OnSceneLoaded instead")]
  69. public void OnLevelWasLoaded(int level)
  70. {
  71. foreach (var plugin in plugins)
  72. {
  73. try
  74. {
  75. plugin.OnLevelWasLoaded(level);
  76. }
  77. catch (Exception ex)
  78. {
  79. Console.WriteLine("{0}: {1}", plugin.Name, ex);
  80. }
  81. }
  82. }
  83. [Obsolete("Use OnSceneLoaded instead")]
  84. public void OnLevelWasInitialized(int level)
  85. {
  86. foreach (var plugin in plugins)
  87. {
  88. try
  89. {
  90. plugin.OnLevelWasInitialized(level);
  91. }
  92. catch (Exception ex)
  93. {
  94. Console.WriteLine("{0}: {1}", plugin.Name, ex);
  95. }
  96. }
  97. }
  98. public string Name {
  99. get { throw new NotImplementedException(); }
  100. }
  101. public string Version {
  102. get { throw new NotImplementedException(); }
  103. }
  104. public void OnLateUpdate() {
  105. Invoke(plugin => {
  106. if (plugin is IEnhancedPlugin)
  107. ((IEnhancedPlugin) plugin).OnLateUpdate();
  108. });
  109. }
  110. }
  111. }