📄 resourcemanager.java
字号:
/*
* Light And Shadow. A Persistent Universe based on Robert Jordan's Wheel of Time Books.
* Copyright (C) 2001-2002 WOTLAS Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package wotlas.common;
import wotlas.utils.*;
import wotlas.libs.persistence.*;
import wotlas.libs.log.LogResourceLocator;
import wotlas.libs.graphics2D.ImageResourceLocator;
import wotlas.libs.graphics2D.FontResourceLocator;
import wotlas.libs.sound.MusicResourceLocator;
import wotlas.libs.sound.SoundResourceLocator;
import wotlas.libs.wizard.WizardResourceLocator;
import wotlas.libs.aswing.ASwingResourceLocator;
import wotlas.common.environment.*;
import java.io.*;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.net.URL;
import java.util.Properties;
/** Manages the different resources found in wotlas. Resources are searched in a JAR
* or via the provided (or default) base path. If we are in a JAR file ( server or
* client jar ) we call 'external resources' resources that are stored outside the
* JAR.
*
* @author Aldiss, Diego
*/
public class ResourceManager implements LogResourceLocator, ImageResourceLocator, FontResourceLocator,
MusicResourceLocator, SoundResourceLocator, WizardResourceLocator,
ASwingResourceLocator {
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Wotlas Version Number for the code.
*/
public static final String WOTLAS_VERSION = "2.0";
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Default location where are stored game resources when NOT packed in a Jar.
* We use this default value if (1) we are not in a JAR file,
* (2) no other base path is provided at start-up.
*/
public static final String DEFAULT_BASE_PATH = "../base";
/** Default location where are stored help docs when NOT packed in a Jar.
* We only use this default value if we are not in a JAR file.
*/
public static final String DEFAULT_HELP_DOCS_PATH = "../docs/help";
/** Default location for binaries and OS dependent scripts
* This is always an external directory.
*/
public static final String DEFAULT_BIN_PATH = "../bin";
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** A part of the Wotlas client JAR file name.
*/
public static final String WOTLAS_CLIENT_JAR = "client.jar";
/** A part of the Wotlas Server JAR file name.
*/
public static final String WOTLAS_SERVER_JAR = "server.jar";
/** Wotlas root dir for resources located in a JAR. We'll search in this
* directory for every kind of resources.
* MUST START WITH A "/".
*/
public static final String WOTLAS_JAR_ROOT_RESOURCE_DIR = "/base";
/** Wotlas root dir for docs located in the JAR
* MUST START WITH A "/".
*/
public static final String WOTLAS_JAR_ROOT_DOCS_DIR = "/docs/help";
/** Tells in which dir we can store external files. This directory will be created in the
* directory where the JAR is stored. See the wotlasJarExternalDir member field for the
* complete path.
*/
public static final String WOTLAS_JAR_EXTERNAL_DIR = "base-ext"; // must be different from other root dirs
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Configs Directory Name
*/
public static final String CONFIGS_DIR = "configs";
/** Fonts Directory Name
*/
public static final String FONTS_DIR = "fonts";
/** GUI Images Directory Name
*/
public static final String GUI_IMAGES_DIR = "gui";
/** Image Library Directory Name
*/
public static final String IMAGE_LIBRARY_DIR = "graphics/imagelib";
/** Layouts Directory Name
*/
public static final String LAYOUTS_DIR = "layouts";
/** Logs Directory Name
*/
public static final String LOGS_DIR = "logs";
/** Configs Macros Directory Name
*/
public static final String MACROS_DIR = "configs/macros";
/** Music Directory Name
*/
public static final String MUSICS_DIR = "music";
/** Home Directory Name (where players are stored)
*/
public static final String PLAYERS_HOME_DIR = "home";
/** Server Configs Directory Name
*/
public static final String SERVER_CONFIGS_DIR = "servers";
/** GUI SMILEYS Images Directory Name
*/
public static final String SMILEYS_IMAGES_DIR = "gui/chat";
/** Sounds Directory Name
*/
public static final String SOUNDS_DIR = "sounds";
/** Universe Data Directory Name
*/
public static final String UNIVERSE_DATA_DIR = "universe";
/** directory to store data divided by environment
*/
public static final String ENVIRONMENTS_DIR = "environments";
/** directory to store data of Wheel of Time environment
*/
public static final String WOT_ENVIRONMENT_DIR = "wot";
/** directory to store data of Rogue like environment
*/
public static final String RLIKE_ENVIRONMENT_DIR = "roguelike";
/** directory to store the definitions file of Npc
*/
public static final String NPC_DEFINITION_DIR = "npcdef";
/** directory to store the definitions file of Npc
*/
public static final String EDITOR_BACKUP_DIR = "editorbackup";
/** Wizard Steps Directory Name
*/
public static final String WIZARD_STEPS_DIR = "wizard";
/** Windows Binary Directory Name
*/
public static final String WIN_BINARY_DIR = "win32";
/** Unix Binary Directory Name
*/
public static final String UNIX_BINARY_DIR = "unix";
/** Transfer script name (without the file extension)
*/
public static final String TRANSFER_SCRIPT_NAME = "transferScript";
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Where the fonts, graphics, musics, sounds, can be found...
* This path is only used if we are not in a JAR file.
*/
private String basePath;
/** Tells if we are in a JAR file or not.
*/
private boolean inJar;
/** Tells if we are in a wotlas client JAR file or not.
*/
private boolean inClientJar;
/** Tells if we are in a wotlas server JAR file or not.
*/
private boolean inServerJar;
/** Current Jar Name with full path.
*/
private String jarName;
/** Where we store external resources. This path is absolute and points out the JAR directory
* plus WOTLAS_JAR_EXTERNAL_DIR directory.
*/
private String wotlasJarExternalDir;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Constructor that uses default resource locations. Check the default
* JAR file names we'll seek (see the beginning of this class).
*
* IMPORTANT : if the wotlas jar file is not found in the classpath
* we'll use the local classes and the default directory pathes.
*/
public ResourceManager() {
// Are we in a JAR FILE ?
inJar = false;
jarName = getJarName();
if(jarName==null) {
// OK we are not in a JAR. We'll use the default external location for resources
basePath = DEFAULT_BASE_PATH;
return;
}
wotlasJarExternalDir = getJarDir()+WOTLAS_JAR_EXTERNAL_DIR; // creation of an absolute path to our external resources
// Are we in a client JAR File ?
if( jarName.indexOf(WOTLAS_CLIENT_JAR) >= 0 ) {
inClientJar = true;
inJar = true;
try{
repairClassPath();
createExternalClientDirectories();
}catch(Exception e) {
e.printStackTrace();
Tools.displayDebugMessage("Deployment Failed","Wotlas failed to extract config files to the local directory."+
"\nCheck the access rights of the installation directory of wotlas.");
Debug.exit();
}
return; // ResourceManager created
}
// Are we in a server JAR File ?
if( jarName.indexOf(WOTLAS_SERVER_JAR) >= 0 ) {
inServerJar = true;
inJar = true;
try{
repairClassPath();
createExternalClientDirectories();
}catch(Exception e) {
e.printStackTrace();
Tools.displayDebugMessage("Deployment Failed","Wotlas failed to extract config files to the local directory."+
"\nCheck the access rights of the installation directory of wotlas.");
Debug.exit();
}
return; // ResourceManager created
}
Tools.displayDebugMessage("Wrong Jar Name","The Jar file name should end with a 'client' or 'server' keyword");
Debug.exit();
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Changes the base path for resources.
* To use only if the resources are NOT in a JAR file.
* @param basePath where the resources are located
*/
public void setBasePath( String basePath ) {
if(inJar) {
Debug.signal(Debug.WARNING, this, "Attempt to change the external basePath ! No need to we are in a JAR !");
return;
}
this.basePath = basePath;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To tell if we are in a JAR.
* @return true if we are in a JAR.
*/
public boolean inJar() {
return inJar;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Returns the name of the JAR we are into. We return the path+name.
* @return null if we are not in a jar.
*/
protected String getJarName() {
URL url = this.getClass().getResource("ResourceManager.class");
if(url==null)
return null;
// check the URL format
String sUrl = ""+url;
if(!sUrl.startsWith("jar:"))
return null;
// We are in a Jar, we extract the jar name.
int end = sUrl.indexOf('!');
if(end<0) return null;
int beg = sUrl.indexOf("/");
if(beg<0) return null;
return sUrl.substring(beg+1,end);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Returns the absolute path to the directory where this JAR is stored. Please retrieve
* the JAR name before calling this method. The path we return ends with a "/".
*/
protected String getJarDir() {
if(jarName==null)
return null;
int index= jarName.lastIndexOf("/"); // always a "/" because the Jar path is found via an URL
if( index<0 )
return null;
return jarName.substring(0,index+1);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Repares the class path with the eventually missing current jar name.
*/
protected void repairClassPath() {
if( Tools.hasJar(jarName) )
return; // our classpath has the current Jar name... nothing to repair.
System.setProperty( "java.class.path", System.getProperty("java.class.path", ".")
+System.getProperty("path.separator", ";")
+jarName);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To tell if a path point out on an external resource of a JAR.
* We just compare the beginning of the file to the wotlasJarExternalDir field.
*
* @param pathName path to analyze
* @return true if this is an external resource, false otherwise
*/
public boolean isExternal( String pathName ) {
if( pathName.startsWith( wotlasJarExternalDir ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -