-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f1587d
commit 380fc18
Showing
12 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/ninjabrainbot/io/savestate/IFileAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ninjabrainbot.io.savestate; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
public interface IFileAccessor { | ||
|
||
ObjectOutputStream getObjectOutputStream() throws IOException; | ||
|
||
ObjectInputStream getObjectInputStream() throws IOException; | ||
|
||
void deleteFile(); | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/ninjabrainbot/io/savestate/TempFileAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ninjabrainbot.io.savestate; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
public class TempFileAccessor implements IFileAccessor { | ||
|
||
private final File file; | ||
|
||
public TempFileAccessor(String fileName) { | ||
file = new File(System.getProperty("java.io.tmpdir"), fileName); | ||
} | ||
|
||
public ObjectOutputStream getObjectOutputStream() throws IOException { | ||
FileOutputStream fileOutputStream = new FileOutputStream(file); | ||
return new ObjectOutputStream(fileOutputStream); | ||
} | ||
|
||
public ObjectInputStream getObjectInputStream() throws IOException { | ||
FileInputStream fileInputStream = new FileInputStream(file); | ||
return new ObjectInputStream(fileInputStream); | ||
} | ||
|
||
@Override | ||
public void deleteFile() { | ||
file.delete(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/ninjabrainbot/model/domainmodel/DomainModelImportExportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package ninjabrainbot.model.domainmodel; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
|
||
import ninjabrainbot.io.preferences.NinjabrainBotPreferences; | ||
import ninjabrainbot.io.savestate.IFileAccessor; | ||
import ninjabrainbot.util.Logger; | ||
|
||
public class DomainModelImportExportService { | ||
|
||
private final IDomainModel domainModel; | ||
private final IFileAccessor fileAccessor; | ||
private final NinjabrainBotPreferences preferences; | ||
|
||
public DomainModelImportExportService(IDomainModel domainModel, IFileAccessor fileAccessor, NinjabrainBotPreferences preferences) { | ||
this.domainModel = domainModel; | ||
this.fileAccessor = fileAccessor; | ||
this.preferences = preferences; | ||
} | ||
|
||
public void triggerDeserialization() { | ||
if (!preferences.saveState.get()) | ||
return; | ||
|
||
ObjectInputStream objectInputStream = null; | ||
try { | ||
objectInputStream = fileAccessor.getObjectInputStream(); | ||
} catch (IOException e) { | ||
Logger.log("Domain model deserialization failed, failed to read file: " + e); | ||
return; | ||
} | ||
|
||
try { | ||
domainModel.deserialize(objectInputStream); | ||
try { | ||
objectInputStream.close(); | ||
} catch (IOException ex) { | ||
Logger.log("Failed to close ObjectInputStream after successful deserialization: " + ex); | ||
} | ||
} catch (SerializationException e) { | ||
Logger.log("Domain model deserialization failed: " + e); | ||
try { | ||
objectInputStream.close(); | ||
fileAccessor.deleteFile(); | ||
} catch (IOException ex) { | ||
Logger.log("Failed to close ObjectInputStream after failed deserialization: " + ex); | ||
} | ||
if (!domainModel.isReset()) | ||
throw new RuntimeException("Domain model is not in the default state after failed deserialization, crashing the application to avoid an inconsistent data state."); | ||
} | ||
} | ||
|
||
private void triggerSerialization() { | ||
ObjectOutputStream objectOutputStream = null; | ||
try { | ||
objectOutputStream = fileAccessor.getObjectOutputStream(); | ||
} catch (IOException e) { | ||
Logger.log("Domain model serialization failed, failed to open file: " + e); | ||
return; | ||
} | ||
|
||
try { | ||
domainModel.serialize(objectOutputStream); | ||
try { | ||
objectOutputStream.flush(); | ||
objectOutputStream.close(); | ||
} catch (IOException ex) { | ||
Logger.log("Failed to close ObjectInputStream after successful serialization: " + ex); | ||
} | ||
} catch (SerializationException e) { | ||
Logger.log("Domain model serialization failed: " + e); | ||
try { | ||
objectOutputStream.flush(); | ||
objectOutputStream.close(); | ||
fileAccessor.deleteFile(); | ||
} catch (IOException ex) { | ||
Logger.log("Failed to close ObjectInputStream after failed serialization: " + ex); | ||
} | ||
} | ||
} | ||
|
||
public void onShutdown() { | ||
triggerSerialization(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.