📄 riskutil.java
字号:
public static InputStream getLoadFileInputStream(String file) throws Exception {
// it is impossible for a applet to get here
if (webstart!=null) {
javax.jnlp.FileContents fc = (javax.jnlp.FileContents)fileio.remove(file);
return fc.getInputStream();
}
else {
return new FileInputStream(file);
}
}
public static String getSaveFileName(Frame frame,String dir,String extension) {
if (applet!=null) {
showAppletWarning(frame);
return null;
}
if (webstart!=null) {
JOptionPane.showMessageDialog(frame,"Please make sure to select a file name ending with \"."+extension+"\"");
return dir+"filename."+extension;
}
else {
JFileChooser fc = new JFileChooser(dir);
fc.setFileFilter(new RiskFileFilter(extension));
int returnVal = fc.showSaveDialog( frame );
if (returnVal == JFileChooser.APPROVE_OPTION) {
java.io.File file = fc.getSelectedFile();
// Write your code here what to do with selected file
String fileName = file.getAbsolutePath();
if (!(fileName.endsWith( "." + extension ))) {
fileName = fileName + "." + extension;
}
return fileName;
} else {
// Write your code here what to do if user has canceled Save dialog
return null;
}
}
}
public static void saveFile(String name,Object obj) throws Exception {
// it is impossible for a applet to get here
if (webstart!=null) {
ByteArrayOutputStream stor = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(stor);
out.writeObject(obj);
out.flush();
out.close();
InputStream stream = new ByteArrayInputStream(stor.toByteArray());
javax.jnlp.FileSaveService fss = (javax.jnlp.FileSaveService)javax.jnlp.ServiceManager.lookup("javax.jnlp.FileSaveService");
javax.jnlp.FileContents fc = fss.saveFileDialog(name.substring(0,name.indexOf('/')+1), new String[]{ name.substring(name.indexOf('.')+1) }, stream, name.substring(name.indexOf('/')+1,name.indexOf('.')) );
}
else {
FileOutputStream fileout = new FileOutputStream(name);
ObjectOutputStream objectout = new ObjectOutputStream(fileout);
objectout.writeObject(obj);
objectout.close();
}
}
private static void showAppletWarning(Frame frame) {
JOptionPane.showMessageDialog(frame,
risk.engine.translation.TranslationBundle.getBundle().getString("core.error.applet")
);
}
public static String sendText(String from, String text,String title) throws Exception {
if (applet!=null) {
throw new Exception("\n"+risk.engine.translation.TranslationBundle.getBundle().getString("core.error.applet"));
}
/* this is doing a get, throws a 414 error on big messages
//t = t.replaceAll("\n","%0A").replaceAll(" ","+").replaceAll("&","%26");
text = java.net.URLEncoder.encode(text,"UTF-8");
from = java.net.URLEncoder.encode(from,"UTF-8");
URL url = new URL("http://yura.net/cgi-sys/FormMail.cgi?recipient=yura@yura.net&subject=bugs%20and%20suggestion&email="+from+"&text="+text );
BufferedReader bufferin=new BufferedReader( new InputStreamReader(url.openStream()) );
StringBuffer buffer = new StringBuffer();
String input = bufferin.readLine();
while(input != null) {
buffer.append(input+"\n");
input = bufferin.readLine(); // get next line
}
return buffer.toString();
*/
StringBuffer buffer = new StringBuffer();
// Construct data
String data = URLEncoder.encode("recipient", "UTF-8") + "=" + URLEncoder.encode("yura@yura.net", "UTF-8");
data += "&" + URLEncoder.encode("subject", "UTF-8") + "=" + URLEncoder.encode(GAME_NAME+" "+Risk.RISK_VERSION+" "+title, "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(from, "UTF-8");
data += "&" + URLEncoder.encode("env_report", "UTF-8") + "=" + URLEncoder.encode("REMOTE_HOST,HTTP_USER_AGENT", "UTF-8");
data += "&" + URLEncoder.encode("info", "UTF-8") + "=" + URLEncoder.encode(getSystemInfoText(), "UTF-8");
data += "&" + URLEncoder.encode("text", "UTF-8") + "=" + URLEncoder.encode(text, "UTF-8");
// Send data
URL url = new URL(RISK_POST_URL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
buffer.append(line+"\n");
}
wr.close();
rd.close();
return buffer.toString();
}
public static String getSystemInfoText() {
ResourceBundle resb = risk.engine.translation.TranslationBundle.getBundle();
String netInfo,patch,home,cpu,name,info;
if (checkForNoSandbox()) {
patch = " " + System.getProperty("sun.os.patch.level") + " (" + System.getProperty("sun.arch.data.model") + "bit)";
home = System.getProperty("java.home");
cpu = System.getProperty("sun.cpu.isalist");
name = System.getProperty("java.runtime.name") + " ("+ System.getProperty("java.runtime.version") +")";
info = System.getProperty("java.vm.info");
// we CAN do this outside the sandbox, but for some reason it promps the webstart
try {
netInfo = InetAddress.getLocalHost().getHostAddress() + " (" + InetAddress.getLocalHost().getHostName() +")" ;
}
catch (UnknownHostException e) {
netInfo = resb.getString("about.nonetwork");
}
}
else {
patch = "?";
home = "?";
cpu = "?";
info = "?";
if (applet!=null) {
name = "applet";
}
else if (webstart!=null) {
name = "web start ("+webstart+")";
}
else {
name = "?";
}
netInfo = "?";
}
return " " + Risk.RISK_VERSION + " (network/save: " + risk.engine.core.RiskGame.SAVE_VERSION + ") \n" +
" " + "system:"+java.util.Locale.getDefault()+" current:" + resb.getLocale() + "\n" +
" " + netInfo + " \n" +
" " + System.getProperty("os.name") + " " + System.getProperty("os.version") +" "+ patch + " on " + System.getProperty("os.arch") + " \n" +
" " + cpu + " \n" +
" " + UIManager.getLookAndFeel() + " \n" +
" " + System.getProperty("java.vendor") + " \n" +
" " + System.getProperty("java.vendor.url") + " \n" +
" " + name +" \n" +
" " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.version") +", "+ info +") \n" +
" " + System.getProperty("java.specification.version") +" ("+ System.getProperty("java.version") +") \n" +
" " + home + " \n" +
" " + System.getProperty("java.class.version");
}
public static void openAbout(Frame frame,String product,String version) {
AboutDialog aboutDialog = new AboutDialog( frame , true, product, version);
Dimension frameSize = frame.getSize();
Dimension aboutSize = aboutDialog.getSize();
int x = frame.getLocation().x + (frameSize.width - aboutSize.width) / 2;
int y = frame.getLocation().y + (frameSize.height - aboutSize.height) / 2;
if (x < 0) x = 0;
if (y < 0) y = 0;
aboutDialog.setLocation(x, y);
aboutDialog.setVisible(true);
}
public static String getStringForColor(Color c) {
if (c.equals(Color.BLACK)) { return "black"; }
if (c.equals(Color.BLUE)) { return "blue"; }
if (c.equals(Color.CYAN)) { return "cyan"; }
if (c.equals(Color.DARK_GRAY)) { return "darkgray"; }
if (c.equals(Color.GRAY)) { return "gray"; }
if (c.equals(Color.GREEN)) { return "green"; }
if (c.equals(Color.LIGHT_GRAY)) { return "lightgray"; }
if (c.equals(Color.MAGENTA)) { return "magenta"; }
if (c.equals(Color.ORANGE)) { return "orange"; }
if (c.equals(Color.PINK)) { return "pink"; }
if (c.equals(Color.RED)) { return "red"; }
if (c.equals(Color.WHITE)) { return "white"; }
if (c.equals(Color.YELLOW)) { return "yellow"; }
return getHexForColor(c);
}
public static Color getTextColorFor(Color c) {
/*
if ( c.getRed() < 100 && c.getBlue() < 100 && c.getGreen() < 100 ) {
return Color.white;
}
else {
return Color.black;
}
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
if (r > 240 || g > 240) {
return Color.black;
}
else {
return Color.white;
}
*/
int r = c.getRed();
int g = c.getGreen();
// int b = c.getBlue();
if ((r > 240 || g > 240) || (r > 150 && g > 150)) {
return Color.black;
}
else {
return Color.white;
}
}
public static String getHexForColor(Color c) {
return "#" + Integer.toHexString(( c.getRGB() & 0xffffff) | 0x1000000).substring(1);
}
public static void donate() throws Exception {
openURL(new URL("https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=yura%40yura%2enet&item_name="+GAME_NAME+"%20Donation&no_shipping=0&no_note=1&tax=0¤cy_code=GBP&lc=GB&bn=PP%2dDonationsBF&charset=UTF%2d8"));
}
private static String lobbyAppletURL;
private static String lobbyURL;
public static boolean getAddLobby(Risk risk) {
boolean canlobby = false;
if (checkForNoSandbox()) {
try {
URL url = new URL(RISK_LOBBY_URL);
BufferedReader bufferin=new BufferedReader( new InputStreamReader(url.openStream()) );
StringBuffer buffer = new StringBuffer();
String input = bufferin.readLine();
while(input != null) {
buffer.append(input+"\n");
input = bufferin.readLine(); // get next line
}
String[] lobbyinfo = buffer.toString().split("\\n");
if (lobbyinfo.length>=1 && lobbyinfo[0].equals("LOBBYOK")) {
if (lobbyinfo.length>=2) {
lobbyAppletURL = lobbyinfo[1];
canlobby = true;
}
if (lobbyinfo.length>=3) {
lobbyURL = lobbyinfo[2];
canlobby = true;
}
}
}
catch(Throwable ex) { }
try {
//try { Thread.sleep(5000); }
//catch(InterruptedException e) {}
URL url = new URL(RISK_VERSION_URL);
BufferedReader bufferin=new BufferedReader( new InputStreamReader(url.openStream()) );
StringBuffer buffer = new StringBuffer();
String input = bufferin.readLine();
while(input != null) {
buffer.append(input+"\n");
input = bufferin.readLine(); // get next line
}
String[] newversion = buffer.toString().split("\\n");
if (newversion[0].startsWith("RISKOK ")) {
String v = newversion[0].substring(7, newversion[0].length() );
if (!v.equals(Risk.RISK_VERSION)) {
for (int c=1;c<newversion.length;c++) {
v = v+"\n"+newversion[c];
}
risk.showMessageDialog("There is a new version of "+GAME_NAME+": "+v);
}
}
}
catch (Throwable e) { }
}
return canlobby;
}
public static void runLobby(Risk risk) {
try {
if (lobbyURL!=null) {
URLClassLoader ucl = URLClassLoader.newInstance(new URL[] { new URL("jar:"+lobbyURL+"/LobbyClient.jar!/") } );
Class lobbyclass = ucl.loadClass("org.lobby.client.LobbyClientGUI");
// TODO: should be like this
//lobbyclass.newInstance();
final javax.swing.JPanel panel = (javax.swing.JPanel)lobbyclass.getConstructor( new Class[] { URL.class } ).newInstance( new Object[] { new URL(lobbyURL) } );
javax.swing.JFrame gui = new javax.swing.JFrame("yura.net Lobby");
gui.setContentPane(panel);
gui.setSize(800, 600);
gui.setVisible(true);
gui.addWindowListener( new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) { panel.setVisible(false); }
});
panel.setVisible(true);
}
else if (lobbyAppletURL!=null) {
openURL(new URL(lobbyAppletURL));
}
}
catch(Exception e) {
risk.showMessageDialog("unable to run the lobby: "+e.toString() );
}
}
private static void setupLookAndFeel() {
// set up system Look&Feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
// only do this check if there is NO sandbox
// as otherwise we will get an exception anyway
if (checkForNoSandbox()) {
// check for java bug with JFileChooser
try {
new JFileChooser();
}
catch (Throwable th) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/* OLD
// set up system Look&Feel
try {
String os = System.getProperty("os.name");
String jv = System.getProperty("java.version");
if ( jv.startsWith("1.4.2") && os != null && os.startsWith("Linux")) {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
}
else {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
}
catch (Exception e) {
e.printStackTrace();
}
*/
}
private static String maps;
private static String cards;
private static HashMap fileio = new HashMap();
public static void parseArgs(String[] args) {
risk.engine.translation.TranslationBundle.parseArgs(args);
for (int nA = 0; nA < args.length; nA++ ) {
if (args[nA].length() > 5 && args[nA].substring(0,5).equals( "maps=")) {
maps = args[nA].substring(5);
}
if (args[nA].length() > 6 && args[nA].substring(0,6).equals( "cards=")) {
cards = args[nA].substring(6);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -