Add sqltoflat converter type

This commit is contained in:
Xephi 2014-08-28 06:24:47 +02:00
parent 922df85a65
commit c12735d2f4
7 changed files with 190 additions and 9 deletions

View File

@ -13,6 +13,7 @@ import fr.xephi.authme.converter.FlatToSql;
import fr.xephi.authme.converter.FlatToSqlite; import fr.xephi.authme.converter.FlatToSqlite;
import fr.xephi.authme.converter.RakamakConverter; import fr.xephi.authme.converter.RakamakConverter;
import fr.xephi.authme.converter.RoyalAuthConverter; import fr.xephi.authme.converter.RoyalAuthConverter;
import fr.xephi.authme.converter.SqlToFlat;
import fr.xephi.authme.converter.vAuthConverter; import fr.xephi.authme.converter.vAuthConverter;
import fr.xephi.authme.converter.xAuthConverter; import fr.xephi.authme.converter.xAuthConverter;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
@ -42,7 +43,7 @@ public class ConverterCommand implements CommandExecutor {
} }
if (args.length == 0) { if (args.length == 0) {
sender.sendMessage("Usage : /converter flattosql | flattosqlite | xauth | crazylogin | rakamak | royalauth | vauth"); sender.sendMessage("Usage : /converter flattosql | flattosqlite | xauth | crazylogin | rakamak | royalauth | vauth | sqltoflat");
return true; return true;
} }
@ -74,6 +75,9 @@ public class ConverterCommand implements CommandExecutor {
case vauth: case vauth:
converter = new vAuthConverter(plugin, database, sender); converter = new vAuthConverter(plugin, database, sender);
break; break;
case sqltoflat:
converter = new SqlToFlat(plugin, database, sender);
break;
default: default:
break; break;
} }
@ -94,7 +98,8 @@ public class ConverterCommand implements CommandExecutor {
crazylogin("crazylogin"), crazylogin("crazylogin"),
rakamak("rakamak"), rakamak("rakamak"),
royalauth("royalauth"), royalauth("royalauth"),
vauth("vauth"); vauth("vauth"),
sqltoflat("sqltoflat");
String name; String name;

View File

@ -54,10 +54,9 @@ public class FlatToSql implements Converter {
source = new File(AuthMe.getInstance().getDataFolder() + File.separator + "auths.db"); source = new File(AuthMe.getInstance().getDataFolder() + File.separator + "auths.db");
source.createNewFile(); source.createNewFile();
output = new File(AuthMe.getInstance().getDataFolder() + File.separator + "authme.sql"); output = new File(AuthMe.getInstance().getDataFolder() + File.separator + "authme.sql");
BufferedReader br = null; output.createNewFile();
BufferedWriter sql = null; BufferedReader br = new BufferedReader(new FileReader(source));
br = new BufferedReader(new FileReader(source)); BufferedWriter sql = new BufferedWriter(new FileWriter(output));
sql = new BufferedWriter(new FileWriter(output));
String createDB = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnID + " INTEGER AUTO_INCREMENT," + columnName + " VARCHAR(255) NOT NULL UNIQUE," + columnPassword + " VARCHAR(255) NOT NULL," + columnIp + " VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'," + columnLastLogin + " BIGINT NOT NULL DEFAULT '" + System.currentTimeMillis() + "'," + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocWorld + " VARCHAR(255) DEFAULT 'world'," + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com'," + columnLogged + " SMALLINT NOT NULL DEFAULT '0'," + "CONSTRAINT table_const_prim PRIMARY KEY (" + columnID + "));"; String createDB = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnID + " INTEGER AUTO_INCREMENT," + columnName + " VARCHAR(255) NOT NULL UNIQUE," + columnPassword + " VARCHAR(255) NOT NULL," + columnIp + " VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'," + columnLastLogin + " BIGINT NOT NULL DEFAULT '" + System.currentTimeMillis() + "'," + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocWorld + " VARCHAR(255) DEFAULT 'world'," + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com'," + columnLogged + " SMALLINT NOT NULL DEFAULT '0'," + "CONSTRAINT table_const_prim PRIMARY KEY (" + columnID + "));";
sql.write(createDB); sql.write(createDB);
String line; String line;

View File

@ -0,0 +1,52 @@
package fr.xephi.authme.converter;
import java.util.List;
import org.bukkit.command.CommandSender;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.FlatFileThread;
import fr.xephi.authme.settings.Messages;
public class SqlToFlat implements Converter {
public AuthMe plugin;
public DataSource database;
public CommandSender sender;
public SqlToFlat(AuthMe plugin, DataSource database, CommandSender sender) {
this.plugin = plugin;
this.database = database;
this.sender = sender;
}
@Override
public void run() {
try {
FlatFileThread flat = new FlatFileThread();
flat.start();
List<PlayerAuth> auths = database.getAllAuths();
int i = 0;
final int size = auths.size();
for (PlayerAuth auth : auths) {
flat.saveAuth(auth);
i++;
if ((i % 100) == 0) {
sender.sendMessage("Conversion Status : " + i + " / " + size);
}
}
if (flat != null && flat.isAlive())
flat.interrupt();
sender.sendMessage("Successfully convert from SQL table to file auths.db");
return;
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
Messages.getInstance()._(sender, "error");
return;
}
}
}

View File

@ -63,4 +63,6 @@ public interface DataSource {
void updateName(String oldone, String newone); void updateName(String oldone, String newone);
List<PlayerAuth> getAllAuths();
} }

View File

@ -12,7 +12,6 @@ import java.util.List;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.settings.PlayersLogs; import fr.xephi.authme.settings.PlayersLogs;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
@ -700,4 +699,45 @@ public class FlatFileThread extends Thread implements DataSource {
this.saveAuth(auth); this.saveAuth(auth);
this.removeAuth(oldone); this.removeAuth(oldone);
} }
@Override
public List<PlayerAuth> getAllAuths() {
BufferedReader br = null;
List<PlayerAuth> auths = new ArrayList<PlayerAuth>();
try {
br = new BufferedReader(new FileReader(source));
String line;
while ((line = br.readLine()) != null) {
String[] args = line.split(":");
switch (args.length) {
case 2:
auths.add(new PlayerAuth(args[0], args[1], "198.18.0.1", 0, "your@email.com"));
case 3:
auths.add(new PlayerAuth(args[0], args[1], args[2], 0, "your@email.com"));
case 4:
auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), "your@email.com"));
case 7:
auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "unavailableworld", "your@email.com"));
case 8:
auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com"));
case 9:
auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8]));
}
}
} catch (FileNotFoundException ex) {
ConsoleLogger.showError(ex.getMessage());
return auths;
} catch (IOException ex) {
ConsoleLogger.showError(ex.getMessage());
return auths;
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ex) {
}
}
}
return auths;
}
} }

View File

@ -13,7 +13,6 @@ import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException; import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException;
import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.HashAlgorithm;
@ -1041,4 +1040,55 @@ public class MySQLThread extends Thread implements DataSource {
return; return;
} }
@Override
public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<PlayerAuth>();
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
con = makeSureConnectionIsReady();
pst = con.prepareStatement("SELECT * FROM " + tableName + ";");
rs = pst.executeQuery();
while (rs.next()) {
PlayerAuth pAuth = null;
int id = rs.getInt(columnID);
if (rs.getString(columnIp).isEmpty() && rs.getString(columnIp) != null) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "198.18.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail));
} else {
if (!columnSalt.isEmpty()) {
if (!columnGroup.isEmpty())
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getInt(columnGroup), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail));
else pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail));
} else {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail));
}
}
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
rs.close();
pst = con.prepareStatement("SELECT * FROM xf_user_authenticate WHERE " + columnID + "=?;");
pst.setInt(1, id);
rs = pst.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("data");
byte[] bytes = blob.getBytes(1, (int) blob.length());
pAuth.setHash(new String(bytes));
}
}
if (pAuth != null)
auths.add(pAuth);
}
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
return auths;
} catch (TimeoutException ex) {
ConsoleLogger.showError(ex.getMessage());
return auths;
} finally {
close(pst);
close(con);
}
return auths;
}
} }

View File

@ -11,7 +11,6 @@ import java.util.List;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.api.API;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException; import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException;
import fr.xephi.authme.settings.PlayersLogs; import fr.xephi.authme.settings.PlayersLogs;
@ -569,4 +568,38 @@ public class SQLiteThread extends Thread implements DataSource {
} }
return; return;
} }
@Override
public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<PlayerAuth>();
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = con.prepareStatement("SELECT * FROM " + tableName + ";");
rs = pst.executeQuery();
while (rs.next()) {
PlayerAuth pAuth = null;
if (rs.getString(columnIp).isEmpty()) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail));
} else {
if (!columnSalt.isEmpty()) {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getInt(columnGroup), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail));
} else {
pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail));
}
}
if (pAuth != null)
auths.add(pAuth);
}
} catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
return auths;
} catch (TimeoutException ex) {
ConsoleLogger.showError(ex.getMessage());
return auths;
} finally {
close(pst);
}
return auths;
}
} }