@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 8.02 kB
JavaScript
import*as Common from"../../core/common/common.js";import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as SDK from"../../core/sdk/sdk.js";import*as Workspace from"../workspace/workspace.js";import{ContentProviderBasedProject}from"./ContentProviderBasedProject.js";import{DebuggerWorkspaceBinding}from"./DebuggerWorkspaceBinding.js";import{NetworkProject}from"./NetworkProject.js";import{metadataForURL}from"./ResourceUtils.js";const UIStrings={liveEditFailed:"`LiveEdit` failed: {PH1}",liveEditCompileFailed:"`LiveEdit` compile failed: {PH1}"},str_=i18n.i18n.registerUIStrings("models/bindings/ResourceScriptMapping.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class ResourceScriptMapping{debuggerModel;#e;debuggerWorkspaceBinding;#t;#r;#i;#o;constructor(e,t,r){this.debuggerModel=e,this.#e=t,this.debuggerWorkspaceBinding=r,this.#t=new Map,this.#r=new Map,this.#i=new Map;const i=e.runtimeModel();this.#o=[this.debuggerModel.addEventListener(SDK.DebuggerModel.Events.ParsedScriptSource,(e=>this.addScript(e.data)),this),this.debuggerModel.addEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared,this.globalObjectCleared,this),i.addEventListener(SDK.RuntimeModel.Events.ExecutionContextDestroyed,this.executionContextDestroyed,this),i.target().targetManager().addEventListener(SDK.TargetManager.Events.InspectedURLChanged,this.inspectedURLChanged,this)]}project(e){const t=(e.isContentScript()?"js:extensions:":"js::")+this.debuggerModel.target().id()+":"+e.frameId;let r=this.#r.get(t);if(!r){const i=e.isContentScript()?Workspace.Workspace.projectTypes.ContentScripts:Workspace.Workspace.projectTypes.Network;r=new ContentProviderBasedProject(this.#e,t,i,"",!1),NetworkProject.setTargetForProject(r,this.debuggerModel.target()),this.#r.set(t,r)}return r}uiSourceCodeForScript(e){return this.#i.get(e)??null}rawLocationToUILocation(e){const t=e.script();if(!t)return null;const r=this.#i.get(t);if(!r)return null;const i=this.#t.get(r);if(!i)return null;if(i.hasDivergedFromVM()&&!i.isMergingToVM()||i.isDivergingFromVM())return null;if(i.script!==t)return null;const{lineNumber:o,columnNumber:s=0}=e;return r.uiLocation(o,s)}uiLocationToRawLocations(e,t,r){const i=this.#t.get(e);if(!i)return[];const{script:o}=i;return o?[this.debuggerModel.createRawLocation(o,t,r)]:[]}uiLocationRangeToRawLocationRanges(e,{startLine:t,startColumn:r,endLine:i,endColumn:o}){const s=this.#t.get(e);if(!s)return null;const{script:n}=s;if(!n)return null;return[{start:this.debuggerModel.createRawLocation(n,t,r),end:this.debuggerModel.createRawLocation(n,i,o)}]}inspectedURLChanged(e){for(let t=this.debuggerModel.target();t!==e.data;t=t.parentTarget())if(null===t)return;for(const e of Array.from(this.#i.keys()))this.removeScripts([e]),this.addScript(e)}addScript(e){if(e.isLiveEdit()||e.isBreakpointCondition)return;let t=e.sourceURL;if(!t)return;if(e.hasSourceURL)t=SDK.SourceMapManager.SourceMapManager.resolveRelativeSourceURL(e.debuggerModel.target(),t);else{if(e.isInlineScript())return;if(e.isContentScript()){if(!new Common.ParsedURL.ParsedURL(t).isValid)return}}const r=this.project(e),i=r.uiSourceCodeForURL(t);if(i){const e=this.#t.get(i);e&&e.script&&this.removeScripts([e.script])}const o=e.originalContentProvider(),s=r.createUISourceCode(t,o.contentType());NetworkProject.setInitialFrameAttribution(s,e.frameId);const n=metadataForURL(this.debuggerModel.target(),e.frameId,t),c=new ResourceScriptFile(this,s,e);this.#t.set(s,c),this.#i.set(e,s);const a=e.isWasm()?"application/wasm":"text/javascript";r.addUISourceCodeWithProvider(s,o,n,a),this.debuggerWorkspaceBinding.updateLocations(e)}scriptFile(e){return this.#t.get(e)||null}removeScripts(e){const t=new Platform.MapUtilities.Multimap;for(const r of e){const e=this.#i.get(r);if(!e)continue;const i=this.#t.get(e);i&&i.dispose(),this.#t.delete(e),this.#i.delete(r),t.set(e.project(),e),this.debuggerWorkspaceBinding.updateLocations(r)}for(const e of t.keysArray()){const r=t.get(e);let i=!0;for(const t of e.uiSourceCodes())if(!r.has(t)){i=!1;break}i?(this.#r.delete(e.id()),e.removeProject()):r.forEach((t=>e.removeUISourceCode(t.url())))}}executionContextDestroyed(e){const t=e.data;this.removeScripts(this.debuggerModel.scriptsForExecutionContext(t))}globalObjectCleared(){const e=Array.from(this.#i.keys());this.removeScripts(e)}resetForTest(){this.globalObjectCleared()}dispose(){Common.EventTarget.removeEventListeners(this.#o),this.globalObjectCleared()}}export class ResourceScriptFile extends Common.ObjectWrapper.ObjectWrapper{#s;#n;#c;#a;#d;#u;#p;#g=new Common.Mutex.Mutex;constructor(e,t,r){super(),this.#s=e,this.#n=t,this.#n.contentType().isScript()&&(this.#c=r),this.#n.addEventListener(Workspace.UISourceCode.Events.WorkingCopyChanged,this.workingCopyChanged,this),this.#n.addEventListener(Workspace.UISourceCode.Events.WorkingCopyCommitted,this.workingCopyCommitted,this)}isDiverged(){if(this.#n.isDirty())return!0;if(!this.#c)return!1;if(void 0===this.#a||null===this.#a)return!1;const e=this.#n.workingCopy();if(!e)return!1;if(!e.startsWith(this.#a.trimEnd()))return!0;const t=this.#n.workingCopy().substr(this.#a.length);return Boolean(t.length)&&!t.match(SDK.Script.sourceURLRegex)}workingCopyChanged(){this.update()}workingCopyCommitted(){if(this.#n.project().canSetFileContent())return;if(!this.#c)return;const e=this.#n.workingCopy();this.#c.editSource(e).then((({status:t,exceptionDetails:r})=>{this.scriptSourceWasSet(e,t,r)}))}async scriptSourceWasSet(e,t,r){if("Ok"===t&&(this.#a=e),await this.update(),"Ok"===t)return;if(!r)return void Common.Console.Console.instance().addMessage(i18nString(UIStrings.liveEditFailed,{PH1:function(e){switch(e){case"BlockedByActiveFunction":return"Functions that are on the stack (currently being executed) can not be edited";case"BlockedByActiveGenerator":return"Async functions/generators that are active can not be edited";case"BlockedByTopLevelEsModuleChange":return"The top-level of ES modules can not be edited";case"CompileError":case"Ok":throw new Error("Compile errors and Ok status must not be reported on the console")}}(t)}),Common.Console.MessageLevel.Warning);const i=i18nString(UIStrings.liveEditCompileFailed,{PH1:r.text});this.#n.addLineMessage(Workspace.UISourceCode.Message.Level.Error,i,r.lineNumber,r.columnNumber)}async update(){const e=await this.#g.acquire();this.isDiverged()&&!this.#u?await this.divergeFromVM():!this.isDiverged()&&this.#u&&await this.mergeToVM(),e()}async divergeFromVM(){this.#c&&(this.#d=!0,await this.#s.debuggerWorkspaceBinding.updateLocations(this.#c),this.#d=void 0,this.#u=!0,this.dispatchEventToListeners("DidDivergeFromVM"))}async mergeToVM(){this.#c&&(this.#u=void 0,this.#p=!0,await this.#s.debuggerWorkspaceBinding.updateLocations(this.#c),this.#p=void 0,this.dispatchEventToListeners("DidMergeToVM"))}hasDivergedFromVM(){return Boolean(this.#u)}isDivergingFromVM(){return Boolean(this.#d)}isMergingToVM(){return Boolean(this.#p)}checkMapping(){this.#c&&void 0===this.#a?this.#c.requestContent().then((e=>{this.#a=e.content,this.update().then((()=>this.mappingCheckedForTest()))})):this.mappingCheckedForTest()}mappingCheckedForTest(){}dispose(){this.#n.removeEventListener(Workspace.UISourceCode.Events.WorkingCopyChanged,this.workingCopyChanged,this),this.#n.removeEventListener(Workspace.UISourceCode.Events.WorkingCopyCommitted,this.workingCopyCommitted,this)}addSourceMapURL(e){this.#c&&this.#c.debuggerModel.setSourceMapURL(this.#c,e)}addDebugInfoURL(e){if(!this.#c)return;const{pluginManager:t}=DebuggerWorkspaceBinding.instance();t&&t.setDebugInfoURL(this.#c,e)}hasSourceMapURL(){return void 0!==this.#c&&Boolean(this.#c.sourceMapURL)}async missingSymbolFiles(){if(!this.#c)return null;const{pluginManager:e}=this.#s.debuggerWorkspaceBinding;if(!e)return null;const t=await e.getSourcesForScript(this.#c);return t&&"missingSymbolFiles"in t?t.missingSymbolFiles:null}get script(){return this.#c||null}get uiSourceCode(){return this.#n}}