mirror of
https://github.com/slawkens/myaac.git
synced 2026-03-16 16:03:31 +01:00
60x faster items.xml loading
Using simplexml, which is enabled by default on php
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
"ext-pdo_mysql": "*",
|
"ext-pdo_mysql": "*",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-xml": "*",
|
"ext-xml": "*",
|
||||||
|
"ext-simplexml": "*",
|
||||||
"ext-dom": "*",
|
"ext-dom": "*",
|
||||||
"phpmailer/phpmailer": "^6.1",
|
"phpmailer/phpmailer": "^6.1",
|
||||||
"composer/semver": "^3.2",
|
"composer/semver": "^3.2",
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ defined('MYAAC') or die('Direct access not allowed!');
|
|||||||
|
|
||||||
use MyAAC\Cache\Cache;
|
use MyAAC\Cache\Cache;
|
||||||
use MyAAC\CsrfToken;
|
use MyAAC\CsrfToken;
|
||||||
use MyAAC\Items;
|
|
||||||
use MyAAC\Models\Config;
|
use MyAAC\Models\Config;
|
||||||
use MyAAC\Models\Guild;
|
use MyAAC\Models\Guild;
|
||||||
use MyAAC\Models\House;
|
use MyAAC\Models\House;
|
||||||
@@ -21,6 +20,7 @@ use MyAAC\Models\PlayerDeath;
|
|||||||
use MyAAC\Models\PlayerKillers;
|
use MyAAC\Models\PlayerKillers;
|
||||||
use MyAAC\News;
|
use MyAAC\News;
|
||||||
use MyAAC\Plugins;
|
use MyAAC\Plugins;
|
||||||
|
use MyAAC\Server\Items;
|
||||||
use MyAAC\Settings;
|
use MyAAC\Settings;
|
||||||
use PHPMailer\PHPMailer\PHPMailer;
|
use PHPMailer\PHPMailer\PHPMailer;
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ namespace MyAAC;
|
|||||||
|
|
||||||
use MyAAC\Cache\Cache;
|
use MyAAC\Cache\Cache;
|
||||||
use MyAAC\Models\Town;
|
use MyAAC\Models\Town;
|
||||||
|
use MyAAC\Server\Items;
|
||||||
|
|
||||||
class DataLoader
|
class DataLoader
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,13 +12,11 @@
|
|||||||
namespace MyAAC;
|
namespace MyAAC;
|
||||||
|
|
||||||
use MyAAC\Models\Monster;
|
use MyAAC\Models\Monster;
|
||||||
|
use MyAAC\Server\Items;
|
||||||
|
|
||||||
class Monsters {
|
class Monsters {
|
||||||
/**
|
private static \OTS_MonstersList $monstersList;
|
||||||
* @var \OTS_MonstersList
|
private static string $lastError = '';
|
||||||
*/
|
|
||||||
private static $monstersList;
|
|
||||||
private static $lastError = '';
|
|
||||||
|
|
||||||
public static function loadFromXML($show = false) {
|
public static function loadFromXML($show = false) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
* @link https://my-aac.org
|
* @link https://my-aac.org
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace MyAAC;
|
namespace MyAAC\Server;
|
||||||
|
|
||||||
use MyAAC\Cache\PHP as CachePHP;
|
use MyAAC\Cache\PHP as CachePHP;
|
||||||
use MyAAC\Models\Spell;
|
use MyAAC\Models\Spell;
|
||||||
@@ -20,17 +20,19 @@ class Items
|
|||||||
|
|
||||||
private static string $error = '';
|
private static string $error = '';
|
||||||
|
|
||||||
|
const FILE_ITEMS_TOML = 'items/items.toml';
|
||||||
|
const FILE_ITEMS_XML = 'items/items.xml';
|
||||||
|
|
||||||
public static function getError(): string {
|
public static function getError(): string {
|
||||||
return self::$error;
|
return self::$error;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function load(): bool {
|
public static function load(): bool {
|
||||||
$file_path = config('data_path') . 'items/items.toml';
|
if (file_exists(config('data_path') . self::FILE_ITEMS_TOML)) {
|
||||||
if (file_exists($file_path)) {
|
$items = new TOML\Items();
|
||||||
$items = new Server\TOML\Items();
|
|
||||||
}
|
}
|
||||||
elseif (file_exists(config('data_path') . 'items/items.xml')) {
|
elseif (file_exists(config('data_path') . self::FILE_ITEMS_XML)) {
|
||||||
$items = new Server\XML\Items();
|
$items = new XML\Items();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
self::$error = 'Cannot load items.xml or items.toml file. Files not found.';
|
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/');
|
$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) {
|
public static function get($id) {
|
||||||
@@ -22,22 +22,23 @@ class Items
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$xml = new \DOMDocument;
|
$items = [];
|
||||||
if(!$xml->load($file)) {
|
|
||||||
|
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.';
|
$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;
|
return false;
|
||||||
}
|
}
|
||||||
|
foreach($xml->xpath('item') as $item) {
|
||||||
$items = [];
|
if ($item->attributes()->fromid) {
|
||||||
foreach ($xml->getElementsByTagName('item') as $item) {
|
for ($id = $item->attributes()->fromid; $id <= $item->attributes()->toid; $id++) {
|
||||||
if ($item->getAttribute('fromid')) {
|
|
||||||
for ($id = $item->getAttribute('fromid'); $id <= $item->getAttribute('toid'); $id++) {
|
|
||||||
$tmp = $this->parseNode($id, $item);
|
$tmp = $this->parseNode($id, $item);
|
||||||
$items[$tmp['id']] = $tmp['content'];
|
$items[$tmp['id']] = $tmp['content'];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$tmp = $this->parseNode($item->getAttribute('id'), $item);
|
$tmp = $this->parseNode($item->attributes()->id, $item);
|
||||||
$items[$tmp['id']] = $tmp['content'];
|
$items[$tmp['id']] = $tmp['content'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,21 +50,21 @@ class Items
|
|||||||
|
|
||||||
public function parseNode($id, $node): array
|
public function parseNode($id, $node): array
|
||||||
{
|
{
|
||||||
$name = $node->getAttribute('name');
|
$name = $node->attributes()->name;
|
||||||
$article = $node->getAttribute('article');
|
$article = $node->attributes()->article;
|
||||||
$plural = $node->getAttribute('plural');
|
$plural = $node->attributes()->plural;
|
||||||
|
|
||||||
$attributes = array();
|
$attributes = [];
|
||||||
foreach($node->getElementsByTagName('attribute') as $attr) {
|
foreach($node->xpath('attribute') as $attr) {
|
||||||
$attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value');
|
$attributes[strtolower($attr->attributes()->key)] = (string)$attr->attributes()->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $id,
|
'id' => (int)$id,
|
||||||
'content' => [
|
'content' => [
|
||||||
'article' => $article,
|
'article' => (string)$article,
|
||||||
'name' => $name,
|
'name' => (string)$name,
|
||||||
'plural' => $plural,
|
'plural' => (string)$plural,
|
||||||
'attributes' => $attributes
|
'attributes' => $attributes
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user