fix: 适配启动服务

This commit is contained in:
unanmed 2025-08-20 15:22:24 +08:00
parent 574d765c69
commit 66ec8190c9
2 changed files with 25 additions and 58 deletions

View File

@ -1,41 +0,0 @@
interface MotaConfig {
name: string;
resourceName?: string;
zip?: Record<string, string[]>;
}
function defineConfig(config: MotaConfig): MotaConfig {
return config;
}
export default defineConfig({
// 这里修改塔的name请保持与全塔属性的完全相同否则发布之后可能无法进行游玩
name: 'HumanBreak',
resourceName: 'HumanBreakRes',
zip: {
'resource.zip': [
'autotiles/*',
'tilesets/*',
'images/*',
'animates/*',
'sounds/*',
'fonts/*',
'!images/bg.jpg'
],
'weather.zip': [
'materials/fog.png',
'materials/cloud.png',
'materials/sun.png'
],
'materials.zip': [
'materials/animates.png',
'materials/enemy48.png',
'materials/enemys.png',
'materials/icons.png',
'materials/items.png',
'materials/npc48.png',
'materials/npcs.png',
'materials/terrains.png'
]
}
});

View File

@ -6,7 +6,6 @@ import {
createServer as http
} from 'http';
import { isNil } from 'lodash-es';
import config from '../mota.config.js';
import fs from 'fs-extra';
import { resolve, basename } from 'path';
import * as rollup from 'rollup';
@ -19,6 +18,10 @@ import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
const [, , vitePortStr = '5173', serverPortStr = '3000'] = process.argv;
const vitePort = parseInt(vitePortStr);
const serverPort = parseInt(serverPortStr);
const base = './public';
type Request = IncomingMessage;
@ -86,7 +89,7 @@ async function getFile(req: Request, res: Response, path: string) {
res.writeHead(200, { 'Content-type': 'text/css' });
if (path.endsWith('.html'))
res.writeHead(200, { 'Content-type': 'text/html' });
return res.end(data), true;
return (res.end(data), true);
} catch (e) {
console.log(e);
return false;
@ -123,7 +126,7 @@ async function getAll(
});
await Promise.all(tasks);
const result = ids.map(v => data[v]);
return res.end(result.join(join)), true;
return (res.end(result.join(join)), true);
}
async function getEsmFile(
@ -468,26 +471,25 @@ ${names}
async function startHttpServer(port: number = 3000) {
if (h) return h;
const server = http();
const data = await fs.readFile('public/project/data.js', 'utf-8');
const json = data.split('\n').slice(1).join('\n');
const parsed = JSON.parse(json);
const name = parsed.firstData.name;
server.listen(port, '127.0.0.1');
const tryNext = () => {
server.listen(port++, '127.0.0.1');
};
server.on('error', () => {
tryNext();
});
server.on('listening', () => {
console.log(`编辑器地址http://127.0.0.1:${port - 1}/editor.html`);
setupHttp(server);
console.log(`编辑器地址http://127.0.0.1:${port}/editor.html`);
setupHttp(server, name);
});
tryNext();
return server;
}
function setupHttp(server: Server) {
function setupHttp(server: Server, name: string) {
server.on('request', async (req, res) => {
const p = req.url
?.replace(`/games/${config.name}`, '')
?.replace(`/games/${name}`, '')
.replace('/all/', '/') // 样板中特殊处理的all文件
.replace('/forceTem/', '/') // 强制用样板的http服务获取文件
.replace('/src/', '../src/'); // src在上一级目录
@ -619,14 +621,20 @@ async function ensureConfig() {
(async function () {
// 1. 启动vite服务
const vite = await createServer();
await vite.listen(5173);
console.log(`游戏地址http://localhost:5173/`);
await vite.listen(vitePort);
console.log(`游戏地址http://localhost:${vitePort}/`);
// 2. 启动样板http服务
await ensureConfig();
const server = await startHttpServer(3000);
const server = await startHttpServer(serverPort);
h = server;
// 3. 启动样板ws热重载服务
await startWsServer(server);
process.on('SIGTERM', () => {
vite.close();
server.close();
process.exit(0);
});
})();