Actually make scripts run. MASSIVE FIXME: DOES NOT RUN IN ECLIPSE
parent
f65083ff23
commit
2b1ccebc35
83
pom.xml
83
pom.xml
|
@ -162,6 +162,84 @@
|
|||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>1.4</version>
|
||||
|
||||
<executions>
|
||||
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<artifactSet>
|
||||
<!-- signed jars -->
|
||||
<excludes>
|
||||
<exclude>bouncycastle:bcprov-jdk15</exclude>
|
||||
</excludes>
|
||||
</artifactSet>
|
||||
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/*.SF</exclude>
|
||||
<exclude>META-INF/*.DSA</exclude>
|
||||
<exclude>META-INF/*.RSA</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<!-- Main class -->
|
||||
<mainClass>net.mosstest.servercore.MossWorld</mainClass>
|
||||
</transformer>
|
||||
<!-- Use resource transformers to prevent file overwrites -->
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>properties.properties</resource>
|
||||
</transformer>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
|
||||
<resource>applicationContext.xml</resource>
|
||||
</transformer>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>META-INF/cxf/cxf.extension</resource>
|
||||
</transformer>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.XmlAppendingTransformer">
|
||||
<resource>META-INF/cxf/bus-extensions.xml</resource>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||
<overWriteReleases>false</overWriteReleases>
|
||||
<overWriteSnapshots>false</overWriteSnapshots>
|
||||
<overWriteIfNewer>true</overWriteIfNewer>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
@ -294,6 +372,11 @@
|
|||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -1,38 +1,9 @@
|
|||
package net.mosstest.scripting;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
* The Class Entity.
|
||||
*/
|
||||
public class Entity {
|
||||
|
||||
/** The id. */
|
||||
int id;
|
||||
|
||||
/** The name. */
|
||||
public String name;
|
||||
|
||||
/** The hp. */
|
||||
int hp;
|
||||
|
||||
/** The max health. */
|
||||
int maxHealth;
|
||||
|
||||
/**
|
||||
* Destroy.
|
||||
*/
|
||||
public void destroy() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new entity.
|
||||
*
|
||||
* @param name the name
|
||||
* @param maxHealth the max health
|
||||
*/
|
||||
protected Entity(String name, int maxHealth) {
|
||||
//TODO DB lookup for entities
|
||||
}
|
||||
}
|
||||
package net.mosstest.scripting;
|
||||
|
||||
public class Entity {
|
||||
public final EntityDefinition def;
|
||||
|
||||
public Entity(EntityDefinition def) {
|
||||
this.def = def;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package net.mosstest.scripting;
|
||||
|
||||
|
||||
public class EntityDefinition {
|
||||
String name;
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package net.mosstest.scripting;
|
||||
|
||||
import net.mosstest.servercore.MossFile;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
|
|
|
@ -267,6 +267,7 @@ public class MossScriptEnv {
|
|||
* myscript:specialdirt. This element must already exist.
|
||||
*/
|
||||
public void registerNodeAlias(String alias, String dst) {
|
||||
System.err.println("<><><><<<><><><><><><<><><");
|
||||
this.nm.putNodeAlias(alias, dst);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package net.mosstest.scripting;
|
||||
|
||||
import net.mosstest.servercore.MossFile;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
/**
|
||||
* The Class GameMesh.
|
||||
*/
|
||||
public class GameMesh {
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
import com.jme3.asset.AssetLocator;
|
||||
|
||||
public interface IFileManager {
|
||||
public IMossFile getFile(String name) throws FileNotFoundException, IOException;
|
||||
|
||||
public void registerFile(String name, String sha256, int size, long version);
|
||||
|
||||
public void receiveFileChunk(String sha512, int chunkId, ByteBuffer buf);
|
||||
|
||||
public Class<? extends AssetLocator> getAssetLocatorClass();
|
||||
|
||||
public List<IMossFile> getFiles();
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
|
||||
public interface IMossFile {
|
||||
long CHUNK_SIZE = 65536;
|
||||
|
||||
public Reader getReader() throws FileNotFoundException;
|
||||
|
||||
public InputStream getInputStream() throws FileNotFoundException;
|
||||
|
||||
public byte[] readChunk(int chunkId) throws IOException;
|
||||
|
||||
public void writeChunk(int chunkId, byte[] buf) throws IOException;
|
||||
|
||||
public String getSha256();
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.jme3.asset.AssetInfo;
|
||||
import com.jme3.asset.AssetKey;
|
||||
import com.jme3.asset.AssetLoadException;
|
||||
import com.jme3.asset.AssetLocator;
|
||||
import com.jme3.asset.AssetManager;
|
||||
|
||||
public class LocalAssetLocator implements AssetLocator {
|
||||
private LocalFileManager lfm;
|
||||
|
||||
@Override
|
||||
public AssetInfo locate(AssetManager manager, AssetKey key) {
|
||||
if (this.lfm == null)
|
||||
this.lfm = LocalFileManager.getFileManager("scripts");
|
||||
if (this.lfm == null)
|
||||
this.lfm = LocalFileManager.scriptsInstance;
|
||||
try {
|
||||
return new LocalAssetInfo(manager, key,
|
||||
this.lfm.getFile(key.getName()));
|
||||
} catch (IOException ex) {
|
||||
throw new AssetLoadException("Failed to open file: "
|
||||
+ key.getName(), ex);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRootPath(String arg0) {
|
||||
this.lfm = LocalFileManager.getFileManager("scripts");
|
||||
}
|
||||
|
||||
private static class LocalAssetInfo extends AssetInfo {
|
||||
|
||||
private LocalFile file;
|
||||
|
||||
public LocalAssetInfo(AssetManager manager, AssetKey key, LocalFile file) {
|
||||
super(manager, key);
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream openStream() {
|
||||
try {
|
||||
return this.file.getInputStream();
|
||||
} catch (IOException ex) {
|
||||
throw new AssetLoadException("Failed to open file: "
|
||||
+ this.file.getFilename(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.Reader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class LocalFile implements IMossFile {
|
||||
|
||||
public String getFilename() {
|
||||
return f.getAbsolutePath();
|
||||
}
|
||||
|
||||
static Logger logger = Logger.getLogger(LocalFile.class);
|
||||
private final File f;
|
||||
private final RandomAccessFile rFile;
|
||||
private final int numChunks;
|
||||
private final long length;
|
||||
private final String sha256;
|
||||
|
||||
public LocalFile(File f) throws IOException {
|
||||
if (!f.canRead())
|
||||
throw new FileNotFoundException(
|
||||
"File not existent or cannot be read");
|
||||
this.f = f;
|
||||
this.rFile = new RandomAccessFile(f, "r");
|
||||
this.length = this.rFile.length();
|
||||
this.numChunks = (int) Math.ceil(this.length
|
||||
/ ((double) IMossFile.CHUNK_SIZE));
|
||||
try {
|
||||
this.sha256 = LocalFileManager.getHash(f);
|
||||
logger.info("Hashed " + f.getAbsolutePath() + " as " + this.sha256);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
logger.error("Could not find algorithm SHA-256 while hashing " + f.getAbsolutePath());
|
||||
throw new IOException("Hashing failed while preparing file");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Reader getReader() throws FileNotFoundException {
|
||||
return new FileReader(this.f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws FileNotFoundException {
|
||||
return new FileInputStream(this.f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] readChunk(int chunkId) throws IOException {
|
||||
this.rFile.seek(chunkId * IMossFile.CHUNK_SIZE);
|
||||
byte[] buf = new byte[(int) (chunkId == (this.numChunks - 1) ? (this.length % IMossFile.CHUNK_SIZE)
|
||||
: IMossFile.CHUNK_SIZE)];
|
||||
this.rFile.readFully(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeChunk(int chunkId, byte[] buf) throws IOException {
|
||||
throw new IOException(
|
||||
"An attempt was made to write to a read-only local file");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSha256() {
|
||||
return this.sha256;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang.NotImplementedException;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.jme3.asset.AssetLocator;
|
||||
|
||||
public class LocalFileManager implements IFileManager {
|
||||
|
||||
public static final LocalFileManager scriptsInstance;
|
||||
private static HashMap<String, LocalFileManager> managers = new HashMap<>();
|
||||
static {
|
||||
scriptsInstance = new LocalFileManager(
|
||||
new File("data/scripts"));
|
||||
managers.put("scripts", scriptsInstance);
|
||||
}
|
||||
|
||||
private ArrayList<LocalFile> files;
|
||||
|
||||
public static LocalFileManager getFileManager(String key) {
|
||||
return managers.get(key);
|
||||
}
|
||||
|
||||
private final File basedir;
|
||||
|
||||
static Logger logger = Logger.getLogger(LocalFileManager.class);
|
||||
|
||||
@Override
|
||||
public LocalFile getFile(String name) throws IOException {
|
||||
String normalized = FilenameUtils.normalize(name);
|
||||
if (normalized == null) {
|
||||
logger.warn("Failed to normalize game resource filename: " + name);
|
||||
|
||||
throw new FileNotFoundException("The filename " + name
|
||||
+ " could not be normalized.");
|
||||
}
|
||||
File f = new File(this.basedir, normalized);
|
||||
logger.info("Got local file " + name + " as " + f.getAbsolutePath());
|
||||
return new LocalFile(f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerFile(String name, String sha256, int size, long version)
|
||||
throws NotImplementedException {
|
||||
throw new NotImplementedException();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveFileChunk(String sha512, int chunkId, ByteBuffer buf) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends AssetLocator> getAssetLocatorClass() {
|
||||
return LocalAssetLocator.class;
|
||||
}
|
||||
|
||||
public LocalFileManager(File basedir) {
|
||||
this.basedir = basedir;
|
||||
}
|
||||
|
||||
public static String getHash(File f) throws IOException,
|
||||
NoSuchAlgorithmException {
|
||||
|
||||
MessageDigest md = null;
|
||||
FileInputStream fis = null;
|
||||
FileChannel fc = null;
|
||||
ByteBuffer bbf = null;
|
||||
StringBuilder hexString = null;
|
||||
|
||||
md = MessageDigest.getInstance("SHA-256");
|
||||
fis = new FileInputStream(f);
|
||||
fc = fis.getChannel();
|
||||
bbf = ByteBuffer.allocateDirect(8192);
|
||||
|
||||
int b;
|
||||
|
||||
b = fc.read(bbf);
|
||||
|
||||
while ((b != -1) && (b != 0)) {
|
||||
bbf.flip();
|
||||
|
||||
byte[] bytes = new byte[b];
|
||||
bbf.get(bytes);
|
||||
|
||||
md.update(bbf);
|
||||
|
||||
bbf.clear();
|
||||
b = fc.read(bbf);
|
||||
}
|
||||
|
||||
fis.close();
|
||||
|
||||
byte[] mdbytes = md.digest();
|
||||
|
||||
hexString = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < mdbytes.length; i++) {
|
||||
hexString.append(Integer.toHexString((0xFF & mdbytes[i])));
|
||||
}
|
||||
|
||||
return hexString.toString();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IMossFile> getFiles() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
* The Class MossFile.
|
||||
*/
|
||||
public abstract class MossFile {
|
||||
|
||||
/**
|
||||
* Get a local copy of the file. This may be either a direct local file or a
|
||||
* cached file.
|
||||
*
|
||||
* @return A valid RandomAccessFile.
|
||||
* @throws FileNotFoundException the file not found exception
|
||||
*/
|
||||
public abstract RandomAccessFile getRandAccessCopy() throws FileNotFoundException;
|
||||
|
||||
|
||||
/**
|
||||
* String denoting the containing directory for the pathname.
|
||||
*/
|
||||
public final String dirName;
|
||||
|
||||
/**
|
||||
* Read chunk.
|
||||
*
|
||||
* @param chk the chk
|
||||
* @return the byte[]
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public abstract byte[] readChunk(int chk) throws IOException;
|
||||
|
||||
/**
|
||||
* String denoting name of resource.
|
||||
*/
|
||||
public final String resourceName;
|
||||
|
||||
|
||||
/**
|
||||
* Gets the size.
|
||||
*
|
||||
* @return the size
|
||||
*/
|
||||
public abstract long getSize();
|
||||
|
||||
/**
|
||||
* Instantiates a new moss file.
|
||||
*
|
||||
* @param dirName the dir name
|
||||
* @param resourceName the resource name
|
||||
*/
|
||||
public MossFile(String dirName, String resourceName) {
|
||||
this.dirName=dirName;
|
||||
this.resourceName = resourceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the file.
|
||||
*
|
||||
* @return the file
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public abstract File getFile() throws IOException;
|
||||
|
||||
}
|
|
@ -2,25 +2,32 @@ package net.mosstest.servercore;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.configuration.ConfigurationException;
|
||||
import org.apache.commons.configuration.XMLConfiguration;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
* The Class MossGame.
|
||||
*/
|
||||
public class MossGame {
|
||||
|
||||
|
||||
/**
|
||||
* Instantiates a new moss game.
|
||||
*
|
||||
* @param name the name
|
||||
* @throws MossWorldLoadException the moss world load exception
|
||||
*
|
||||
* @param name
|
||||
* the name
|
||||
* @throws MossWorldLoadException
|
||||
* the moss world load exception
|
||||
* @throws IOException
|
||||
*/
|
||||
@SuppressWarnings("nls")
|
||||
public MossGame(String name) throws MossWorldLoadException {
|
||||
public MossGame(String name) throws MossWorldLoadException, IOException {
|
||||
this.baseDir = new File("data/games/" + name); //$NON-NLS-1$
|
||||
this.cfgFile = new File(this.baseDir, "game.xml"); //$NON-NLS-1$
|
||||
if (!(this.baseDir.isDirectory() && this.cfgFile.isFile())) {
|
||||
|
@ -37,32 +44,33 @@ public class MossGame {
|
|||
String[] scNames = this.gameCfg.getStringArray("plugin"); //$NON-NLS-1$
|
||||
for (String scName : scNames) {
|
||||
try {
|
||||
this.scripts.add(new MossScript(new MossLocalFile(new File("data/scripts/"), //$NON-NLS-1$
|
||||
scName, "init.js"))); //$NON-NLS-1$
|
||||
this.scripts.add(LocalFileManager.scriptsInstance
|
||||
.getFile(scName + "/init.js")); //$NON-NLS-1$
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new MossWorldLoadException(Messages.getString("MossGame.FILE_NOT_FOUND") + scName); //$NON-NLS-1$
|
||||
} // TODO directory structure and proper iteration
|
||||
throw new MossWorldLoadException(
|
||||
Messages.getString("MossGame.FILE_NOT_FOUND") + scName); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The base dir. */
|
||||
private File baseDir;
|
||||
|
||||
|
||||
/** The game cfg. */
|
||||
private XMLConfiguration gameCfg;
|
||||
|
||||
|
||||
/** The cfg file. */
|
||||
private File cfgFile;
|
||||
|
||||
|
||||
/** The scripts. */
|
||||
private ArrayList<MossScript> scripts;
|
||||
private ArrayList<IMossFile> scripts;
|
||||
|
||||
/**
|
||||
* Gets the scripts.
|
||||
*
|
||||
*
|
||||
* @return the scripts
|
||||
*/
|
||||
public ArrayList<MossScript> getScripts() {
|
||||
return this.scripts;
|
||||
public List<IMossFile> getScripts() {
|
||||
return ImmutableList.copyOf(this.scripts);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,77 +0,0 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
* The Class MossLocalFile.
|
||||
*/
|
||||
public class MossLocalFile extends MossFile {
|
||||
|
||||
/** The Constant CHUNK_LENGTH. */
|
||||
public static final int CHUNK_LENGTH = 65536;
|
||||
|
||||
/**
|
||||
* Instantiates a new moss local file.
|
||||
*
|
||||
* @param baseDir the base dir
|
||||
* @param dirName the dir name
|
||||
* @param resourceName the resource name
|
||||
* @throws FileNotFoundException the file not found exception
|
||||
*/
|
||||
public MossLocalFile(File baseDir, String dirName, String resourceName) throws FileNotFoundException {
|
||||
// super call to establish fields.
|
||||
super(dirName, resourceName);
|
||||
//ensure filename is valid.
|
||||
if(!(dirName.matches("[a-zA-Z0-9.]*")&&resourceName.matches("[a-zA-Z0-9.]*"))) throw new FileNotFoundException(); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
this.file = new File(baseDir, dirName);
|
||||
this.file = new File(this.file, resourceName);
|
||||
}
|
||||
|
||||
/** The file. */
|
||||
private File file;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#getRandAccessCopy()
|
||||
*/
|
||||
@Override
|
||||
public RandomAccessFile getRandAccessCopy() throws FileNotFoundException {
|
||||
return new RandomAccessFile(this.file, "r"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#readChunk(int)
|
||||
*/
|
||||
public byte[] readChunk(int chk) throws IOException {
|
||||
if ((chk < 0) || (chk > 65535))
|
||||
throw new IllegalArgumentException(
|
||||
"attempted to access a chunk with an invalid length"); //$NON-NLS-1$
|
||||
byte[] buf = new byte[CHUNK_LENGTH];
|
||||
RandomAccessFile rf = new RandomAccessFile(this.file, this.dirName);
|
||||
rf.seek(CHUNK_LENGTH * chk);
|
||||
rf.readFully(buf);
|
||||
rf.close();
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#getSize()
|
||||
*/
|
||||
@Override
|
||||
public long getSize() {
|
||||
|
||||
return this.file.length();
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#getFile()
|
||||
*/
|
||||
public File getFile() {
|
||||
return this.file;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,133 +0,0 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.util.BitSet;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
* The Class MossRemoteFile.
|
||||
*/
|
||||
public class MossRemoteFile extends MossFile {
|
||||
|
||||
/**
|
||||
* The Class IncompleteFileException.
|
||||
*/
|
||||
public class IncompleteFileException extends IOException {
|
||||
|
||||
|
||||
/** The Constant serialVersionUID. */
|
||||
private static final long serialVersionUID = -4932174729349395760L;
|
||||
|
||||
}
|
||||
|
||||
/** The Constant CHUNK_LENGTH. */
|
||||
public static final int CHUNK_LENGTH = 65536;
|
||||
|
||||
/** The num chunks. */
|
||||
public final int numChunks;
|
||||
|
||||
/** The length. */
|
||||
public final int length;
|
||||
|
||||
/** The chunks done. */
|
||||
private BitSet chunksDone;
|
||||
|
||||
/**
|
||||
* Instantiates a new moss remote file.
|
||||
*
|
||||
* @param cacheDir the cache dir
|
||||
* @param dirName the dir name
|
||||
* @param resourceName the resource name
|
||||
* @param length the length
|
||||
* @throws FileNotFoundException the file not found exception
|
||||
*/
|
||||
public MossRemoteFile(File cacheDir, String dirName, String resourceName,
|
||||
int length) throws FileNotFoundException {
|
||||
// super call to establish fields.
|
||||
super(dirName, resourceName);
|
||||
//ensure filename is valid.
|
||||
if(!(dirName.matches("[a-zA-Z0-9]*")&&resourceName.matches("[a-zA-Z0-9]*"))) throw new FileNotFoundException(); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
this.file = new File(cacheDir, dirName);
|
||||
this.file = new File(this.file, resourceName);
|
||||
this.length = length;
|
||||
this.numChunks = (length / 65536) + 1;
|
||||
this.chunksDone = new BitSet(this.numChunks);
|
||||
}
|
||||
|
||||
/** The file. */
|
||||
private File file;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#getRandAccessCopy()
|
||||
*/
|
||||
@Override
|
||||
public RandomAccessFile getRandAccessCopy() throws FileNotFoundException {
|
||||
return new RandomAccessFile(this.file, "rwd"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#readChunk(int)
|
||||
*/
|
||||
public byte[] readChunk(int chk) throws IOException {
|
||||
if ((chk < 0) || (chk > 65535))
|
||||
throw new IllegalArgumentException(
|
||||
"attempted to access a chunk with an invalid length"); //$NON-NLS-1$
|
||||
byte[] buf = new byte[CHUNK_LENGTH];
|
||||
RandomAccessFile rf = new RandomAccessFile(this.file, "r"); //$NON-NLS-1$
|
||||
rf.seek(CHUNK_LENGTH * chk);
|
||||
rf.readFully(buf);
|
||||
rf.close();
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write chunk.
|
||||
*
|
||||
* @param chk the chk
|
||||
* @param data the data
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public void writeChunk(int chk, byte[] data) throws IOException {
|
||||
// this will check if the chunk is the last one. If it is then it will
|
||||
// use only the correct number of bytes. Otherwise it will use
|
||||
// CHUNK_LENGTH.
|
||||
if (data.length != ((chk == this.numChunks - 1) ? this.length
|
||||
% CHUNK_LENGTH : CHUNK_LENGTH))
|
||||
throw new ArrayIndexOutOfBoundsException("Array is not 65536 bytes"); //$NON-NLS-1$
|
||||
RandomAccessFile rf = new RandomAccessFile(this.file, "rwd"); //$NON-NLS-1$
|
||||
rf.seek(chk*CHUNK_LENGTH);
|
||||
rf.write(data);
|
||||
this.chunksDone.set(chk, true);
|
||||
rf.close();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#getSize()
|
||||
*/
|
||||
@Override
|
||||
public long getSize() {
|
||||
|
||||
return this.file.length();
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.MossFile#getFile()
|
||||
*/
|
||||
public File getFile() throws IncompleteFileException {
|
||||
if(!this.isReady()) throw new IncompleteFileException();
|
||||
return this.file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if is ready.
|
||||
*
|
||||
* @return true, if is ready
|
||||
*/
|
||||
public boolean isReady() {
|
||||
return (this.chunksDone.cardinality()==this.numChunks);
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
* The Class MossScript.
|
||||
*/
|
||||
public class MossScript {
|
||||
|
||||
/** The file. */
|
||||
MossFile file;
|
||||
|
||||
/**
|
||||
* Instantiates a new moss script.
|
||||
*
|
||||
* @param file the file
|
||||
* @throws MossWorldLoadException the moss world load exception
|
||||
*/
|
||||
public MossScript(MossFile file) throws MossWorldLoadException {
|
||||
this.file = file;
|
||||
try {
|
||||
if (!this.file.getFile().isFile())
|
||||
throw new MossWorldLoadException(Messages.getString("MossScript.MSG_SCRIPT_NAMED_[NAME]") + file.dirName //$NON-NLS-1$
|
||||
+ "/" + file.resourceName + Messages.getString("MossScript.[NAME]_NOT_FOUND")); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
} catch (IOException e) {
|
||||
throw new MossWorldLoadException(Messages.getString("MossScript.MSG_IO_EXCEPTION") //$NON-NLS-1$
|
||||
+ file.dirName + "/" + file.resourceName); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reader.
|
||||
*
|
||||
* @return the reader
|
||||
* @throws MossWorldLoadException the moss world load exception
|
||||
*/
|
||||
Reader getReader() throws MossWorldLoadException {
|
||||
try {
|
||||
return new FileReader(this.file.getFile());
|
||||
} catch (IOException e) {
|
||||
// whoa there, something REALLY bad happened
|
||||
throw new MossWorldLoadException(
|
||||
Messages.getString("MossScript.MSG_OOPS")); //$NON-NLS-1$
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
/**
|
||||
* The Class MossSecurityManager.
|
||||
*/
|
||||
public class MossSecurityManager {
|
||||
//Static crap only, shall use DB4O.
|
||||
}
|
|
@ -2,10 +2,9 @@ package net.mosstest.servercore;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import net.mosstest.scripting.MapGenerators;
|
||||
import net.mosstest.scripting.MossEvent;
|
||||
import net.mosstest.scripting.MossScriptEnv;
|
||||
import net.mosstest.scripting.ScriptableDatabase;
|
||||
import net.mosstest.scripting.events.IMossEvent;
|
||||
|
@ -101,8 +100,8 @@ public class MossWorld {
|
|||
this.fp = new FuturesProcessor();
|
||||
this.mossEnv = new MossScriptEnv(this.sdb, this.nc, this.fp, this.nm);
|
||||
this.sEnv = new ScriptEnv(this.mossEnv);
|
||||
ArrayList<MossScript> scripts = this.game.getScripts();
|
||||
for (MossScript sc : scripts) {
|
||||
List<IMossFile> scripts = this.game.getScripts();
|
||||
for (IMossFile sc : scripts) {
|
||||
this.sEnv.runScript(sc);
|
||||
}
|
||||
this.evp = new EventProcessor(this.mossEnv, ThreadContext.CONTEXT_SCRIPT);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileDescriptor;
|
||||
import java.net.InetAddress;
|
||||
import java.security.Permission;
|
||||
|
@ -20,7 +21,7 @@ public class MosstestSecurityManager extends SecurityManager {
|
|||
*/
|
||||
@Override
|
||||
public void checkPermission(Permission perm) {
|
||||
System.err.println(perm.toString());
|
||||
logger.trace("Requested permssion " + perm);
|
||||
if (this.threadContext.get() != ThreadContext.CONTEXT_ENGINE) {
|
||||
|
||||
logger.warn("MosstestSecurityManager prevented the use of arbitrary permissions outside engine contexts.");
|
||||
|
@ -168,16 +169,16 @@ public class MosstestSecurityManager extends SecurityManager {
|
|||
*/
|
||||
@Override
|
||||
public void checkCreateClassLoader() {
|
||||
if (this.threadContext.get() != ThreadContext.CONTEXT_ENGINE) {
|
||||
//if (this.threadContext.get() != ThreadContext.CONTEXT_ENGINE) {
|
||||
|
||||
logger.warn("MosstestSecurityManager stopped an attempt to create a classloader");
|
||||
throw new SecurityException(
|
||||
"MosstestSecurityManager stopped an attempt to create a classloader");
|
||||
// logger.warn("MosstestSecurityManager stopped an attempt to create a classloader");
|
||||
// throw new SecurityException(
|
||||
// "MosstestSecurityManager stopped an attempt to create a classloader");
|
||||
|
||||
} else {
|
||||
super.checkCreateClassLoader();
|
||||
//} else {
|
||||
// super.checkCreateClassLoader();
|
||||
return;
|
||||
}
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
|
@ -202,7 +203,6 @@ public class MosstestSecurityManager extends SecurityManager {
|
|||
*/
|
||||
@Override
|
||||
public void checkAccess(ThreadGroup g) {
|
||||
System.err.println(this.threadContext.get());
|
||||
if (this.threadContext.get() != ThreadContext.CONTEXT_ENGINE) {
|
||||
|
||||
logger.warn("MosstestSecurityManager stopped an attempt to modify a ThreadGroup");
|
||||
|
@ -263,7 +263,7 @@ public class MosstestSecurityManager extends SecurityManager {
|
|||
public void checkRead(FileDescriptor fd) {
|
||||
if (this.threadContext.get() != ThreadContext.CONTEXT_ENGINE) {
|
||||
|
||||
logger.warn("MosstestSecurityManager stopped an attempt to read a file from non-core code");
|
||||
logger.warn("1MosstestSecurityManager stopped an attempt to read a file from non-core code " + fd.toString());
|
||||
throw new SecurityException(
|
||||
"MosstestSecurityManager stopped an attempt to read a file from non-core code");
|
||||
|
||||
|
@ -276,8 +276,7 @@ public class MosstestSecurityManager extends SecurityManager {
|
|||
@Override
|
||||
public void checkRead(String file) {
|
||||
if (this.threadContext.get() != ThreadContext.CONTEXT_ENGINE) {
|
||||
|
||||
logger.warn("MosstestSecurityManager stopped an attempt to read a file from non-core code");
|
||||
logger.warn("MosstestSecurityManager stopped an attempt to read a file from non-core code" + file);
|
||||
throw new SecurityException(
|
||||
"MosstestSecurityManager stopped an attempt to read a file from non-core code");
|
||||
|
||||
|
@ -291,7 +290,7 @@ public class MosstestSecurityManager extends SecurityManager {
|
|||
public void checkRead(String file, Object context) {
|
||||
if (this.threadContext.get() != ThreadContext.CONTEXT_ENGINE) {
|
||||
|
||||
logger.warn("MosstestSecurityManager stopped an attempt to read a file from non-core code");
|
||||
logger.warn("3MosstestSecurityManager stopped an attempt to read a file from non-core code: " + file);
|
||||
throw new SecurityException(
|
||||
"MosstestSecurityManager stopped an attempt to read a file from non-core code");
|
||||
|
||||
|
|
|
@ -3,16 +3,19 @@ package net.mosstest.servercore;
|
|||
import java.io.IOException;
|
||||
|
||||
import net.mosstest.scripting.MossScriptEnv;
|
||||
import net.mosstest.servercore.MosstestSecurityManager.ThreadContext;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.mozilla.javascript.ClassShutter;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.ContextFactory;
|
||||
import org.mozilla.javascript.GeneratedClassLoader;
|
||||
import org.mozilla.javascript.NativeJavaObject;
|
||||
import org.mozilla.javascript.RhinoException;
|
||||
import org.mozilla.javascript.Script;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
import org.mozilla.javascript.ScriptableObject;
|
||||
import org.mozilla.javascript.SecurityController;
|
||||
import org.mozilla.javascript.WrapFactory;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
|
@ -97,11 +100,16 @@ public class ScriptEnv {
|
|||
* result.
|
||||
* @throws MossWorldLoadException the moss world load exception
|
||||
*/
|
||||
public ScriptResult runScript(MossScript script)
|
||||
public ScriptResult runScript(IMossFile script)
|
||||
throws MossWorldLoadException {
|
||||
try {
|
||||
Script sc = this.cx.compileReader(script.getReader(),
|
||||
script.file.toString(), 0, null);
|
||||
script.toString(), 0, null);
|
||||
|
||||
Object lock = new Object();
|
||||
MosstestSecurityManager.instance.lock(lock, ThreadContext.CONTEXT_SCRIPT);
|
||||
sc.exec(this.cx, this.globalScope);
|
||||
MosstestSecurityManager.instance.unlock(lock);
|
||||
} catch (IOException e) {
|
||||
return ScriptResult.RESULT_ERROR;
|
||||
} catch (RhinoException e) {
|
||||
|
@ -111,40 +119,39 @@ public class ScriptEnv {
|
|||
}
|
||||
return ScriptResult.RESULT_EXECUTED;
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * A factory for creating SandboxWrap objects.
|
||||
// */
|
||||
// protected static class SandboxWrapFactory extends WrapFactory {
|
||||
//
|
||||
// /* (non-Javadoc)
|
||||
// * @see org.mozilla.javascript.WrapFactory#wrapAsJavaObject(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object, java.lang.Class)
|
||||
// */
|
||||
// @Override
|
||||
// public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
|
||||
// Object javaObject, Class<?> staticType) {
|
||||
// return new SandboxNativeJavaObject(scope, javaObject, staticType);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * A factory for creating SandboxContext objects.
|
||||
// */
|
||||
// protected static class SandboxContextFactory extends ContextFactory {
|
||||
//
|
||||
// /* (non-Javadoc)
|
||||
// * @see org.mozilla.javascript.ContextFactory#makeContext()
|
||||
// */
|
||||
// @Override
|
||||
// protected Context makeContext() {
|
||||
// Context cx = super.makeContext();
|
||||
// cx.setClassShutter(new ScriptClassShutter());
|
||||
// cx.setWrapFactory(new SandboxWrapFactory());
|
||||
// return cx;
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* A factory for creating SandboxWrap objects.
|
||||
*/
|
||||
protected static class SandboxWrapFactory extends WrapFactory {
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.mozilla.javascript.WrapFactory#wrapAsJavaObject(org.mozilla.javascript.Context, org.mozilla.javascript.Scriptable, java.lang.Object, java.lang.Class)
|
||||
*/
|
||||
@Override
|
||||
public Scriptable wrapAsJavaObject(Context cx, Scriptable scope,
|
||||
Object javaObject, Class<?> staticType) {
|
||||
return new SandboxNativeJavaObject(scope, javaObject, staticType);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for creating SandboxContext objects.
|
||||
*/
|
||||
protected static class SandboxContextFactory extends ContextFactory {
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.mozilla.javascript.ContextFactory#makeContext()
|
||||
*/
|
||||
@Override
|
||||
protected Context makeContext() {
|
||||
Context cx = super.makeContext();
|
||||
cx.setClassShutter(new ScriptClassShutter());
|
||||
cx.setWrapFactory(new SandboxWrapFactory());
|
||||
return cx;
|
||||
}
|
||||
}
|
||||
|
||||
/** The cx. */
|
||||
private Context cx;
|
||||
|
||||
/**
|
||||
|
@ -153,7 +160,8 @@ public class ScriptEnv {
|
|||
* @param ev the ev
|
||||
*/
|
||||
public ScriptEnv(MossScriptEnv ev) {
|
||||
ContextFactory.initGlobal(new SandboxContextFactory());
|
||||
//ContextFactory.initGlobal(new SandboxContextFactory());
|
||||
ContextFactory.getGlobal().initApplicationClassLoader(MossScriptEnv.class.getClassLoader());
|
||||
this.cx = ContextFactory.getGlobal().enterContext();
|
||||
this.globalScope = this.cx.initStandardObjects();
|
||||
this.globalScope.put("moss", this.globalScope, ev); //$NON-NLS-1$
|
||||
|
|
Loading…
Reference in New Issue