From 11e4ff34d683ee450934b559796889876f862e75 Mon Sep 17 00:00:00 2001 From: T00thpick1 Date: Mon, 1 Jul 2013 01:37:24 -0400 Subject: [PATCH] Flatfile repair --- .../database/FlatfileDatabaseManager.java | 40 ++++++++++++++++++- .../nossr50/database/SQLDatabaseManager.java | 12 ++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java index 861997ec0..9a03c3a7f 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -38,7 +38,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { protected FlatfileDatabaseManager() { usersFile = new File(mcMMO.getUsersFilePath()); - createDatabase(); + checkStructure(); updateLeaderboards(); } @@ -555,8 +555,44 @@ public final class FlatfileDatabaseManager implements DatabaseManager { playerStatHash.put(SkillType.FISHING, fishing); } - private void createDatabase() { + /** + * Checks that the file is present and valid + */ + private void checkStructure() { if (usersFile.exists()) { + BufferedReader in = null; + FileWriter out = null; + String usersFilePath = mcMMO.getUsersFilePath(); + + synchronized (fileWritingLock) { + try { + in = new BufferedReader(new FileReader(usersFilePath)); + StringBuilder writer = new StringBuilder(); + String line = ""; + + while ((line = in.readLine()) != null) { + String[] character = line.split(":"); + + // If they're valid, rewrite them to the file. + if (character.length >= 37) { + writer.append(line).append("\r\n"); + } else { + // Placeholder, repair row if needed (I.E. when new skills are added and such) + } + } + + // Write the new file + out = new FileWriter(usersFilePath); + out.write(writer.toString()); + } + catch (IOException e) { + mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString()); + } + finally { + tryClose(in); + tryClose(out); + } + } return; } diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index 2361fb43f..ddbb27bef 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -51,7 +51,7 @@ public final class SQLDatabaseManager implements DatabaseManager { protected SQLDatabaseManager() { checkConnected(); - createStructure(); + checkStructure(); } public void purgePowerlessUsers() { @@ -589,9 +589,9 @@ public final class SQLDatabaseManager implements DatabaseManager { } /** - * Attempt to create the database structure. + * Checks that the database structure is present and correct */ - private void createStructure() { + private void checkStructure() { write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` (" + "`id` int(10) unsigned NOT NULL AUTO_INCREMENT," + "`user` varchar(40) NOT NULL," @@ -1042,6 +1042,12 @@ public final class SQLDatabaseManager implements DatabaseManager { } } + /** + * Retrieve the database id for a player + * + * @param playerName The name of the user to retrieve the id for + * @return the requested id or 0 if not found + */ private int readId(String playerName) { int id = 0;