You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.0 KiB
118 lines
2.0 KiB
10 years ago
|
package mightypork.gamecore.core;
|
||
10 years ago
|
|
||
|
|
||
|
import java.io.File;
|
||
10 years ago
|
import java.util.HashMap;
|
||
|
import java.util.Map;
|
||
10 years ago
|
|
||
|
import mightypork.gamecore.logging.Log;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Static application workdir accessor.
|
||
|
*
|
||
|
* @author MightyPork
|
||
|
*/
|
||
|
public class WorkDir {
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Route configurator.
|
||
|
*/
|
||
|
public static interface RouteSetup {
|
||
|
|
||
|
public void addRoutes(RouteOpts routeOpts);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Route configurator access
|
||
|
*/
|
||
|
public static class RouteOpts {
|
||
|
|
||
|
public void addPath(String alias, String path)
|
||
|
{
|
||
|
WorkDir.addPath(alias, path);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static RouteOpts routeOpts = new RouteOpts();
|
||
10 years ago
|
private static File workdir;
|
||
10 years ago
|
private static Map<String, String> namedPaths = new HashMap<>();
|
||
10 years ago
|
|
||
|
|
||
|
public static void init(File workdir)
|
||
|
{
|
||
|
WorkDir.workdir = workdir;
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Add a path alias (dir or file)
|
||
10 years ago
|
*
|
||
10 years ago
|
* @param alias path alias
|
||
|
* @param path path relative to workdir
|
||
|
*/
|
||
10 years ago
|
public static void addPath(String alias, String path)
|
||
|
{
|
||
|
namedPaths.put(alias, path);
|
||
|
}
|
||
|
|
||
|
|
||
|
public static void registerRoutes(RouteSetup rs)
|
||
|
{
|
||
|
rs.addRoutes(routeOpts);
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* Get workdir folder, create if not exists.
|
||
|
*
|
||
|
* @param path dir path relative to workdir
|
||
|
* @return dir file
|
||
|
*/
|
||
|
public static File getDir(String path)
|
||
|
{
|
||
10 years ago
|
if (namedPaths.containsKey(path)) path = namedPaths.get(path);
|
||
|
|
||
10 years ago
|
final File f = new File(workdir, path);
|
||
10 years ago
|
if (!f.exists()) {
|
||
|
if (!f.mkdirs()) {
|
||
|
Log.w("Could not create a directory: " + f + " (path: " + path + ")");
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
return f;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Get workdir file, create parent if not exists.
|
||
|
*
|
||
|
* @param path dir path relative to workdir
|
||
|
* @return dir file
|
||
|
*/
|
||
|
public static File getFile(String path)
|
||
|
{
|
||
10 years ago
|
if (namedPaths.containsKey(path)) path = namedPaths.get(path);
|
||
|
|
||
10 years ago
|
final File f = new File(workdir, path);
|
||
|
|
||
|
// create the parent dir
|
||
10 years ago
|
if (!f.getParent().equals(workdir)) {
|
||
|
f.getParentFile().mkdirs();
|
||
|
}
|
||
10 years ago
|
|
||
|
return f;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/**
|
||
|
* @return the workdir File
|
||
|
*/
|
||
10 years ago
|
public static File getWorkDir()
|
||
|
{
|
||
|
return workdir;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
}
|