Forum in READ ONLY mode! All questions and discussions on Discord official server, invite link: https://discord.gg/VxsGzJ7

[Python] GetGumpId makes stealth crash

Ask for help
Post Reply
Boydon
Neophyte
Neophyte
Posts: 36
Joined: 12.02.2012 18:06

[Python] GetGumpId makes stealth crash

Post by Boydon »

Whenever i use the following snippet:

Code: Select all

print(GetGumpID(GetGumpsCount() - 1))
with the Gump below stealth will crash. This seems to work fine in normal PascalScript, but returns a negative number. I'm using the latest version of stealth (4.04).

Probably, but this is just a guess, GumpID is not expecting such a big number as 93A564C3?
Serial: C153
GumpID: 93A564C3
X: 0000
Y: 0000
Pages: 1
Gump Options:

ResizePics: X Y ID Width Height Page ElemNum
0 ResizePic: 10 10 2600 500 135 0 1

XmfHtmlGump: X Y Width Height ClilocID Background scrollbar Page ElemNum ClilocText
0 XmfHtmlGump: 52 35 420 55 1006046 1 1 0 2 You have reward items available. Click 'ok' below to get the selection menu or 'cancel' to be prompted upon your next login.
1 XmfHtmlGump: 95 96 150 35 1006044 0 0 0 4 OK
2 XmfHtmlGump: 320 96 150 35 1006045 0 0 0 6 Cancel

GumpButtons: X Y Released_ID Pressed_ID Quit Page_ID Return_value Page ElemNum
0 GumpButton: 60 95 4005 4007 1 0 1 0 3
1 GumpButton: 285 95 4017 4019 1 0 0 0 5
User avatar
Vizit0r
Developer
Developer
Posts: 3958
Joined: 24.03.2005 17:05
Contact:

Re: [Python] GetGumpId makes stealth crash

Post by Vizit0r »

bug repeated, tomorrow will see...
"Пишите код так, как будто сопровождать его будет склонный к насилию психопат, который знает, где вы живете". (с) Макконнелл, "Совершенный код".
User avatar
Vizit0r
Developer
Developer
Posts: 3958
Joined: 24.03.2005 17:05
Contact:

Re: [Python] GetGumpId makes stealth crash

Post by Vizit0r »

fixed this bug and same type bugs
"Пишите код так, как будто сопровождать его будет склонный к насилию психопат, который знает, где вы живете". (с) Макконнелл, "Совершенный код".
Alex
Moderator
Moderator
Posts: 351
Joined: 07.04.2005 4:41

Re: [Python] GetGumpId makes stealth crash

Post by Alex »

Thanks for this bug report, its really helps to remove a bunch of bugs in pyscripting part of Stealth
Новый стелс лежит тут. Стелс это произведение искусства! :)
Boydon
Neophyte
Neophyte
Posts: 36
Joined: 12.02.2012 18:06

Re: [Python] GetGumpId makes stealth crash

Post by Boydon »

No problem, it is my pleasure. :mrgreen: Hope this is the right board to report bugs. ;)

Before you release the hot-fix here is the my dirty work-around.

Code: Select all

def GetGumpID(gump_index):

	for line in GetGumpFullLines(gump_index):
		if line.find('GumpID') != -1:
			gump_id_line = line
			break
	else:
		return 0
	
	return int(gump_id_line.split(' ')[1], 16)
Boydon
Neophyte
Neophyte
Posts: 36
Joined: 12.02.2012 18:06

Re: [Python] GetGumpId makes stealth crash

Post by Boydon »

I see that there is another error with Python and Gumps.

A binding is missing to a Pascal Script available Method:

Code: Select all

NameError: global name 'GetGumpInfo' is not defined
Can this be fixed also with the new version of Stealth?
User avatar
Vizit0r
Developer
Developer
Posts: 3958
Joined: 24.03.2005 17:05
Contact:

Re: [Python] GetGumpId makes stealth crash

Post by Vizit0r »

alex will see this function tomorrow
"Пишите код так, как будто сопровождать его будет склонный к насилию психопат, который знает, где вы живете". (с) Макконнелл, "Совершенный код".
Boydon
Neophyte
Neophyte
Posts: 36
Joined: 12.02.2012 18:06

Re: [Python] GetGumpId makes stealth crash

Post by Boydon »

This is the surrogate replacement function that I put together while Alex come out with an official fix.

Only partially tested with Python 3.x

Code: Select all

def gumpDict(gumpIndex):
	"""Returns the Gump information obtained by GetGumpFullLines formatted in a handy and pretty dictionary"""
	
	# we split every two spaces, should the format change this will be broken
	reTokens = re.compile(r"[\s]{2,}")
	gumpDict = {}
	
	# internal use only
	def _gumpList2Dict(strToList, dictKey, keys):
		"""For internal use only, this sub will exctract all the tokens in strToList, and combine them with keys to form a dictionary.
			This new dictionay will be appended to the gumpDict dictonary in a list of dictionraies with key dictKey"""
			
		nonlocal gumpDict
		
		# when we split we can have integers or strings. If we get integer they will be forced to that type (comparison will be easier)
		# we also want to be sure that we are not extracting the header line with the keys
		tokenList = [int(token) if str.isdigit(token) else token for token in reTokens.split(strToList) if token not in keys]
		
		if tokenList:
			if gumpDict.get(dictKey, False):
				gumpDict[dictKey].append(dict(zip(keys, tokenList)))
			else:
				gumpDict[dictKey] = [dict(zip(keys, tokenList))]
		pass
	
	lines = GetGumpFullLines(gumpIndex)
	for line in lines:
		pieces = line.split(': ')
		if pieces[0]:
			if pieces[0] == 'Serial':
				gumpDict['Serial'] = pieces[1]
			elif pieces[0] == 'GumpID':
				gumpDict['Id'] = pieces[1]
			elif pieces[0] == 'X':
				gumpDict['X'] = pieces[1]
			elif pieces[0] == 'Y':
				gumpDict['Y'] = pieces[1]
			elif pieces[0] == 'Pages':
				gumpDict['Pages'] = pieces[1]
			elif pieces[0] == 'Gump Options':
				subpieces = pieces[1].split(' ')
				if subpieces:
					gumpDict['GumpOptions'] = [subpiece for subpiece in subpieces]
			elif pieces[0] == 'GumpPicTiled':
				_gumpList2Dict(pieces[1], 'GumpPicTiled', ['X', 'Y', 'Width', 'Height', 'Gump_ID', 'ElemNum'])
			elif pieces[0].find('ResizePic') != -1:
				_gumpList2Dict(pieces[1], 'ResizePic', ['X', 'Y', 'ID', 'Width', 'Height' ,'Page', 'ElemNum'])
			elif pieces[0].find('XmfHTMLGumpColor') != -1:
				_gumpList2Dict(pieces[1], 'XmfHTMLGumpColor', ['X', 'Y', 'Width', 'Height', 'ClilocID', 'Background', 'scrollbar', 'Hue', 'ElemNum', 'ClilocText'])
			elif pieces[0].find('XmfHTMLTok') != -1:
				_gumpList2Dict(pieces[1], 'XmfHTMLTok', ['X', 'Y', 'Width', 'Height', 'Background', 'scrollbar', 'Color', 'ClilocID', 'Arguments', 'ElemNum', 'ClilocText'])
			elif pieces[0].find('CheckerTrans') != -1:
				_gumpList2Dict(pieces[1], 'CheckerTrans', ['X', 'Y', 'Width', 'Height', 'Page', 'ElemNum'])
			elif pieces[0].find('GumpButton') != -1:
				_gumpList2Dict(pieces[1], 'GumpButton', ['X', 'Y', 'Released_ID', 'Pressed_ID', 'Quit', 'Page_ID', 'Return_value', 'Page', 'ElemNum'])
			elif pieces[0].find('GumpText') != -1:
				_gumpList2Dict(pieces[1], 'GumpText', ['X', 'Y','Color', 'Text_ID', 'Page', 'ElemNum'])
			elif pieces[0].find('GumpPic') != -1:
				_gumpList2Dict(pieces[1], 'GumpPic', ['X', 'Y', 'ID', 'Hue', 'Page', 'ElemNum'])
	
	return gumpDict
Post Reply