You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.4 KiB
104 lines
2.4 KiB
|
2 years ago
|
|
||
|
|
const express = require('express');
|
||
|
|
const fs = require('fs');
|
||
|
|
const app = express();
|
||
|
|
|
||
|
|
const AdvancableBuffer = require('./backend/utilities/AdvancableBuffer.js');
|
||
|
|
|
||
|
|
// handling CORS
|
||
|
|
app.use((req, res, next) => {
|
||
|
|
res.header("Access-Control-Allow-Origin",
|
||
|
|
"http://localhost:4200");
|
||
|
|
res.header("Access-Control-Allow-Headers",
|
||
|
|
"Origin, X-Requested-With, Content-Type, Accept");
|
||
|
|
next();
|
||
|
|
});
|
||
|
|
|
||
|
|
const upload = require('./backend/routes/upload-replay');
|
||
|
|
|
||
|
|
function readStringAndAdvance(buffer, start, numBytes)
|
||
|
|
{
|
||
|
|
let pos = start
|
||
|
|
let string = buffer.subarray(pos, pos+=numBytes);
|
||
|
|
return {str : string.toString(), pos : pos};
|
||
|
|
}
|
||
|
|
|
||
|
|
function readUInt16AndAdvance(buffer, start)
|
||
|
|
{
|
||
|
|
let pos = start;
|
||
|
|
let t = buffer.subarray(pos, pos+=1);
|
||
|
|
let value = t.readInt8(0)
|
||
|
|
return {value: value, pos: pos};
|
||
|
|
}
|
||
|
|
|
||
|
|
function readInt8AndAdvance(buffer, start)
|
||
|
|
{
|
||
|
|
let pos = start;
|
||
|
|
let t = buffer.subarray(pos, pos+=2);
|
||
|
|
let value = t.readUInt16LE(0)
|
||
|
|
return {value: value, pos: pos};
|
||
|
|
}
|
||
|
|
|
||
|
|
// route for handling requests from the Angular client
|
||
|
|
app.post('/api/upload-replay', upload.single('file'), (req, res) => {
|
||
|
|
let file = req.file;
|
||
|
|
console.log("File uploaded: ", req.file);
|
||
|
|
fs.readFile(file.path, function(err, buffer)
|
||
|
|
{
|
||
|
|
buff = new AdvancableBuffer(buffer);
|
||
|
|
console.log(buffer)
|
||
|
|
let header_magic = buff.readString(3);
|
||
|
|
console.log(header_magic);
|
||
|
|
|
||
|
|
let version = buff.readUInt16();
|
||
|
|
console.log(version)
|
||
|
|
|
||
|
|
if (version < 5 || version >7 )
|
||
|
|
{
|
||
|
|
console.log("Unsupported version")
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let format = buff.readInt8();
|
||
|
|
if (format != 66)
|
||
|
|
{
|
||
|
|
console.log("Unsupported format")
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let compressionofRefTable = buff.readInt8();
|
||
|
|
|
||
|
|
if (compressionofRefTable != 67 && compressionofRefTable != 85)
|
||
|
|
{
|
||
|
|
console.log("Unsupported compression format, Ref")
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let compressionofRefBody = buff.readInt8();
|
||
|
|
if (compressionofRefBody != 67 && compressionofRefBody != 85)
|
||
|
|
{
|
||
|
|
console.log("Unsupported compression format, Body")
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
let unknownByte = '';
|
||
|
|
if (version >= 4)
|
||
|
|
{
|
||
|
|
unknownByte = buff.readString(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
let id = buff.readUInt32();
|
||
|
|
console.log(id)
|
||
|
|
|
||
|
|
if (version >=6)
|
||
|
|
{
|
||
|
|
userData = buff.readBytes()
|
||
|
|
}
|
||
|
|
|
||
|
|
let numNodes = buff.readUInt32();
|
||
|
|
})
|
||
|
|
});
|
||
|
|
|
||
|
|
app.listen(3000, () => {
|
||
|
|
console.log('Server listening on port 3000');
|
||
|
|
});
|