Advertisement
Guest User

GW2 Item/Recipe ID Toolkit

a guest
May 24th, 2013
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. # GW2 Item/Recipe ID Toolkit
  2. # Authored by Reddit user NotTheHead
  3. # On December 24, 2013
  4. #
  5. # It should be noted that much of the error checking that could be
  6. # included is left out for simplicity's sake. Don't use negative
  7. # numbers or zero and you should be fine.
  8.  
  9. from base64 import *
  10.  
  11. def GetRecipeCode(recipe_id):
  12.   """Using a Recipe's ID, create a chat code you can share in-game.
  13.    
  14.     Parameters:
  15.     recipe_id   -- The ID of the recipe to create a chat code for.
  16.    
  17.     Returns:
  18.     A string containing the chat code to type in-game.
  19.     """
  20.   hexa = '\x09'
  21.   for i in range(4): #For both Python 3 and 2.
  22.     #Little endian format
  23.     hexa += chr(recipe_id % 256)
  24.     recipe_id /= 256
  25.   return '[&' + b64encode(hexa) + ']'
  26.  
  27. def GetItemCode(item_id, num_items=1):
  28.   """Using an Item's ID, create a chat code you can share in-game.
  29.    
  30.     Parameters:
  31.     item_id   -- The ID of the item to create a chat code for.
  32.    
  33.     Returns:
  34.     A string containing the chat code to type in-game.
  35.     """
  36.   hexa = '\x02' + chr(num_items)
  37.   for i in range(4): #For both Python 3 and 2.
  38.     #Little endian format
  39.     hexa += chr(item_id % 256)
  40.     item_id /= 256
  41.   return '[&' + b64encode(hexa) + ']'
  42.  
  43. def GetChatCodeB64(chat_code):
  44.   """Strips the [& and ] off of the chat code, if they are on the
  45.     chat code at all.
  46.    
  47.     Parameters:
  48.     chat_code   -- The chat code to be processed.
  49.    
  50.     Returns:
  51.     A string containing the chat code's b64 portion without the
  52.     containing braces.
  53.     """
  54.   if chat_code[0] == '[':
  55.     chat_code = chat_code[1:]
  56.   if chat_code[0] == '&':
  57.     chat_code = chat_code[1:]
  58.   if chat_code[-1] == ']':
  59.     chat_code = chat_code[:-1]
  60.   return chat_code
  61.  
  62. def GetRecipeID(chat_code):
  63.   """Using a Recipe's chat code, retrieve the Recipe's ID.
  64.    
  65.     WARNING: Do not use the chat code given in-game. As of the writing
  66.     of this code, those item codes are wrong. They use an incorrect
  67.     header, and some give the wrong ID as well.
  68.    
  69.     Parameters:
  70.     chat_code   -- The chat code of the recipe.
  71.    
  72.     Returns:
  73.     The recipe's ID in numerical form.
  74.     """
  75.   hexa = b64decode(GetChatCodeB64(chat_code))[1:] #Remove header
  76.   decimal = 0
  77.   for byte in reversed(hexa): #Little endian format
  78.     decimal *= 256
  79.     decimal += ord(byte)
  80.   return decimal
  81.  
  82. def GetItemID(chat_code):
  83.   """Using an Item's chat code, retrieve the Recipe's ID.
  84.    
  85.     Parameters:
  86.     chat_code   -- The chat code of the item.
  87.    
  88.     Returns:
  89.     The item's ID in numerical form.
  90.     """
  91.   hexa = b64decode(GetChatCodeB64(chat_code))[2:] #Remove header
  92.   decimal = 0
  93.   for byte in reversed(hexa): #Little endian format
  94.     decimal *= 256
  95.     decimal += ord(byte)
  96.   return decimal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement