From db2713be7a7cc5bde25897b0a772a4e6169439e7 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 24 Nov 2022 16:51:10 +0100 Subject: [PATCH] Removing unused properties --- assets/Data/GameSettings.json | 19 ++++++++++++- assets/Scripts/Game/Data/GameSettings.ts | 5 ++++ utils/regenerateGameSettings.ts | 35 ++++++++++++++++++++---- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/assets/Data/GameSettings.json b/assets/Data/GameSettings.json index fe9477a..fee30f7 100644 --- a/assets/Data/GameSettings.json +++ b/assets/Data/GameSettings.json @@ -1 +1,18 @@ -{"playerSettings":{"defaultHP":50,"requiredXP":[],"collisionDelay":0},"weaponSettings":{"strikeDelay":0.5,"newTest":33}} \ No newline at end of file +{ + "playerSettings": { + "defaultHP": 0, + "requiredXP": [ + 0, + 0, + 0, + 0 + ], + "collisionDelay": 0, + "testSettings": { + "test": 0 + } + }, + "weaponSettings": { + "strikeDelay": 0 + } +} \ No newline at end of file diff --git a/assets/Scripts/Game/Data/GameSettings.ts b/assets/Scripts/Game/Data/GameSettings.ts index 4da1209..d542616 100644 --- a/assets/Scripts/Game/Data/GameSettings.ts +++ b/assets/Scripts/Game/Data/GameSettings.ts @@ -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; +} diff --git a/utils/regenerateGameSettings.ts b/utils/regenerateGameSettings.ts index ce6a985..ecc50be 100644 --- a/utils/regenerateGameSettings.ts +++ b/utils/regenerateGameSettings.ts @@ -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 = 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; }