View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.tournament;
2   
3   import java.io.File;
4   import java.io.InputStream;
5   import java.net.URISyntaxException;
6   
7   import cz.cuni.amis.utils.IniFile;
8   import cz.cuni.amis.utils.exception.PogamutException;
9   
10  /**
11   * Ordinary {@link IniFile} that loads its defaults from classpath:/cz/cuni/amis/pogamut/ut2004/tournament/deathmatch/GameBots2004-Deathmatch.ini 
12   * if not specified.
13   * <p><p>
14   * Additionally it provides definitions of common constants that applies to the GameBots2004.ini as well as handy shortcuts for setting
15   * various interesting properties such as time limit or frag limit, etc.
16   * 
17   * DOES NOT WORK !!! 
18   * 
19   * UT2004.ini file contains multi-map of keys-values !!!
20   * 
21   * @author Jimmy
22   */
23  public class UT2004Ini extends IniFile {
24  
25  	//
26  	// SECTION
27  	// 
28  	
29  	public static final String Section_URL = "URL";
30  	public static final String Section_Engine_GameReplicationInfo = "Engine.GameReplicationInfo";
31  	
32  	//
33  	// PROPERTY KEYS
34  	//
35  	
36  	public static final String Key_Port = "Port";
37  	
38  	public static final String Key_ServerName = "ServerName";
39  	public static final String Key_ShortName = "ShortName";
40  	
41  	/**
42  	 * Constructs Ini file with defaults taken from 'classpath:/cz/cuni/amis/pogamut/ut2004/tournament/deathmatch/GameBots2004-Deathmatch.ini'.
43  	 */
44  	public UT2004Ini() {
45  		InputStream defaults = UT2004Ini.class.getResourceAsStream("/cz/cuni/amis/pogamut/ut2004/tournament/UT2004.ini");
46  		load(defaults);
47  	}
48  	
49  	/**
50  	 * Constructs GameBots2004Ini with defaults taken 'source' (file must exists!).
51  	 * 
52  	 * @param source
53  	 */
54  	public UT2004Ini(File source) {
55  		if (!source.exists()) {
56  			throw new PogamutException("File with defaults for UT2004.ini does not exist at: " + source.getAbsolutePath() + ".", this);
57  		}
58  		load(source);
59  	}
60  	
61  	public UT2004Ini(UT2004Ini ut2004Ini) {
62  		super(ut2004Ini);
63  	}
64  
65  	/**
66  	 * Returns "server name", string that will appear when advertising the server via LAN.
67  	 * @return
68  	 */
69  	public String getServerName() {
70  		return getSection(Section_Engine_GameReplicationInfo).get(Key_ServerName);
71  	}
72  	
73  	/**
74  	 * Returns "short server name", dunno when is that used.
75  	 * @return
76  	 */
77  	public String getServerShortName() {
78  		return getSection(Section_Engine_GameReplicationInfo).get(Key_ShortName);
79  	}
80  	
81  	/**
82  	 * Sets (short) server name for the server that will get advertised via LAN.
83  	 * @param serverName
84  	 * @param shortName
85  	 */
86  	public void setServerName(String serverName, String shortName) {
87  		getSection(Section_Engine_GameReplicationInfo).set(Key_ServerName, serverName);
88  		getSection(Section_Engine_GameReplicationInfo).set(Key_ShortName,  shortName);
89  	}
90  	
91  	/**
92  	 * Returns port where UT2004 dedicated server will be listening at / announcing itself.
93  	 * @return
94  	 */
95  	public int getPort() {
96  		String port = getSection(Section_URL).get(Key_Port);
97  		try {
98  			return Integer.parseInt(port);
99  		} catch (Exception e) {
100 			return 0;
101 		}	
102 	}
103 	
104 	/**
105 	 * Sets port where UT2004 dedicated server will be listening at / announcing itself.
106 	 * @param port
107 	 */
108 	public void setPort(int port) {
109 		getSection(Section_URL).set(Key_Port, String.valueOf(port));
110 	}
111 	
112 }