[Release] No more XML, Database Driven Data

Re: [Pre] No more XML, Database Driven Data

Thanks, Ill try that update you gave me.

This would help my server 75% in space and memory usage.

Edit: it worked.

It had to connect to the database first and the do everything else. :D
 
Re: [Pre] No more XML, Database Driven Data

Great :] Now just to do this for _every_ type of XML file, and we'll be set. I'll probably use a different xml parser, perhaps a Pull rather than Push model one (like XMLReader), which will make the script writing go alot faster..
 
Re: [Pre] No more XML, Database Driven Data

Hey I am very interested in converting the xmls to sql, if you could contact me on aim, msn, or here that would be great. Thanks.
 
Last edited:
Re: [Pre] No more XML, Database Driven Data

Zin, Only problem is.

When you kill the mob, It disconnects you, because it doesn't know where the drops are located at.
 
Re: [Pre] No more XML, Database Driven Data

I got most stuff to SQL.

Maps, Skill, Items, Equips.

I got stuck at Mobs, cause my script's XML reader gets stuck when you try to define an element twice.
i.e. Two or more summons.
 
Re: [Pre] No more XML, Database Driven Data

People for your information, don't use InnoDB for MySQL storage engine, use MyISAM instead, it will give a better performance.

For more information please refer to
 
Re: [Pre] No more XML, Database Driven Data

InnoDB is actually better performance, but the only problem is that it doesn't support full text searching AND it's data is scrambled in log files.
 
Re: [Pre] No more XML, Database Driven Data

A suggestion, create another table for each data table, postfixed with "2". These "2" tables overrides the data in their original table this can be used to separate custum stuff from "official" stuff.
 
Re: [Pre] No more XML, Database Driven Data

wow man , it rocks :) spank you very much :)
 
Re: [Pre] No more XML, Database Driven Data

I kinda agree with moving everything over to SQL storage, but I'd be hesitant to do that for several reasons:

1) Stability. If something happened to crash and corrupt your database or something like that, you'd be fucked.

2) Security. You can SQL inject a database quite easily, it'd be rather difficult for someone to hack into your computer and gain access to flatfile editing like XMLs.

3) Convenience. Although it does take a while for the XMLs to load initially, in a real server environment you wouldn't be restarting the server enough for it to matter, like you said. It's easier to add things by simply editing things in notepad than having to put it in a database.

Anyway, good job so far, I like the progress. Better than what most here could do, at least. I might help out a little, working on making my own server in C# right now though, lol.
 
Re: [Pre] No more XML, Database Driven Data

1) Stability. If something happened to crash and corrupt your database or something like that, you'd be fucked.
The stuff we are talking about is pretty much static data, except for when Wizet updates their deal. Therefore, you could very easily just drop tables and readd them without any issues.

2) Security. You can SQL inject a database quite easily, it'd be rather difficult for someone to hack into your computer and gain access to flatfile editing like XMLs.
As long as you are behind a NAT firewall, there is no need to foward the port for SQL so this is not going to be an issue at all. If you are not behind a NAT firewall, you have no business running a public server. Also, MySQL is a lot more secure than you're giving it credit for being.

3) Convenience. Although it does take a while for the XMLs to load initially, in a real server environment you wouldn't be restarting the server enough for it to matter, like you said. It's easier to add things by simply editing things in notepad than having to put it in a database.
To some degree, yes. However, it is plenty easy if you understand SQL and in some cases it would be even easier(no ctrl+f, easier to search for duplicates, etc).

@zinmirai: As for this whole project, it is an awesome idea seeing as how this is how it is in Ascent for WoW and it works wonders, and as an SQL and PHP guru myself I'd love to pitch in and help. Read my PM for contact info so we can coordinate our efforts. lol
 
Re: [Pre] No more XML, Database Driven Data

2) Security. You can SQL inject a database quite easily, it'd be rather difficult for someone to hack into your computer and gain access to flatfile editing like XMLs.

If there's an SQL Injection bug (maybe there is, the source isn't written with the best database knowledge), there's something more to worry about than changing some maps. You should worry about the user table :busted_co
 
Re: [Pre] No more XML, Database Driven Data

If there's an SQL Injection bug (maybe there is, the source isn't written with the best database knowledge), there's something more to worry about than changing some maps. You should worry about the user table :busted_co


Especially with the server offering no form of password hashing whatsoever x] That's going to be one of my next projects (and a rather easy one at that...)


Quick post, at work right now ^^;
 
Re: [Pre] No more XML, Database Driven Data

Well, that code is very flawed(at least what is posted on the first page).

First of all, it does not create a new instance of type MobInfo each time, and therefore is adding EVERY summon monster to EVERY monster. Well, it would be, if not for this next error:

Code:
mob.summon.push_back(atoi(mrow[2]));

Which is getting the HP of the monster as the summon ID, which in turn will crash your client all the time unless by some lucky ass chance the summon monster's HP is the ID of another monster, but I doubt it.

So in order for it to perform properly, you would need to have the following:

Code:
void Initializing::initializeMobs(){
	MYSQL_RES *mres;
	MYSQL_ROW mrow;
	char query[255];

	sprintf_s(query,255,"SELECT * FROM dataMob");
	mysql_real_query(&MySQL::maple_db, query, strlen(query));
	mres = mysql_store_result(&MySQL::maple_db);
	mrow = mysql_fetch_row(mres);
	int ret = 0;
	while ((mrow = mysql_fetch_row(mres))) {
		MobInfo mob; // Create new instance of type MobInfo each loop for proper data
		MYSQL_RES *mres2;
		MYSQL_ROW mrow2;
		// Col0 : RowID
		//    1 : MobNumber
		//    2 : HP
		//    3 : MP
		//    4 : Exp
		int mobnumber = atoi(mrow[1]); // Mob Number
		mob.hp = atoi(mrow[2]);
		mob.mp = atoi(mrow[3]);
		mob.exp = atoi(mrow[4]);

		sprintf_s(query,255,"SELECT * FROM dataMobSummon WHERE MobNumber = '%i'", mobnumber);
		mysql_real_query(&MySQL::maple_db, query, strlen(query));
		mres2 = mysql_store_result(&MySQL::maple_db);
		while((mrow2 = mysql_fetch_row(mres2))) {
			mob.summon.push_back(atoi(mrow2[2]));
		}

		/* We do special handling for the summons, as it's stored in another table
		   to preserve normalization. */
		Mobs::addMob(mobnumber,mob);
	}
}

I also did shops, as an addition to this project. However, I believe this should be posted in Development, because it is a development issue and this section is mostly filled with leechers that have no idea wtf all of this means, and I doubt klook pays too much attention to this section either due to the whole cluttered business. Anyway, here is the Shops stuff.

Tables to add:
Code:
CREATE TABLE shops (id bigint not null auto_increment primary key, shopid int not null, npcid int not null);
CREATE TABLE shopitems (id bigint not null auto_increment primary key, shopid int not null, itemid int not null, price int not null default 1);

XML->MySQL PHP script:
Code:
<?php

/**************************
** Copyright zinmirai, 2008
** Released under the BSD License
** Original File: mobinsert.php
***************************/

/*
 * Shops dump by Emily Naomi
 * Release under the BSD License
 * File: shops.php
 */

/*********************
** Configuration
*********************/

$path = "G:\\Documents and Settings\\emily\\My Documents\\msserver\\MapleStoryServer\\MapleStoryServer\\Shops\\";
$mysql['host'] = 'localhost';
$mysql['user'] = 'root';
$mysql['pass'] = '';
$mysql['db']   = 'maplestory';

/*********************
** Don't edit below here if you don't
** know what you're doing! I won't fix
** your errors!
*********************/

// First we delete the entire table data.
// Yeah, yeah, I know.. But duplicates would suck
// don't you think? :P
$conn = DatabaseConnector();
mysql_query('TRUNCATE TABLE shops', $conn);
mysql_query('TRUNCATE TABLE shopitems',$conn);
 
$dir = opendir($path);
while (false !== ($file = readdir($dir))) {
 	if($file != "." && $file != ".." && stripos($file,'.xml'))
 	{
 	 	$CurrentShop = new Shop();
 	 	$CurrentTag = '';

	 	$CurrentShop->SHOPNUMBER = substr($file,0,strlen($file)-4);
		$xmlparser = xml_parser_create();
		xml_set_element_handler($xmlparser, "start_tag", "end_tag");
		xml_set_character_data_handler($xmlparser, "tag_contents");
		
		if (!($fp = fopen($path.$file, "r")))
		{
		 	print("WARNING: Cannot open ".$path.$file."\n");
		}
		else
		{
			print ('Opened ' . $path.$file . "...\n");
			while ($data = fread($fp, 4096))
			{
				// Strip whitespace
				$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
				if (!xml_parse($xmlparser, $data, feof($fp))) {
				$reason = xml_error_string(xml_get_error_code($xmlparser));
				$reason .= xml_get_current_line_number($xmlparser);
				print("XML ERROR :: " . $reason);
			   }
			}
		}
		xml_parser_free($xmlparser);
		$CurrentShop->Insert();
		unset($CurrentShop);
	}
}



// $parser = handle to our parser
// $name = name of the current tag
// $attrib = an array containing any attributes of the current tag

function start_tag($parser, $name, $attribs) {
	global $CurrentTag;
// 	echo "Current tag : ".$name."\n";
	$CurrentTag = $name;
	if (is_array($attribs)) {
//		echo "Attributes : \n";
		while(list($key,$val) = each($attribs))
		{
//    		echo "Attribute ".$key." has value ".$val."\n";
		}
	}
}

// $parser = handle to our parser
// $name = name of the current tag

function end_tag($parser, $name) {
//   echo "Reached ending tag ".$name."\n\n";
}

function tag_contents($parser, $data)
{
	global $CurrentShop,$CurrentTag,$lastItem;
	if(strtoupper($CurrentTag) == "ID") {
		array_push($CurrentShop->ITEMS, $data);
		$lastItem = $data;
	}
	// Make sure to account for varying shop prices
	else if(strtoupper($CurrentTag) == "PRICE") {
		$CurrentShop->PRICES[$CurrentShop->SHOPNUMBER][$lastItem] = $data;
	}
	else if(strtoupper($CurrentTag) == "NPC") {
		$CurrentShop->NPC = $data;
	}
}

class Shop {
 	public $FILE;
 	public $SHOPNUMBER;
	public $NPC;
	public $ITEMS = array();
	public $PRICES = array();
	
	public function __constructor()	{
	 	$FILE = '';
		$SHOPNUMBER = '';
		$NPC;
		$ITEMS = array();
	}
	
	public function Insert() {
		$conn = DatabaseConnector();
		$sql = 'INSERT INTO shops (shopid, npcid) VALUES ("'.$this->SHOPNUMBER.'", "'.$this->NPC.'")';
		mysql_query($sql,$conn);
		print(mysql_error($conn));
		$this->InsertItems();
	}
	
	private function InsertItems() {
		$conn = DatabaseConnector();
		foreach($this->ITEMS As $itemid)
		{
			$sql = 'INSERT INTO shopitems (shopid, itemid, price) VALUES ("'.$this->SHOPNUMBER.'", "'.$itemid.'", "'.$this->PRICES[$this->SHOPNUMBER][$itemid].'")';
			mysql_query($sql,$conn);
			print(mysql_error($conn));
		}
	}
}

// Returns MySQL Connection Resource ID
function DatabaseConnector()
{
 	global $mysql;
	$conn = mysql_connect($mysql['host'],$mysql['user'],$mysql['pass']) or die(mysql_error());
	mysql_select_db($mysql['db'],$conn);
	return $conn;
}
?>

Then for Initializing.cpp:
Code:
void Initializing::initializeShops(){
	MYSQL_RES *mres;
	MYSQL_ROW mrow;
	char query[255];

	sprintf_s(query,255,"SELECT * FROM shops");
	mysql_real_query(&MySQL::maple_db, query, strlen(query));
	mres = mysql_store_result(&MySQL::maple_db);
	mrow = mysql_fetch_row(mres);
	int ret = 0;
	while ((mrow = mysql_fetch_row(mres))) {
		ShopInfo shop;
		MYSQL_RES *mres2;
		MYSQL_ROW mrow2;
		// Col0 : RowID
		//    1 : shopid
		//    2 : NPC ID
		int shopid = atoi(mrow[1]);
		shop.npc = atoi(mrow[2]);

		// Add in all the items
		sprintf_s(query,255,"SELECT * FROM shopitems WHERE shopid = '%i'", shopid);
		mysql_real_query(&MySQL::maple_db, query, strlen(query));
		mres2 = mysql_store_result(&MySQL::maple_db);
		mrow2 = mysql_fetch_row(mres2);
		while((mrow2 = mysql_fetch_row(mres2))) {
			ShopItemInfo item;
			item.id = atoi(mrow2[2]);
			item.price = atoi(mrow2[3]);
			shop.items.push_back(item);
		}
		Shops::addShop(shopid, shop);
	}
}

Anyway, contact me via IM sometime so we can better coordinate resources on this project. Cheers.

PS: I believe that all the data should be in a separate database from the user data. That would deal with some security issues, and would also better organisation and such. Also, table names like datamob and datamobsummon are quite long and unecessary, and column names like mobnumber are misleading when the DB's main lingo is id. Try to stick to what klook did rather than reinventing the wheel. KISS is a good thing to follow for any DB that is going to be read by others. Here is a good template for such a DB:

Code:
CREATE DATABASE mapledata;
create table mobs (id bigint not null auto_increment primary key, mobid int not null, hp bigint not null, mp bigint not null, exp bigint not null);
create table mobsummons (id bigint not null auto_increment primary key, mobid int not null, summonid int not null);
CREATE TABLE shops (id bigint not null auto_increment primary key, shopid int not null, npcid int not null);
CREATE TABLE shopitems (id bigint not null auto_increment primary key, shopid int not null, itemid int not null, price int not null default 1);

I have some more stuff in mine which I'll be filling with map info and all that good stuff in the future(people want to play atm lol), and I'll post more here as it develops.

@klook: I am more than willing to take up the database end(with or without help) of the releases for you so you can focus on the coding end of things.
 
Last edited:
Re: [Pre] No more XML, Database Driven Data

Well, that code is very flawed(at least what is posted on the first page).

First of all, it does not create a new instance of type MobInfo each time, and therefore is adding EVERY summon monster to EVERY monster. Well, it would be, if not for this next error:

Code:
mob.summon.push_back(atoi(mrow[2]));
Which is getting the HP of the monster as the summon ID, which in turn will crash your client all the time unless by some lucky ass chance the summon monster's HP is the ID of another monster, but I doubt it.

Haha, that's incredible, because it works with the code I posted, really well =/ Not entirely sure how, because I agree with every fix you posted above.. As for the mrow[2] error, that's a result of my ambiguous naming haha. Are monster summons actually implemented yet into the server? If not, that would be one reason I would point at it actually still functioning (and not crashing and burning like it should.) The MobInfo not being initialized each time is due to me combining the init mob functions from two into one.. I guess it's just hard to keep track of what you changed and where you changed it hehe.

I'll update the main post with these fixes.. thanks for protecting people from my ignorance haha.


So in order for it to perform properly, you would need to have the following:

Code:
void Initializing::initializeMobs(){
    MYSQL_RES *mres;
    MYSQL_ROW mrow;
    char query[255];

    sprintf_s(query,255,"SELECT * FROM dataMob");
    mysql_real_query(&MySQL::maple_db, query, strlen(query));
    mres = mysql_store_result(&MySQL::maple_db);
    mrow = mysql_fetch_row(mres);
    int ret = 0;
    while ((mrow = mysql_fetch_row(mres))) {
        MobInfo mob; // Create new instance of type MobInfo each loop for proper data
        MYSQL_RES *mres2;
        MYSQL_ROW mrow2;
        // Col0 : RowID
        //    1 : MobNumber
        //    2 : HP
        //    3 : MP
        //    4 : Exp
        int mobnumber = atoi(mrow[1]); // Mob Number
        mob.hp = atoi(mrow[2]);
        mob.mp = atoi(mrow[3]);
        mob.exp = atoi(mrow[4]);

        sprintf_s(query,255,"SELECT * FROM dataMobSummon WHERE MobNumber = '%i'", mobnumber);
        mysql_real_query(&MySQL::maple_db, query, strlen(query));
        mres2 = mysql_store_result(&MySQL::maple_db);
        while((mrow2 = mysql_fetch_row(mres2))) {
            mob.summon.push_back(atoi(mrow2[2]));
        }

        /* We do special handling for the summons, as it's stored in another table
           to preserve normalization. */
        Mobs::addMob(mobnumber,mob);
    }
}
That's really close, although you'll want to initialize MobInfo mob; because if it's used later, without having values set (lets say, when there are no summons for a monster, the summon.push_back function will never be ran, resulting in it possibly being a pointer to who knows where.)
Thank you so much for your help ^^ That's a great contribution.. I just wish that my errors would've been more apparent to me haha. Little embarassing (:

EDIT: Hm, was thinking those were classes, but they're structs.. (though still uninitialized) I've just seen alot of crashes happen around the uninit'ed variables in the project so they seem to be problem areas (understandably so), but I don't think they will be in these cases.. ^^; sorry bout that.. haha

I also did shops, as an addition to this project. However, I believe this should be posted in Development, because it is a development issue and this section is mostly filled with leechers that have no idea wtf all of this means, and I doubt klook pays too much attention to this section either due to the whole cluttered business
Agreed, anyone feel like rallying a Mod to move it? Also, once it's done, it would probably be a good idea to point klook to it... It's very easy to implement into the existing source code haha. (of course, that should probably after it goes under thorough testing and code critique (like mine just did :d) to make sure it's stable and effective.

Regarding the code you posted. Would you mind if I included it into the original post?

Code:
PS: I believe that all the data should be in a separate database from the user data. That would deal with some security issues, and would also better organisation and such.

Slightly agree, but that's ignoring (several) bigger problems, such as the unhashed passwords in the database, the server administrators not making users specific to the maple database and with only certain necessary permissions. Not to mention that the user that the server defaults to is root, with no password. How many kids are running around with a MySQL server that has no root password? The case gets even worse if they have their server bound on their NIC, which would result in anyone being able to remote admin in, and do whatever they want with the database (including dump the users table... thank god there are no email addresses attached.. yet) Also, selecting against multiple databases is a pain and expensive haha. But really, the above issues need to be addressed first =/ Maybe in .8 klook can make it mandatory to not use root, and to have a password on the server (or at least default that way)

Also, table names like datamob and datamobsummon are quite long and unecessary
Slightly agreed, moreso than the above quote. They can be trimmed down, but I tend to prefix things to prevent naming collisions, especially if someone wants to add easy IPB+MapleDB integration or something similar. (That problem already comes up with the Users table.. maybe a maple_ prefix would be better just for every table?) Long table names are, well, long, but short table names can be hellish. Imagine if I opted for mbsm for mobsummon instead.. And when you have hundreds of tables, things start getting really confusing. I guess this is just personal perference, but I really would like to avoid itm.cnsm... haha :]

EDIT: I just ran a script, and the person who wrote it used 'equip' for the, well, equip data table.. haha, so I had my entire Equip table truncated :] oops (not their fault :d I should've checked, nor was it a big loss haha) But that's even more reason to try to prevent against table ambiguoity... maybe if we just use a 'd' prefix?

Code:
column names like mobnumber are misleading when the DB's main lingo is id. Try to stick to what klook did rather than reinventing the wheel. KISS is a good thing to follow for any DB that is going to be read by others.
Touche. I couldn't think of any alternatives, and was trying to avoid ID, as well, Mob.MobID, that's not Mob.ID but is the ID of a Mob, although not in the context of a table, gets confusing too XD

Okay, ignore that :d I agree, it should be MobID or something similar. External/Internal ID's are hard to deal with without making too ambiguous... Thanks for your help and comments :]
 
Last edited:
Re: [Pre] No more XML, Database Driven Data

Hmm, I think my suggestion got lost somewhere... let me repost this.
I suggest that every data table should have a second table. (mobSummon -> mobSummon2) which overrides the first table. These tables should be used for custom stuff in order to separate "official" stuff from custom stuffs.
 
Re: [Pre] No more XML, Database Driven Data

Hmm, I think my suggestion got lost somewhere... let me repost this.
I suggest that every data table should have a second table. (mobSummon -> mobSummon2) which overrides the first table. These tables should be used for custom stuff in order to separate "official" stuff from custom stuffs.

Oh, I saw that... but forgot about it x-x

That's a decent idea, although I'm not entirely sure how it would be implemented... (except for checking on each select to see if a corrosponding record exists in the 'custom' table.. but that's expensive.. maybe if we just do a join and check the 2nd values first?)
 
Back