1212 * all copies or substantial portions of the Software.
1313 */
1414
15- import 'package:meta/meta.dart' ;
16-
1715import '../../freewig.dart' ;
1816import '../binary_reader.dart' ;
1917import 'media.dart' ;
2018
2119/// A [Cartridge] represents the GWC file
22- @immutable
2320class Cartridge {
2421 /// The altitude of the start coordinate
2522 final double altitude;
@@ -60,16 +57,20 @@ class Cartridge {
6057 /// Completion code (can be encrypted with lua)
6158 final String completionCode;
6259
63- /// Map of all [Media] objects
64- final Map <int , Media > mediaObjects;
65-
6660 final int _splashScreenId;
6761
6862 final int _smallIconId;
6963
64+ final BinaryReader _source;
65+
66+ final Map <int , int > _references;
67+
68+ var _lastObject = - 1 ;
69+
70+ Media ? _lastMedia;
71+
7072 Cartridge ._(
7173 this .cartridgeGuid,
72- this .mediaObjects,
7374 this .altitude,
7475 this .author,
7576 this .cartridgeDesc,
@@ -84,11 +85,13 @@ class Cartridge {
8485 this .startLocationDesc,
8586 this .typeOfCartridge,
8687 this .version,
88+ this ._references,
89+ this ._source,
8790 );
8891
8992 /// Reading the GWC file and create a [Cartridge] or null in case of any
9093 /// parsing error.
91- factory Cartridge (BinaryReader reader) {
94+ static Cartridge ? create (BinaryReader reader) {
9295 try {
9396 final count = reader.getUShort ();
9497 final references = < int , int > {};
@@ -129,14 +132,8 @@ class Cartridge {
129132
130133 final completionCode = reader.getASCIIZ ();
131134
132- // initialise objects after cartridge data is loaded
133- final objects = < int , Media > {};
134- references.forEach ((index, address) =>
135- {objects.putIfAbsent (index, () => Media (reader, index, address))});
136-
137135 return Cartridge ._(
138136 cartridgeGuid,
139- objects,
140137 altitude,
141138 author,
142139 cartridgeDesc,
@@ -154,6 +151,8 @@ class Cartridge {
154151 startLocationDesc,
155152 typeOfCartridge,
156153 version,
154+ references,
155+ reader,
157156 );
158157 } on Exception catch (ex) {
159158 print ('Exception: $ex ' );
@@ -166,22 +165,42 @@ class Cartridge {
166165 identical (this , other) ||
167166 other is Cartridge &&
168167 runtimeType == other.runtimeType &&
169- cartridgeGuid == other.cartridgeGuid &&
170- mediaObjects == other.mediaObjects;
168+ cartridgeGuid == other.cartridgeGuid;
171169
172170 @override
173- int get hashCode => cartridgeGuid.hashCode ^ mediaObjects.hashCode;
171+ int get hashCode => cartridgeGuid.hashCode;
172+
173+ /// get Media with objectId or null, if not available
174+ Media ? getMedia (int objectId) {
175+ if (_lastObject == objectId && _lastMedia != null ) {
176+ return _lastMedia! ;
177+ }
178+
179+ if (_references.containsKey (objectId)) {
180+ final address = _references[objectId];
181+ final media = Media .create (_source, objectId, address! );
182+
183+ if (media != null ) {
184+ if (media.data.length < 128000 ) {
185+ _lastObject = objectId;
186+ _lastMedia = media;
187+ }
188+
189+ return media;
190+ }
191+ }
192+ return null ;
193+ }
194+
195+ /// get the count of attached media
196+ int get mediaCount => _references.length;
174197
175198 /// get splash screen media or null if not available
176- Media get splashScreen => mediaObjects.containsKey (_splashScreenId)
177- ? mediaObjects[_splashScreenId]
178- : null ;
199+ Media ? get splashScreen => getMedia (_splashScreenId);
179200
180201 /// get small icon media or null if not available
181- Media get smallIcon => mediaObjects.containsKey (_smallIconId)
182- ? mediaObjects[_smallIconId]
183- : null ;
202+ Media ? get smallIcon => getMedia (_smallIconId);
184203
185204 /// get luac media or null if not available
186- Media get luac => mediaObjects. containsKey (0 ) ? mediaObjects[ 0 ] : null ;
205+ Media ? get luac => getMedia (0 );
187206}
0 commit comments