60x faster items.xml loading

Using simplexml, which is enabled by default on php
This commit is contained in:
slawkens
2026-03-07 20:54:47 +01:00
parent 2a80bc1b46
commit a6e658b1a2
6 changed files with 35 additions and 32 deletions

View File

@@ -5,6 +5,7 @@
"ext-pdo_mysql": "*",
"ext-json": "*",
"ext-xml": "*",
"ext-simplexml": "*",
"ext-dom": "*",
"phpmailer/phpmailer": "^6.1",
"composer/semver": "^3.2",

View File

@@ -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;

View File

@@ -27,6 +27,7 @@ namespace MyAAC;
use MyAAC\Cache\Cache;
use MyAAC\Models\Town;
use MyAAC\Server\Items;
class DataLoader
{

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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
],
];