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.

134 lines
3.8 KiB

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