|
| 1 | +# Created by Deltaion Lee (MCMi460) on Github |
| 2 | +# Grab icons and title information from IDBE database |
| 3 | + |
| 4 | +# A solid afternoon was spent. :D |
| 5 | + |
| 6 | +# Thank you to 3DBrew's IDBE page (https://www.3dbrew.org/wiki/IDBE) which was written by Cruel (https://www.3dbrew.org/w/index.php?title=User:Cruel). |
| 7 | +# And thank you to Cruel again (https://github.com/Cruel) for helping with understanding the RGB565 data type and Nintendo's anger issues |
| 8 | + |
| 9 | +from Cryptodome.Cipher import AES |
| 10 | +import Cryptodome.Cipher.AES |
| 11 | +from binascii import hexlify, unhexlify |
| 12 | +import requests, io, math |
| 13 | +from PIL import Image |
| 14 | + |
| 15 | +def getTitleData(titleID:hex): |
| 16 | + data = requests.get('https://idbe-ctr.cdn.nintendo.net/icondata/10/%s.idbe' % titleID.zfill(16), verify = False).content |
| 17 | + |
| 18 | + IV = unhexlify('A46987AE47D82BB4FA8ABC0450285FA4') |
| 19 | + |
| 20 | + K0 = unhexlify('4AB9A40E146975A84BB1B4F3ECEFC47B') |
| 21 | + K1 = unhexlify('90A0BB1E0E864AE87D13A6A03D28C9B8') |
| 22 | + K2 = unhexlify('FFBB57C14E98EC6975B384FCF40786B5') |
| 23 | + K3 = unhexlify('80923799B41F36A6A75FB8B48C95F66F') |
| 24 | + |
| 25 | + AES_KEYS = [K0, K1, K2, K3] |
| 26 | + |
| 27 | + key = AES_KEYS[data[1]] |
| 28 | + |
| 29 | + decipher = AES.new(key,AES.MODE_CBC,IV) |
| 30 | + |
| 31 | + return decipher.decrypt(data[2:]) |
| 32 | + |
| 33 | +def getTitleInfo(titleID:hex): |
| 34 | + int(titleID, 16) # Errors if not HEX |
| 35 | + if isinstance(titleID, str): |
| 36 | + titleID = hex(int(titleID)).replace('0x', '') |
| 37 | + |
| 38 | + titleID = titleID.zfill(16).upper() |
| 39 | + try: |
| 40 | + data = getTitleData(titleID) |
| 41 | + except: |
| 42 | + return None |
| 43 | + |
| 44 | + for i in range(3): |
| 45 | + offset = 0x50 + i * 0x200 |
| 46 | + |
| 47 | + n = data[offset:offset+0x200] |
| 48 | + short = n[0x00:0x00+0x80].decode('utf-16').replace('\x00', '') |
| 49 | + long = n[0x80:0x80+0x100].decode('utf-16').replace('\x00', '') |
| 50 | + publisher = n[0x180:0x180+0x80].decode('utf-16').replace('\x00', '') |
| 51 | + if short: |
| 52 | + break |
| 53 | + |
| 54 | + icon_data = data[0x24D0:0x24D0+0x1200] # 48x48 icon data |
| 55 | + |
| 56 | + # Taken from Repo3DS (https://github.com/Repo3DS/shop-cache/blob/master/TitleInfo.py) -- Thank you so much, Cruel! |
| 57 | + (w, h) = (48, 48) |
| 58 | + tiled_icon = Image.frombuffer('RGB', (w, h), icon_data, 'raw', 'BGR;16') |
| 59 | + # Untile the image |
| 60 | + tile_order = [0,1,8,9,2,3,10,11,16,17,24,25,18,19,26,27,4,5,12,13,6,7,14,15,20,21,28,29,22,23,30,31,32,33,40,41,34,35,42,43,48,49,56,57,50,51,58,59,36,37,44,45,38,39,46,47,52,53,60,61,54,55,62,63] |
| 61 | + icon = Image.new('RGB', (w, h)) |
| 62 | + pos = 0 |
| 63 | + for y in range(0, h, 8): |
| 64 | + for x in range(0, w, 8): |
| 65 | + for k in range(8 * 8): |
| 66 | + xoff = tile_order[k] % 8 |
| 67 | + yoff = int((tile_order[k] - xoff) / 8) |
| 68 | + |
| 69 | + posx = pos % w |
| 70 | + posy = math.floor(pos / w) |
| 71 | + pos += 1 |
| 72 | + |
| 73 | + pixel = tiled_icon.getpixel((posx, posy)) |
| 74 | + icon.putpixel((x + xoff, y + yoff), pixel) |
| 75 | + |
| 76 | + icon.save('cache/' + titleID + '.png', 'PNG') |
| 77 | + |
| 78 | + return { |
| 79 | + 'short': short, |
| 80 | + 'long': long, |
| 81 | + 'publisher': publisher, |
| 82 | + 'imageID': titleID, |
| 83 | + } |
0 commit comments