Compare commits

...

3 Commits

View File

@ -30,28 +30,83 @@ var serversPath = path.join(userData, "servers.json");
var versionsPath = path.join(userData, "versions.json");
var hashPath = path.join(userData, "hashes.json");
function initialSetup(firstTime) {
if (!firstTime) {
// Migration from pre-1.6
// Back everything up, just in case
if (fs.existsSync(configPath))
fs.copySync(configPath, configPath + ".bak");
function backup() {
if (fs.existsSync(configPath)) fs.copySync(configPath, configPath + ".bak");
if (fs.existsSync(serversPath))
fs.copySync(serversPath, serversPath + ".bak");
if (fs.existsSync(versionsPath))
fs.copySync(versionsPath, versionsPath + ".bak");
if (fs.existsSync(hashPath)) fs.copySync(hashPath, hashPath + ".bak");
} else {
}
function patchVersions() {
var current = fs.readJsonSync(versionsPath);
var newDefaults = fs.readJsonSync(
path.join(__dirname, "/defaults/versions.json")
);
for (var i = 0; i < newDefaults["versions"].length; i++) {
var newDefault = newDefaults["versions"][i];
var found = false;
for (var j = 0; j < current["versions"].length; j++) {
var version = current["versions"][j];
if (newDefault["name"] === version["name"]) {
current["versions"][j] = newDefault;
found = true;
break;
}
}
if (!found) {
current["versions"].push(newDefault);
}
}
fs.writeFileSync(versionsPath, JSON.stringify(current, null, 4));
}
function patchServers() {
var current = fs.readJsonSync(serversPath);
var newDefaults = fs.readJsonSync(
path.join(__dirname, "/defaults/servers.json")
);
for (var i = 0; i < newDefaults["servers"].length; i++) {
var newDefault = newDefaults["servers"][i];
var found = false;
for (var j = 0; j < current["servers"].length; j++) {
var server = current["servers"][j];
if (newDefault["uuid"] === server["uuid"]) {
current["servers"][j] = newDefault;
found = true;
break;
}
}
if (!found) {
current["servers"].push(newDefault);
}
}
fs.writeFileSync(serversPath, JSON.stringify(current, null, 4));
}
function initialSetup(firstTime) {
backup();
if (firstTime) {
// First-time setup
// Copy default servers
fs.copySync(
path.join(__dirname, "/defaults/servers.json"),
serversPath
);
fs.copySync(
path.join(__dirname, "/defaults/versions.json"),
versionsPath
);
} else if (fs.existsSync(serversPath)) {
// Migration
// Update default servers and versions
patchVersions();
patchServers();
}
// Copy default versions and config
fs.copySync(path.join(__dirname, "/defaults/versions.json"), versionsPath);
fs.copySync(path.join(__dirname, "/defaults/config.json"), configPath);
fs.copySync(path.join(__dirname, "/defaults/hashes.json"), hashPath);