forked from LBALab/lba2remake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
devserver.js
142 lines (126 loc) · 4.46 KB
/
devserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
require('@babel/register')({
presets: ['@babel/preset-env', '@babel/preset-react']
});
const fs = require('fs');
const http = require('http');
const React = require('react');
const {renderToStaticMarkup} = require('react-dom/server');
const express = require('express');
const middleware = require('webpack-dev-middleware');
const webpackConfig = require('./webpack.config.js');
const webpack = require('webpack');
const bodyParser = require('body-parser');
const Main = require('./main.jsx');
const app = express();
app.set('port', process.env.PORT || 8080);
app.set('host', process.env.HOST || '0.0.0.0');
app.use(bodyParser.json());
app.use(bodyParser.raw({
limit: '64mb'
}));
app.post('/metadata/:game', function (req, res) {
const body = req.body;
const game = req.params.game;
console.log(`Saving metadata, type=${body.type}`);
let fileName;
let kind = 'partial';
switch (body.type) {
case 'scene':
fileName = `./www/metadata/${game}/scene_${body.index}.json`;
break;
case 'game':
fileName = `./www/metadata/${game}/game.json`;
break;
case 'models':
fileName = `./www/metadata/${game}/models.json`;
break;
case 'islands':
fileName = `./www/metadata/${game}/islands.json`;
break;
case 'layouts':
fileName = `./www/metadata/${game}/layouts.json`;
kind = 'full';
break;
}
if (fileName) {
if (kind === 'full') {
fs.writeFile(fileName, JSON.stringify(body.content, null, 2), () => {});
} else {
fs.readFile(fileName, 'utf8', (err, file) => {
if (err) {
console.error(err);
} else {
const content = JSON.parse(file);
if (!(body.subType in content)) {
content[body.subType] = [];
}
content[body.subType][body.subIndex] = body.value;
fs.writeFile(fileName, JSON.stringify(content, null, 2), () => {});
}
});
}
}
res.end();
});
app.post('/crash', function (req, res) {
console.log('Saving crash report');
const report = req.body;
fs.writeFile('./crash_report.json', JSON.stringify(report, null, 2), () => {});
res.end();
});
app.post('/lut.dat', function(req, res) {
fs.writeFile('./www/lut.dat', req.body, () => {
console.log('Saved lut.dat');
res.end();
});
});
app.post('/grid_metadata/:game', function(req, res) {
const game = req.params.game;
fs.writeFile(`./www/metadata/${game}/grids.json`, req.body, () => {
console.log(`Saved metadata/${game}/grids.json`);
res.end();
});
});
app.post('/iso_replacements/:game/:entry', function(req, res) {
const entry = Number(req.params.entry);
const game = req.params.game;
fs.writeFile(`./www/models/${game}/iso_scenes/${entry}.glb`, req.body, () => {
console.log(`Saved models/${game}/iso_scenes/${entry}.glb`);
res.end();
fs.readFile(`./www/metadata/${game}/iso_scenes.json`, 'utf8', (err, file) => {
if (err) {
console.error(err);
} else {
const content = JSON.parse(file);
if (!content.includes(entry)) {
content.push(entry);
fs.writeFile(`./www/metadata/${game}/iso_scenes.json`, JSON.stringify(content, null, 2), () => {
console.log('Saved iso_scenes metadata');
});
}
}
});
});
});
app.get('/layout_models/:game', function(req, res) {
const game = req.params.game;
fs.readdir(`./www/models/${game}/layouts`, (err, files) => {
res.end(JSON.stringify(files));
});
});
app.use('/', express.static('./www'));
app.use('/doc', express.static('./doc'));
app.use('/webxr-assets', express.static('./node_modules/@webxr-input-profiles/assets/dist/profiles'));
const indexBody = renderToStaticMarkup(React.createElement(Main, {
scriptTag: '',
script: 'window.gtag=function(){};\nwindow.isLocalServer=true;',
crashReportUrl: '/crash'
}));
app.get('/', (req, res) => {
res.end(indexBody);
});
webpackConfig.devtool = process.env.SRCMAP === 'true' ? 'source-map' : undefined;
const compiler = webpack(webpackConfig);
app.use(middleware(compiler));
app.listen(app.get('port'), app.get('host'));