1
0
mirror of https://github.com/Rogiel/star-replay synced 2025-12-05 22:32:46 +00:00

Added support for replay attribute events

This commit is contained in:
2016-07-24 19:52:14 -03:00
parent a7f59cbb1a
commit cd64346ee5
5 changed files with 570 additions and 1 deletions

117
src/Attribute/Attribute.php Normal file
View File

@@ -0,0 +1,117 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\StarReplay\Attribute;
class Attribute {
/**
* @var integer
*/
private $id;
/**
* @var integer
*/
private $namespace;
/**
* @var integer
*/
private $playerID;
/**
* @var string
*/
private $value;
// -----------------------------------------------------------------------------------------------------------------
/**
* Attribute constructor.
*
* @param int $id
* @param int $namespace
* @param int $playerID
* @param string $value
*/
public function __construct($id, $namespace, $playerID, $value) {
$this->id = $id;
$this->namespace = $namespace;
$this->playerID = $playerID;
$this->value = $value;
}
// -----------------------------------------------------------------------------------------------------------------
/**
* @return int
*/
public function getID() {
return $this->id;
}
/**
* @return int
*/
public function getNamespace() {
return $this->namespace;
}
/**
* @return int
*/
public function getPlayerID() {
return $this->playerID;
}
/**
* @return string
*/
public function getValue() {
return $this->value;
}
// -----------------------------------------------------------------------------------------------------------------
/**
* @return bool true if the attribute is for a player
*/
public function isPlayerAttribute() {
return $this->playerID != 16;
}
/**
* @return bool true if the attribute is for the game
*/
public function isGameAttribute() {
return $this->playerID == 16;
}
}

View File

@@ -0,0 +1,161 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\StarReplay\Attribute;
use Rogiel\MPQ\Stream\Parser\BinaryStreamParser;
use Rogiel\MPQ\Stream\Stream;
class AttributeMap {
/**
* In StarCraft 2, game atributes are stored as a player
*/
const GAME_ATTRIBUTES_PLAYER_ID = 16;
// -----------------------------------------------------------------------------------------------------------------
/**
* @var integer
*/
private $source;
/**
* @var integer
*/
private $namespace;
/**
* @var Attribute[]
*/
private $attributes;
// -----------------------------------------------------------------------------------------------------------------
public function __construct(Stream $stream) {
$stream = new BinaryStreamParser($stream);
$this->source = $stream->readByte();
$this->namespace = $stream->readUInt32();
$count = $stream->readUInt32();
for($i = 0; $i<$count; $i++) {
$namespace = $stream->readUInt32();
$attrid = $stream->readUInt32();
$scope = $stream->readByte();
$value = trim(strrev($stream->readBytes(4)));
if(!isset($this->attributes[$scope])) {
$this->attributes[$scope] = array();
}
$this->attributes[$scope][$attrid] = new Attribute(
$attrid, $namespace, $scope, $value
);
}
}
// -----------------------------------------------------------------------------------------------------------------
/**
* Get the player attribute for the given player ID
*
* @param $playerID integer the player ID
* @param $attributeID integer the attribute ID
*
* @return Attribute|null
*/
public function getPlayerAttribute($playerID, $attributeID) {
if(!isset($this->attributes[$playerID])) {
return NULL;
}
$playerScope = $this->attributes[$playerID];
if(!isset($playerScope[$attributeID])) {
return NULL;
}
return $playerScope[$attributeID];
}
/**
* Get a list of all player attributes for the given player ID
*
* @param $playerID integer the player ID
*
* @return Attribute[]
*/
public function getPlayerAttributes($playerID) {
if(!isset($this->attributes[$playerID])) {
return array();
}
return $this->attributes[$playerID];
}
/**
* Get the game attribute given by the attribute ID
*
* @param $attributeID integer the attribute ID
*
* @return null|Attribute
*/
public function getGameAttribute($attributeID) {
return $this->getPlayerAttribute(self::GAME_ATTRIBUTES_PLAYER_ID, $attributeID);
}
/**
* Get a list of all game attributes
*
* @return Attribute[]
*/
public function getGameAttributes() {
return $this->getPlayerAttributes(self::GAME_ATTRIBUTES_PLAYER_ID);
}
// -----------------------------------------------------------------------------------------------------------------
/**
* @return int
*/
public function getSource() {
return $this->source;
}
/**
* @return int
*/
public function getNamespace() {
return $this->namespace;
}
/**
* @return Attribute[]
*/
public function getAttributes() {
return $this->attributes;
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\StarReplay\Attribute;
class GameAttributes {
/**
* Controller attribute id.
*/
const CONTROLLER = 500;
/**
* Rules attribute id.
*/
const RULES = 1000;
/**
* Is premade game attribute id.
*/
const IS_PREMADE_GAME = 1001;
/**
* Parties private attribute id.
*/
const PARTIES_PRIVATE = 2000;
/**
* Parties premade attribute id.
*/
const PARTIES_PREMADE = 2001;
/**
* Game speed attribute id.
*/
const GAME_SPEED = 3000;
/**
* Lobby delay attribute id.
*/
const LOBBY_DELAY = 3006;
/**
* Game mode attribute id.
*/
const GAME_MODE = 3009;
/**
* Privacy option attribute id.
*/
const PRIVACY_OPTION = 4000;
/**
* Locked alliances attribute id.
*/
const LOCKED_ALLIANCES = 3010;
}

View File

@@ -0,0 +1,183 @@
<?php
/**
* Copyright (c) 2016, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace Rogiel\StarReplay\Attribute;
class PlayerAttributes {
/**
* Parties premade 1v1 attribute id.
*/
const PARTIES_PREMADE_1V1 = 2002;
/**
* Parties premade 2v2 attribute id.
*/
const PARTIES_PREMADE_2V2 = 2003;
/**
* Parties premade 3v3 attribute id.
*/
const PARTIES_PREMADE_3V3 = 2004;
/**
* Parties premade 4v4 attribute id.
*/
const PARTIES_PREMADE_4V4 = 2005;
/**
* Parties premade FFA attribute id.
*/
const PARTIES_PREMADE_FFA = 2006;
/**
* Parties premade 5v5 attribute id.
*/
const PARTIES_PREMADE_5V5 = 2007;
/**
* Parties premade 6v6 attribute id.
*/
const PARTIES_PREMADE_6V6 = 2008;
/**
* Parties private one attribute id.
*/
const PARTIES_PRIVATE_ONE = 2010;
/**
* Parties private two attribute id.
*/
const PARTIES_PRIVATE_TWO = 2011;
/**
* Parties private three attribute id.
*/
const PARTIES_PRIVATE_THREE = 2012;
/**
* Parties private four attribute id.
*/
const PARTIES_PRIVATE_FOUR = 2013;
/**
* Parties private five attribute id.
*/
const PARTIES_PRIVATE_FIVE = 2014;
/**
* Parties private six attribute id.
*/
const PARTIES_PRIVATE_SIX = 2015;
/**
* Parties private seven attribute id.
*/
const PARTIES_PRIVATE_SEVEN = 2016;
/**
* Parties private FFA attribute id.
*/
const PARTIES_PRIVATE_FFA = 2017;
/**
* Parties private custom attribute id.
*/
const PARTIES_PRIVATE_CUSTOM = 2018;
/**
* Parties private eight attribute id.
*/
const PARTIES_PRIVATE_EIGHT = 2019;
/**
* Parties private nine attribute id.
*/
const PARTIES_PRIVATE_NINE = 2020;
/**
* Parties private ten attribute id.
*/
const PARTIES_PRIVATE_TEN = 2021;
/**
* Parties private eleven attribute id.
*/
const PARTIES_PRIVATE_ELEVEN = 2022;
/**
* Race attribute id.
*/
const RACE = 3001;
/**
* Party color attribute id.
*/
const PARTY_COLOR = 3002;
/**
* Handicap attribute id.
*/
const HANDICAP = 3003;
/**
* AI skill attribute id.
*/
const AI_SKILL = 3004;
/**
* AI race attribute id.
*/
const AI_RACE = 3005;
/**
* Participant role attribute id.
*/
const PARTICIPANT_ROLE = 3007;
/**
* Watcher type attribute id.
*/
const WATCHER_TYPE = 3008;
/**
* AI build first attribute id.
*/
const AI_BUILD_FIRST = 3100;
/**
* AI build last attribute id.
*/
const AI_BUILD_LAST = 3300;
/**
* Using custom observer UI attribute id.
*/
const USING_CUSTOM_OBSERVER_UI = 4001;
}

View File

@@ -30,6 +30,7 @@ namespace Rogiel\StarReplay;
use Rogiel\MPQ\MPQFile;
use Rogiel\MPQ\Stream\MemoryStream;
use Rogiel\StarReplay\Attribute\AttributeMap;
use Rogiel\StarReplay\Event\AbstractEvent;
use Rogiel\StarReplay\Exception\ReplayException;
use Rogiel\StarReplay\Hydrator\GeneratedHydratorFactory;
@@ -86,6 +87,11 @@ class Replay {
*/
private $initData;
/**
* @var AttributeMap
*/
private $attributeMap;
// -----------------------------------------------------------------------------------------------------------------
/**
@@ -161,6 +167,12 @@ class Replay {
return $serializer->parse($this->getVersion()->getReplayInitDataNode());
}
private function parseAttributeMap() {
$this->file->parse();
$stream = $this->file->openStream('replay.attributes.events');
return new AttributeMap($stream);
}
// -----------------------------------------------------------------------------------------------------------------
// EVENT PARSERS
// -----------------------------------------------------------------------------------------------------------------
@@ -302,6 +314,19 @@ class Replay {
return $this->initData;
}
/**
* Gets the replay attribute map. If the data is not yet parsed,
* it is parsed and its values are stored in memory.
*
* @return AttributeMap
*/
public function getAttributeMap() {
if($this->attributeMap === null) {
$this->attributeMap = $this->parseAttributeMap();
}
return $this->attributeMap;
}
// -----------------------------------------------------------------------------------------------------------------
/**