1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
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 |
|
|
22 |
|
|
23 |
|
|
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 |
0 |
private static ResourceManager _instance = null; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
0 |
private PekoResourceLoader _loader = null; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
0 |
private Resources _resources = null; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
0 |
private LocationResources _locationResources = null; |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
protected ResourceManager() |
90 |
|
{ |
91 |
0 |
super(); |
92 |
0 |
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
public boolean exists( String resourceID ) |
101 |
|
{ |
102 |
0 |
return (_resources.getResource(resourceID) != null); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
public Object getResource( String resourceID ) |
111 |
|
{ |
112 |
0 |
return getResource( resourceID, false ); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
public Object getResource( String resourceID, boolean nowarn ) |
122 |
|
{ |
123 |
0 |
Object resource = _resources.getResource( resourceID ); |
124 |
0 |
if( !nowarn ) { |
125 |
0 |
if( resource == null ) { |
126 |
0 |
Logger.warn( MessageIDs.RES0001W.getMessage( new Object[]{resourceID} ) ); |
127 |
|
} |
128 |
|
} |
129 |
0 |
return resource; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
public LocationResources getLocationResources() |
137 |
|
{ |
138 |
0 |
return _locationResources; |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
private PekoResourceLoader getResourceLoader() |
147 |
|
{ |
148 |
|
try { |
149 |
0 |
if( _loader == null ) { |
150 |
0 |
_loader = new PekoResourceLoader(); |
151 |
|
} |
152 |
0 |
return _loader; |
153 |
|
} |
154 |
0 |
catch( Exception e ) { |
155 |
0 |
InitializationError ie = new InitializationError( "fail to create instance of PekoResourceLoader", e ); |
156 |
0 |
Logger.fatal( MessageIDs.RES0014F, e ); |
157 |
0 |
throw ie; |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
private void loadLogMessages() |
165 |
|
{ |
166 |
|
try { |
167 |
0 |
URL resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_MESSAGE ); |
168 |
0 |
if( resourceURL == null ) { |
169 |
0 |
InitializationError ie = new InitializationError( "missing resource file of log messgages." ); |
170 |
0 |
Logger.fatal( "[RES9998F] missing resource file of log messgages.", ie ); |
171 |
0 |
throw ie; |
172 |
|
} |
173 |
0 |
Logger.debug( "loading log messages from " + resourceURL.toString() ); |
174 |
|
|
175 |
0 |
_resources = Resources.newInstance( resourceURL ); |
176 |
|
} |
177 |
0 |
catch( Exception e ) { |
178 |
0 |
InitializationError ie = new InitializationError( "fail to load resources of log messages.", e ); |
179 |
0 |
Logger.fatal( "[RES9999F] fail to load resources of log messages.", ie ); |
180 |
0 |
throw ie; |
181 |
0 |
} |
182 |
0 |
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
private void loadSystemConfigurationResources() |
188 |
|
{ |
189 |
|
try { |
190 |
0 |
URL resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_SYSTEM ); |
191 |
0 |
if( resourceURL == null ) { |
192 |
0 |
InitializationError ie = new InitializationError( "missing resource file of PVNS resource." ); |
193 |
0 |
Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_SYSTEM}, ie ); |
194 |
0 |
throw ie; |
195 |
|
} |
196 |
0 |
Logger.debug( "loadingresources of system configuration from " + resourceURL.toString() ); |
197 |
|
|
198 |
0 |
PekoResourceLoader loader = getResourceLoader(); |
199 |
0 |
loader.loadResource( resourceURL, _resources ); |
200 |
|
} |
201 |
0 |
catch( Exception e ) { |
202 |
0 |
InitializationError ie = new InitializationError( "fail to load resources of system configuration", e ); |
203 |
0 |
Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_SYSTEM}, ie ); |
204 |
0 |
throw ie; |
205 |
0 |
} |
206 |
|
|
207 |
0 |
LocationConfigurator configurator = new LocationConfigurator(); |
208 |
0 |
_locationResources = configurator.configure( _resources ); |
209 |
0 |
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
private void loadMenuResources() |
215 |
|
{ |
216 |
|
try { |
217 |
0 |
URL resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_MENU ); |
218 |
0 |
if( resourceURL == null ) { |
219 |
0 |
InitializationError ie = new InitializationError( "missing resource file of menu items." ); |
220 |
0 |
Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_MENU}, ie ); |
221 |
0 |
throw ie; |
222 |
|
} |
223 |
0 |
Logger.debug( "loading resources of menu items from " + resourceURL.toString() ); |
224 |
|
|
225 |
0 |
PekoResourceLoader loader = getResourceLoader(); |
226 |
0 |
loader.loadResource( resourceURL, _resources ); |
227 |
|
} |
228 |
0 |
catch( Exception e ) { |
229 |
0 |
InitializationError ie = new InitializationError( "fail to load resources of menu item configuration", e ); |
230 |
0 |
Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_SYSTEM}, ie ); |
231 |
0 |
throw ie; |
232 |
0 |
} |
233 |
0 |
} |
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
private void loadDialogConfigurationResources() |
239 |
|
{ |
240 |
|
try { |
241 |
0 |
URL resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_DIALOG ); |
242 |
0 |
if( resourceURL == null ) { |
243 |
0 |
InitializationError ie = new InitializationError( "missing resource file of dialog configuration." ); |
244 |
0 |
Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_DIALOG}, ie ); |
245 |
0 |
throw ie; |
246 |
|
} |
247 |
0 |
Logger.debug( "loading resources of dialog configuration from " + resourceURL.toString() ); |
248 |
|
|
249 |
0 |
PekoResourceLoader loader = getResourceLoader(); |
250 |
0 |
loader.loadResource( resourceURL, _resources ); |
251 |
|
} |
252 |
0 |
catch( Exception e ) { |
253 |
0 |
InitializationError ie = new InitializationError( "fail to load resources of dialog configuration", e ); |
254 |
0 |
Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_DIALOG}, ie ); |
255 |
0 |
throw ie; |
256 |
0 |
} |
257 |
0 |
} |
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
private void loadCanvasConfigurationResources() |
263 |
|
{ |
264 |
|
try { |
265 |
0 |
URL resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_CANVAS ); |
266 |
0 |
if( resourceURL == null ) { |
267 |
0 |
InitializationError ie = new InitializationError( "missing resource file of canvas configuration." ); |
268 |
0 |
Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_CANVAS}, ie ); |
269 |
0 |
throw ie; |
270 |
|
} |
271 |
0 |
Logger.debug( "loading resources of canvas configuration from " + resourceURL.toString() ); |
272 |
|
|
273 |
0 |
PekoResourceLoader loader = getResourceLoader(); |
274 |
0 |
loader.loadResource( resourceURL, _resources ); |
275 |
|
} |
276 |
0 |
catch( Exception e ) { |
277 |
0 |
InitializationError ie = new InitializationError( "fail to load resources of canvas configuration", e ); |
278 |
0 |
Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_CANVAS}, ie ); |
279 |
0 |
throw ie; |
280 |
0 |
} |
281 |
0 |
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
private void loadTitleConfigurationResources() |
287 |
|
{ |
288 |
|
try { |
289 |
0 |
URL resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_TITLE ); |
290 |
0 |
if( resourceURL == null ) { |
291 |
0 |
InitializationError ie = new InitializationError( "missing resource file of title configuration." ); |
292 |
0 |
Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_TITLE}, ie ); |
293 |
0 |
throw ie; |
294 |
|
} |
295 |
0 |
Logger.debug( "loading resources of title configuration from " + resourceURL.toString() ); |
296 |
|
|
297 |
0 |
PekoResourceLoader loader = getResourceLoader(); |
298 |
0 |
loader.loadResource( resourceURL, _resources ); |
299 |
|
} |
300 |
0 |
catch( Exception e ) { |
301 |
0 |
InitializationError ie = new InitializationError( "fail to load resources of title configuration", e ); |
302 |
0 |
Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_TITLE}, ie ); |
303 |
0 |
throw ie; |
304 |
0 |
} |
305 |
0 |
} |
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
private void loadGameInfoResources() |
313 |
|
{ |
314 |
|
try { |
315 |
0 |
URL resourceURL = getClass().getClassLoader().getResource( ResourceManager.RES_GAME ); |
316 |
0 |
if( resourceURL == null ) { |
317 |
0 |
InitializationError ie = new InitializationError( "missing resource file of game information resource." ); |
318 |
0 |
Logger.fatal( MessageIDs.RES0002F, new Object[]{ResourceManager.RES_GAME}, ie ); |
319 |
0 |
throw ie; |
320 |
|
} |
321 |
0 |
Logger.debug( "loading game information resources from " + resourceURL.toString() ); |
322 |
|
|
323 |
0 |
PekoResourceLoader loader = getResourceLoader(); |
324 |
|
|
325 |
0 |
DeserializerMapping mapping = loader.getDeserializerMapping(); |
326 |
0 |
IconDeserializer deserializer = (IconDeserializer)mapping.getResourceDeserializer( "peko:icon" ); |
327 |
0 |
deserializer.setBaseURL( _locationResources.getMiscDirecotryURL() ); |
328 |
|
|
329 |
0 |
loader.loadResource( resourceURL, _resources ); |
330 |
|
} |
331 |
0 |
catch( Exception e ) { |
332 |
0 |
InitializationError ie = new InitializationError( "fail to load resources of game information resource", e ); |
333 |
0 |
Logger.fatal( MessageIDs.RES0003F, new Object[]{ResourceManager.RES_GAME}, ie ); |
334 |
0 |
throw ie; |
335 |
0 |
} |
336 |
0 |
} |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
private void initialize() |
343 |
|
{ |
344 |
0 |
Logger.debug( "[resource] start initializing ResourceManager." ); |
345 |
|
|
346 |
0 |
loadLogMessages(); |
347 |
|
|
348 |
0 |
loadSystemConfigurationResources(); |
349 |
0 |
loadMenuResources(); |
350 |
0 |
loadDialogConfigurationResources(); |
351 |
0 |
loadCanvasConfigurationResources(); |
352 |
0 |
loadTitleConfigurationResources(); |
353 |
|
|
354 |
0 |
loadGameInfoResources(); |
355 |
|
|
356 |
0 |
Logger.debug( "[resource] finish initializing ResourceManager." ); |
357 |
0 |
} |
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
public static ResourceManager getInstance() |
365 |
|
{ |
366 |
0 |
if( _instance == null ) { |
367 |
0 |
synchronized( ResourceManager.class ) { |
368 |
0 |
if( _instance == null ) { |
369 |
0 |
_instance = new ResourceManager(); |
370 |
0 |
_instance.initialize(); |
371 |
|
} |
372 |
0 |
} |
373 |
|
} |
374 |
0 |
return _instance; |
375 |
|
} |
376 |
|
} |