* better error handling for monsters and spells loader

* check if file exist before loading
* save errors to system/logs/error.log
This commit is contained in:
slawkens1 2017-10-16 23:31:28 +02:00
parent c914a73352
commit 66a3c46aaf
9 changed files with 119 additions and 22 deletions

View File

@ -175,13 +175,26 @@ INSERT INTO `myaac_news` (`id`, `type`, `date`, `category`, `title`, `body`, `pl
success($locale['step_database_imported_players']); success($locale['step_database_imported_players']);
} }
require LIBS . 'creatures.php'; require(LIBS . 'creatures.php');
if(Creatures::loadFromXML()) if(Creatures::loadFromXML()) {
success($locale['step_database_loaded_creatures']); success($locale['step_database_loaded_monsters']);
if(Creatures::getMonstersList()->hasErrors()) {
$locale['step_database_error_monsters'] = str_replace('$LOG$', 'system/logs/error.log', $locale['step_database_error_monsters']);
warning($locale['step_database_error_monsters']);
}
}
else {
error(Creatures::getLastError());
}
require LIBS . 'spells.php'; require(LIBS . 'spells.php');
if(Spells::loadFromXML()) if(Spells::loadFromXML()) {
success($locale['step_database_loaded_spells']); success($locale['step_database_loaded_spells']);
}
else {
error(Spells::getLastError());
}
$locale['step_finish_desc'] = str_replace('$ADMIN_PANEL$', generateLink(ADMIN_URL, $locale['step_finish_admin_panel'], true), $locale['step_finish_desc']); $locale['step_finish_desc'] = str_replace('$ADMIN_PANEL$', generateLink(ADMIN_URL, $locale['step_finish_admin_panel'], true), $locale['step_finish_desc']);
$locale['step_finish_desc'] = str_replace('$HOMEPAGE$', generateLink(BASE_URL, $locale['step_finish_homepage'], true), $locale['step_finish_desc']); $locale['step_finish_desc'] = str_replace('$HOMEPAGE$', generateLink(BASE_URL, $locale['step_finish_homepage'], true), $locale['step_finish_desc']);

View File

@ -15,6 +15,7 @@ if(file_exists(BASE . 'config.local.php')) // user customizations
require(BASE . 'config.local.php'); require(BASE . 'config.local.php');
if(!isset($config['installed']) || !$config['installed']) { if(!isset($config['installed']) || !$config['installed']) {
header('Location: ' . BASE_URL);
die('AAC has not been installed yet or there was error during installation. Please install again.'); die('AAC has not been installed yet or there was error during installation. Please install again.');
} }

View File

@ -12,6 +12,9 @@
defined('MYAAC') or die('Direct access not allowed!'); defined('MYAAC') or die('Direct access not allowed!');
class Creatures { class Creatures {
private static $monstersList = null;
private static $lastError = '';
public static function loadFromXML($show = false) { public static function loadFromXML($show = false) {
global $config, $db; global $config, $db;
@ -22,15 +25,22 @@ class Creatures {
echo "<h2>All records deleted from table 'myaac_monsters' in database.</h2>"; echo "<h2>All records deleted from table 'myaac_monsters' in database.</h2>";
} }
$allmonsters = new OTS_MonstersList($config['data_path'].'monster/'); try {
self::$monstersList = new OTS_MonstersList($config['data_path'].'monster/');
}
catch(Exception $e) {
self::$lastError = $e->getMessage();
return false;
}
//$names_added must be an array //$names_added must be an array
$names_added[] = ''; $names_added[] = '';
//add monsters //add monsters
foreach($allmonsters as $lol) { foreach(self::$monstersList as $lol) {
$monster = $allmonsters->current(); $monster = self::$monstersList->current();
if(!$monster->loaded()) { if(!$monster->loaded()) {
if($show) { if($show) {
warning('Error while adding monster: ' . $allmonsters->currentFile()); warning('Error while adding monster: ' . self::$monstersList->currentFile());
} }
continue; continue;
} }
@ -98,7 +108,7 @@ class Creatures {
if(!in_array($name, $names_added)) { if(!in_array($name, $names_added)) {
try { try {
$db->query("INSERT INTO `myaac_monsters` (`hide_creature`, `name`, `mana`, `exp`, `health`, `speed_lvl`, `use_haste`, `voices`, `immunities`, `summonable`, `convinceable`, `race`, `gfx_name`, `file_path`) VALUES (0, " . $db->quote($name) . ", " . $db->quote(empty($mana) ? 0 : $mana) . ", " . $db->quote($exp) . ", " . $db->quote($health) . ", " . $db->quote($speed_lvl) . ", " . $db->quote($use_haste) . ", " . $db->quote($voices_string) . ", " . $db->quote($immunities_string) . ", " . $db->quote($flags['summonable'] > 0 ? 1 : 0) . ", " . $db->quote($flags['convinceable'] > 0 ? 1 : 0) . ", ".$db->quote($race).", ".$db->quote($gfx_name).", " . $db->quote($allmonsters->currentFile()) . ")"); $db->query("INSERT INTO `myaac_monsters` (`hide_creature`, `name`, `mana`, `exp`, `health`, `speed_lvl`, `use_haste`, `voices`, `immunities`, `summonable`, `convinceable`, `race`, `gfx_name`, `file_path`) VALUES (0, " . $db->quote($name) . ", " . $db->quote(empty($mana) ? 0 : $mana) . ", " . $db->quote($exp) . ", " . $db->quote($health) . ", " . $db->quote($speed_lvl) . ", " . $db->quote($use_haste) . ", " . $db->quote($voices_string) . ", " . $db->quote($immunities_string) . ", " . $db->quote($flags['summonable'] > 0 ? 1 : 0) . ", " . $db->quote($flags['convinceable'] > 0 ? 1 : 0) . ", ".$db->quote($race).", ".$db->quote($gfx_name).", " . $db->quote(self::$monstersList->currentFile()) . ")");
if($show) { if($show) {
success("Added: ".$name."<br/>"); success("Added: ".$name."<br/>");
@ -116,4 +126,12 @@ class Creatures {
return true; return true;
} }
public static function getMonstersList() {
return self::$monstersList;
}
public static function getLastError() {
return self::$lastError;
}
} }

View File

@ -36,6 +36,8 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
*/ */
private $monsters = array(); private $monsters = array();
private $lastMonsterFile = '';
private $hasErrors = false;
/** /**
* Loads monsters mapping file. * Loads monsters mapping file.
* *
@ -57,9 +59,18 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
$this->monstersPath .= '/'; $this->monstersPath .= '/';
} }
// check if monsters.xml exist
if(!@file_exists($this->monstersPath . 'monsters.xml')) {
log_append('error.log', '[OTS_MonstersList.php] Fatal error: Cannot load monsters.xml. File does not exist. (' . $this->monstersPath . 'monsters.xml' . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load monsters.xml. File not found. More info in system/logs/error.log file.');
}
// loads monsters mapping file // loads monsters mapping file
$monsters = new DOMDocument(); $monsters = new DOMDocument();
$monsters->load($this->monstersPath . 'monsters.xml'); if(!@$monsters->load($this->monstersPath . 'monsters.xml')) {
log_append('error.log', '[OTS_MonstersList.php] Fatal error: Cannot load monsters.xml (' . $this->monstersPath . 'monsters.xml' . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load monsters.xml. File is invalid. More info in system/logs/error.log file.');
}
foreach( $monsters->getElementsByTagName('monster') as $monster) foreach( $monsters->getElementsByTagName('monster') as $monster)
{ {
@ -101,6 +112,16 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
return isset($this->monsters[$name]); return isset($this->monsters[$name]);
} }
function xmlErrorHandler($errno, $errstr, $errfile, $errline)
{
if($errno==E_WARNING && (substr_count($errstr,"DOMDocument::loadXML()")>0)) {
//throw new DOMException($errstr);
log_append('error.log', '[OTS_MonstersList.php] Fatal error: Cannot load ' . $this->lastMonsterFile . ' - ' . $errstr);
$this->hasErrors = true;
}
else
return false;
}
/** /**
* Returns loaded data of given monster. * Returns loaded data of given monster.
* *
@ -112,21 +133,31 @@ class OTS_MonstersList implements Iterator, Countable, ArrayAccess
*/ */
public function getMonster($name) public function getMonster($name)
{ {
global $lastMonsterFile;
// checks if monster exists // checks if monster exists
if( isset($this->monsters[$name]) ) if( isset($this->monsters[$name]) )
{ {
// loads file // loads file
$monster = new OTS_Monster(); $monster = new OTS_Monster();
//echo $this->monstersPath . $this->monsters[$name]; //echo $this->monstersPath . $this->monsters[$name];
// check if monster file exist
if(file_exists($this->monstersPath . $this->monsters[$name])) { if(file_exists($this->monstersPath . $this->monsters[$name])) {
$monster->loadXML(trim(file_get_contents($this->monstersPath . $this->monsters[$name]))); set_error_handler(array($this, 'xmlErrorHandler'));
$this->lastMonsterFile = $this->monstersPath . $this->monsters[$name];
@$monster->loadXML(trim(file_get_contents($this->monstersPath . $this->monsters[$name])));
restore_error_handler();
} }
return $monster; return $monster;
} }
throw new OutOfBoundsException(); throw new OutOfBoundsException();
} }
public function hasErrors() {
return $this->hasErrors;
}
/** /**
* Returns amount of monsters loaded. * Returns amount of monsters loaded.
* *

View File

@ -93,9 +93,18 @@ class OTS_SpellsList implements IteratorAggregate, Countable
*/ */
public function __construct($file) public function __construct($file)
{ {
// loads DOM document // check if spells.xml exist
if(!@file_exists($file)) {
log_append('error.log', '[OTS_SpellsList.php] Fatal error: Cannot load spells.xml. File does not exist. (' . $file . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load spells.xml. File not found. More info in system/logs/error.log file.');
}
// loads monsters mapping file
$spells = new DOMDocument(); $spells = new DOMDocument();
$spells->load($file); if(!@$spells->load($file)) {
log_append('error.log', '[OTS_SpellsList.php] Fatal error: Cannot load spells.xml (' . $file . '). Error: ' . print_r(error_get_last(), true));
throw new Exception('Error: Cannot load spells.xml. File is invalid. More info in system/logs/error.log file.');
}
// loads runes // loads runes
foreach( $spells->getElementsByTagName('rune') as $rune) foreach( $spells->getElementsByTagName('rune') as $rune)

View File

@ -12,6 +12,9 @@
defined('MYAAC') or die('Direct access not allowed!'); defined('MYAAC') or die('Direct access not allowed!');
class Spells { class Spells {
private static $spellsList = null;
private static $lastError = '';
public static function loadFromXML($show = false) { public static function loadFromXML($show = false) {
global $config, $db; global $config, $db;
@ -26,15 +29,21 @@ class Spells {
$vocations_ids[$voc_name] = $voc_id; $vocations_ids[$voc_name] = $voc_id;
} }
$allspells = new OTS_SpellsList($config['data_path'].'spells/spells.xml'); try {
self::$spellsList = new OTS_SpellsList($config['data_path'].'spells/spells.xml');
}
catch(Exception $e) {
self::$lastError = $e->getMessage();
return false;
}
//add conjure spells //add conjure spells
$conjurelist = $allspells->getConjuresList(); $conjurelist = self::$spellsList->getConjuresList();
if($show) { if($show) {
echo "<h3>Conjure:</h3>"; echo "<h3>Conjure:</h3>";
} }
foreach($conjurelist as $spellname) { foreach($conjurelist as $spellname) {
$spell = $allspells->getConjure($spellname); $spell = self::$spellsList->getConjure($spellname);
$lvl = $spell->getLevel(); $lvl = $spell->getLevel();
$mlvl = $spell->getMagicLevel(); $mlvl = $spell->getMagicLevel();
$mana = $spell->getMana(); $mana = $spell->getMana();
@ -88,13 +97,13 @@ class Spells {
} }
//add instant spells //add instant spells
$instantlist = $allspells->getInstantsList(); $instantlist = self::$spellsList->getInstantsList();
if($show) { if($show) {
echo "<h3>Instant:</h3>"; echo "<h3>Instant:</h3>";
} }
foreach($instantlist as $spellname) { foreach($instantlist as $spellname) {
$spell = $allspells->getInstant($spellname); $spell = self::$spellsList->getInstant($spellname);
$lvl = $spell->getLevel(); $lvl = $spell->getLevel();
$mlvl = $spell->getMagicLevel(); $mlvl = $spell->getMagicLevel();
$mana = $spell->getMana(); $mana = $spell->getMana();
@ -151,4 +160,12 @@ class Spells {
return true; return true;
} }
public static function getSpellsList() {
return self::$spellsList;
}
public static function getLastError() {
return self::$lastError;
}
} }

View File

@ -69,7 +69,8 @@ $locale['step_database_adding_field'] = 'Adding field';
$locale['step_database_modifying_field'] = 'Modifying field'; $locale['step_database_modifying_field'] = 'Modifying field';
$locale['step_database_changing_field'] = 'Changing $FIELD$ to $FIELD_NEW$...'; $locale['step_database_changing_field'] = 'Changing $FIELD$ to $FIELD_NEW$...';
$locale['step_database_imported_players'] = 'Imported player samples...'; $locale['step_database_imported_players'] = 'Imported player samples...';
$locale['step_database_loaded_creatures'] = 'Loaded creatures...'; $locale['step_database_loaded_monsters'] = 'Loaded monsters...';
$locale['step_database_error_monsters'] = 'There were some problems loading your monsters.xml file. Please check $LOG$ for more info.';
$locale['step_database_loaded_spells'] = 'Loaded spells...'; $locale['step_database_loaded_spells'] = 'Loaded spells...';
$locale['step_database_created_account'] = 'Created admin account...'; $locale['step_database_created_account'] = 'Created admin account...';
$locale['step_database_created_news'] = 'Created newses...'; $locale['step_database_created_news'] = 'Created newses...';

View File

@ -65,7 +65,12 @@ $canEdit = hasFlag(FLAG_CONTENT_MONSTERS) || admin();
if(isset($_POST['reload_monsters']) && $canEdit) if(isset($_POST['reload_monsters']) && $canEdit)
{ {
require LIBS . 'creatures.php'; require LIBS . 'creatures.php';
Creatures::loadFromXML(true); if(Creatures::loadFromXML(true))
if(Creatures::getMonstersList()->hasErrors())
error('There were some problems loading your monsters.xml file. Please check system/logs/error.log for more info.');
else {
error(Creatures::getLastError());
}
} }
if($canEdit) if($canEdit)

View File

@ -16,7 +16,9 @@ $canEdit = hasFlag(FLAG_CONTENT_SPELLS) || admin();
if(isset($_POST['reload_spells']) && $canEdit) if(isset($_POST['reload_spells']) && $canEdit)
{ {
require LIBS . 'spells.php'; require LIBS . 'spells.php';
Spells::loadFromXML(true); if(!Spells::loadFromXML(true)) {
error(Spells::getLastError());
}
} }
if($canEdit) if($canEdit)