Slash-The-Hordes/utils/regenerateGameSettings.ts

43 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-11-24 12:17:05 +00:00
import { readFileSync, writeFileSync } from "fs";
2022-11-24 15:51:10 +00:00
import { merge, unset } from "lodash";
2022-11-24 12:17:05 +00:00
import { GameSettings } from "../assets/Scripts/Game/Data/GameSettings";
regenerateGameSettings();
function regenerateGameSettings(): void {
const settingsPath: string = process.argv[2];
2022-11-24 15:51:10 +00:00
const templateSettings: GameSettings = new GameSettings();
2022-11-24 12:17:05 +00:00
const savedSettingsJson: string = readFileSync(settingsPath, "utf8");
const savedSettings: GameSettings = <GameSettings>JSON.parse(savedSettingsJson);
2022-11-24 15:51:10 +00:00
deleteUnusedProperties(templateSettings, savedSettings);
const result: GameSettings = merge(templateSettings, savedSettings);
2022-11-24 12:17:05 +00:00
writeFileSync(settingsPath, JSON.stringify(result));
2022-11-24 15:51:10 +00:00
}
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]}`);
}
2022-11-24 12:20:30 +00:00
2022-11-24 15:51:10 +00:00
return keys;
2022-11-24 12:17:05 +00:00
}