|
|
|
@ -32,31 +32,48 @@ |
|
|
|
|
package com.jme3.gde.core.assets; |
|
|
|
|
|
|
|
|
|
import com.jme3.asset.AssetKey; |
|
|
|
|
import java.io.BufferedOutputStream; |
|
|
|
|
import java.io.BufferedInputStream; |
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Properties; |
|
|
|
|
import java.util.logging.Level; |
|
|
|
|
import java.util.logging.Logger; |
|
|
|
|
import org.openide.cookies.SaveCookie; |
|
|
|
|
import org.openide.filesystems.FileAlreadyLockedException; |
|
|
|
|
import org.openide.filesystems.FileLock; |
|
|
|
|
import org.openide.filesystems.FileObject; |
|
|
|
|
import org.openide.filesystems.FileUtil; |
|
|
|
|
import org.openide.util.Exceptions; |
|
|
|
|
import org.openide.util.Mutex; |
|
|
|
|
import org.openide.util.Mutex.Action; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Global object to access actual jME3 data within an AssetDataObject, available |
|
|
|
|
* through the Lookup of any AssetDataObject. AssetDataObjects that wish to use |
|
|
|
|
* |
|
|
|
|
* @author normenhansen |
|
|
|
|
*/ |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
public class AssetData extends Properties { |
|
|
|
|
public class AssetData { |
|
|
|
|
|
|
|
|
|
private static final Logger logger = Logger.getLogger(AssetData.class.getName()); |
|
|
|
|
private final Mutex propsMutex = new Mutex(); |
|
|
|
|
private final Properties props = new Properties(); |
|
|
|
|
private AssetDataObject file; |
|
|
|
|
private String extension = "jmpdata"; |
|
|
|
|
private Date lastLoaded; |
|
|
|
|
|
|
|
|
|
public AssetData(AssetDataObject file) { |
|
|
|
|
this.file = file; |
|
|
|
|
FileObject primaryFile = file.getPrimaryFile(); |
|
|
|
|
if (primaryFile != null) { |
|
|
|
|
extension = primaryFile.getExt() + "data"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public AssetData(AssetDataObject file, String extension) { |
|
|
|
@ -64,6 +81,10 @@ public class AssetData extends Properties { |
|
|
|
|
this.extension = extension; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setExtension(String extension) { |
|
|
|
|
this.extension = extension; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public AssetKey<?> getAssetKey() { |
|
|
|
|
return file.getAssetKey(); |
|
|
|
|
} |
|
|
|
@ -104,41 +125,61 @@ public class AssetData extends Properties { |
|
|
|
|
return file.getFailedList(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public synchronized String getProperty(String key) { |
|
|
|
|
return super.getProperty(key); |
|
|
|
|
public synchronized String getProperty(final String key) { |
|
|
|
|
return propsMutex.readAccess(new Action<String>() { |
|
|
|
|
public String run() { |
|
|
|
|
readProperties(); |
|
|
|
|
return props.getProperty(key); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public synchronized String getProperty(String key, String defaultValue) { |
|
|
|
|
// loadProperties();
|
|
|
|
|
return super.getProperty(key, defaultValue); |
|
|
|
|
public synchronized String getProperty(final String key, final String defaultValue) { |
|
|
|
|
return propsMutex.readAccess(new Action<String>() { |
|
|
|
|
public String run() { |
|
|
|
|
readProperties(); |
|
|
|
|
return props.getProperty(key, defaultValue); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public synchronized Object setProperty(String key, String value) { |
|
|
|
|
Object obj= super.setProperty(key, value); |
|
|
|
|
// try {
|
|
|
|
|
// saveProperties();
|
|
|
|
|
// } catch (FileAlreadyLockedException ex) {
|
|
|
|
|
// Exceptions.printStackTrace(ex);
|
|
|
|
|
// } catch (IOException ex) {
|
|
|
|
|
// Exceptions.printStackTrace(ex);
|
|
|
|
|
// }
|
|
|
|
|
return obj; |
|
|
|
|
public synchronized String setProperty(final String key, final String value) { |
|
|
|
|
return propsMutex.writeAccess(new Action<String>() { |
|
|
|
|
public String run() { |
|
|
|
|
String ret = (String) props.setProperty(key, value); |
|
|
|
|
readProperties(); |
|
|
|
|
writeProperties(); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Deprecated |
|
|
|
|
public void loadProperties() { |
|
|
|
|
clear(); |
|
|
|
|
FileObject myFile = FileUtil.findBrother(file.getPrimaryFile(), extension); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Deprecated |
|
|
|
|
public void saveProperties() throws FileAlreadyLockedException, IOException { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void readProperties() { |
|
|
|
|
propsMutex.readAccess(new Runnable() { |
|
|
|
|
public void run() { |
|
|
|
|
final FileObject myFile = FileUtil.findBrother(file.getPrimaryFile(), extension); |
|
|
|
|
if (myFile == null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
final Date lastMod = myFile.lastModified(); |
|
|
|
|
if (!lastMod.equals(lastLoaded)) { |
|
|
|
|
propsMutex.writeAccess(new Runnable() { |
|
|
|
|
public void run() { |
|
|
|
|
props.clear(); |
|
|
|
|
lastLoaded = lastMod; |
|
|
|
|
InputStream in = null; |
|
|
|
|
try { |
|
|
|
|
in = myFile.getInputStream(); |
|
|
|
|
in = new BufferedInputStream(myFile.getInputStream()); |
|
|
|
|
try { |
|
|
|
|
load(in); |
|
|
|
|
props.load(in); |
|
|
|
|
} catch (IOException ex) { |
|
|
|
|
Exceptions.printStackTrace(ex); |
|
|
|
|
} |
|
|
|
@ -151,9 +192,19 @@ public class AssetData extends Properties { |
|
|
|
|
Exceptions.printStackTrace(ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
logger.log(Level.INFO, "Read AssetData properties for {0}", file); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void saveProperties() throws FileAlreadyLockedException, IOException { |
|
|
|
|
private void writeProperties() { |
|
|
|
|
//writeAccess because we write lastMod date, not because we write to the file
|
|
|
|
|
//the mutex protects the properties object, not the file
|
|
|
|
|
propsMutex.writeAccess(new Runnable() { |
|
|
|
|
public void run() { |
|
|
|
|
OutputStream out = null; |
|
|
|
|
FileLock lock = null; |
|
|
|
|
try { |
|
|
|
@ -163,19 +214,26 @@ public class AssetData extends Properties { |
|
|
|
|
myFile = FileUtil.createData(pFile.getParent(), pFile.getName() + "." + extension); |
|
|
|
|
} |
|
|
|
|
lock = myFile.lock(); |
|
|
|
|
out = myFile.getOutputStream(lock); |
|
|
|
|
store(out, ""); |
|
|
|
|
out = new BufferedOutputStream(myFile.getOutputStream(lock)); |
|
|
|
|
props.store(out, ""); |
|
|
|
|
out.flush(); |
|
|
|
|
lastLoaded = myFile.lastModified(); |
|
|
|
|
logger.log(Level.INFO, "Written AssetData properties for {0}", file); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
Exceptions.printStackTrace(e); |
|
|
|
|
} finally { |
|
|
|
|
if (out != null) { |
|
|
|
|
try { |
|
|
|
|
out.close(); |
|
|
|
|
} catch (IOException ex) { |
|
|
|
|
Exceptions.printStackTrace(ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (lock != null) { |
|
|
|
|
lock.releaseLock(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setExtension(String extension) { |
|
|
|
|
this.extension = extension; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|