mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-09-24 14:06:51 +00:00
Removing unused properties
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user