init, thx MrExtremez

This commit is contained in:
Nicola Paolucci
2019-06-18 15:04:35 -04:00
commit ea51313125
2488 changed files with 150207 additions and 0 deletions
@@ -0,0 +1,40 @@
package org.apollo.jagcached.resource;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* A resource provider composed of multiple resource providers.
* @author Graham Edgecombe
*/
public final class CombinedResourceProvider extends ResourceProvider {
/**
* An array of resource providers.
*/
private final ResourceProvider[] providers;
/**
* Creates the combined resource providers.
* @param providers The providers this provider delegates to.
*/
public CombinedResourceProvider(ResourceProvider... providers) {
this.providers = providers;
}
@Override
public boolean accept(String path) throws IOException {
return true;
}
@Override
public ByteBuffer get(String path) throws IOException {
for (ResourceProvider provider : providers) {
if (provider.accept(path)) {
return provider.get(path);
}
}
return null;
}
}
@@ -0,0 +1,64 @@
package org.apollo.jagcached.resource;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel.MapMode;
/**
* A {@link ResourceProvider} which provides additional hypertext resources.
* @author Graham Edgecombe
*/
public final class HypertextResourceProvider extends ResourceProvider {
/**
* The base directory from which documents are served.
*/
private final File base;
/**
* Creates a new hypertext resource provider with the specified base
* directory.
* @param base The base directory.
*/
public HypertextResourceProvider(File base) {
this.base = base;
}
@Override
public boolean accept(String path) throws IOException {
File f = new File(base, path);
URI target = f.toURI().normalize();
if (target.toASCIIString().startsWith(base.toURI().normalize().toASCIIString())) {
if (f.isDirectory()) {
f = new File(f, "index.html");
}
return f.exists();
}
return false;
}
@Override
public ByteBuffer get(String path) throws IOException {
File f = new File(base, path);
if (f.isDirectory()) {
f = new File(f, "index.html");
}
if (!f.exists()) {
return null;
}
RandomAccessFile raf = new RandomAccessFile(f, "r");
ByteBuffer buf;
try {
buf = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
} finally {
raf.close();
}
return buf;
}
}
@@ -0,0 +1,30 @@
package org.apollo.jagcached.resource;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* A class which provides resources.
* @author Graham Edgecombe
*/
public abstract class ResourceProvider {
/**
* Checks that this provider can fulfil a request to the specified
* resource.
* @param path The path to the resource, e.g. {@code /crc}.
* @return {@code true} if the provider can fulfil a request to the
* resource, {@code false} otherwise.
* @throws IOException if an I/O error occurs.
*/
public abstract boolean accept(String path) throws IOException;
/**
* Gets a resource by its path.
* @param path The path.
* @return The resource, or {@code null} if it doesn't exist.
* @throws IOException if an I/O error occurs.
*/
public abstract ByteBuffer get(String path) throws IOException;
}
@@ -0,0 +1,70 @@
package org.apollo.jagcached.resource;
import java.io.IOException;
import java.nio.ByteBuffer;
import org.apollo.jagcached.fs.IndexedFileSystem;
/**
* A {@link ResourceProvider} which maps virtual resources (such as
* {@code /media}) to files in an {@link IndexedFileSystem}.
* @author Graham Edgecombe
*/
public final class VirtualResourceProvider extends ResourceProvider {
/**
* An array of valid prefixes.
*/
private static final String[] VALID_PREFIXES = {
"crc", "title", "config", "interface", "media", "versionlist",
"textures", "wordenc", "sounds"
};
/**
* The file system.
*/
private final IndexedFileSystem fs;
/**
* Creates a new virtual resource provider with the specified file system.
* @param fs The file system.
*/
public VirtualResourceProvider(IndexedFileSystem fs) {
this.fs = fs;
}
@Override
public boolean accept(String path) throws IOException {
for (String prefix : VALID_PREFIXES) {
if (path.startsWith("/" + prefix)) {
return true;
}
}
return false;
}
@Override
public ByteBuffer get(String path) throws IOException {
if (path.startsWith("/crc")) {
return fs.getCrcTable();
} else if (path.startsWith("/title")) {
return fs.getFile(0, 1);
} else if (path.startsWith("/config")) {
return fs.getFile(0, 2);
} else if (path.startsWith("/interface")) {
return fs.getFile(0, 3);
} else if (path.startsWith("/media")) {
return fs.getFile(0, 4);
} else if (path.startsWith("/versionlist")) {
return fs.getFile(0, 5);
} else if (path.startsWith("/textures")) {
return fs.getFile(0, 6);
} else if (path.startsWith("/wordenc")) {
return fs.getFile(0, 7);
} else if (path.startsWith("/sounds")) {
return fs.getFile(0, 8);
}
return null;
}
}