Removing unused properties

This commit is contained in:
Martin 2022-11-24 16:51:10 +01:00
parent a0ef7d518e
commit db2713be7a
3 changed files with 53 additions and 6 deletions

View File

@ -1 +1,18 @@
{"playerSettings":{"defaultHP":50,"requiredXP":[],"collisionDelay":0},"weaponSettings":{"strikeDelay":0.5,"newTest":33}}
{
"playerSettings": {
"defaultHP": 0,
"requiredXP": [
0,
0,
0,
0
],
"collisionDelay": 0,
"testSettings": {
"test": 0
}
},
"weaponSettings": {
"strikeDelay": 0
}
}

View File

@ -7,8 +7,13 @@ export class PlayerSettings {
public defaultHP = 0;
public requiredXP: number[] = [];
public collisionDelay = 0;
public testSettings = new TestSettings();
}
export class WeaponSettings {
public strikeDelay = 0;
}
export class TestSettings {
public test = 0;
}

View File

@ -1,17 +1,42 @@
import { readFileSync, writeFileSync } from "fs";
import { merge } from "lodash";
import { merge, unset } from "lodash";
import { GameSettings } from "../assets/Scripts/Game/Data/GameSettings";
regenerateGameSettings();
function regenerateGameSettings(): void {
const settingsPath: string = process.argv[2];
const blankSettings: GameSettings = new GameSettings();
const templateSettings: GameSettings = new GameSettings();
const savedSettingsJson: string = readFileSync(settingsPath, "utf8");
const savedSettings: GameSettings = <GameSettings>JSON.parse(savedSettingsJson);
deleteUnusedProperties(templateSettings, savedSettings);
const result: GameSettings = merge(templateSettings, savedSettings);
const result: GameSettings = merge(blankSettings, savedSettings);
writeFileSync(settingsPath, JSON.stringify(result));
console.log("Game settings regenerated");
}
function deleteUnusedProperties(templateSettings: GameSettings, savedSettings: GameSettings): void {
const templateKeys: string[] = getAllKeys(templateSettings);
const usedSettings: string[] = getAllKeys(savedSettings);
usedSettings.forEach((key) => {
if (key.match(/.\d+/)) return; // ignore arrays
if (!templateKeys.includes(key)) {
console.log("Removing unused property " + key);
unset(savedSettings, key);
}
});
}
function getAllKeys(objectWithKeys: any, prefix = ""): string[] {
const keys: string[] = [];
const objectKeys: string[] = Object.keys(objectWithKeys);
for (let i = 0; i < objectKeys.length; i++) {
keys.push(...getAllKeys(objectWithKeys[objectKeys[i]], `${prefix}${objectKeys[i]}.`));
keys.push(`${prefix}${objectKeys[i]}`);
}
return keys;
}