From a52396008dcc2993a1de3691a69c3a5a52a0df4f Mon Sep 17 00:00:00 2001 From: slawkens Date: Fri, 9 Oct 2020 00:12:07 +0200 Subject: [PATCH] Automatically load towns from .OTBM file Takes up to 10 seconds for otservbr on my PC Taken from old Smart AAC - class SpawnsReader.php --- config.php | 4 +- system/init.php | 58 ++++++++++++++------------ system/libs/TownsReader.php | 82 +++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 27 deletions(-) create mode 100644 system/libs/TownsReader.php diff --git a/config.php b/config.php index 5189c198..8f2aafff 100644 --- a/config.php +++ b/config.php @@ -165,7 +165,9 @@ $config = array( 'character_name_max_length' => 21, // list of towns - // if you use TFS 1.3 with support for 'towns' table in database, then you can ignore this - it will be configured automatically (generated from your .OTBM map) + // if you use TFS 1.3 with support for 'towns' table in database, then you can ignore this - it will be configured automatically (from MySQL database - Table - towns) + // otherwise it will try to load from your .OTBM map file + // if you don't see towns on website, then you need to fill this out 'towns' => array( 0 => 'No town', 1 => 'Sample town' diff --git a/system/init.php b/system/init.php index 0fcfccf7..139fa582 100644 --- a/system/init.php +++ b/system/init.php @@ -159,28 +159,9 @@ else { } unset($tmp, $id, $vocation); -// load towns -/* TODO: doesnt work -ini_set('memory_limit', '-1'); -$tmp = ''; - -if($cache->enabled() && $cache->fetch('towns', $tmp)) { - $config['towns'] = unserialize($tmp); -} -else { - $towns = new OTS_OTBMFile(); - $towns->loadFile('D:/Projekty/opentibia/wodzislawski/data/world/wodzislawski.otbm'); - - $config['towns'] = $towns->getTownsList(); - if($cache->enabled()) { - $cache->set('towns', serialize($config['towns']), 120); - } -} -*/ - -//////////////////////////////////////// -// load towns from database (TFS 1.3) // -//////////////////////////////////////// +////////////////////////////////////// +// load towns from database or OTBM // +////////////////////////////////////// $tmp = ''; $towns = []; @@ -198,7 +179,32 @@ else { unset($query); } else { - $towns = config('towns'); + $mapName = configLua('mapName'); + if (!isset($mapName)) { + $mapName = configLua('map'); + $mapFile = $config['server_path'] . $mapName; + } + + if (strpos($mapName, '.otbm') === false) { + $mapName .= '.otbm'; + } + + if (!isset($mapFile)) { + $mapFile = config('data_path') . 'world/' . $mapName; + } + + if (file_exists($mapFile)) { + ini_set('memory_limit', '-1'); + + require LIBS . 'TownsReader.php'; + $townsReader = new TownsReader($mapFile); + $townsReader->load(); + + $towns = $townsReader->get(); + } + else { + $towns = config('towns'); + } } if($cache->enabled()) { @@ -207,6 +213,6 @@ else { } config(['towns', $towns]); -////////////////////////////////////////////// -// END - load towns from database (TFS 1.3) // -////////////////////////////////////////////// +//////////////////////////////////////////// +// END - load towns from database or OTBM // +//////////////////////////////////////////// diff --git a/system/libs/TownsReader.php b/system/libs/TownsReader.php new file mode 100644 index 00000000..71c00798 --- /dev/null +++ b/system/libs/TownsReader.php @@ -0,0 +1,82 @@ +file = fopen($file, 'rb'); + } + + public function load() + { + // checks if file is opened correctly + if ($this->file) { + // skips version + fseek($this->file, 4); + + // reads nodes chain + while (!feof($this->file)) { + // reads byte + switch (ord(fgetc($this->file))) { + // maybe a town node + case self::NODE_START: + // reads node type + if (ord(fgetc($this->file)) == self::OTBM_TOWN) { + $id = unpack('L', fread($this->file, 4)); + $length = unpack('S', fread($this->file, 2)); + + // reads town name + $this->towns[$id[1]] = fread($this->file, $length[1]); + } + break; + + // escape next character - it might be NODE_START character which is in fact not + case self::ESCAPE_CHAR: + fgetc($this->file); + break; + } + } + } + } + + public function get() { + return $this->towns; + } +} \ No newline at end of file