Keybindings can be saved and loaded, and work when Jar'd!

- trying to solve jar file reading issue
- learnt a lot about jars including their lack of file structure
- changed location of keybindings settings to outside of jar
- added new folder/file that is generated on first run

#story[1197]
This commit is contained in:
Jessica Syder
2017-09-04 12:30:01 +12:00
parent cfa6fc37e0
commit 85eb66d508
3 changed files with 32 additions and 49 deletions
@@ -3,7 +3,6 @@ package visualiser.gameController.Keys;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
@@ -71,16 +70,15 @@ public class KeyFactory {
*/
public void save(){
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(getClass().getResource(
"/visualiser/userSettings/keyFactory.xml")
.toURI().getPath())));
encoder.writeObject(this.keyState);
encoder.close();
// open the filestream and write to it
FileOutputStream fos = new FileOutputStream(
System.getProperty("user.dir")+
"/settings/keyBindings.xml");
XMLEncoder xmlEncoder = new XMLEncoder(fos);
xmlEncoder.writeObject(this.keyState);
xmlEncoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
@@ -89,18 +87,35 @@ public class KeyFactory {
*/
public void load(){
try {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream(getClass().getResource(
"/visualiser/userSettings/keyFactory.xml")
.toURI().getPath())));
// access settings folder, create if it doesn't exist
File settingsFolder = new File(
System.getProperty("user.dir")+"/settings");
if (!settingsFolder.exists()){
settingsFolder.mkdir();
}
// access keybindings xml file, create if it doesn't exist
File savedFile = new File(
settingsFolder+"/keyBindings.xml");
if (!savedFile.exists()){
savedFile.createNewFile();
FileOutputStream fos = new FileOutputStream(savedFile);
XMLEncoder xmlEncoder = new XMLEncoder(fos);
xmlEncoder.writeObject(this.keyState);
xmlEncoder.close();
}
// load the saved settings into the game
InputStream is = new FileInputStream(savedFile);
XMLDecoder xmlDecoder = new XMLDecoder(is);
Map<String, ControlKey> savedKeyState
= (Map<String, ControlKey>)decoder.readObject();
decoder.close();
= (Map<String, ControlKey>)xmlDecoder.readObject();
xmlDecoder.close();
this.keyState = savedKeyState;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
} catch (IOException e) {
e.printStackTrace();
}
}