11/* global window */
22
33window . CC_EDITOR = true ;
4+ const serverUrl = window . WebEnv . serverURL ;
45
56window . Editor = {
67 Message : {
78 request : async function ( target , method , uuid ) {
89 if ( method === 'query-asset-info' ) {
9- const currentUrl = window . location . origin ;
10- return await fetch ( `${ currentUrl } /query-asset-info/${ uuid } ` )
10+ return await fetch ( `${ serverUrl } /query-asset-info/${ uuid } ` )
11+ . then ( function ( r ) { return r . json ( ) ; } )
12+ . catch ( function ( ) { return '' ; } ) ;
13+ } else if ( method === 'query-engine-info' ) {
14+ return await fetch ( `${ serverUrl } /engine/query-engine-info` )
1115 . then ( function ( r ) { return r . json ( ) ; } )
1216 . catch ( function ( ) { return '' ; } ) ;
1317 }
@@ -16,9 +20,49 @@ window.Editor = {
1620 } ,
1721} ;
1822
23+ if ( typeof window . require === 'undefined' ) {
24+ const fsMock = {
25+ readFile : function ( filePath ) {
26+ const requestUrl = `${ serverUrl } /engine/read-file-sync?path=${ encodeURIComponent ( filePath ) } ` ;
27+ return fetch ( requestUrl ) . then ( function ( res ) {
28+ if ( res . ok ) {
29+ return res . arrayBuffer ( ) ;
30+ }
31+ throw new Error ( 'Failed to read file: ' + filePath ) ;
32+ } ) ;
33+ } ,
34+ readFileSync : function ( filePath ) {
35+ const requestUrl = `${ serverUrl } /engine/read-file-sync?path=${ encodeURIComponent ( filePath ) } ` ;
36+ const xhr = new XMLHttpRequest ( ) ;
37+ xhr . open ( 'GET' , requestUrl , false ) ; // synchronous
38+ xhr . overrideMimeType ( 'text/plain; charset=x-user-defined' ) ;
39+ xhr . send ( null ) ;
40+
41+ if ( xhr . status === 200 ) {
42+ const val = xhr . responseText ;
43+ const len = val . length ;
44+ const buf = new Uint8Array ( len ) ;
45+ for ( let i = 0 ; i < len ; i ++ ) {
46+ buf [ i ] = val . charCodeAt ( i ) & 0xff ;
47+ }
48+ return buf ;
49+ }
50+ throw new Error ( 'Failed to read file synchronously: ' + filePath ) ;
51+ }
52+ } ;
53+
54+ window . require = function ( name ) {
55+ if ( name === 'fs' || name === 'fs-extra' ) {
56+ return fsMock ;
57+ }
58+ throw new Error ( 'Module ' + name + ' not found in editor-stub-preload require mock' ) ;
59+ } ;
60+ }
61+
1962window . EditorExtends = {
2063 emit : function ( ) { } ,
2164 on : function ( ) { } ,
65+ off : function ( ) { } ,
2266 removeListener : function ( ) { } ,
2367 UuidUtils : {
2468 uuid : function ( ) { return '' ; } ,
@@ -28,18 +72,54 @@ window.EditorExtends = {
2872 isUuid : function ( ) { return false ; } ,
2973 } ,
3074 Component : {
75+ allow : false ,
3176 addMenu : function ( ) { } ,
3277 removeMenu : function ( ) { } ,
78+ getMenus : function ( ) { return [ ] ; } ,
3379 add : function ( ) { } ,
3480 remove : function ( ) { } ,
81+ clear : function ( ) { } ,
82+ getComponent : function ( ) { return null ; } ,
83+ getComponentFromPath : function ( ) { return null ; } ,
84+ getPathFromUuid : function ( ) { return '' ; } ,
85+ getComponents : function ( ) { return { } ; } ,
86+ changeUUID : function ( ) { } ,
87+ emit : function ( ) { } ,
88+ on : function ( ) { } ,
89+ off : function ( ) { } ,
90+ removeListener : function ( ) { } ,
3591 } ,
3692 Node : {
93+ allow : false ,
3794 add : function ( ) { } ,
3895 remove : function ( ) { } ,
96+ clear : function ( ) { } ,
97+ updateNodeName : function ( ) { } ,
3998 getNode : function ( ) { return null ; } ,
99+ getNodeByPath : function ( ) { return null ; } ,
100+ getNodePath : function ( ) { return '' ; } ,
101+ getNodeUuidByPath : function ( ) { return null ; } ,
102+ getNodeByPathOrThrow : function ( ) { throw new Error ( 'Not implemented' ) ; } ,
103+ getNodeUuidByPathOrThrow : function ( ) { throw new Error ( 'Not implemented' ) ; } ,
104+ getNodes : function ( ) { return { } ; } ,
105+ getNodesByAsset : function ( ) { return [ ] ; } ,
106+ getNodesInScene : function ( ) { return { } ; } ,
107+ changeNodeUUID : function ( ) { } ,
108+ emit : function ( ) { } ,
109+ on : function ( ) { } ,
110+ off : function ( ) { } ,
111+ removeListener : function ( ) { } ,
112+ } ,
113+ Script : {
114+ allow : false ,
115+ add : function ( ) { } ,
116+ remove : function ( ) { } ,
117+ getCtors : function ( ) { return [ ] ; } ,
40118 emit : function ( ) { } ,
119+ on : function ( ) { } ,
120+ off : function ( ) { } ,
121+ removeListener : function ( ) { } ,
41122 } ,
42- Script : { allow : false } ,
43123 MissingReporter : {
44124 classInstance : ( function ( ) {
45125 const finder = function ( type , data , owner , propName ) {
0 commit comments