mirror of
				https://github.com/unanmed/HumanBreak.git
				synced 2025-10-31 12:12:58 +08:00 
			
		
		
		
	重写客户端热重载
This commit is contained in:
		
							parent
							
								
									0edbf05e9e
								
							
						
					
					
						commit
						8ea94ae092
					
				| @ -537,6 +537,7 @@ async function startWsServer(port: number = 8080) { | |||||||
|             }); |             }); | ||||||
|         }); |         }); | ||||||
|     }; |     }; | ||||||
|  |     return tryNext(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| (async function () { | (async function () { | ||||||
|  | |||||||
| @ -4,33 +4,6 @@ export {}; | |||||||
| /* @__PURE__ */ (function () { | /* @__PURE__ */ (function () { | ||||||
|     if (main.mode !== 'play' || main.replayChecking) return; |     if (main.mode !== 'play' || main.replayChecking) return; | ||||||
| 
 | 
 | ||||||
|     /** |  | ||||||
|      * 发送请求 |  | ||||||
|      * @param {string} url |  | ||||||
|      * @param {string} type |  | ||||||
|      * @param {string} data |  | ||||||
|      * @returns {Promise<string>} |  | ||||||
|      */ |  | ||||||
|     async function post(url, type, data) { |  | ||||||
|         const xhr = new XMLHttpRequest(); |  | ||||||
|         xhr.open(type, url); |  | ||||||
|         xhr.send(data); |  | ||||||
|         const res = await new Promise(res => { |  | ||||||
|             xhr.onload = () => { |  | ||||||
|                 if (xhr.status !== 200) { |  | ||||||
|                     console.error(`hot reload: http ${xhr.status}`); |  | ||||||
|                     res('@error'); |  | ||||||
|                 } else res('success'); |  | ||||||
|             }; |  | ||||||
|             xhr.onerror = () => { |  | ||||||
|                 res('@error'); |  | ||||||
|                 console.error(`hot reload: error on connection`); |  | ||||||
|             }; |  | ||||||
|         }); |  | ||||||
|         if (res === 'success') return xhr.response; |  | ||||||
|         else return '@error'; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * 热重载css |      * 热重载css | ||||||
|      * @param {string} data |      * @param {string} data | ||||||
| @ -44,7 +17,7 @@ export {}; | |||||||
|         link.href = data; |         link.href = data; | ||||||
|         link.id = 'mota-css'; |         link.id = 'mota-css'; | ||||||
|         document.head.appendChild(link); |         document.head.appendChild(link); | ||||||
|         console.log(`css hot reload: ${data}`); |         console.log(`Css hot reload: ${data}`); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -60,7 +33,7 @@ export {}; | |||||||
|             return; |             return; | ||||||
|         // 首先重新加载main.floors对应的楼层
 |         // 首先重新加载main.floors对应的楼层
 | ||||||
|         await import( |         await import( | ||||||
|             /* @vite-ignore */ `./project/floors/${data}.js?v=${Date.now()}` |             /* @vite-ignore */ `/forceTem/project/floors/${data}.js?v=${Date.now()}` | ||||||
|         ); |         ); | ||||||
|         // 然后写入core.floors并解析
 |         // 然后写入core.floors并解析
 | ||||||
|         core.floors[data] = main.floors[data]; |         core.floors[data] = main.floors[data]; | ||||||
| @ -79,19 +52,18 @@ export {}; | |||||||
|             } |             } | ||||||
|             core.updateStatusBar(true, true); |             core.updateStatusBar(true, true); | ||||||
|         } |         } | ||||||
|         console.log(`floor hot reload: ${data}`); |         console.log(`Floor hot reload: ${data}`); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * 热重载脚本编辑 |      * 热重载脚本编辑 | ||||||
|      * @param {string} data |  | ||||||
|      */ |      */ | ||||||
|     async function reloadScript() { |     async function reloadScript() { | ||||||
|         // 脚本编辑略微麻烦点
 |         // 脚本编辑略微麻烦点
 | ||||||
|         const before = functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a; |         const before = functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a; | ||||||
|         // 这里不能用动态导入,因为动态导入会变成模块,变量就不是全局的了
 |         // 这里不能用动态导入,因为动态导入会变成模块,变量就不是全局的了
 | ||||||
|         const script = document.createElement('script'); |         const script = document.createElement('script'); | ||||||
|         script.src = `/project/functions.js?v=${Date.now()}`; |         script.src = `/forceTem/project/functions.js?v=${Date.now()}`; | ||||||
|         document.body.appendChild(script); |         document.body.appendChild(script); | ||||||
|         await new Promise(res => { |         await new Promise(res => { | ||||||
|             script.onload = () => res('success'); |             script.onload = () => res('success'); | ||||||
| @ -118,7 +90,7 @@ export {}; | |||||||
|                             core.ui.uidata[id] = now; |                             core.ui.uidata[id] = now; | ||||||
|                         } |                         } | ||||||
|                         core.updateStatusBar(true, true); |                         core.updateStatusBar(true, true); | ||||||
|                         console.log(`function hot reload: ${mod}.${id}`); |                         console.log(`Function hot reload: ${mod}.${id}`); | ||||||
|                     } catch (e) { |                     } catch (e) { | ||||||
|                         console.error(e); |                         console.error(e); | ||||||
|                     } |                     } | ||||||
| @ -127,19 +99,13 @@ export {}; | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async function reloadPlugin(data) { |  | ||||||
|         // 直接import就完事了
 |  | ||||||
|         await import(/* @vite-ignore */ `./src/plugin/game/${data}.js`); |  | ||||||
|         console.log(`plugin hot reload: ${data}.js`); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     /** |     /** | ||||||
|      * 属性热重载,包括全塔属性等 |      * 属性热重载,包括全塔属性等 | ||||||
|      * @param {string} data |      * @param {string} data | ||||||
|      */ |      */ | ||||||
|     async function reloadData(data) { |     async function reloadData(data) { | ||||||
|         const script = document.createElement('script'); |         const script = document.createElement('script'); | ||||||
|         script.src = `/project/${data}.js?v=${Date.now()}`; |         script.src = `.forceTem/project/${data}.js?v=${Date.now()}`; | ||||||
|         document.body.appendChild(script); |         document.body.appendChild(script); | ||||||
|         await new Promise(res => { |         await new Promise(res => { | ||||||
|             script.onload = () => res('success'); |             script.onload = () => res('success'); | ||||||
| @ -189,38 +155,19 @@ export {}; | |||||||
|             location.reload(); |             location.reload(); | ||||||
|         } |         } | ||||||
|         core.updateStatusBar(true, true); |         core.updateStatusBar(true, true); | ||||||
|         console.log(`data hot reload: ${data}`); |         console.log(`Data hot reload: ${data}`); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // 初始化
 |     const ws = new WebSocket('ws://127.0.0.1:8080'); | ||||||
|     (async function () { |     ws.addEventListener('open', () => { | ||||||
|         const data = await post('/reload', 'POST', 'test'); |         console.log(`Web socket connect successfully`); | ||||||
|         if (data === '@error') { |     }); | ||||||
|             console.log(`未检测到node服务,热重载插件将无法使用`); |     ws.addEventListener('message', e => { | ||||||
|         } else { |         const data = JSON.parse(e.data); | ||||||
|             console.log(`热重载插件加载成功`); |         if (data.type === 'reload') location.reload(); | ||||||
|             // reload
 |         if (data.type === 'floorHotReload') reloadFloor(data.floor); | ||||||
|             setInterval(async () => { |         if (data.type === 'dataHotReload') reloadData(data.data); | ||||||
|                 const res = await post('/reload', 'POST'); |         if (data.type === 'functionsHotReload') reloadScript(); | ||||||
|                 if (res === '@error') return; |         if (data.type === 'cssHotReload') reloadCss(data.path); | ||||||
|                 if (res === 'true') location.reload(); |  | ||||||
|                 else return; |  | ||||||
|             }, 1000); |  | ||||||
| 
 |  | ||||||
|             // hot reload
 |  | ||||||
|             setInterval(async () => { |  | ||||||
|                 const res = await post('/hotReload', 'POST'); |  | ||||||
|                 const data = res.split('@@'); |  | ||||||
|                 data.forEach(v => { |  | ||||||
|                     if (v === '') return; |  | ||||||
|                     const [type, file] = v.split(':'); |  | ||||||
|                     if (type === 'css') reloadCss(file); |  | ||||||
|                     if (type === 'data') reloadData(file); |  | ||||||
|                     if (type === 'floor') reloadFloor(file); |  | ||||||
|                     if (type === 'script') reloadScript(file); |  | ||||||
|                     if (type === 'plugin') reloadPlugin(file); |  | ||||||
|     }); |     }); | ||||||
|             }, 1000); |  | ||||||
|         } |  | ||||||
|     })(); |  | ||||||
| })(); | })(); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user