Skip to content

Commit

Permalink
Fix for Wurm Unlimited update 2015-12-03. DbConnector class changed q…
Browse files Browse the repository at this point in the history
…uite a bit.
  • Loading branch information
Mthec committed Dec 7, 2015
1 parent a000754 commit 7273fbe
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<target name="jar">
<jar destfile="../out/jar/mods/deitymanager/deitymanager.jar">
<fileset dir="../out/production/Deity-Manager" includes="mod/" />
<zipfileset file="../out/production/Deity-Manager/com/wurmonline/server/DbConnector$WurmDatabaseSchema.class" prefix="mod/wurmonline/mods/deitymanager" />
</jar>
<copy file="deitymanager.properties" tofile="../out/jar/mods/deitymanager.properties" />
<copy file="locales/DeityManager.properties" tofile="../out/jar/locales/DeityManager.properties" />
Expand Down
31 changes: 31 additions & 0 deletions src/com/wurmonline/server/DbConnector$WurmDatabaseSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.wurmonline.server;

import java.lang.String;

public enum DbConnector$WurmDatabaseSchema {
CREATURES("WURMCREATURES"),
SPELLS("WURMSPELLS"),
DEITIES("WURMDEITIES"),
ECONOMY("WURMECONOMY"),
ITEMS("WURMITEMS"),
LOGIN("WURMLOGIN"),
LOGS("WURMLOGS"),
PLAYERS("WURMPLAYERS"),
TEMPLATES("WURMTEMPLATES"),
ZONES("WURMZONES"),
SITE("WURMSITE");

private final java.lang.String database;

private DbConnector$WurmDatabaseSchema(java.lang.String database) {
this.database = database;
}

public java.lang.String toString() {
return this.name();
}

public java.lang.String getDatabase() {
return this.database;
}
}
8 changes: 4 additions & 4 deletions src/mod/wurmonline/mods/deitymanager/DeityDBInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public static void loadAllData() {
deityCon = DbConnector.getDeityDbCon();
deityPS = deityCon.prepareStatement(GET_ALL_DEITIES);
drs = deityPS.executeQuery();
Method getSpellDbCon = ReflectionUtil.getMethod(DbConnector.class, "getSpellDbCon");
Method getSpellsDbCon = ReflectionUtil.getMethod(DbConnector.class, "getSpellsDbCon");
Spell[] allSpells = Spells.getAllSpells();
if (allSpells.length == 0) {
SpellGenerator.createSpells();
allSpells = Spells.getAllSpells();
}
spellCon = (Connection)getSpellDbCon.invoke(DbConnector.class);
spellCon = (Connection)getSpellsDbCon.invoke(DbConnector.class);
if (!spellCon.getMetaData().getTables(null, null, "DEITY_SPELLS", null).next()) {
Statement create = spellCon.createStatement();
create.executeUpdate("CREATE TABLE DEITY_SPELLS\n" +
Expand Down Expand Up @@ -139,8 +139,8 @@ public static void saveDeityData (DeityData deityData) throws SQLException {
deityPS.executeUpdate();

// Spells
Method getSpellDbCon = ReflectionUtil.getMethod(DbConnector.class, "getSpellDbCon");
spellCon = (Connection)getSpellDbCon.invoke(DbConnector.class);
Method getSpellsDbCon = ReflectionUtil.getMethod(DbConnector.class, "getSpellsDbCon");
spellCon = (Connection)getSpellsDbCon.invoke(DbConnector.class);
boolean autoCommit = spellCon.getAutoCommit();
spellCon.setAutoCommit(false);
spellPS = spellCon.prepareStatement(SAVE_DEITY_SPELL);
Expand Down
76 changes: 39 additions & 37 deletions src/mod/wurmonline/mods/deitymanager/DeityManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mod.wurmonline.mods.deitymanager;

import com.ibm.icu.text.MessageFormat;
import com.wurmonline.server.ServerDirInfo;
import com.wurmonline.server.deities.Deities;
import com.wurmonline.server.deities.Deity;
import com.wurmonline.server.spells.Spell;
Expand All @@ -16,10 +18,10 @@
import javafx.scene.layout.Region;
import javafx.util.Callback;
import javassist.*;
import com.ibm.icu.text.MessageFormat;
import mod.wurmonline.serverlauncher.gui.ServerGuiController;
import mod.wurmonline.serverlauncher.LocaleHelper;
import mod.wurmonline.serverlauncher.gui.ServerGuiController;
import org.gotti.wurmunlimited.modloader.ReflectionUtil;
import org.gotti.wurmunlimited.modloader.classhooks.HookManager;
import org.gotti.wurmunlimited.modloader.interfaces.PreInitable;
import org.gotti.wurmunlimited.modloader.interfaces.ServerStartedListener;
import org.gotti.wurmunlimited.modloader.interfaces.WurmMod;
Expand Down Expand Up @@ -68,7 +70,7 @@ public Region getRegion(ServerGuiController guiController) {

@Override
public void preInit() {
ClassPool pool = ClassPool.getDefault();
ClassPool pool = HookManager.getInstance().getClassPool();
try {

CtClass deity = pool.get("com.wurmonline.server.deities.Deity");
Expand Down Expand Up @@ -107,45 +109,43 @@ public void preInit() {

try {
CtClass dbConnector = pool.get("com.wurmonline.server.DbConnector");
CtField db = new CtField(pool.get("java.lang.String"), "spellDb", dbConnector);
db.setModifiers(Modifier.STATIC);
dbConnector.addField(db, CtField.Initializer.constant("WURMSPELLS"));
CtField con = new CtField(pool.get("java.sql.Connection"), "spellDbcon", dbConnector);
con.setModifiers(Modifier.STATIC);
dbConnector.addField(con, "null");
CtField lastUsed = new CtField(CtClass.longType, "spellLastUsed", dbConnector);
lastUsed.setModifiers(Modifier.STATIC);
dbConnector.addField(lastUsed, "System.currentTimeMillis()");

CtMethod method = CtNewMethod.make("public static final java.sql.Connection getSpellDbCon () throws java.sql.SQLException {" +
"if (!isInitialized()) {" +
" initialize();" +
"}" +
"spellDbcon = refreshDbConnection(spellDbcon, spellLastUsed, dbDriver, dbHost, spellDb, dbUser, dbPass, \"spellDbcon\");" +
"spellLastUsed = System.currentTimeMillis();" +
"return spellDbcon;}", dbConnector);
dbConnector.addMethod(method);
} catch (NotFoundException | CannotCompileException ex) {
logger.warning(messages.getString("dbconnector_error"));
ex.printStackTrace();
System.exit(-1);
}
pool.get("com.wurmonline.server.DbConnector$WurmDatabaseSchema").detach();
pool.makeClass(DeityManager.class.getResourceAsStream("DbConnector$WurmDatabaseSchema.class"));
dbConnector.rebuildClassFile();

try {
CtClass dbConnector = pool.get("com.wurmonline.server.DbConnector");
CtMethod closeAll = dbConnector.getDeclaredMethod("closeAll");
closeAll.insertAfter("{ attemptClose(spellDbcon, \"spellDbcon\"); spellDbcon = null; }");
dbConnector.getDeclaredMethod("initialize").insertAfter(
"final String dbUser = com.wurmonline.server.Constants.dbUser;" +
"final String dbPass = com.wurmonline.server.Constants.dbPass;" +
"String dbHost;" +
"String dbDriver;" +
"if(isSqlite()) {" +
" dbHost = com.wurmonline.server.Constants.dbHost;" +
" config.setJournalMode(org.sqlite.SQLiteConfig.JournalMode.WAL);" +
" config.setSynchronous(org.sqlite.SQLiteConfig.SynchronousMode.NORMAL);" +
" dbDriver = \"org.sqlite.JDBC\";" +
" } else {" +
" dbHost = com.wurmonline.server.Constants.dbHost + com.wurmonline.server.Constants.dbPort;" +
" dbDriver = com.wurmonline.server.Constants.dbDriver;" +
" }" +
"CONNECTORS.put(com.wurmonline.server.DbConnector.WurmDatabaseSchema.SPELLS, new com.wurmonline.server.DbConnector(" +
"dbDriver, dbHost, com.wurmonline.server.DbConnector.WurmDatabaseSchema.SPELLS.getDatabase(), dbUser, dbPass, \"spellsDbcon\"));");

CtMethod method = CtNewMethod.make("public static final java.sql.Connection getSpellsDbCon() throws java.sql.SQLException {" +
"return refreshConnectionForSchema(com.wurmonline.server.DbConnector.WurmDatabaseSchema.SPELLS);}", dbConnector);
dbConnector.addMethod(method);
dbConnector.writeFile();

} catch (NotFoundException | CannotCompileException | IOException ex) {
logger.warning(messages.getString("closeall_error"));
logger.warning(messages.getString("dbconnector_error"));
ex.printStackTrace();
System.exit(-1);
}
// TODO - Do I need to do getConnectionForSchema, WurmDatabaseSchema enum?
}

@Override
public void onServerStarted () {
ServerDirInfo.getFileDBPath();
try {
if (deities.length == 0) {
DeityDBInterface.loadAllData();
Expand Down Expand Up @@ -216,12 +216,14 @@ void populateDeitiesList () {
void deitiesListChanged () {
if (!rebuilding) {
DeityData selectedDeity = deitiesList.getSelectionModel().getSelectedItem();
lastSelectedDeity = selectedDeity.getNumber();
String name = selectedDeity.getName();
logger.info(MessageFormat.format(messages.getString("selecting"), name));
deityPropertySheet = new DeityPropertySheet(selectedDeity, Spells.getAllSpells(), controller.serverIsRunning());
deityProperties.setContent(deityPropertySheet);
deityPropertySheet.requestFocus();
if (selectedDeity != null) {
lastSelectedDeity = selectedDeity.getNumber();
String name = selectedDeity.getName();
logger.info(MessageFormat.format(messages.getString("selecting"), name));
deityPropertySheet = new DeityPropertySheet(selectedDeity, Spells.getAllSpells(), controller.serverIsRunning());
deityProperties.setContent(deityPropertySheet);
deityPropertySheet.requestFocus();
}
}
}

Expand Down

0 comments on commit 7273fbe

Please sign in to comment.