Thread: Say Links
View Single Post
  #11  
Old 04-21-2009, 11:04 AM
realityincarnate
Developer
 
Join Date: Dec 2007
Posts: 122
Default

I haven't gotten a chance to look at using the augments section to store anything yet, but I did come up with some code for storing a link as a perl variable. The syntax is just $var = quest::itemlink2(item id).

My source is way too messed up for a diff right now, but here's the changes I made for this. The total string, including all the link variables and stuff, is limited to 250 chars (totally arbitrary number) right now, so no ridiculously long item names.

questmgr.h add:
Code:
const char* ItemLink2(char* perltext, int item_id);
questmgr.cpp:
Code:
const char* QuestManager::ItemLink2(char* perltext, int item_id) {
	const ItemInst* inst = database.CreateItem(item_id, 20, 99999, 99999, 99999, 99999, 99998);
	if (!inst) return 0;	// return an empty string if the item is invalid
	char* link = 0;
	char* tempstr = 0;
	if (initiator->MakeItemLink(link, inst)) {	// make a link to the item
		MakeAnyLenString(&tempstr, "%c%s%s%c", 0x12, link, inst->GetItem()->Name, 0x12);	// format the string that the client receives
		strncpy(perltext, tempstr,250);	// the perl string is only 250 chars, so make sure the link isn't too large
		safe_delete_array(tempstr);	// MakeAnyLenString() uses new, so clean up after it
	}
	safe_delete_array(link);	// MakeItemLink() uses new also
	return perltext;
}
perlparser.cpp:
Code:
XS(XS__ItemLink2);
XS(XS__ItemLink2) {
	dXSARGS;
	if (items != 1)
		Perl_croak(aTHX_ "Usage: itemlink2(itemID)");
	dXSTARG;

	Const_char * RETVAL;
	char text[250];
	uint16 itemID;
	itemID = (int)SvUV(ST(0));
	
	RETVAL = quest_manager.ItemLink2(text, itemID);

	sv_setpv(TARG, RETVAL); XSprePUSH; PUSHTARG;
	XSRETURN(1);
}
and at the bottom of perlparser add:
Code:
newXS(strcpy(buf, "itemlink2"), XS__ItemLink2, file);

By the way, the most likely cause of your zone crashes is in passing (char *) strings back and forth between functions. It's really easy to accidently write more to the char array than you allocated and overwrite other stuff on the stack. At least, that's what seemed to cause all of my problems.
Reply With Quote