Added SceneryInfo parsing
+Added scv (SceneryInfo) block parser and scenery format version check. If the block is invalid, an error is thrown, if the version is not 29, a warning is logged +Added errors to SceneryParserLog
This commit is contained in:
parent
6a9cba7630
commit
6ba732b304
@ -5,7 +5,8 @@ const Constants = {
|
||||
},
|
||||
parser: {
|
||||
forceAutoSwitches: false,
|
||||
logNewAutoSwitches: true
|
||||
logNewAutoSwitches: true,
|
||||
sceneryInfoVersion: 29
|
||||
},
|
||||
warnings: {
|
||||
all: false, // enable all warnings
|
||||
@ -20,6 +21,10 @@ const Constants = {
|
||||
switchInvalidDataFormat: true,
|
||||
switchNoModel: true,
|
||||
signalBoxUndefinedPrefabName: true,
|
||||
invalidSceneryInfoVersion: true,
|
||||
},
|
||||
errors: {
|
||||
invalidSceneryInfo: true
|
||||
},
|
||||
layers: [
|
||||
{
|
||||
|
||||
33
src/model/scenery-info.js
Normal file
33
src/model/scenery-info.js
Normal file
@ -0,0 +1,33 @@
|
||||
export default class SceneryInfo {
|
||||
name;
|
||||
version;
|
||||
description;
|
||||
link;
|
||||
author;
|
||||
main_signalbox;
|
||||
|
||||
constructor(name, version, description, link, author, main_signalbox) {
|
||||
Object.assign(this, {
|
||||
name,
|
||||
version,
|
||||
description,
|
||||
link,
|
||||
author,
|
||||
main_signalbox
|
||||
});
|
||||
}
|
||||
|
||||
static fromText(text) {
|
||||
const values = text.split(";");
|
||||
const sceneryInfo = new SceneryInfo(
|
||||
values[1], // name
|
||||
parseInt(values[2]), // version
|
||||
values[3], // description
|
||||
values[4], // link
|
||||
values[5], // author
|
||||
values[6] // main_signalbox
|
||||
);
|
||||
|
||||
return sceneryInfo;
|
||||
}
|
||||
}
|
||||
@ -6,4 +6,8 @@ export default class SceneryParserLog {
|
||||
|
||||
console.warn(`[SceneryParserLog] ${message}`);
|
||||
}
|
||||
|
||||
static error(type, message) {
|
||||
throw new Error(`[SceneryParserLog] ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,9 +7,19 @@ import Route from './route';
|
||||
import Misc from './misc';
|
||||
import SceneryParserLog from './scenery-parser-log';
|
||||
import SignalBox from './signalbox';
|
||||
import Constants from '../helpers/constants';
|
||||
import SceneryInfo from './scenery-info';
|
||||
|
||||
/*
|
||||
TODO: move parsers to another static class
|
||||
TODO: add support for scenery info entries
|
||||
TODO: add support for WorldRotation and WorldTranslation
|
||||
TODO: check CameraHome and MainCamera
|
||||
*/
|
||||
|
||||
export default class Scenery
|
||||
{
|
||||
sceneryInfo = null; // SceneryInfo object
|
||||
objects = {};
|
||||
signalBoxes = [];
|
||||
spawnSignals = [];
|
||||
@ -25,8 +35,13 @@ export default class Scenery
|
||||
const lines = scText.split("\n").map(line => line.trim());
|
||||
const scenery = new Scenery();
|
||||
|
||||
lines.forEach(line => {
|
||||
lines.forEach((line, index) => {
|
||||
if(!line?.length) return;
|
||||
if(index === 0) {
|
||||
this.sceneryInfo = Scenery._parseSceneryInfo(line);
|
||||
return;
|
||||
}
|
||||
|
||||
const object = Scenery._parseObject(line);
|
||||
if(!object) return; // skip null objects
|
||||
scenery.addObject(object);
|
||||
@ -87,6 +102,20 @@ export default class Scenery
|
||||
return this.trackAliases[idOrAlias] || idOrAlias;
|
||||
}
|
||||
|
||||
static _parseSceneryInfo(text) {
|
||||
const infoHeader = text.split(";", 2)[0];
|
||||
if(!infoHeader.startsWith("scv")) {
|
||||
SceneryParserLog.error('invalidSceneryInfo', `Invalid scenery info header: ${infoHeader}`);
|
||||
}
|
||||
|
||||
const version = parseInt(infoHeader.split("v")[1]);
|
||||
if(version !== Constants.parser.sceneryInfoVersion) {
|
||||
SceneryParserLog.warn('invalidSceneryInfoVersion', `Invalid scenery info version: ${version}. Expected: ${Constants.parser.sceneryInfoVersion}`);
|
||||
}
|
||||
|
||||
return SceneryInfo.fromText(text);
|
||||
}
|
||||
|
||||
static _parseObject(text) {
|
||||
const type = text.split(";", 2)[0];
|
||||
switch(type) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user