diff --git a/src/Model/GameSettings.php b/src/Model/GameSettings.php index e4025a2..18e6d3a 100644 --- a/src/Model/GameSettings.php +++ b/src/Model/GameSettings.php @@ -3,6 +3,7 @@ namespace RecAnalyst\Model; use RecAnalyst\RecordedGame; +use RecAnalyst\Processors\MapName as MapNameExtractor; class GameSettings { @@ -216,10 +217,22 @@ public function isScenario() /** * Get the map name. * + * @param array $options Options. + * - `$options['extractRMSName']` - Whether to attempt to find the RMS + * file names of custom random maps. Defaults to `true`. + * * @return string Map name. */ - public function mapName() + public function mapName($options = []) { + $extractRmsName = isset($options['extractRMSName']) ? $options['extractRMSName'] : true; + if ($extractRmsName && $this->isCustomMap()) { + $nameExtractor = new MapNameExtractor($this->rec); + $likelyName = $nameExtractor->run(); + if ($likelyName) { + return $likelyName; + } + } return $this->rec->trans('map_names', $this->mapId); } diff --git a/src/Processors/MapName.php b/src/Processors/MapName.php new file mode 100644 index 0000000..b4159e6 --- /dev/null +++ b/src/Processors/MapName.php @@ -0,0 +1,85 @@ +rec = $rec; + + // The Map Type strings used in the Objectives message tab are stored in + // string id 9654. This array was generated using: + // grep "9654" "SteamApps/common/Age2HD/resources/*/strings/key-value/key-value-strings-utf8.txt" + // + // HD Edition uses UTF-8, but older versions used localization-specific code pages. + // Code page information for zh, jp and ko was taken from: + // https://msdn.microsoft.com/en-us/library/cc194886.aspx + $this->mapTypeRegexes = [ + 'br' => '/Tipo de Mapa: (.*)/', + 'de' => '/Kartentyp: (.*)/', + 'en' => '/Map Type: (.*)/', + 'es' => '/Tipo de mapa: (.*)/', + 'fr' => '/Type de carte : (.*)/', + 'it' => '/Tipo di mappa: (.*)/', + 'jp' => '/' . mb_convert_encoding('マップの種類', 'cp932', 'utf-8') . ': (.*)/', + 'jp_utf8' => '/マップの種類: (.*)/', + 'ko' => '/' . mb_convert_encoding('지도 종류', 'cp949', 'utf-8') . ': (.*)/', + 'ko_utf8' => '/지도 종류: (.*)/', + 'nl' => '/Kaarttype: (.*)/', + 'ru' => '/' . mb_convert_encoding('Тип карты', 'windows-1251', 'utf-8') . ': (.*)/', + 'ru_utf8' => '/Тип карты: (.*)/', + 'zh' => '/' . mb_convert_encoding('地图类别', 'cp936', 'utf-8') . ': (.*)/', + 'zh_utf8' => '/地图类型: (.*)/', + 'zh_wide' => '/' . mb_convert_encoding('地图类别:', 'cp936', 'utf-8') . '(.*)/', + ]; + } + + /** + * Run the processor. + * + * @return string|null The map name, if found. + */ + public function run() + { + $header = $this->rec->header(); + $messages = $header->messages; + $instructions = $messages->instructions; + $lines = explode("\n", $instructions); + foreach ($lines as $line) { + // We don't know what language the game was played in, so we try + // every language we know. + foreach ($this->mapTypeRegexes as $rx) { + $matches = []; + if (preg_match($rx, $line, $matches)) { + return $matches[1]; + } + } + } + } +} diff --git a/test/MapNameTest.php b/test/MapNameTest.php new file mode 100644 index 0000000..829b0e7 --- /dev/null +++ b/test/MapNameTest.php @@ -0,0 +1,32 @@ +load('recs/game-settings/[AoFE-FF DE R1] RoOk_FaLCoN - [Pervert]Moneimon (pov) G1.mgz'); + $this->assertEquals($rec->gameSettings()->mapName(), 'Acropolis'); + $this->assertEquals($rec->gameSettings()->mapName([ + 'extractRMSName' => false + ]), 'Custom'); + } + + public function testNonEnglishMapNameExtract() + { + $rec = $this->load('recs/game-settings/rec.20140311-034826.mgz'); + $this->assertEquals($rec->gameSettings()->mapName(), 'Golden Pit'); + $this->assertEquals($rec->gameSettings()->mapName([ + 'extractRMSName' => false + ]), 'Custom'); + } +} diff --git a/test/recs/game-settings/[AoFE-FF DE R1] RoOk_FaLCoN - [Pervert]Moneimon (pov) G1.mgz b/test/recs/game-settings/[AoFE-FF DE R1] RoOk_FaLCoN - [Pervert]Moneimon (pov) G1.mgz new file mode 100755 index 0000000..b08ec9b Binary files /dev/null and b/test/recs/game-settings/[AoFE-FF DE R1] RoOk_FaLCoN - [Pervert]Moneimon (pov) G1.mgz differ diff --git a/test/recs/game-settings/rec.20140311-034826.mgz b/test/recs/game-settings/rec.20140311-034826.mgz new file mode 100755 index 0000000..d685b65 Binary files /dev/null and b/test/recs/game-settings/rec.20140311-034826.mgz differ