1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
11 * ROM related structures, this replicates what is in @c JarPackageShelf.
16 #ifndef SQUIRRELJME_ROM_H
17 #define SQUIRRELJME_ROM_H
20 #include "sjme/list.h"
21 #include "sjme/romInternal.h"
22 #include "sjme/stream.h"
23 #include "sjme/seekable.h"
28 #ifndef SJME_CXX_IS_EXTERNED
29 #define SJME_CXX_IS_EXTERNED
30 #define SJME_CXX_SQUIRRELJME_ROM_H
32 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
33 #endif /* #ifdef __cplusplus */
35 /*--------------------------------------------------------------------------*/
38 * Standard ROM library structure.
42 typedef struct sjme_rom_libraryBase sjme_rom_libraryBase
;
44 /** Synthetic library structure. */
45 typedef sjme_rom_libraryBase
* sjme_rom_library
;
47 /** List of ROM libraries. */
48 SJME_LIST_DECLARE(sjme_rom_library
, 0);
50 /** The type ID of ROM libraries. */
51 #define SJME_TYPEOF_BASIC_sjme_rom_library SJME_BASIC_TYPE_ID_OBJECT
54 * Common cache between suites and libraries.
58 typedef struct sjme_rom_cache
60 /** The allocation pool to use. */
61 sjme_alloc_pool
* allocPool
;
63 /** Wrapped object, if applicable. */
64 sjme_frontEnd frontEnd
;
68 * Internal cache for ROM libraries.
72 typedef struct sjme_rom_libraryCache
74 /** Common cache data. */
75 sjme_rom_cache common
;
77 /** Cached size of the library. */
80 /** Is raw access checked? */
81 sjme_jboolean checkedRawAccess
: 1;
83 /** Is raw access valid. */
84 sjme_jboolean validRawAccess
: 1;
85 } sjme_rom_libraryCache
;
88 * Internal cache for ROM suites.
92 typedef struct sjme_rom_suiteCache
94 /** Common cache data. */
95 sjme_rom_cache common
;
97 /** Libraries that exist within the suite. */
98 sjme_list_sjme_rom_library
* libraries
;
99 } sjme_rom_suiteCache
;
102 * Functions used to access a single library.
106 typedef struct sjme_rom_libraryFunctions sjme_rom_libraryFunctions
;
109 * Functions used to access a suite, which is an entire ROM.
113 typedef struct sjme_rom_suiteFunctions sjme_rom_suiteFunctions
;
115 struct sjme_rom_libraryBase
118 sjme_nvm_commonBase common
;
120 /** Functions used to access library information. */
121 const sjme_rom_libraryFunctions
* functions
;
123 /** The handle, may be to a seekable. */
126 /** The prefix, if applicable. */
129 /** The library ID. */
132 /** The library name. */
135 /** Hash of the library name. */
138 /** Internal cache, used by internal library functions. */
139 sjme_rom_libraryCache cache
;
143 * Closes the library.
145 * @param inLibrary The library being closed.
146 * @return Any resultant error, if any.
149 typedef sjme_errorCode (*sjme_rom_libraryCloseFunc
)(
150 sjme_attrInNotNull sjme_rom_library inLibrary
);
153 * Initializes the library.
155 * @param inLibrary The input library.
156 * @param data Any data passed to init.
157 * @return Any resultant error, if any.
160 typedef sjme_errorCode (*sjme_rom_libraryInitFunc
)(
161 sjme_attrInNotNull sjme_rom_library inLibrary
,
162 sjme_attrInNullable sjme_pointer data
);
164 typedef sjme_errorCode (*sjme_rom_libraryPathFunc
)();
167 * Access the direct raw data of a given library.
169 * @param inLibrary The library to access in a raw fashion.
170 * @param dest The destination buffer.
171 * @param srcPos The source position within the file.
172 * @param length The number of bytes to read.
173 * @return Any resultant error, if any.
176 typedef sjme_errorCode (*sjme_rom_libraryRawData
)(
177 sjme_attrInNotNull sjme_rom_library inLibrary
,
178 sjme_attrOutNotNullBuf(length
) sjme_pointer dest
,
179 sjme_attrInPositive sjme_jint srcPos
,
180 sjme_attrInPositive sjme_jint length
);
183 * Returns the raw size of the library, also can be used to check if raw
184 * access is supported in opaque handlers.
186 * @param inLibrary The input library
187 * @param outSize The output raw size, if the result is @c -1 then it indicates
188 * that this operation is not supported on the given library and it should
189 * not try to access resources and otherwise using raw data access.
190 * @return Any resultant error code.
193 typedef sjme_errorCode (*sjme_rom_libraryRawSizeFunc
)(
194 sjme_attrInNotNull sjme_rom_library inLibrary
,
195 sjme_attrOutNotNull sjme_jint
* outSize
);
198 * Opens the given resource as a stream.
200 * @param inLibrary The library to read the resource from.
201 * @param outStream The resultant stream for accessing data.
202 * @param resourceName The name of the resource.
203 * @return Any resultant error code.
206 typedef sjme_errorCode (*sjme_rom_libraryResourceStreamFunc
)(
207 sjme_attrInNotNull sjme_rom_library inLibrary
,
208 sjme_attrOutNotNull sjme_stream_input
* outStream
,
209 sjme_attrInNotNull sjme_lpcstr resourceName
);
212 * Obtains the default launch parameters from the given suite.
214 * @param inPool The pool to allocate within.
215 * @param inSuite The suite to get the launch parameters from.
216 * @param outMainClass The main class to launch.
217 * @param outMainArgs The arguments to pass to the main class.
218 * @param outById The main class path by ID.
219 * @param outByName The main class path by name.
220 * @return Any resultant error, if any.
223 typedef sjme_errorCode (*sjme_rom_suiteDefaultLaunchFunc
)(
224 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
225 sjme_attrInNotNull sjme_rom_suite inSuite
,
226 sjme_attrOutNotNull sjme_lpstr
* outMainClass
,
227 sjme_attrOutNotNull sjme_list_sjme_lpstr
** outMainArgs
,
228 sjme_attrOutNotNull sjme_list_sjme_jint
** outById
,
229 sjme_attrOutNotNull sjme_list_sjme_lpstr
** outByName
);
232 * Function used to initialize the suite.
234 * @param inSuite The input suite.
235 * @param data Any data passed to init.
236 * @return Any error state.
239 typedef sjme_errorCode (*sjme_rom_suiteInitFunc
)(
240 sjme_attrInNotNull sjme_rom_suite inSuite
,
241 sjme_attrInNullable sjme_pointer data
);
244 * Returns the ID of the library for the given suite.
246 * @param functions The suite functions.
247 * @param inSuite The current suite being accessed.
248 * @param inLibrary The library to get the ID of.
249 * @param outId The output library ID, cannot be zero.
250 * @return Any resultant error code.
253 typedef sjme_errorCode (*sjme_rom_suiteLibraryIdFunc
)(
254 sjme_attrInNotNull sjme_rom_suite inSuite
,
255 sjme_attrInNotNull sjme_rom_library inLibrary
,
256 sjme_attrOutNotNull sjme_jint
* outId
);
259 * Determines the list of libraries within the suite.
261 * @param inSuite The suite the request is being made in.
262 * @param outLibraries The output library list.
263 * @return Any resultant error code.
266 typedef sjme_errorCode (*sjme_rom_suiteListLibrariesFunc
)(
267 sjme_attrInNotNull sjme_rom_suite inSuite
,
268 sjme_attrOutNotNull sjme_list_sjme_rom_library
** outLibraries
);
270 typedef sjme_errorCode (*sjme_rom_suiteLoadLibraryFunc
)();
272 struct sjme_rom_libraryFunctions
274 /** Closes the library. */
275 sjme_rom_libraryCloseFunc close
;
277 /** Initializes the library, implementation specific. */
278 sjme_rom_libraryInitFunc init
;
280 /** Function to get the path of a library. */
281 sjme_rom_libraryPathFunc path
;
283 /** Access of raw bytes of the input library. */
284 sjme_rom_libraryRawData rawData
;
286 /** The size of this library. */
287 sjme_rom_libraryRawSizeFunc rawSize
;
289 /** Access a resource as a stream. */
290 sjme_rom_libraryResourceStreamFunc resourceStream
;
293 struct sjme_rom_suiteFunctions
295 /** Optional default launch parameters. */
296 sjme_rom_suiteDefaultLaunchFunc defaultLaunch
;
298 /** Initialize suite cache. */
299 sjme_rom_suiteInitFunc init
;
301 /** Returns the ID of the given library. */
302 sjme_rom_suiteLibraryIdFunc libraryId
;
304 /** Lists the libraries in the suite. */
305 sjme_rom_suiteListLibrariesFunc list
;
307 /** Loads a single library. */
308 sjme_rom_suiteLoadLibraryFunc loadLibrary
;
311 struct sjme_rom_suiteBase
314 sjme_nvm_commonBase common
;
317 const sjme_rom_suiteFunctions
* functions
;
319 /** The handle, may be to a seekable. */
322 /** Internal cache, used by suite implementations. */
323 sjme_rom_suiteCache cache
;
327 * Initializes a library from a Zip that is already loaded.
329 * @param pool The pool to use for allocations.
330 * @param outLibrary The resultant library.
331 * @param libName The library name.
332 * @param prefix Optional prefix when accessing Zip contents.
333 * @param zip The Zip to access as a library.
334 * @return Any resultant error, if any.
337 sjme_errorCode
sjme_rom_libraryFromZip(
338 sjme_attrInNotNull sjme_alloc_pool
* pool
,
339 sjme_attrOutNotNull sjme_rom_library
* outLibrary
,
340 sjme_attrInNotNull sjme_lpcstr libName
,
341 sjme_attrInNullable sjme_lpcstr prefix
,
342 sjme_attrInNotNull sjme_zip zip
);
345 * Initializes a library from a Zip that is in memory, this will load the Zip.
347 * @param pool The pool to use for allocations.
348 * @param outLibrary The resultant library.
349 * @param libName The library name.
350 * @param base The base address where the Zip is.
351 * @param length The length of the Zip.
352 * @return Any resultant error, if any.
355 sjme_errorCode
sjme_rom_libraryFromZipMemory(
356 sjme_attrInNotNull sjme_alloc_pool
* pool
,
357 sjme_attrOutNotNull sjme_rom_library
* outLibrary
,
358 sjme_attrInNotNull sjme_lpcstr libName
,
359 sjme_attrInNotNull sjme_cpointer base
,
360 sjme_attrInPositive sjme_jint length
);
363 * Initializes a library from a Zip that is a given seekable, this will load
366 * @param pool The pool to use for allocations.
367 * @param outLibrary The resultant library.
368 * @param libName The library name.
369 * @param seekable The seekable to access the Zip through.
370 * @return Any resultant error, if any.
373 sjme_errorCode
sjme_rom_libraryFromZipSeekable(
374 sjme_attrInNotNull sjme_alloc_pool
* pool
,
375 sjme_attrOutNotNull sjme_rom_library
* outLibrary
,
376 sjme_attrInNotNull sjme_lpcstr libName
,
377 sjme_attrInNotNull sjme_seekable seekable
);
380 * Calculates the hash of the given library.
382 * @param library The library to calculate the hash of.
383 * @param outHash The output hash.
384 * @return On any resultant error.
387 sjme_errorCode
sjme_rom_libraryHash(
388 sjme_attrInNotNull sjme_rom_library library
,
389 sjme_attrOutNotNull sjme_jint
* outHash
);
392 * Makes a virtual library from the given functions.
394 * @param pool The pool to allocate within.
395 * @param outLibrary The output library.
396 * @param libName The library name.
397 * @param data Any data to forward to the initializer.
398 * @param inFunctions The functions which define how to access the library.
399 * @param inFrontEnd Input front end initialization, is optional.
400 * @return Any error code.
403 sjme_errorCode
sjme_rom_libraryNew(
404 sjme_attrInNotNull sjme_alloc_pool
* pool
,
405 sjme_attrOutNotNull sjme_rom_library
* outLibrary
,
406 sjme_attrInNotNull sjme_lpcstr libName
,
407 sjme_attrInNullable sjme_pointer data
,
408 sjme_attrInNotNull
const sjme_rom_libraryFunctions
* inFunctions
,
409 sjme_attrInNullable
const sjme_frontEnd
* copyFrontEnd
);
412 * Reads from a library directly to access its raw bytes.
414 * @param library The library to access.
415 * @param destPtr The destination buffer.
416 * @param srcPos The source position.
417 * @param length The number of bytes to read.
418 * @return Any resultant error if any.
421 sjme_errorCode
sjme_rom_libraryRawRead(
422 sjme_attrInNotNull sjme_rom_library library
,
423 sjme_attrOutNotNullBuf(length
) sjme_pointer destPtr
,
424 sjme_attrInPositive sjme_jint srcPos
,
425 sjme_attrInPositive sjme_jint length
);
428 * Reads from a library directly to access its raw bytes, this variant of
429 * the function provides more control over offsets so that it can more easily
430 * be used with iterators and such.
432 * @param library The library to access.
433 * @param destPtr The destination buffer.
434 * @param destOffset The offset into the destination.
435 * @param srcPos The source position.
436 * @param srcOffset The source offset.
437 * @param length The number of bytes to read.
438 * @return Any resultant error if any.
441 sjme_errorCode
sjme_rom_libraryRawReadIter(
442 sjme_attrInNotNull sjme_rom_library library
,
443 sjme_attrOutNotNullBuf(length
) void* destPtr
,
444 sjme_attrInPositive sjme_jint destOffset
,
445 sjme_attrInPositive sjme_jint srcPos
,
446 sjme_attrInPositive sjme_jint srcOffset
,
447 sjme_attrInPositive sjme_jint length
);
450 * Returns the raw size of the library.
452 * @param library The library to get the raw size of.
453 * @param outSize The resultant size.
454 * @return Any resultant error code.
457 sjme_errorCode
sjme_rom_libraryRawSize(
458 sjme_attrInNotNull sjme_rom_library library
,
459 sjme_attrOutNotNull sjme_jint
* outSize
);
462 * Obtains the given resource as a stream.
464 * @param library The library to get the resource from.
465 * @param outStream The resultant stream.
466 * @param rcName The name of the resource to obtain.
467 * @return On any errors, if any.
470 sjme_errorCode
sjme_rom_libraryResourceAsStream(
471 sjme_attrInNotNull sjme_rom_library library
,
472 sjme_attrOutNotNull sjme_stream_input
* outStream
,
473 sjme_attrInNotNull sjme_lpcstr rcName
);
476 * Resolves the class path library by their ID.
478 * @param inSuite The suite to look within.
479 * @param inIds The IDs to obtain.
480 * @param outLibs The output libraries.
481 * @return Any resultant error state.
484 sjme_errorCode
sjme_rom_resolveClassPathById(
485 sjme_attrInNotNull sjme_rom_suite inSuite
,
486 sjme_attrInNotNull
const sjme_list_sjme_jint
* inIds
,
487 sjme_attrOutNotNull sjme_list_sjme_rom_library
** outLibs
);
490 * Resolves the class path library by their name.
492 * @param inSuite The suite to look within.
493 * @param inNames The names to obtain.
494 * @param outLibs The output libraries.
495 * @return Any resultant error state.
498 sjme_errorCode
sjme_rom_resolveClassPathByName(
499 sjme_attrInNotNull sjme_rom_suite inSuite
,
500 sjme_attrInNotNull
const sjme_list_sjme_lpcstr
* inNames
,
501 sjme_attrOutNotNull sjme_list_sjme_rom_library
** outLibs
);
504 * Obtains the default launch parameters from the given suite.
506 * @param inPool The pool to allocate within.
507 * @param inSuite The suite to get the launch parameters from.
508 * @param outMainClass The main class to launch.
509 * @param outMainArgs The arguments to pass to the main class.
510 * @param outById The main class path by ID.
511 * @param outByName The main class path by name.
512 * @return Any resultant error, if any.
515 sjme_errorCode
sjme_rom_suiteDefaultLaunch(
516 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
517 sjme_attrInNotNull sjme_rom_suite inSuite
,
518 sjme_attrOutNotNull sjme_lpstr
* outMainClass
,
519 sjme_attrOutNotNull sjme_list_sjme_lpstr
** outMainArgs
,
520 sjme_attrOutNotNull sjme_list_sjme_jint
** outById
,
521 sjme_attrOutNotNull sjme_list_sjme_lpstr
** outByName
);
524 * Combines multiple suites into one.
526 * @param pool The pool to allocate within.
527 * @param outSuite The output suite.
528 * @param inSuites The input suites.
529 * @param numInSuites The number of input suites.
532 sjme_errorCode
sjme_rom_suiteFromMerge(
533 sjme_attrInNotNull sjme_alloc_pool
* pool
,
534 sjme_attrOutNotNull sjme_rom_suite
* outSuite
,
535 sjme_attrInNotNull sjme_rom_suite
* inSuites
,
536 sjme_attrInPositive sjme_jint numInSuites
);
539 * Scans the payload for suites
541 * @param pool The pool to allocate within.
542 * @param outSuite The output resultant suite, if there would be nothing in
543 * here then this outputs @c NULL .
544 * @param payloadConfig The payload configuration used.
545 * @return Any error status.
548 sjme_errorCode
sjme_rom_suiteFromPayload(
549 sjme_attrInNotNull sjme_alloc_pool
* pool
,
550 sjme_attrOutNotNull sjme_rom_suite
* outSuite
,
551 sjme_attrInNotNull
const sjme_payload_config
* payloadConfig
);
554 * Initializes a suite from a Zip.
556 * @param pool The pool to use for allocations.
557 * @param outSuite The resultant suite.
558 * @param seekable The seekable to access the Zip through.
559 * @return Any resultant error, if any.
562 sjme_errorCode
sjme_rom_suiteFromZipSeekable(
563 sjme_attrInNotNull sjme_alloc_pool
* pool
,
564 sjme_attrOutNotNull sjme_rom_suite
* outSuite
,
565 sjme_attrInNotNull sjme_seekable seekable
);
568 * Returns all of the libraries which are available within this suite.
570 * @param inSuite The input suite.
571 * @param outLibs The resultant libraries.
572 * @return Any resultant error state.
575 sjme_errorCode
sjme_rom_suiteLibraries(
576 sjme_attrInNotNull sjme_rom_suite inSuite
,
577 sjme_attrOutNotNull sjme_list_sjme_rom_library
** outLibs
);
580 * Makes a virtual suite from the given functions.
582 * @param pool The pool to allocate within.
583 * @param outSuite The output suite.
584 * @param data Any data to pass to the initializer.
585 * @param inFunctions The functions which define how to access the suite.
586 * @param copyFrontEnd Input front end initialization, is optional.
587 * @return Any error code.
590 sjme_errorCode
sjme_rom_suiteNew(
591 sjme_attrInNotNull sjme_alloc_pool
* pool
,
592 sjme_attrOutNotNull sjme_rom_suite
* outSuite
,
593 sjme_attrInNullable sjme_pointer data
,
594 sjme_attrInNotNull
const sjme_rom_suiteFunctions
* inFunctions
,
595 sjme_attrInNullable
const sjme_frontEnd
* copyFrontEnd
);
597 /*--------------------------------------------------------------------------*/
601 #ifdef SJME_CXX_SQUIRRELJME_ROM_H
603 #undef SJME_CXX_SQUIRRELJME_ROM_H
604 #undef SJME_CXX_IS_EXTERNED
605 #endif /* #ifdef SJME_CXX_SQUIRRELJME_ROM_H */
606 #endif /* #ifdef __cplusplus */
608 #endif /* SQUIRRELJME_ROM_H */