mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-13 12:04:06 +09:00
added sources for Slick
Former-commit-id: 1647fa32ef6894bd7db44f741f07c2f4dcdf9054 Former-commit-id: 0e5810dcfbe1fd59b13e7cabe9f1e93c5542da2d
This commit is contained in:
74
lib/slick-source/org/newdawn/slick/muffin/FileMuffin.java
Normal file
74
lib/slick-source/org/newdawn/slick/muffin/FileMuffin.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package org.newdawn.slick.muffin;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.newdawn.slick.util.Log;
|
||||
|
||||
/**
|
||||
* An implementation of the muffin load/save mechanism based around using the
|
||||
* local file system.
|
||||
*
|
||||
* @author kappaOne
|
||||
*/
|
||||
public class FileMuffin implements Muffin {
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.muffin.Muffin#saveFile(java.util.HashMap,
|
||||
* java.lang.String)
|
||||
*/
|
||||
public void saveFile(HashMap scoreMap, String fileName) throws IOException {
|
||||
String userHome = System.getProperty("user.home");
|
||||
File file = new File(userHome);
|
||||
file = new File(file, ".java");
|
||||
if (!file.exists()) {
|
||||
file.mkdir();
|
||||
}
|
||||
|
||||
file = new File(file, fileName);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
||||
|
||||
// save hashMap
|
||||
oos.writeObject(scoreMap);
|
||||
|
||||
oos.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.muffin.Muffin#loadFile(java.lang.String)
|
||||
*/
|
||||
public HashMap loadFile(String fileName) throws IOException {
|
||||
HashMap hashMap = new HashMap();
|
||||
String userHome = System.getProperty("user.home");
|
||||
|
||||
File file = new File(userHome);
|
||||
file = new File(file, ".java");
|
||||
file = new File(file, fileName);
|
||||
|
||||
if (file.exists()) {
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
ObjectInputStream ois = new ObjectInputStream(fis);
|
||||
|
||||
hashMap = (HashMap) ois.readObject();
|
||||
|
||||
ois.close();
|
||||
|
||||
} catch (EOFException e) {
|
||||
// End of the file reached, do nothing
|
||||
} catch (ClassNotFoundException e) {
|
||||
Log.error(e);
|
||||
throw new IOException("Failed to pull state from store - class not found");
|
||||
}
|
||||
}
|
||||
|
||||
return hashMap;
|
||||
}
|
||||
}
|
||||
29
lib/slick-source/org/newdawn/slick/muffin/Muffin.java
Normal file
29
lib/slick-source/org/newdawn/slick/muffin/Muffin.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package org.newdawn.slick.muffin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* A description of any class with the ability to store state locally
|
||||
*
|
||||
* @author kappaOne
|
||||
*/
|
||||
public interface Muffin {
|
||||
/**
|
||||
* Save a file of data
|
||||
*
|
||||
* @param data The data to store
|
||||
* @param fileName The name of the file to store it against
|
||||
* @throws IOException Indicates a failure to save the state
|
||||
*/
|
||||
public abstract void saveFile(HashMap data, String fileName) throws IOException;
|
||||
|
||||
/**
|
||||
* Load a file of data from the store
|
||||
*
|
||||
* @param fileName The name of the file to retrieve
|
||||
* @return The data retrieved
|
||||
* @throws IOException Indicates a failure to load the state
|
||||
*/
|
||||
public abstract HashMap loadFile(String fileName) throws IOException;
|
||||
}
|
||||
138
lib/slick-source/org/newdawn/slick/muffin/WebstartMuffin.java
Normal file
138
lib/slick-source/org/newdawn/slick/muffin/WebstartMuffin.java
Normal file
@@ -0,0 +1,138 @@
|
||||
package org.newdawn.slick.muffin;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.jnlp.BasicService;
|
||||
import javax.jnlp.FileContents;
|
||||
import javax.jnlp.PersistenceService;
|
||||
import javax.jnlp.ServiceManager;
|
||||
|
||||
import org.newdawn.slick.util.Log;
|
||||
|
||||
/**
|
||||
* A muffin load/save implementation based on using Webstart Muffins (a bit like cookies only
|
||||
* for webstart)
|
||||
*
|
||||
* @author kappaOne
|
||||
*/
|
||||
public class WebstartMuffin implements Muffin {
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.muffin.Muffin#saveFile(java.util.HashMap, java.lang.String)
|
||||
*/
|
||||
public void saveFile(HashMap scoreMap, String fileName) throws IOException {
|
||||
|
||||
PersistenceService ps;
|
||||
BasicService bs;
|
||||
URL configURL;
|
||||
|
||||
try {
|
||||
ps = (PersistenceService) ServiceManager
|
||||
.lookup("javax.jnlp.PersistenceService");
|
||||
bs = (BasicService) ServiceManager
|
||||
.lookup("javax.jnlp.BasicService");
|
||||
URL baseURL = bs.getCodeBase();
|
||||
// System.out.println("CodeBase was " + baseURL);
|
||||
configURL = new URL(baseURL, fileName);
|
||||
} catch (Exception e) {
|
||||
Log.error(e);
|
||||
throw new IOException("Failed to save state: ");
|
||||
}
|
||||
|
||||
try {
|
||||
ps.delete(configURL);
|
||||
} catch (Exception e) {
|
||||
Log.info("No exisiting Muffin Found - First Save");
|
||||
}
|
||||
|
||||
try {
|
||||
ps.create(configURL, 1024); // 1024 bytes for our data
|
||||
|
||||
FileContents fc = ps.get(configURL);
|
||||
DataOutputStream oos = new DataOutputStream(fc
|
||||
.getOutputStream(false));
|
||||
|
||||
// scroll through hashMap and write key and value to file
|
||||
Set keys = scoreMap.keySet(); // get the keys
|
||||
|
||||
// get values using keys
|
||||
for (Iterator i = keys.iterator(); i.hasNext();) {
|
||||
String key = (String) i.next();
|
||||
|
||||
oos.writeUTF(key);
|
||||
|
||||
if (fileName.endsWith("Number")) {
|
||||
double value = ((Double) scoreMap.get(key)).doubleValue();
|
||||
oos.writeDouble(value);
|
||||
} else {
|
||||
String value = (String) scoreMap.get(key);
|
||||
oos.writeUTF(value);
|
||||
}
|
||||
}
|
||||
|
||||
oos.flush();
|
||||
oos.close();
|
||||
} catch (Exception e) {
|
||||
Log.error(e);
|
||||
throw new IOException("Failed to store map of state data");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.newdawn.slick.muffin.Muffin#loadFile(java.lang.String)
|
||||
*/
|
||||
public HashMap loadFile(String fileName) throws IOException {
|
||||
HashMap hashMap = new HashMap();
|
||||
|
||||
try {
|
||||
PersistenceService ps = (PersistenceService) ServiceManager
|
||||
.lookup("javax.jnlp.PersistenceService");
|
||||
BasicService bs = (BasicService) ServiceManager
|
||||
.lookup("javax.jnlp.BasicService");
|
||||
URL baseURL = bs.getCodeBase();
|
||||
URL configURL = new URL(baseURL, fileName);
|
||||
FileContents fc = ps.get(configURL);
|
||||
DataInputStream ois = new DataInputStream(fc.getInputStream());
|
||||
|
||||
// read in data from muffin
|
||||
String key;
|
||||
|
||||
// load hashMap as <String, Int> or <String, String>
|
||||
if (fileName.endsWith("Number")) {
|
||||
double value;
|
||||
// while not end of file
|
||||
while ((key = ois.readUTF()) != null) {
|
||||
value = ois.readDouble();
|
||||
// load value into hashMap
|
||||
hashMap.put(key, new Double(value));
|
||||
}
|
||||
} else {
|
||||
String value;
|
||||
// while not end of file
|
||||
while ((key = ois.readUTF()) != null) {
|
||||
value = ois.readUTF();
|
||||
// load value into hashMap
|
||||
hashMap.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
ois.close();
|
||||
} catch (EOFException e) {
|
||||
// End of the file reached, do nothing
|
||||
} catch (IOException e) {
|
||||
// No data there - thats ok, just not saved before
|
||||
} catch (Exception e) {
|
||||
Log.error(e);
|
||||
throw new IOException("Failed to load state from webstart muffin");
|
||||
}
|
||||
|
||||
return hashMap;
|
||||
}
|
||||
}
|
||||
3
lib/slick-source/org/newdawn/slick/muffin/package.html
Normal file
3
lib/slick-source/org/newdawn/slick/muffin/package.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<BODY>
|
||||
Muffins for storing local data
|
||||
</BODY>
|
||||
Reference in New Issue
Block a user