Browse Source

* compile the typescript files prior to the player launching
* added the ability for the extensions to publish and consume custom events via sendEvent and subscribeToEvent on the ServiceLocator instance

Shaddock Heath 10 năm trước cách đây
mục cha
commit
9bf06efa8c

+ 5 - 1
Script/AtomicEditor/editor/Editor.ts

@@ -149,7 +149,11 @@ class Editor extends Atomic.ScriptObject {
             return false;
 
         }
-        return system.loadProject(event.path);
+        const loaded = system.loadProject(event.path);
+        if (loaded) {
+            this.sendEvent(EditorEvents.LoadProjectNotification, event);
+        }
+        return loaded;
     }
 
     closeAllResourceEditors() {

+ 43 - 3
Script/AtomicEditor/extensionServices/EditorExtensionServices.ts

@@ -42,6 +42,7 @@ export interface ResourceService extends EditorService {
 export interface ProjectService extends EditorService {
     projectUnloaded?();
     projectLoaded?(ev: EditorEvents.LoadProjectEvent);
+    playerStarted?();
 }
 
 interface ServiceEventSubscriber {
@@ -81,8 +82,9 @@ class ProjectServiceRegistry extends ServiceRegistry<ProjectService> implements
      * @param  {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
      */
     subscribeToEvents(topLevelWindow: Atomic.UIWidget) {
-        topLevelWindow.subscribeToEvent(EditorEvents.LoadProject, (ev) => this.projectLoaded(ev));
+        topLevelWindow.subscribeToEvent(EditorEvents.LoadProjectNotification, (ev) => this.projectLoaded(ev));
         topLevelWindow.subscribeToEvent(EditorEvents.CloseProject, (ev) => this.projectUnloaded(ev));
+        topLevelWindow.subscribeToEvent(EditorEvents.PlayerStartRequest, () => this.playerStarted());
     }
 
     /**
@@ -118,6 +120,19 @@ class ProjectServiceRegistry extends ServiceRegistry<ProjectService> implements
             }
         });
     }
+
+    playerStarted() {
+        this.registeredServices.forEach((service) => {
+            try {
+                // Notify services that the project has just been loaded
+                if (service.playerStarted) {
+                    service.playerStarted();
+                }
+            } catch (e) {
+                EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n ${e}\n ${e.stack}`);
+            }
+        });
+    }
 }
 
 /**
@@ -203,6 +218,8 @@ export class ServiceLocatorType {
         this.projectServices = new ProjectServiceRegistry();
     }
 
+    private eventDispatcher: Atomic.UIWidget = null;
+
     resourceServices: ResourceServiceRegistry;
     projectServices: ProjectServiceRegistry;
 
@@ -215,7 +232,30 @@ export class ServiceLocatorType {
      * @param  {Atomic.UIWidget} frame
      */
     subscribeToEvents(frame: Atomic.UIWidget) {
-        this.resourceServices.subscribeToEvents(frame);
-        this.projectServices.subscribeToEvents(frame);
+        this.eventDispatcher = frame;
+        this.resourceServices.subscribeToEvents(this.eventDispatcher);
+        this.projectServices.subscribeToEvents(this.eventDispatcher);
+    }
+
+    /**
+     * Send a custom event.  This can be used by services to publish custom events
+     * @param  {string} eventType
+     * @param  {any} data
+     */
+    sendEvent(eventType: string, data: any) {
+        if (this.eventDispatcher) {
+            this.eventDispatcher.sendEvent(eventType, data);
+        }
+    }
+
+    /**
+     * Subscribe to an event and provide a callback.  This can be used by services to subscribe to custom events
+     * @param  {string} eventType
+     * @param  {any} callback
+     */
+    subscribeToEvent(eventType: string, callback: (data: any) => void) {
+        if (this.eventDispatcher) {
+            this.eventDispatcher.subscribeToEvent(eventType, callback);
+        }
     }
 }

+ 19 - 1
Script/AtomicEditor/extensionServices/resourceServices/TypescriptLanguageService.ts

@@ -110,7 +110,7 @@ export default class TypescriptLanguageService implements ExtensionServices.Reso
         this.refreshProjectFiles(files);
         let errors: ts.Diagnostic[] = [];
 
-        if (!this.languageService) {
+        if (!this.languageService || files == null) {
             // This is the first time in.  Need to create a language service
 
             // Create the language service host to allow the LS to communicate with the host
@@ -402,7 +402,25 @@ export default class TypescriptLanguageService implements ExtensionServices.Reso
         console.log(`${this.name}: received a project loaded event for project at ${ev.path}`);
         this.resetLanguageService();
 
+        this.refreshProjectFiles();
+
         //TODO: do we want to run through and compile at this point?
     }
 
+    /**
+     * Called when the player is launged
+     */
+    playerStarted() {
+        console.log(`${this.name}: received a player started event for project`);
+        if (this.fullCompile) {
+            this.compile(null, {
+                noEmitOnError: true,
+                noImplicitAny: false,
+                target: ts.ScriptTarget.ES5,
+                module: ts.ModuleKind.CommonJS,
+                noLib: true
+            });
+        }
+    }
+
 }