View Javadoc

1   /*
2    * "Peko" Visual Novel System
3    *
4    * All Rights Reserved.
5    * Copyright (c) 1999-2003 Tsukuba Bunko.
6    *
7    * $Id: ResourceManager.java,v 1.1 2005/07/11 12:49:19 ppoi Exp $
8    */
9   package tsukuba_bunko.peko.resource;
10  
11  import	java.net.URL;
12  
13  import	tsukuba_bunko.resource.DeserializerMapping;
14  import	tsukuba_bunko.resource.Resources;
15  
16  import	tsukuba_bunko.peko.InitializationError;
17  import	tsukuba_bunko.peko.Logger;
18  
19  
20  /***
21   * "Peko" が使用するリソースを管理する機能を提供します。
22   * @author	$Author: ppoi $
23   * @version	$Revision: 1.1 $
24   */
25  public class ResourceManager	{
26  
27  	/***
28  	 * ログメッセージを格納したファイル
29  	 */
30  	protected static final String	RES_MESSAGE = "messages.xml";
31  
32  	/***
33  	 * システム構成リソースを格納したファイル
34  	 */
35  	protected static final String	RES_SYSTEM = "system.xml";
36  
37  	/***
38  	 * メニュー項目リソースを格納したファイル
39  	 */
40  	protected static final String	RES_MENU = "menu.xml";
41  
42  	/***
43  	 * ダイアログ構成リソースを格納したファイル
44  	 */
45  	protected static final String	RES_DIALOG = "dialog.xml";
46  
47  	/***
48  	 * キャンバス構成リソースを格納したファイル
49  	 */
50  	protected static final String	RES_CANVAS = "canvas.xml";
51  
52  	/***
53  	 * ゲーム構成リソースを格納したファイル
54  	 */
55  	protected static final String	RES_GAME ="game-info.xml";
56  
57  	/***
58  	 * タイトル画面構成リソースを格納したファイル
59  	 */
60  	protected static final String	RES_TITLE = "title.xml";
61  
62  
63  	/***
64  	 * 唯一のインスタンス
65  	 */
66  	private static ResourceManager	_instance = null;
67  
68  
69  	/***
70  	 * リソースローダ
71  	 */
72  	private PekoResourceLoader	_loader = null;
73  
74  
75  	/***
76  	 * リソース
77  	 */
78  	private Resources	_resources = null;
79  
80  	/***
81  	 * ロケーションリソース
82  	 */
83  	private LocationResources	_locationResources = null;
84  
85  
86  	/***
87  	 * <code>ResourceManager</code> のインスタンスを作成します。
88  	 */
89  	protected ResourceManager()
90  	{
91  		super();
92  	}
93  
94  
95  	/***
96  	 * リソースが存在するかどうかを判定します。
97  	 * @param	resourceID	リソース ID
98  	 * @return	存在する場合 <code>true</code>、存在しない場合 <code>false</code>
99  	 */
100 	public boolean exists( String resourceID )
101 	{
102 		return (_resources.getResource(resourceID) != null);
103 	}
104 
105 	/***
106 	 * リソースを取得します。
107 	 * @param	resourceID	リソース ID
108 	 * @return	リソース。対応するリソースが存在しない場合 <code>null</code>。
109 	 */
110 	public Object getResource( String resourceID )
111 	{
112 		return getResource( resourceID, false );
113 	}
114 
115 	/***
116 	 * リソースを取得します。
117 	 * @param	resourceID	リソース ID
118 	 * @param	nowarn	<code>true</code> の場合、ワーニングログを出力しない
119 	 * @return	リソース。対応するリソースが存在しない場合 <code>null</code>。
120 	 */
121 	public Object getResource( String resourceID, boolean nowarn )
122 	{
123 		Object	resource = _resources.getResource( resourceID );
124 		if( !nowarn )	{
125 			if( resource == null )	{
126 				Logger.warn( MessageIDs.RES0001W.getMessage( new Object[]{resourceID} ) );
127 			}
128 		}
129 		return resource;
130 	}
131 
132 	/***
133 	 * ロケーションリソースを取得します。
134 	 * @return	ロケーションリソース
135 	 */
136 	public LocationResources getLocationResources()
137 	{
138 		return _locationResources;
139 	}
140 
141 
142 	/***
143 	 * リソースローダを取得します。
144 	 * @return	リソースローダ
145 	 */
146 	private PekoResourceLoader getResourceLoader()
147 	{
148 		try	{
149 			if( _loader == null )	{
150 				_loader = new PekoResourceLoader();
151 			}
152 			return _loader;
153 		}
154 		catch( Exception e )	{
155 			InitializationError	ie = new InitializationError( "fail to create instance of PekoResourceLoader", e );
156 			Logger.fatal( MessageIDs.RES0014F, e );
157 			throw ie;
158 		}
159 	}
160 
161 	/***
162 	 * ログメッセージを読み込みます。
163 	 */
164 	private void loadLogMessages()
165 	{
166 		try	{
167 			URL	resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_MESSAGE );
168 			if( resourceURL == null )	{
169 				InitializationError	ie = new InitializationError( "missing resource file of log messgages." );
170 				Logger.fatal( "[RES9998F] missing resource file of log messgages.", ie );
171 				throw ie;
172 			}
173 			Logger.debug( "loading log messages from " + resourceURL.toString() );
174 
175 			_resources = Resources.newInstance( resourceURL );
176 		}
177 		catch( Exception e )	{
178 			InitializationError	ie = new InitializationError( "fail to load resources of log messages.", e );
179 			Logger.fatal( "[RES9999F] fail to load resources of log messages.", ie );
180 			throw ie;
181 		}
182 	}
183 
184 	/***
185 	 * システム構成リソースを読み込みます。
186 	 */
187 	private void loadSystemConfigurationResources()
188 	{
189 		try	{
190 			URL	resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_SYSTEM );
191 			if( resourceURL == null )	{
192 				InitializationError	ie = new InitializationError( "missing resource file of PVNS resource." );
193 				Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_SYSTEM}, ie );
194 				throw ie;
195 			}
196 			Logger.debug( "loadingresources of system configuration from " + resourceURL.toString() );
197 
198 			PekoResourceLoader	loader = getResourceLoader();
199 			loader.loadResource( resourceURL, _resources );
200 		}
201 		catch( Exception e )	{
202 			InitializationError	ie = new InitializationError( "fail to load resources of system configuration", e );
203 			Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_SYSTEM}, ie );
204 			throw ie;
205 		}
206 
207 		LocationConfigurator	configurator = new LocationConfigurator();
208 		_locationResources = configurator.configure( _resources );
209 	}
210 
211 	/***
212 	 * メニュー項目構成リソースを読み込みます。
213 	 */
214 	private void loadMenuResources()
215 	{
216 		try	{
217 			URL	resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_MENU );
218 			if( resourceURL == null )	{
219 				InitializationError	ie = new InitializationError( "missing resource file of menu items." );
220 				Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_MENU}, ie );
221 				throw ie;
222 			}
223 			Logger.debug( "loading resources of menu items from " + resourceURL.toString() );
224 
225 			PekoResourceLoader	loader = getResourceLoader();
226 			loader.loadResource( resourceURL, _resources );
227 		}
228 		catch( Exception e )	{
229 			InitializationError	ie = new InitializationError( "fail to load resources of menu item configuration", e );
230 			Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_SYSTEM}, ie );
231 			throw ie;
232 		}
233 	}
234 
235 	/***
236 	 * ダイアログ構成リソースを読み込みます。
237 	 */
238 	private void loadDialogConfigurationResources()
239 	{
240 		try	{
241 			URL	resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_DIALOG );
242 			if( resourceURL == null )	{
243 				InitializationError	ie = new InitializationError( "missing resource file of dialog configuration." );
244 				Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_DIALOG}, ie );
245 				throw ie;
246 			}
247 			Logger.debug( "loading resources of dialog configuration from " + resourceURL.toString() );
248 
249 			PekoResourceLoader	loader = getResourceLoader();
250 			loader.loadResource( resourceURL, _resources );
251 		}
252 		catch( Exception e )	{
253 			InitializationError	ie = new InitializationError( "fail to load resources of dialog configuration", e );
254 			Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_DIALOG}, ie );
255 			throw ie;
256 		}
257 	}
258 
259 	/***
260 	 * キャンバス構成リソースを読み込みます。
261 	 */
262 	private void loadCanvasConfigurationResources()
263 	{
264 		try	{
265 			URL	resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_CANVAS );
266 			if( resourceURL == null )	{
267 				InitializationError	ie = new InitializationError( "missing resource file of canvas configuration." );
268 				Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_CANVAS}, ie );
269 				throw ie;
270 			}
271 			Logger.debug( "loading resources of canvas configuration from " + resourceURL.toString() );
272 
273 			PekoResourceLoader	loader = getResourceLoader();
274 			loader.loadResource( resourceURL, _resources );
275 		}
276 		catch( Exception e )	{
277 			InitializationError	ie = new InitializationError( "fail to load resources of canvas configuration", e );
278 			Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_CANVAS}, ie );
279 			throw ie;
280 		}
281 	}
282 
283 	/***
284 	 * タイトル画面構成リソースを読み込みます。
285 	 */
286 	private void loadTitleConfigurationResources()
287 	{
288 		try	{
289 			URL	resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_TITLE );
290 			if( resourceURL == null )	{
291 				InitializationError	ie = new InitializationError( "missing resource file of title configuration." );
292 				Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_TITLE}, ie );
293 				throw ie;
294 			}
295 			Logger.debug( "loading resources of title configuration from " + resourceURL.toString() );
296 
297 			PekoResourceLoader	loader = getResourceLoader();
298 			loader.loadResource( resourceURL, _resources );
299 		}
300 		catch( Exception e )	{
301 			InitializationError	ie = new InitializationError( "fail to load resources of title configuration", e );
302 			Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_TITLE}, ie );
303 			throw ie;
304 		}
305 	}
306 
307 
308 
309 	/***
310 	 * ゲーム情報リソースを読み込みます。
311 	 */
312 	private void loadGameInfoResources()
313 	{
314 		try	{
315 			URL	resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_GAME );
316 			if( resourceURL == null )	{
317 				InitializationError	ie = new InitializationError( "missing resource file of game information resource." );
318 				Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_GAME}, ie );
319 				throw ie;
320 			}
321 			Logger.debug( "loading game information resources from " + resourceURL.toString() );
322 
323 			PekoResourceLoader	loader = getResourceLoader();
324 
325 			DeserializerMapping	mapping = loader.getDeserializerMapping();
326 			IconDeserializer	deserializer = (IconDeserializer)mapping.getResourceDeserializer( "peko:icon" );
327 			deserializer.setBaseURL( _locationResources.getMiscDirecotryURL() );
328 
329 			loader.loadResource( resourceURL, _resources );
330 		}
331 		catch( Exception e )	{
332 			InitializationError	ie = new InitializationError( "fail to load resources of game information resource", e );
333 			Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_GAME}, ie );
334 			throw ie;
335 		}
336 	}
337 
338 	/***
339 	 * <code>ResourceManager</code> のインスタンスを初期化します。
340 	 * @throws	InitializationError	初期化に失敗した場合
341 	 */
342 	private void initialize()
343 	{
344 		Logger.debug( "[resource] start initializing ResourceManager." );
345 
346 		loadLogMessages();
347 
348 		loadSystemConfigurationResources();
349 		loadMenuResources();
350 		loadDialogConfigurationResources();
351 		loadCanvasConfigurationResources();
352 		loadTitleConfigurationResources();
353 
354 		loadGameInfoResources();
355 
356 		Logger.debug( "[resource] finish initializing ResourceManager." );
357 	}
358 
359 
360 	/***
361 	 * <code>ResourceManager</code> の唯一のインスタンスを取得します。
362 	 * @return	唯一の <code>ResourceManager</code> のインスタンス
363 	 */
364 	public static ResourceManager getInstance()
365 	{
366 		if( _instance == null )	{
367 			synchronized( ResourceManager.class )	{
368 				if( _instance == null )	{
369 					_instance = new ResourceManager();
370 					_instance.initialize();
371 				}
372 			}
373 		}
374 		return _instance;
375 	}
376 }