⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 riskutil.java

📁 java 开源游戏源码 RISK 联机对战 战棋类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package risk.engine;

import risk.engine.core.*;
import risk.engine.guishared.*;
import risk.engine.ai.AIPlayer;

import java.util.StringTokenizer;
import java.awt.Color;
import java.util.Vector;
import java.io.*;
import java.net.*;
import java.util.Random;
import java.util.ResourceBundle;
import java.awt.Toolkit;
import javax.crypto.SealedObject;
import javax.crypto.NullCipher;
import java.util.Enumeration;
import java.applet.Applet;
import javax.swing.JFileChooser;
import java.awt.Frame;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import java.awt.Container;
import java.util.Properties;
import java.awt.Dimension;
import javax.swing.UIManager;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.HashMap;

public class RiskUtil {

	private static final String RISK_VERSION_URL;
	private static final String RISK_LOBBY_URL;
	private static final String RISK_POST_URL;
	private static final String GAME_NAME;
	private static final String DEFAULT_MAP;

	public static final String SAVES_DIR = "saves/";

	public static URL mapsdir;
	public static Applet applet;
	public static RiskIO streamOpener;

	private static String webstart;
	private static boolean nosandbox;

	public static boolean checkForNoSandbox() {
		return nosandbox;
	}

	private final static Properties settings;
	static {

		settings = new Properties();

		try {
			settings.load(RiskUtil.class.getResourceAsStream("settings.ini"));
		}
		catch (Exception ex) {
			throw new RuntimeException("can not find settings.ini file!",ex);
		}

		RISK_VERSION_URL = settings.getProperty("VERSION_URL");
		RISK_LOBBY_URL = settings.getProperty("LOBBY_URL");
		RISK_POST_URL = settings.getProperty("POST_URL");
		GAME_NAME = settings.getProperty("name");
		DEFAULT_MAP = settings.getProperty("defaultmap");

	}
	public static String getGameName() {
		return GAME_NAME;
	}
	public static String getDefaultMap() {
		return DEFAULT_MAP;
	}

	public static InputStream openMapStream(String a) throws IOException {

		if (streamOpener==null) {

			return new URL(mapsdir,a).openStream();

		}
		else {

			return streamOpener.openMapStream(a);

		}

	}

	public static InputStream openStream(String a) throws IOException {

		if (streamOpener==null) {

			return getRiskFileURL(a).openStream();

		}
		else {

			return streamOpener.openStream(a);

		}

	}

	public static ResourceBundle getResourceBundle(Class c,String n,Locale l) {

		if (streamOpener==null) {

			return ResourceBundle.getBundle(c.getPackage().getName()+"."+n, l );

		}
		else {

			return streamOpener.getResourceBundle(c,n,l);

		}

	}

	public static void openURL(URL url) throws Exception {


		if (streamOpener==null) {

			riskOpenURL(url);

		}
		else {

			streamOpener.openURL(url);

		}



	}


	public static void openDocs(String docs) throws Exception {

		if (streamOpener==null) {

			riskOpenURL(getRiskFileURL(docs));

		}
		else {

			streamOpener.openDocs(docs);

		}



	}







	/**
	 * Opens the online help
	 * @return boolean Return true if you open the online help, returns false otherwise
	 */
	private static void riskOpenURL(URL docs) throws Exception {



		if (applet != null) {

			applet.getAppletContext().showDocument(docs,"_blank");
		}
		else if (webstart != null) {

			javax.jnlp.BasicService bs = (javax.jnlp.BasicService)javax.jnlp.ServiceManager.lookup("javax.jnlp.BasicService");

			boolean good = bs.showDocument(docs);

			if (!good) {

				throw new Exception("unable to open URL: "+docs);
			}

		}
		else {

			BrowserLauncher.openURL(docs.toString());

		}



/*
		if (applet == null ) {

			File file = new File(docs);
			openURL(new URL("file://" + file.getAbsolutePath()) );

		}
		else {

			URL url = applet.getCodeBase(); // get url of the applet

			openURL(new URL(url+docs));

		}


		try {

			String cmd=null;

			String os = System.getProperty("os.name");

			if ( os != null && os.startsWith("Windows")) {
				cmd = "rundll32 url.dll,FileProtocolHandler file://"+ file.getAbsolutePath();
			}
			else {
				cmd = "mozilla file://"+ file.getAbsolutePath();
			}

			Runtime.getRuntime().exec(cmd);

			return true;
		}
		catch(IOException x) {
			return false;
		}
*/
	}




	private static URL getRiskFileURL(String a) {

		try {

			if (applet!=null) {

				return new URL( applet.getCodeBase(), a );

			}
			else if (webstart!=null) {

				javax.jnlp.BasicService bs = (javax.jnlp.BasicService)javax.jnlp.ServiceManager.lookup("javax.jnlp.BasicService");

				return new URL( bs.getCodeBase() , a);

			}
			else {

				return new File(a).toURI().toURL();

			}



		}
		catch (Exception e) {

			throw new RuntimeException(e);

		}

	}


	public static void setupMapsDir(Applet a) {

		applet = a;

		// only ever call this method once
		if (mapsdir==null) {

		    // if applet is null check for webstart!
		    if (applet==null) {

			webstart = System.getProperty("javawebstart.version");

			if (webstart==null) {

				nosandbox=true;

				// we only want to setup the look and feel outside a sandbox
				// though this WILL work inside too
				setupLookAndFeel();

			}

		    }

		    try {

			if (checkForNoSandbox()) {

				File mapsdir1 = new File("maps");

				// riskconfig.getProperty("default.map")

				String dmname = DEFAULT_MAP;

				while ( !(new File(mapsdir1, dmname ).exists()) ) {

					JOptionPane.showMessageDialog(null,"Can not find map: "+dmname );

					JFileChooser fc = new JFileChooser();

					//RiskFileFilter filter = new RiskFileFilter(RiskFileFilter.RISK_SAVE_FILES);
					//fc.setFileFilter(filter);

					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
					fc.setDialogTitle("Select maps directory");

					int returnVal = fc.showOpenDialog(null);
					if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {

						mapsdir1 = fc.getSelectedFile();

					}
					else {

						System.exit(0);

					}


				}


				mapsdir = mapsdir1.toURI().toURL();
			}
			else {

				mapsdir = getRiskFileURL( "maps/");

			}

		    }
		    catch (Exception e) {

			throw new RuntimeException(e);

		    }
		}

	}

	public static Frame findParentFrame(Container c) {


		return (Frame)javax.swing.SwingUtilities.getAncestorOfClass(Frame.class, c);

/*
		// this does not work as using the method setContentPane makes this method return null

		while(c != null) {

			if (c instanceof Frame) return (Frame)c; 

			c = c.getParent(); 
		} 
		return (Frame)null;
*/
	}

	public static String getNewFile(Frame f,String a) {

		if (checkForNoSandbox()) {

			String dir = mapsdir.toString();
			File md;

			try {

				md = new File(new URI(dir));

			}
			catch(IllegalArgumentException e) {

				// this is an attempt at a crazy workaround that should not really work
				if ( dir.startsWith("file://") ) {

					md = new File( dir.substring(5,dir.length()).replaceAll("\\%20"," ") );
				}
				else {

					System.err.println("this should never happen! bad file: "+dir);

					md = new File( "maps/" );

				}

				// There is a bug in java 1.4/1.5 where it can not convert a URL like
				// file://Claire/BIG_DISK/Program Files/Risk/maps/
				// into a File Object so we will just try and make a simple file
				// object, and hope it works

				// java.lang.IllegalArgumentException: URI has an authority component
				// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5086147
				// This has been fixed in java 1.6

				// also can not have %20 in the name on 1.4/1.5, needs to be " ".

			}
			catch(Exception e) {

				throw new RuntimeException("Cant create file: "+ dir, e);

			}

			JFileChooser fc = new JFileChooser( md );
			RiskFileFilter filter = new RiskFileFilter(a);
			fc.setFileFilter(filter);

			int returnVal = fc.showOpenDialog( f );

			if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {

				java.io.File file = fc.getSelectedFile();

				// useless chack, but sometimes this is null
				if (file==null) { return null; }

				return file.getName();

			}

			return null;

		}
		else {

			String names=null;

			if (applet!=null) {

				names = applet.getParameter(a);
			}
			else if (webstart!=null) {

				if ("map".equals(a)) {

					names = maps;
				}
				else if ("cards".equals(a)) {

					names = cards;
				}
			}

			Vector namesvector = new Vector();

			StringTokenizer tok = new StringTokenizer( names, ",");
			while (tok.hasMoreTokens()) {

				namesvector.add( tok.nextToken() );

			}

			JComboBox combobox = new JComboBox( namesvector );

			// Messages 
			Object[] message = new Object[] { 
				risk.engine.translation.TranslationBundle.getBundle().getString("core.error.applet"),
				combobox
			};

			// Options 
			String[] options = { "OK","Cancel" }; 

			int result = JOptionPane.showOptionDialog( 
				f,				// the parent that the dialog blocks 
				message,			// the dialog message array 
				"select "+a,			// the title of the dialog window 
				JOptionPane.OK_CANCEL_OPTION,	// option type 
				JOptionPane.QUESTION_MESSAGE,	// message type 
				null,				// optional icon, use null to use the default icon 
				options,			// options string array, will be made into buttons 
				options[0]			// option that should be made into a default button 
			); 

			if (result==JOptionPane.OK_OPTION) {
				return combobox.getSelectedItem()+"";
			}

			return null;

		}

	}

	public static String getLoadFileName(Frame frame,String dir,String extension) {

		if (applet!=null) {

			showAppletWarning(frame);

			return null;
		}
		if (webstart!=null) {

			try {

				javax.jnlp.FileOpenService fos = (javax.jnlp.FileOpenService)javax.jnlp.ServiceManager.lookup("javax.jnlp.FileOpenService"); 

				javax.jnlp.FileContents fc = fos.openFileDialog(dir, new String[] { extension } );

				if (fc!=null) {

					fileio.put(fc.getName(),fc);

					return fc.getName();
				}
				else {

					return null;
				}
			}
			catch(Exception e) {

				return null;

			}
		}
		else {

			JFileChooser fc = new JFileChooser(dir);

			fc.setFileFilter(new RiskFileFilter(extension));

			int returnVal = fc.showDialog( frame , risk.engine.translation.TranslationBundle.getBundle().getString("mainmenu.loadgame.loadbutton"));
			if (returnVal == javax.swing.JFileChooser.APPROVE_OPTION) {

				java.io.File file = fc.getSelectedFile();
				// Write your code here what to do with selected file
				return file.getAbsolutePath();

			} else {
				// Write your code here what to do if user has canceled Open dialog
				return null;
			}
		}


	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -