Fix Sprite object collection

This commit is contained in:
DataHoarder 2021-12-30 02:09:08 +01:00
parent 98a06228ed
commit f10edc70c8
2 changed files with 40 additions and 13 deletions

28
src/ObjectCollection.php Normal file
View file

@ -0,0 +1,28 @@
<?php
namespace swf2ass;
class ObjectCollection {
/** @var ObjectDefinition[] */
private array $objects = [];
public function add(ObjectDefinition $object){
if(isset($this->objects[$object->getObjectId()])){
throw new \Exception("Object " . $object->getObjectId() . " already exists");
}
$this->objects[$object->getObjectId()] = $object;
}
public function get($objectId) : ?ObjectDefinition {
return $this->objects[$objectId] ?? null;
}
/**
* @return ObjectDefinition[]
*/
public function getAll() : array{
return $this->objects;
}
}

View file

@ -6,13 +6,14 @@ namespace swf2ass;
class SWFTreeProcessor {
protected \DOMElement $root;
protected ViewLayout $layout;
/** @var ObjectDefinition[] */
protected array $objects = [];
protected ObjectCollection $objects;
protected ?\DOMElement $currentElement = null;
protected int $frame;
public function __construct(int $objectId, \DOMElement $root) {
public function __construct(int $objectId, \DOMElement $root, ?ObjectCollection $objects = null) {
$this->objects = $objects ?? new ObjectCollection();
$this->frame = 0;
$this->root = $root;
$this->layout = new ViewLayout($objectId, null);
@ -45,10 +46,7 @@ class SWFTreeProcessor {
return $this->currentElement;
}
/**
* @return ObjectDefinition[]
*/
public function getObjects(): array {
public function getObjectCollection(): ObjectCollection {
return $this->objects;
}
@ -65,13 +63,13 @@ class SWFTreeProcessor {
case "DefineShape4":
case "DefineShape5":
$shape = ShapeDefinition::fromXML($node);
$this->objects[$shape->getObjectId()] = $shape;
$this->objects->add($shape);
break;
case "DefineSprite":
$objectID = (int)$node->getAttribute("objectID");
$framesCount = (int)$node->getAttribute("frames");
$spriteTree = new SWFTreeProcessor($objectID, $node);
$spriteTree = new SWFTreeProcessor($objectID, $node, $this->objects);
$actions = new ActionList();
/** @var ViewFrame[] $frames */
$frames = [];
@ -81,17 +79,18 @@ class SWFTreeProcessor {
$sprite = new SpriteDefinition($objectID, $frames);
$this->objects[$sprite->getObjectId()] = $sprite;
$this->objects->add($sprite);
break;
case "DefineBitsLossless":
case "DefineBitsLossless2":
$bitmap = BitmapDefinition::fromXML($node);
$this->objects[$bitmap->getObjectId()] = $bitmap;
$this->objects->add($bitmap);
break;
case "DefineBitsJPEG2":
case "DefineBitsJPEG3":
$bitmap = JPEGBitmapDefinition::fromXML($node);
$this->objects[$bitmap->getObjectId()] = $bitmap;
$this->objects->add($bitmap);
break;
case "RemoveObject":
case "RemoveObject2":
@ -106,7 +105,7 @@ class SWFTreeProcessor {
$replace = $node->getAttribute("replace") === "1";
$object = $objectID === null ? $this->layout->get($depth) : ($this->objects[$objectID] ?? null);
$object = $objectID === null ? $this->layout->get($depth) : $this->objects->get($objectID);
if ($object === null) {
var_dump("Object oid:$objectID depth:$depth not found");
/*if($replace){