From a6e658b1a2b7029c3c2e8f91c52e1d70d97ee076 Mon Sep 17 00:00:00 2001 From: slawkens Date: Sat, 7 Mar 2026 20:54:47 +0100 Subject: [PATCH] 60x faster items.xml loading Using simplexml, which is enabled by default on php --- composer.json | 1 + system/functions.php | 2 +- system/src/DataLoader.php | 1 + system/src/Monsters.php | 8 +++---- system/src/{ => Server}/Items.php | 16 +++++++------ system/src/Server/XML/Items.php | 39 ++++++++++++++++--------------- 6 files changed, 35 insertions(+), 32 deletions(-) rename system/src/{ => Server}/Items.php (89%) diff --git a/composer.json b/composer.json index fcc4bac7..1e90b5ae 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "ext-pdo_mysql": "*", "ext-json": "*", "ext-xml": "*", + "ext-simplexml": "*", "ext-dom": "*", "phpmailer/phpmailer": "^6.1", "composer/semver": "^3.2", diff --git a/system/functions.php b/system/functions.php index 1ae4384a..2a933905 100644 --- a/system/functions.php +++ b/system/functions.php @@ -11,7 +11,6 @@ defined('MYAAC') or die('Direct access not allowed!'); use MyAAC\Cache\Cache; use MyAAC\CsrfToken; -use MyAAC\Items; use MyAAC\Models\Config; use MyAAC\Models\Guild; use MyAAC\Models\House; @@ -21,6 +20,7 @@ use MyAAC\Models\PlayerDeath; use MyAAC\Models\PlayerKillers; use MyAAC\News; use MyAAC\Plugins; +use MyAAC\Server\Items; use MyAAC\Settings; use PHPMailer\PHPMailer\PHPMailer; diff --git a/system/src/DataLoader.php b/system/src/DataLoader.php index e2c3c88c..419e3b0d 100644 --- a/system/src/DataLoader.php +++ b/system/src/DataLoader.php @@ -27,6 +27,7 @@ namespace MyAAC; use MyAAC\Cache\Cache; use MyAAC\Models\Town; +use MyAAC\Server\Items; class DataLoader { diff --git a/system/src/Monsters.php b/system/src/Monsters.php index 5a469722..f893f4ff 100644 --- a/system/src/Monsters.php +++ b/system/src/Monsters.php @@ -12,13 +12,11 @@ namespace MyAAC; use MyAAC\Models\Monster; +use MyAAC\Server\Items; class Monsters { - /** - * @var \OTS_MonstersList - */ - private static $monstersList; - private static $lastError = ''; + private static \OTS_MonstersList $monstersList; + private static string $lastError = ''; public static function loadFromXML($show = false) { try { diff --git a/system/src/Items.php b/system/src/Server/Items.php similarity index 89% rename from system/src/Items.php rename to system/src/Server/Items.php index 5a45985f..a41bf2e6 100644 --- a/system/src/Items.php +++ b/system/src/Server/Items.php @@ -9,7 +9,7 @@ * @link https://my-aac.org */ -namespace MyAAC; +namespace MyAAC\Server; use MyAAC\Cache\PHP as CachePHP; use MyAAC\Models\Spell; @@ -20,17 +20,19 @@ class Items private static string $error = ''; + const FILE_ITEMS_TOML = 'items/items.toml'; + const FILE_ITEMS_XML = 'items/items.xml'; + public static function getError(): string { return self::$error; } public static function load(): bool { - $file_path = config('data_path') . 'items/items.toml'; - if (file_exists($file_path)) { - $items = new Server\TOML\Items(); + if (file_exists(config('data_path') . self::FILE_ITEMS_TOML)) { + $items = new TOML\Items(); } - elseif (file_exists(config('data_path') . 'items/items.xml')) { - $items = new Server\XML\Items(); + elseif (file_exists(config('data_path') . self::FILE_ITEMS_XML)) { + $items = new XML\Items(); } else { self::$error = 'Cannot load items.xml or items.toml file. Files not found.'; @@ -51,7 +53,7 @@ class Items } $cache_php = new CachePHP(config('cache_prefix'), CACHE . 'persistent/'); - self::$items = $cache_php->get('items'); + self::$items = (array)$cache_php->get('items'); } public static function get($id) { diff --git a/system/src/Server/XML/Items.php b/system/src/Server/XML/Items.php index fbb1994a..2ea3e40e 100644 --- a/system/src/Server/XML/Items.php +++ b/system/src/Server/XML/Items.php @@ -22,22 +22,23 @@ class Items return false; } - $xml = new \DOMDocument; - if(!$xml->load($file)) { + $items = []; + + try { + $xml = new \SimpleXMLElement(file_get_contents($file)); + } catch (\Exception $e) { $this->error = 'Error: Cannot load items.xml. More info in system/logs/error.log file.'; - log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load items.xml - $file. Error: " . print_r(error_get_last(), true)); + log_append('error.log', "[" . __CLASS__ . "] Fatal error: Cannot load items.xml - $file. Error: " . $e->getMessage()); return false; } - - $items = []; - foreach ($xml->getElementsByTagName('item') as $item) { - if ($item->getAttribute('fromid')) { - for ($id = $item->getAttribute('fromid'); $id <= $item->getAttribute('toid'); $id++) { + foreach($xml->xpath('item') as $item) { + if ($item->attributes()->fromid) { + for ($id = $item->attributes()->fromid; $id <= $item->attributes()->toid; $id++) { $tmp = $this->parseNode($id, $item); $items[$tmp['id']] = $tmp['content']; } } else { - $tmp = $this->parseNode($item->getAttribute('id'), $item); + $tmp = $this->parseNode($item->attributes()->id, $item); $items[$tmp['id']] = $tmp['content']; } } @@ -49,21 +50,21 @@ class Items public function parseNode($id, $node): array { - $name = $node->getAttribute('name'); - $article = $node->getAttribute('article'); - $plural = $node->getAttribute('plural'); + $name = $node->attributes()->name; + $article = $node->attributes()->article; + $plural = $node->attributes()->plural; - $attributes = array(); - foreach($node->getElementsByTagName('attribute') as $attr) { - $attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value'); + $attributes = []; + foreach($node->xpath('attribute') as $attr) { + $attributes[strtolower($attr->attributes()->key)] = (string)$attr->attributes()->value; } return [ - 'id' => $id, + 'id' => (int)$id, 'content' => [ - 'article' => $article, - 'name' => $name, - 'plural' => $plural, + 'article' => (string)$article, + 'name' => (string)$name, + 'plural' => (string)$plural, 'attributes' => $attributes ], ];