HostExtensionServices.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import * as EditorEvents from "../editor/EditorEvents";
  23. import * as EditorUI from "../ui/EditorUI";
  24. /**
  25. * Generic registry for storing Editor Extension Services
  26. */
  27. class ServiceRegistry<T extends Editor.Extensions.EditorService> implements Editor.Extensions.ServiceRegistry<T> {
  28. registeredServices: T[] = [];
  29. /**
  30. * Adds a service to the registered services list for this type of service
  31. * @param {T} service the service to register
  32. */
  33. register(service: T) {
  34. this.registeredServices.push(service);
  35. }
  36. }
  37. interface ServiceEventSubscriber {
  38. /**
  39. * Allow this service registry to subscribe to events that it is interested in
  40. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  41. */
  42. subscribeToEvents(topLevelWindow: Atomic.UIWidget);
  43. }
  44. /**
  45. * Registry for service extensions that are concerned about project events
  46. */
  47. export class ProjectServiceRegistry extends ServiceRegistry<Editor.HostExtensions.ProjectService> implements ServiceEventSubscriber {
  48. constructor() {
  49. super();
  50. }
  51. /**
  52. * Allow this service registry to subscribe to events that it is interested in
  53. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  54. */
  55. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  56. eventDispatcher.subscribeToEvent(EditorEvents.LoadProjectNotification, (ev) => this.projectLoaded(ev));
  57. eventDispatcher.subscribeToEvent(EditorEvents.CloseProject, (ev) => this.projectUnloaded(ev));
  58. eventDispatcher.subscribeToEvent(EditorEvents.PlayerStartRequest, () => this.playerStarted());
  59. }
  60. /**
  61. * Called when the project is unloaded
  62. * @param {[type]} data Event info from the project unloaded event
  63. */
  64. projectUnloaded(data) {
  65. this.registeredServices.forEach((service) => {
  66. // Notify services that the project has been unloaded
  67. try {
  68. if (service.projectUnloaded) {
  69. service.projectUnloaded();
  70. }
  71. } catch (e) {
  72. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  73. }
  74. });
  75. }
  76. /**
  77. * Called when the project is loaded
  78. * @param {[type]} data Event info from the project unloaded event
  79. */
  80. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  81. this.registeredServices.forEach((service) => {
  82. try {
  83. // Notify services that the project has just been loaded
  84. if (service.projectLoaded) {
  85. service.projectLoaded(ev);
  86. }
  87. } catch (e) {
  88. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  89. }
  90. });
  91. }
  92. playerStarted() {
  93. this.registeredServices.forEach((service) => {
  94. try {
  95. // Notify services that the project has just been loaded
  96. if (service.playerStarted) {
  97. service.playerStarted();
  98. }
  99. } catch (e) {
  100. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  101. }
  102. });
  103. }
  104. }
  105. /**
  106. * Registry for service extensions that are concerned about Resources
  107. */
  108. export class ResourceServiceRegistry extends ServiceRegistry<Editor.HostExtensions.ResourceService> {
  109. constructor() {
  110. super();
  111. }
  112. /**
  113. * Allow this service registry to subscribe to events that it is interested in
  114. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  115. */
  116. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  117. eventDispatcher.subscribeToEvent(EditorEvents.SaveResourceNotification, (ev) => this.saveResource(ev));
  118. eventDispatcher.subscribeToEvent(EditorEvents.DeleteResourceNotification, (ev) => this.deleteResource(ev));
  119. eventDispatcher.subscribeToEvent(EditorEvents.RenameResourceNotification, (ev) => this.renameResource(ev));
  120. }
  121. /**
  122. * Called after a resource has been saved
  123. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  124. */
  125. saveResource(ev: Editor.EditorEvents.SaveResourceEvent) {
  126. // run through and find any services that can handle this.
  127. this.registeredServices.forEach((service) => {
  128. try {
  129. // Verify that the service contains the appropriate methods and that it can save
  130. if (service.save) {
  131. service.save(ev);
  132. }
  133. } catch (e) {
  134. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  135. }
  136. });
  137. }
  138. /**
  139. * Called when a resource has been deleted
  140. */
  141. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  142. this.registeredServices.forEach((service) => {
  143. try {
  144. // Verify that the service contains the appropriate methods and that it can delete
  145. if (service.delete) {
  146. service.delete(ev);
  147. }
  148. } catch (e) {
  149. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n ${e}\n ${e.stack}`);
  150. }
  151. });
  152. }
  153. /**
  154. * Called when a resource has been renamed
  155. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  156. */
  157. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  158. this.registeredServices.forEach((service) => {
  159. try {
  160. // Verify that the service contains the appropriate methods and that it can handle the rename
  161. if (service.rename) {
  162. service.rename(ev);
  163. }
  164. } catch (e) {
  165. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  166. }
  167. });
  168. }
  169. }