Begin using IntelliJ IDEA, update file manager to bypass loading files for a missing index file (while it should instead recursively search the directory for this)
parent
303bc1f834
commit
82b310c5e1
|
@ -31,3 +31,8 @@ src/git.properties
|
|||
/stacktraces
|
||||
/*.log
|
||||
*.log
|
||||
.idea
|
||||
/.idea
|
||||
/out
|
||||
/.idea/*
|
||||
/out/*
|
|
@ -1,10 +1,11 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import com.jme3.asset.AssetLocator;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang.NotImplementedException;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.security.MessageDigest;
|
||||
|
@ -12,147 +13,140 @@ import java.security.NoSuchAlgorithmException;
|
|||
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);
|
||||
}
|
||||
public static final LocalFileManager scriptsInstance;
|
||||
private static HashMap<String, LocalFileManager> managers = new HashMap<>();
|
||||
|
||||
private HashMap<String, LocalFile> files = new HashMap<>();
|
||||
static {
|
||||
scriptsInstance = new LocalFileManager(new File("data/scripts"));
|
||||
managers.put("scripts", scriptsInstance);
|
||||
}
|
||||
|
||||
public static LocalFileManager getFileManager(String key) {
|
||||
return managers.get(key);
|
||||
}
|
||||
private HashMap<String, LocalFile> files = new HashMap<>();
|
||||
|
||||
private final File basedir;
|
||||
public static LocalFileManager getFileManager(String key) {
|
||||
return managers.get(key);
|
||||
}
|
||||
|
||||
static Logger logger = Logger.getLogger(LocalFileManager.class);
|
||||
private final File basedir;
|
||||
|
||||
@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);
|
||||
static Logger logger = Logger.getLogger(LocalFileManager.class);
|
||||
|
||||
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());
|
||||
LocalFile lf = new LocalFile(f);
|
||||
this.files.put(name, lf);
|
||||
return lf;
|
||||
}
|
||||
@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);
|
||||
|
||||
@Override
|
||||
public void registerFile(String name, String sha256, int size, long version)
|
||||
throws NotImplementedException {
|
||||
throw new NotImplementedException();
|
||||
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());
|
||||
LocalFile lf = new LocalFile(f);
|
||||
this.files.put(name, lf);
|
||||
return lf;
|
||||
}
|
||||
|
||||
}
|
||||
@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;
|
||||
}
|
||||
@Override
|
||||
public void receiveFileChunk(String sha512, int chunkId, ByteBuffer buf) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public LocalFileManager(File basedir) {
|
||||
this.basedir = basedir;
|
||||
@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;
|
||||
public static String getHash(File f) throws IOException,
|
||||
NoSuchAlgorithmException {
|
||||
|
||||
md = MessageDigest.getInstance("SHA-256");
|
||||
fis = new FileInputStream(f);
|
||||
fc = fis.getChannel();
|
||||
bbf = ByteBuffer.allocateDirect(8192);
|
||||
|
||||
int b;
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
FileInputStream fis = new FileInputStream(f);
|
||||
FileChannel fc = fis.getChannel();
|
||||
ByteBuffer bbf = ByteBuffer.allocateDirect(8192);
|
||||
|
||||
b = fc.read(bbf);
|
||||
int bytesRead;
|
||||
|
||||
while ((b != -1) && (b != 0)) {
|
||||
bbf.flip();
|
||||
bytesRead = fc.read(bbf);
|
||||
|
||||
byte[] bytes = new byte[b];
|
||||
bbf.get(bytes);
|
||||
while ((bytesRead != -1) && (bytesRead != 0)) {
|
||||
bbf.flip();
|
||||
|
||||
md.update(bbf);
|
||||
byte[] bytes = new byte[bytesRead];
|
||||
bbf.get(bytes);
|
||||
|
||||
bbf.clear();
|
||||
b = fc.read(bbf);
|
||||
}
|
||||
md.update(bbf);
|
||||
|
||||
fis.close();
|
||||
bbf.clear();
|
||||
bytesRead = fc.read(bbf);
|
||||
}
|
||||
|
||||
byte[] mdbytes = md.digest();
|
||||
fis.close();
|
||||
|
||||
hexString = new StringBuilder();
|
||||
byte[] mdBytes = md.digest();
|
||||
|
||||
for (int i = 0; i < mdbytes.length; i++) {
|
||||
hexString.append(Integer.toHexString((0xFF & mdbytes[i])));
|
||||
}
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
|
||||
return hexString.toString();
|
||||
for (byte b : mdBytes) {
|
||||
hexString.append(Integer.toHexString((0xFF & b)));
|
||||
}
|
||||
|
||||
}
|
||||
return hexString.toString();
|
||||
|
||||
@Override
|
||||
public List<IMossFile> getFiles() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public IMossFile getScriptInitFile(String scName) throws IOException {
|
||||
String normalized = FilenameUtils.normalize(scName);
|
||||
if (normalized == null) {
|
||||
logger.warn("Failed to normalize game resource filename: " + scName);
|
||||
@Override
|
||||
public List<IMossFile> getFiles() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new FileNotFoundException("The filename " + scName
|
||||
+ " could not be normalized.");
|
||||
}
|
||||
LocalFile scriptFile = getFile(normalized + "init.js");
|
||||
LocalFile fileIndex = getFile(normalized + "index");
|
||||
BufferedReader idxR = new BufferedReader(fileIndex.getReader());
|
||||
String line = null;
|
||||
while ((line = idxR.readLine()) != null) {
|
||||
String normalizedLine = FilenameUtils.normalize(line.trim());
|
||||
if (normalizedLine == null) {
|
||||
logger.warn("Failed to normalize game resource filename from file index: "
|
||||
+ line);
|
||||
public IMossFile getScriptInitFile(String scName) throws IOException {
|
||||
String normalized = FilenameUtils.normalize(scName);
|
||||
if (normalized == null) {
|
||||
System.out.println("FOO");
|
||||
logger.warn("Failed to normalize game resource filename: " + scName);
|
||||
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
// side effect of registering the file in the map
|
||||
LocalFile listedFile = getFile(normalized + normalizedLine);
|
||||
} catch (IOException e) {
|
||||
logger.warn("File " + normalized + normalizedLine
|
||||
+ " does not exist but was listed in the index");
|
||||
}
|
||||
}
|
||||
return scriptFile;
|
||||
}
|
||||
throw new FileNotFoundException("The filename " + scName
|
||||
+ " could not be normalized.");
|
||||
}
|
||||
LocalFile scriptFile = getFile(normalized + "/init.js");
|
||||
try {
|
||||
LocalFile fileIndex = getFile(normalized + "/ignore");
|
||||
BufferedReader idxR = new BufferedReader(fileIndex.getReader());
|
||||
String line;
|
||||
while ((line = idxR.readLine()) != null) {
|
||||
String normalizedLine = FilenameUtils.normalize(line.trim());
|
||||
if (normalizedLine == null) {
|
||||
logger.warn("Failed to normalize game resource filename from file index: "
|
||||
+ line);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// side effect of registering the file in the map
|
||||
getFile(normalized + normalizedLine);
|
||||
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
logger.warn("No index file found; no files will be served to the client.");
|
||||
// TODO use directory listing in this case
|
||||
}
|
||||
return scriptFile;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
package net.mosstest.servercore;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.apache.commons.configuration.ConfigurationException;
|
||||
import org.apache.commons.configuration.XMLConfiguration;
|
||||
|
||||
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.
|
||||
|
@ -42,22 +41,26 @@ public class MossGame {
|
|||
}
|
||||
this.scripts = new ArrayList<>();
|
||||
String[] scNames = this.gameCfg.getStringArray("plugin"); //$NON-NLS-1$
|
||||
for (String scName : scNames) {
|
||||
try {
|
||||
this.scripts.add(LocalFileManager.scriptsInstance
|
||||
.getScriptInitFile(scName)); //$NON-NLS-1$
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new MossWorldLoadException(
|
||||
Messages.getString("MossGame.FILE_NOT_FOUND") + scName); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
for (String scName : scNames)
|
||||
try {
|
||||
this.scripts.add(LocalFileManager.scriptsInstance
|
||||
.getScriptInitFile(scName)); //$NON-NLS-1$
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
throw new MossWorldLoadException(
|
||||
Messages.getString("MossGame.FILE_NOT_FOUND") + scName); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/** The base dir. */
|
||||
private File baseDir;
|
||||
/**
|
||||
* The base dir.
|
||||
*/
|
||||
private File baseDir;
|
||||
|
||||
/** The game cfg. */
|
||||
private XMLConfiguration gameCfg;
|
||||
/**
|
||||
* The game cfg.
|
||||
*/
|
||||
private XMLConfiguration gameCfg;
|
||||
|
||||
/** The cfg file. */
|
||||
private File cfgFile;
|
||||
|
|
|
@ -1,66 +1,68 @@
|
|||
package net.mosstest.tests;
|
||||
|
||||
import net.mosstest.scripting.AirNodeParams;
|
||||
import net.mosstest.scripting.DefaultNodeParams;
|
||||
import net.mosstest.scripting.MapNode;
|
||||
import net.mosstest.servercore.INodeManager;
|
||||
import net.mosstest.servercore.MossWorldLoadException;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
|
||||
/**
|
||||
* The Class MockNodeManager.
|
||||
*/
|
||||
public class MockNodeManager implements INodeManager {
|
||||
|
||||
/** The Constant MOCK_SOLID_MAPNODE. */
|
||||
private static final MapNode MOCK_SOLID_MAPNODE = new MapNode(
|
||||
new DefaultNodeParams(), "mock", "!mock:mock", "Mock node", 1);
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#getNode(short)
|
||||
*/
|
||||
@Override
|
||||
public MapNode getNode(short nodeId) {
|
||||
return MOCK_SOLID_MAPNODE;
|
||||
}
|
||||
/**
|
||||
* The Constant MOCK_SOLID_MAPNODE.
|
||||
*/
|
||||
private static final MapNode MOCK_SOLID_MAPNODE = new MapNode(
|
||||
new DefaultNodeParams(), "mock", "!mock:mock", "Mock node", 1);
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#putNode(net.mosstest.scripting.MapNode)
|
||||
*/
|
||||
@Override
|
||||
public short putNode(MapNode node) throws MossWorldLoadException {
|
||||
// TODO Auto-generated method stub
|
||||
return 1337;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#getNode(short)
|
||||
*/
|
||||
@Override
|
||||
public MapNode getNode(short nodeId) {
|
||||
return MOCK_SOLID_MAPNODE;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#putNodeAlias(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public void putNodeAlias(String alias, String dst) {
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#putNode(net.mosstest.scripting.MapNode)
|
||||
*/
|
||||
@Override
|
||||
public short putNode(MapNode node) throws MossWorldLoadException {
|
||||
// TODO Auto-generated method stub
|
||||
return 1337;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#getNode(java.lang.String, boolean)
|
||||
*/
|
||||
@Override
|
||||
public MapNode getNode(String string, boolean isModified) {
|
||||
return MOCK_SOLID_MAPNODE;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#putNodeAlias(java.lang.String, java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public void putNodeAlias(String alias, String dst) {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#getNode(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public MapNode getNode(String string) {
|
||||
return MOCK_SOLID_MAPNODE;
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#getNode(java.lang.String, boolean)
|
||||
*/
|
||||
@Override
|
||||
public MapNode getNode(String string, boolean isModified) {
|
||||
return MOCK_SOLID_MAPNODE;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#init()
|
||||
*/
|
||||
@Override
|
||||
public void init() {
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#getNode(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public MapNode getNode(String string) {
|
||||
return MOCK_SOLID_MAPNODE;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.mosstest.servercore.INodeManager#init()
|
||||
*/
|
||||
@Override
|
||||
public void init() {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue