forked from openfl/openfl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWFTimelineContainer.hx
601 lines (530 loc) · 19.5 KB
/
SWFTimelineContainer.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package format.swf;
import format.abc.Data.ABCData;
import format.abc.Data.ClassDef;
import format.abc.Data.Field;
import format.abc.Data.IName;
import format.abc.Data.Index;
import format.abc.Data.Name;
import format.abc.Data.Namespace;
import format.SWF;
import format.swf.data.SWFFrameLabel;
import format.swf.data.SWFRawTag;
import format.swf.data.SWFRecordHeader;
import format.swf.data.SWFScene;
import format.swf.data.consts.SoundCompression;
import format.swf.events.SWFErrorEvent;
import format.swf.events.SWFEventDispatcher;
import format.swf.events.SWFProgressEvent;
import format.swf.events.SWFWarningEvent;
import format.swf.factories.ISWFTagFactory;
import format.swf.factories.SWFTagFactory;
import format.swf.tags.IDefinitionTag;
import format.swf.tags.IDisplayListTag;
import format.swf.tags.ITag;
import format.swf.tags.TagDefineMorphShape;
import format.swf.tags.TagDefineScalingGrid;
import format.swf.tags.TagDefineSceneAndFrameLabelData;
import format.swf.tags.TagDoABC;
import format.swf.tags.TagEnd;
import format.swf.tags.TagFrameLabel;
import format.swf.tags.TagJPEGTables;
import format.swf.tags.TagPlaceObject;
import format.swf.tags.TagPlaceObject2;
import format.swf.tags.TagPlaceObject3;
import format.swf.tags.TagRemoveObject;
import format.swf.tags.TagRemoveObject2;
import format.swf.tags.TagSetBackgroundColor;
import format.swf.tags.TagShowFrame;
import format.swf.tags.TagSoundStreamBlock;
import format.swf.tags.TagSoundStreamHead;
import format.swf.tags.TagSoundStreamHead2;
import format.swf.tags.TagSymbolClass;
import format.swf.timeline.Frame;
import format.swf.timeline.FrameObject;
import format.swf.timeline.Layer;
import format.swf.timeline.LayerStrip;
import format.swf.timeline.Scene;
import format.swf.timeline.SoundStream;
import format.swf.utils.StringUtils;
import flash.errors.Error;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.utils.Endian;
//import flash.utils.getTimer;
class SWFTimelineContainer extends SWFEventDispatcher
{
// We're just being lazy here.
public static var TIMEOUT:Int = 50;
public static var AUTOBUILD_LAYERS:Bool = false;
public static var EXTRACT_SOUND_STREAM:Bool = true;
public static var scalingGrids(default, null):Map<Int, Int>;
public var tags(default, null):Array<ITag>;
public var tagsRaw(default, null):Array<SWFRawTag>;
public var dictionary(default, null):Map<Int, Int>;
public var scenes(default, null):Array<Scene>;
public var frames(default, null):Array<Frame>;
public var layers(default, null):Array<Layer>;
public var soundStream(default, null):SoundStream;
public var frameLabels(default, null):Map<Int, String>;
public var frameIndexes(default, null):Map<String, Int>;
private var currentFrame:Frame;
private var hasSoundStream:Bool;
private var enterFrameProvider:Sprite;
private var eof:Bool;
private var _tmpData:SWFData;
private var _tmpVersion:Int;
private var _tmpTagIterator:Int = 0;
public var tagFactory:ISWFTagFactory;
private var rootTimelineContainer:SWFTimelineContainer;
public var backgroundColor:Int;
public var jpegTablesTag:TagJPEGTables;
public var abcTag:TagDoABC;
public var abcData:ABCData;
public var pcode:Array<Dynamic>;
public var abcClasses(default, null):Map<Int, ClassDef>;
public function new()
{
super();
if (scalingGrids == null) scalingGrids = new Map<Int, Int>();
backgroundColor = 0xffffff;
tags = new Array<ITag>();
tagsRaw = new Array<SWFRawTag>();
dictionary = new Map<Int, Int>();
scenes = new Array<Scene>();
frames = new Array<Frame>();
layers = new Array<Layer>();
tagFactory = new SWFTagFactory();
rootTimelineContainer = this;
enterFrameProvider = new Sprite();
}
public function getCharacter(characterId:Int):IDefinitionTag {
var tagIndex:Int = rootTimelineContainer.dictionary.get (characterId);
if (tagIndex >= 0 && tagIndex < rootTimelineContainer.tags.length) {
return cast rootTimelineContainer.tags[tagIndex];
}
return null;
}
public function getScalingGrid(characterId:Int):TagDefineScalingGrid {
//trace(characterId + " getScalingGrid" );
//trace(scalingGrids);
if (scalingGrids.exists (characterId)) {
return cast rootTimelineContainer.tags[scalingGrids.get (characterId)];
}
return null;
}
public function parseTags(data:SWFData, version:Int):Void {
var tag:ITag;
parseTagsInit(data, version);
while ((tag = parseTag(data)) != null && tag.type != TagEnd.TYPE) {};
parseTagsFinalize();
}
public function parseTagsAsync(data:SWFData, version:Int):Void {
parseTagsInit(data, version);
enterFrameProvider.addEventListener(Event.ENTER_FRAME, parseTagsAsyncHandler);
}
private function parseTagsAsyncHandler(event:Event):Void {
enterFrameProvider.removeEventListener(Event.ENTER_FRAME, parseTagsAsyncHandler);
if(dispatchEvent(new SWFProgressEvent(SWFProgressEvent.PROGRESS, _tmpData.position, _tmpData.length, false, true))) {
parseTagsAsyncInternal();
}
}
private function parseTagsAsyncInternal():Void {
var tag:ITag;
var time:Int = flash.Lib.getTimer();
while ((tag = parseTag(_tmpData, true)) != null && tag.type != TagEnd.TYPE) {
if((flash.Lib.getTimer() - time) > TIMEOUT) {
enterFrameProvider.addEventListener(Event.ENTER_FRAME, parseTagsAsyncHandler);
return;
}
}
parseTagsFinalize();
if(eof) {
dispatchEvent(new SWFErrorEvent(SWFErrorEvent.ERROR, SWFErrorEvent.REASON_EOF));
} else {
dispatchEvent(new SWFProgressEvent(SWFProgressEvent.PROGRESS, _tmpData.position, _tmpData.length));
dispatchEvent(new SWFProgressEvent(SWFProgressEvent.COMPLETE, _tmpData.position, _tmpData.length));
}
}
private function parseTagsInit(data:SWFData, version:Int):Void {
tags = new Array<ITag>();
frames = new Array<Frame>();
layers = new Array<Layer>();
dictionary = new Map<Int, Int>();
currentFrame = new Frame();
frameLabels = new Map<Int, String>();
frameIndexes = new Map<String, Int>();
hasSoundStream = false;
_tmpData = data;
_tmpVersion = version;
//trace(":: Container parseTagsInit");
}
private function parseTag(data:SWFData, async:Bool = false):ITag {
var pos:Int = data.position;
// Bail out if eof
eof = (pos >= data.length);
if(eof) {
trace("WARNING: end of file encountered, no end tag.");
return null;
}
var tagRaw:SWFRawTag = data.readRawTag();
var tagHeader:SWFRecordHeader = tagRaw.header;
var tag:ITag = tagFactory.create(tagHeader.type);
try {
if(Std.is (tag, SWFTimelineContainer)) {
var timelineContainer:SWFTimelineContainer = cast tag;
// Currently, the only SWFTimelineContainer (other than the SWF root
// itself) is TagDefineSprite (MovieClips have their own timeline).
// Inject the current tag factory there.
timelineContainer.tagFactory = tagFactory;
timelineContainer.rootTimelineContainer = this;
}
// Parse tag
tag.parse(data, tagHeader.contentLength, _tmpVersion, async);
} catch(e:Error) {
// If we get here there was a problem parsing this particular tag.
// Corrupted SWF, possible SWF exploit, or obfuscated SWF.
// TODO: register errors and warnings
trace("WARNING: parse error: " + e.message + ", Tag: " + tag.name + ", Index: " + tags.length);
throw(e);
}
// Register tag
tags.push(tag);
tagsRaw.push(tagRaw);
// Build dictionary and display list etc
processTag(tag);
// Adjust position (just in case the parser under- or overflows)
var position:UInt = pos + tagHeader.tagLength;
if(data.position != position) {
var index:Int = tags.length - 1;
var excessBytes:Int = data.position - (pos + tagHeader.tagLength);
var eventType:String = (excessBytes < 0) ? SWFWarningEvent.WARN_UNDERFLOW : SWFWarningEvent.WARN_OVERFLOW;
var eventData:Dynamic = {
pos: pos,
bytes: (excessBytes < 0) ? -excessBytes : excessBytes
};
if(rootTimelineContainer == this) {
trace("WARNING: excess bytes: " + excessBytes + ", " +
"Tag: " + tag.name + ", " +
"Index: " + index
);
} else {
eventData.indexRoot = rootTimelineContainer.tags.length;
trace("WARNING: excess bytes: " + excessBytes + ", " +
"Tag: " + tag.name + ", " +
"Index: " + index + ", " +
"IndexRoot: " + eventData.indexRoot
);
}
var event:SWFWarningEvent = new SWFWarningEvent(eventType, index, eventData, false, true);
var cancelled:Bool = !dispatchEvent(event);
if (cancelled) {
tag = null;
}
data.position = pos + tagHeader.tagLength;
}
return tag;
}
private function parseTagsFinalize():Void {
if(soundStream != null && soundStream.data.length == 0) {
soundStream = null;
}
if(AUTOBUILD_LAYERS) {
// TODO: This needs to go into processTags()
buildLayers();
}
//trace(":: Container parseTagsFinalize");
}
public function publishTags(data:SWFData, version:Int):Void {
var tag:ITag;
var tagRaw:SWFRawTag;
for (i in 0...tags.length) {
tag = tags[i];
tagRaw = (i < tagsRaw.length) ? tagsRaw[i] : null;
publishTag(data, tag, tagRaw, version);
}
}
public function publishTagsAsync(data:SWFData, version:Int):Void {
_tmpData = data;
_tmpVersion = version;
_tmpTagIterator = 0;
enterFrameProvider.addEventListener(Event.ENTER_FRAME, publishTagsAsyncHandler);
}
private function publishTagsAsyncHandler(event:Event):Void {
enterFrameProvider.removeEventListener(Event.ENTER_FRAME, publishTagsAsyncHandler);
if(dispatchEvent(new SWFProgressEvent(SWFProgressEvent.PROGRESS, _tmpTagIterator, tags.length))) {
publishTagsAsyncInternal();
}
}
private function publishTagsAsyncInternal():Void {
var tag:ITag;
var tagRaw:SWFRawTag;
var time:Int = flash.Lib.getTimer();
do {
tag = (_tmpTagIterator < tags.length) ? tags[_tmpTagIterator] : null;
tagRaw = (_tmpTagIterator < tagsRaw.length) ? tagsRaw[_tmpTagIterator] : null;
publishTag(_tmpData, tag, tagRaw, _tmpVersion);
_tmpTagIterator++;
if((flash.Lib.getTimer() - time) > TIMEOUT) {
enterFrameProvider.addEventListener(Event.ENTER_FRAME, publishTagsAsyncHandler);
return;
}
}
while (tag.type != TagEnd.TYPE);
dispatchEvent(new SWFProgressEvent(SWFProgressEvent.PROGRESS, _tmpTagIterator, tags.length));
dispatchEvent(new SWFProgressEvent(SWFProgressEvent.COMPLETE, _tmpTagIterator, tags.length));
}
public function publishTag(data:SWFData, tag:ITag, rawTag:SWFRawTag, version:Int):Void {
try {
tag.publish(data, version);
}
catch (e:Error) {
trace("WARNING: publish error: " + e.message + " (tag: " + tag.name + ")");
if(rawTag != null) {
rawTag.publish(data);
} else {
trace("FATAL: publish error: No raw tag fallback");
}
}
}
private function processTag(tag:ITag):Void {
//trace(" ..Process: " + tag.type + " - name: " + tag.name);
var currentTagIndex:Int = tags.length - 1;
if(Std.is (tag, IDefinitionTag)) {
processDefinitionTag(cast tag, currentTagIndex);
return;
} else if(Std.is (tag, IDisplayListTag)) {
processDisplayListTag(cast tag, currentTagIndex);
return;
}
switch(cast (tag.type, Int)) {
// Frame labels and scenes
case TagFrameLabel.TYPE, TagDefineSceneAndFrameLabelData.TYPE:
processFrameLabelTag(tag, currentTagIndex);
// Sound stream
case TagSoundStreamHead.TYPE, TagSoundStreamHead2.TYPE, TagSoundStreamBlock.TYPE:
if(EXTRACT_SOUND_STREAM) {
processSoundStreamTag(tag, currentTagIndex);
}
// Background color
case TagSetBackgroundColor.TYPE:
processBackgroundColorTag(cast tag, currentTagIndex);
// Global JPEG Table
case TagJPEGTables.TYPE:
processJPEGTablesTag(cast tag, currentTagIndex);
// Scale-9 grids
case TagDefineScalingGrid.TYPE:
processScalingGridTag(cast tag, currentTagIndex);
// Actionscript 3
case TagDoABC.TYPE:
processAS3Tag(cast tag, currentTagIndex);
}
}
private function processDefinitionTag(tag:IDefinitionTag, currentTagIndex:Int):Void {
if(tag.characterId > 0) {
// Register definition tag in dictionary
// key: character id
// value: definition tag index
dictionary.set (tag.characterId, currentTagIndex);
// Register character id in the current frame's character array
currentFrame.characters.push(tag.characterId);
}
}
private function processDisplayListTag(tag:IDisplayListTag, currentTagIndex:Int):Void {
switch(cast (tag.type, Int)) {
case TagShowFrame.TYPE:
currentFrame.tagIndexEnd = currentTagIndex;
if (currentFrame.label == null && frameLabels.exists (currentFrame.frameNumber)) {
currentFrame.label = frameLabels.get (currentFrame.frameNumber);
}
frames.push(currentFrame);
currentFrame = currentFrame.clone();
currentFrame.frameNumber = frames.length;
currentFrame.tagIndexStart = currentTagIndex + 1;
case TagPlaceObject.TYPE, TagPlaceObject2.TYPE, TagPlaceObject3.TYPE:
currentFrame.placeObject(currentTagIndex, cast tag);
case TagRemoveObject.TYPE, TagRemoveObject2.TYPE:
currentFrame.removeObject(cast tag);
}
}
private function processFrameLabelTag(tag:ITag, currentTagIndex:Int):Void {
switch(cast (tag.type, Int)) {
case TagDefineSceneAndFrameLabelData.TYPE:
// This seems to be unnecessary (at least for frame labels) because frameLabels appear again as TagFrameLabel, and TagDefineSceneAndFrameLabelData does not appear on every MC
var tagSceneAndFrameLabelData:TagDefineSceneAndFrameLabelData = cast tag;
var i:Int;
for(i in 0...tagSceneAndFrameLabelData.frameLabels.length) {
var frameLabel:SWFFrameLabel = tagSceneAndFrameLabelData.frameLabels[i];
frameLabels.set (frameLabel.frameNumber, frameLabel.name);
frameIndexes.set (frameLabel.name, frameLabel.frameNumber + 1);
}
for(i in 0...tagSceneAndFrameLabelData.scenes.length) {
var scene:SWFScene = cast tagSceneAndFrameLabelData.scenes[i];
scenes.push(new Scene(scene.offset, scene.name));
}
case TagFrameLabel.TYPE:
var tagFrameLabel:TagFrameLabel = cast tag;
currentFrame.label = tagFrameLabel.frameName;
frameLabels.set(currentFrame.frameNumber, tagFrameLabel.frameName);
frameIndexes.set (tagFrameLabel.frameName, currentFrame.frameNumber + 1);
}
}
private function processSoundStreamTag(tag:ITag, currentTagIndex:Int):Void {
switch(cast (tag.type, Int)) {
case TagSoundStreamHead.TYPE, TagSoundStreamHead2.TYPE:
var tagSoundStreamHead:TagSoundStreamHead = cast tag;
soundStream = new SoundStream();
soundStream.compression = tagSoundStreamHead.streamSoundCompression;
soundStream.rate = tagSoundStreamHead.streamSoundRate;
soundStream.size = tagSoundStreamHead.streamSoundSize;
soundStream.type = tagSoundStreamHead.streamSoundType;
soundStream.numFrames = 0;
soundStream.numSamples = 0;
case TagSoundStreamBlock.TYPE:
if(soundStream != null) {
if(!hasSoundStream) {
hasSoundStream = true;
soundStream.startFrame = currentFrame.frameNumber;
}
var tagSoundStreamBlock:TagSoundStreamBlock = cast tag;
var soundData:ByteArray = tagSoundStreamBlock.soundData;
soundData.endian = Endian.LITTLE_ENDIAN;
soundData.position = 0;
switch(soundStream.compression) {
case SoundCompression.ADPCM: // ADPCM
// TODO
case SoundCompression.MP3: // MP3
var numSamples:Int = soundData.readUnsignedShort();
var seekSamples:Int = soundData.readShort();
if(numSamples > 0) {
soundStream.numSamples += numSamples;
soundStream.data.writeBytes(soundData, 4);
}
}
soundStream.numFrames++;
}
}
}
private function processBackgroundColorTag(tag:TagSetBackgroundColor, currentTagIndex:Int):Void {
backgroundColor = tag.color;
}
private function processJPEGTablesTag(tag:TagJPEGTables, currentTagIndex:Int):Void {
jpegTablesTag = tag;
}
private function processScalingGridTag(tag:TagDefineScalingGrid, currentTagIndex:Int):Void {
scalingGrids.set (tag.characterId, currentTagIndex);
}
private function processAS3Tag(tag:TagDoABC, currentTagIndex:Int):Void {
// Just store it for now
abcTag = tag;
//trace("ABC: " + tag.toString());
var bytes = #if flash haxe.io.Bytes.ofData(tag.bytes) #else tag.bytes #end;
var input = new haxe.io.BytesInput(bytes);
var reader = new format.abc.Reader(input);
//trace("Reading...");
abcData = reader.read();
pcode = new Array();
for (fn in abcData.functions) {
pcode.push(format.abc.OpReader.decode(new haxe.io.BytesInput(fn.code)));
}
}
public function buildLayers():Void {
var i:Int;
var depth:String;
var depthInt:Int;
var depths = new Map <Int, Array <Int>>();
var depthsAvailable:Array<Int> = [];
for(i in 0...frames.length) {
var frame:Frame = frames[i];
for(depth in frame.objects.keys()) {
depthInt = Std.int(depth);
var foundIndex = false;
for (index in depthsAvailable) {
if (index == depthInt) foundIndex = true; break;
}
if(foundIndex) {
(depths.get (depth)).push(frame.frameNumber);
} else {
depths.set (depth, [ frame.frameNumber ]);
depthsAvailable.push(depthInt);
}
}
}
depthsAvailable.sort(sortNumeric);
for(i in 0...depthsAvailable.length) {
var layer:Layer = new Layer(depthsAvailable[i], frames.length);
var frameIndices = depths.get (depthsAvailable[i]);
var frameIndicesLen:Int = frameIndices.length;
if(frameIndicesLen > 0) {
var curStripType:Int = LayerStrip.TYPE_EMPTY;
//var startFrameIndex:Int = uint.MAX_VALUE;
var startFrameIndex:Int = Std.int (SWFData.MAX_FLOAT_VALUE);
//var endFrameIndex:Int = uint.MAX_VALUE;
var endFrameIndex:Int = Std.int (SWFData.MAX_FLOAT_VALUE);
for(j in 0...frameIndicesLen) {
var curFrameIndex:Int = frameIndices[j];
var curFrameObject:FrameObject = frames[curFrameIndex].objects.get (layer.depth);
if(curFrameObject.isKeyframe) {
// a keyframe marks the start of a new strip: save current strip
layer.appendStrip(curStripType, startFrameIndex, endFrameIndex);
// set start of new strip
startFrameIndex = curFrameIndex;
// evaluate type of new strip (motion tween detection see below)
curStripType = (Std.is (getCharacter(curFrameObject.characterId), TagDefineMorphShape)) ? LayerStrip.TYPE_SHAPETWEEN : LayerStrip.TYPE_STATIC;
} else if(curStripType == LayerStrip.TYPE_STATIC && curFrameObject.lastModifiedAtIndex > 0) {
// if one of the matrices of an object in a static strip is
// modified at least once, we are dealing with a motion tween:
curStripType = LayerStrip.TYPE_MOTIONTWEEN;
}
// update the end of the strip
endFrameIndex = curFrameIndex;
}
layer.appendStrip(curStripType, startFrameIndex, endFrameIndex);
}
layers.push(layer);
}
for(i in 0...frames.length) {
var frameObjs = frames[i].objects;
for (depth in frameObjs.keys()) {
for (j in 0...depthsAvailable.length) {
if (depth == depthsAvailable[j]) {
frameObjs.get (depth).layer = j;
}
}
}
}
}
private function sortNumeric (a:Int, b:Int):Int {
return a - b;
}
public function toString(indent:Int = 0):String {
var i:Int;
var str:String = "";
if (tags.length > 0) {
str += "\n" + StringUtils.repeat(indent + 2) + "Tags:";
for (i in 0...tags.length) {
str += "\n" + tags[i].toString(indent + 4);
}
}
if (scenes.length > 0) {
str += "\n" + StringUtils.repeat(indent + 2) + "Scenes:";
for (i in 0...scenes.length) {
str += "\n" + scenes[i].toString(indent + 4);
}
}
if (frames.length > 0) {
str += "\n" + StringUtils.repeat(indent + 2) + "Frames:";
for (i in 0...frames.length) {
str += "\n" + frames[i].toString(indent + 4);
}
}
if (layers.length > 0) {
str += "\n" + StringUtils.repeat(indent + 2) + "Layers:";
for (i in 0...layers.length) {
str += "\n" + StringUtils.repeat(indent + 4) +
"[" + i + "] " + layers[i].toString(indent + 4);
}
}
return str;
}
}