patternjavaMinor
Loading modifications at runtime
Viewed 0 times
runtimemodificationsloading
Problem
I'm working on a game engine among other things in Java, and would like to know how to optimise my code, as it is ridiculously ugly and bloated. I'm terrible with operating the
In essence, I have a folder hierarchy setup in the following manner:
If all of this works, then the mod is loaded into memory.
```
public static void loadModifications(String folder) { // TODO: Optimise modification loading.
File enclosingFolder = new File(folder);
if (enclosingFolder.exists()) {
for (File modFolder : enclosingFolder.listFiles()) {
if (modFolder.isDirectory()) {
File modConfig = new File(modFolder.getPath() + "/mod.cfg");
if (modConfig.exists()) {
String modName = (String) ConfigHandler.getPropertyValue(modConfig.getPath(), "mod_name");
String modMain = (String) ConfigHandler.getPropertyValue(modConfig.getPath(), "mod_main");
try {
if (modMain != null) {
try {
String className = modFolder.getPath().replaceFirst(enclosingFolder.getPath(), "");
className = className.substring(1, className.length());
className += "." + modMain;
className = className.replaceAll("/", ".");
Object modClass = Class.forName(className).newInstance();
if (modName != null) {
if (modClass
java.util.File and need my code to be reviewed by others.In essence, I have a folder hierarchy setup in the following manner:
- Custom folder which the user decides on. Variable is
folder.
- More folders. Each one corresponding to a mod.
- mod.cfg file. I do checks to see if it contains information too.
- Java class referred from the mod.cfg file, needs to exist AND be of a specific object, namely
Modification.
If all of this works, then the mod is loaded into memory.
```
public static void loadModifications(String folder) { // TODO: Optimise modification loading.
File enclosingFolder = new File(folder);
if (enclosingFolder.exists()) {
for (File modFolder : enclosingFolder.listFiles()) {
if (modFolder.isDirectory()) {
File modConfig = new File(modFolder.getPath() + "/mod.cfg");
if (modConfig.exists()) {
String modName = (String) ConfigHandler.getPropertyValue(modConfig.getPath(), "mod_name");
String modMain = (String) ConfigHandler.getPropertyValue(modConfig.getPath(), "mod_main");
try {
if (modMain != null) {
try {
String className = modFolder.getPath().replaceFirst(enclosingFolder.getPath(), "");
className = className.substring(1, className.length());
className += "." + modMain;
className = className.replaceAll("/", ".");
Object modClass = Class.forName(className).newInstance();
if (modName != null) {
if (modClass
Solution
1) Few tweaks on condition checking to improve performance a bit.
If modName is NULL, there is no point in loading the class details and creating its instance. This saves some time. Handle exceptions accordingly.
2) If you have access to Modification class and can create its instance using NEW operator, it is preferred over reflection as reflection takes more time.
This is my suggestion. Please try and let me know if you found any difference in performance.
if (modMain != null) {
try {
if (modName != null) {
String className = modFolder.getPath().replaceFirst(enclosingFolder.getPath(), "");
className = className.substring(1, className.length());
className += "." + modMain;
className = className.replaceAll("/", ".");
Object modClass = Class.forName(className).newInstance();
if (modClass instanceof Modification) {
Logger.log(LogLevel.MESSAGE, "Loading modification: '" + modClass.getClass().getName() + "' as '" + modName + "'");
((Modification) modClass).setConfigFile(modConfig.getPath());
((Modification) modClass).init();
Logger.log(LogLevel.MESSAGE, ((Modification) modClass).getInfo());
mods.put(modName, (Modification) modClass);
} else {
Logger.log(LogLevel.WARNING, "Modification: '" + modName + "' does not derive from abstract class: 'Modification', ignoring.");
}
} else {
Logger.log(LogLevel.WARNING, "'mod_name' undefined for: '" + modFolder.getPath() + "'");
}
} catch (InstantiationException | IllegalAccessException e) {
Logger.log(LogLevel.WARNING, "Error loading main class for modification '" + modName + "', ignoring.");
}
} else {
Logger.log(LogLevel.WARNING, "'mod_main' undefined for: '" + modFolder.getPath() + "', ignoring.");
}If modName is NULL, there is no point in loading the class details and creating its instance. This saves some time. Handle exceptions accordingly.
2) If you have access to Modification class and can create its instance using NEW operator, it is preferred over reflection as reflection takes more time.
This is my suggestion. Please try and let me know if you found any difference in performance.
Code Snippets
if (modMain != null) {
try {
if (modName != null) {
String className = modFolder.getPath().replaceFirst(enclosingFolder.getPath(), "");
className = className.substring(1, className.length());
className += "." + modMain;
className = className.replaceAll("/", ".");
Object modClass = Class.forName(className).newInstance();
if (modClass instanceof Modification) {
Logger.log(LogLevel.MESSAGE, "Loading modification: '" + modClass.getClass().getName() + "' as '" + modName + "'");
((Modification) modClass).setConfigFile(modConfig.getPath());
((Modification) modClass).init();
Logger.log(LogLevel.MESSAGE, ((Modification) modClass).getInfo());
mods.put(modName, (Modification) modClass);
} else {
Logger.log(LogLevel.WARNING, "Modification: '" + modName + "' does not derive from abstract class: 'Modification', ignoring.");
}
} else {
Logger.log(LogLevel.WARNING, "'mod_name' undefined for: '" + modFolder.getPath() + "'");
}
} catch (InstantiationException | IllegalAccessException e) {
Logger.log(LogLevel.WARNING, "Error loading main class for modification '" + modName + "', ignoring.");
}
} else {
Logger.log(LogLevel.WARNING, "'mod_main' undefined for: '" + modFolder.getPath() + "', ignoring.");
}Context
StackExchange Code Review Q#129006, answer score: 2
Revisions (0)
No revisions yet.