From 9c45f48e84a69f3d690311efc2b37b7b491c3c41 Mon Sep 17 00:00:00 2001
From: EricGoldsteinNz <EricGoldsteinNz–––@users.noreply.github.com>
Date: Thu, 28 Dec 2023 14:05:06 +1300
Subject: [PATCH] - Add the backend code for changing shortcut text. - Fix a
 typo in the readme and small structure changes to the readme

---
 README.md            |  9 +++------
 build.bat            |  4 ++--
 tadpole.py           |  4 ++--
 tadpole_functions.py | 44 +++++++++++++++++++++-----------------------
 versioninfo          |  8 ++++----
 5 files changed, 32 insertions(+), 37 deletions(-)

diff --git a/README.md b/README.md
index 6868cf1..671d31c 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ Tadpole
 
 by EricGoldstein  
 
-Tadpole is a device management tool for rthe SF2000 / Datafrog. It currently provides the following main features:
+Tadpole is a device management tool for the SF2000 / Datafrog. It currently provides the following main features:
 * Rebuilding of the console specific rom lists
 * Changing the firmware to official releases or multicore (including rebuilding multicore gamelists)
 * Merging ROM zips and jpg files with the same name to the relevant Zxx fileformat for each console
@@ -26,12 +26,9 @@ in the proper system categories instead of only being able to add them in the us
 DISCLAIMER
 ----------
 
-This program is experimental and you should use with caution!
+This program is not developed or authorised by any company or individual connected with the SF2000 handheld and is based on public reverse engineering of SF2000 file formats.
 
-It is not developed or authorised by any company or individual connected with the SF2000 handheld and is based on public
-reverse engineering of SF2000 file formats.
-
-Although it will back up the files it modifies, you should make your own backup of the Resources folder and ideally your
+You should make your own backups of the Resources and bios folders and ideally your
 whole SD card so you can restore the original state of your device if anything goes wrong.
 
 
diff --git a/build.bat b/build.bat
index 5bdf73c..5519980 100644
--- a/build.bat
+++ b/build.bat
@@ -1,5 +1,5 @@
 @echo off
-set ver=1.3.2
+set ver=1.3.3
 rem build script for the distributable versions of tadpole
 if not exist "venv\" (
     python -m venv venv
@@ -13,4 +13,4 @@ if not exist "venv\Lib\site-packages\PIL" (
 if not exist "venv\Lib\site-packages\PyQt5" (
     venv\Scripts\python -m pip install PyQt5
 )
-python -m PyInstaller tadpole.py -n tadpole-%ver%.exe --onefile -F --icon frog.ico --clean --noconsole --version-file versioninfo --add-data="frog.ico;." --add-data="README.md;."
+pyinstaller tadpole.py -n tadpole-%ver%.exe -F --icon frog.ico --clean --noconsole --version-file versioninfo --add-data="frog.ico;." --add-data="README.md;."
diff --git a/tadpole.py b/tadpole.py
index 11d6ede..4b08942 100644
--- a/tadpole.py
+++ b/tadpole.py
@@ -225,8 +225,8 @@ def create_actions(self):
 
     def testFunction(self):
         drive = self.combobox_drive.currentText()
-        tadpole_functions.updateShortcutTextforConsole(drive, tadpole_functions._static_shortcut_ARCADE, "test1", "test2", "test3", "test4")
-
+        tadpole_functions.updateShortcutTextforConsole(drive, tadpole_functions._static_shortcut_ARCADE, "Call", "games", "anything", "you want")
+        print("finished test function")
 
     def loadMenus(self):
         self.menu_file = self.menuBar().addMenu("&File")
diff --git a/tadpole_functions.py b/tadpole_functions.py
index 2b61a32..49d37cc 100644
--- a/tadpole_functions.py
+++ b/tadpole_functions.py
@@ -17,7 +17,7 @@
 import time
 
 try:
-    from PIL import Image, ImageDraw
+    from PIL import Image, ImageDraw, ImageFont
     image_lib_avail = True
 except ImportError:
     Image = None
@@ -1080,27 +1080,23 @@ def updateShortcutTextforConsole(drive: str, console: int, game1:str, game2:str,
         raise Exception_InvalidConsole
     gakne_path = os.path.join(drive, "Resources", "gakne.ctp")
     try:
-        shortcutText = openBRGAasImage(gakne_path)
-        shortcutNewText = ImageDraw.Draw(Image.new("RGBA", (144, 32), (255, 255, 255, 255)))        
-        shortcutNewText.text((0,0), game1, (255,255,255))
-        shortcutText.paste(shortcutNewText, (0,0), shortcutNewText)
-        shortcutNewText = ImageDraw.Draw(Image.new("RGBA", (144, 32), (255, 255, 255, 255)))     
-        shortcutNewText.text((0,0), game2, (255,255,255))
-        shortcutText.paste(shortcutNewText, (0,0), shortcutNewText)
-        shortcutNewText = ImageDraw.Draw(Image.new("RGBA", (144, 32), (255, 255, 255, 255)))     
-        shortcutNewText.text((0,0), game3, (255,255,255))
-        shortcutText.paste(shortcutNewText, (0,0), shortcutNewText)
-        shortcutNewText = ImageDraw.Draw(Image.new("RGBA", (144, 32), (255, 255, 255, 255)))   
-        shortcutNewText.text((0,0), game4, (255,255,255))
-        shortcutText.paste(shortcutNewText, (0,0), shortcutNewText)
-
         # Gakne is made up of 8 rows of 4 items for a total of 32 items.
         # Each image is 144 x 32. Total image size 576 x 256.
         # To only strip the shortcut text we want to leave the settings menu items. So we have to skip the first 18,432 bytes
         # Console order: FC, SFC, MD, GB, GBC, GBA, ARCADE
-        
+        newText = [game1,game2,game3,game4]
+        shortcutText = openBRGAasImage(gakne_path)
+        replaceMask = Image.new("RGBA", (144, 32), (255, 255, 255, 255))
+        fnt = ImageFont.truetype("arial.ttf", 24)
+        for i in range(len(newText)):
+            #Game Slot 1
+            img_g = Image.new("RGBA", (144, 32), (0, 0, 0, 0)) #In the alpha channel 0 is fully transparent, 255 is fully opaque
+            ImageDraw.Draw(img_g).text((72,16), newText[i], (255,255,255),font=fnt, anchor="mm")        
+            shortcutText.paste(img_g, (144*i,(console+1)*32), replaceMask)
+
+        shortcutText.save("C:\\Users\\OEM\\Documents\\test.png")
         # TODO XXXXXX
-        writeImagetoBGRAfile(gakne_path)
+        writeImagetoBGRAfile(shortcutText, gakne_path)
         return True
     except (OSError, IOError) as e:
         print(f"! Failed updating shortcut labels. {e}")
@@ -1111,10 +1107,10 @@ def openBRGAasImage(inputFile):
     with open(inputFile, 'rb') as file:
         data = file.read()
     # Unpack the BGRA8888 data
-    pixels = struct.unpack('<' + ('H' * (len(data) // 2)), data)
+    pixels = struct.unpack('>' + ('L' * (len(data) // 4)), data)
     # Convert the BGRA8888 values to RGBA888 format
     rgba8888_pixels = [
-            ((pixel & 0xFF000000) >> 16, (pixel & 0x00FF0000) >> 8, (pixel & 0x0000FF00), (pixel & 0x000000FF))
+            ((pixel & 0x0000FF00) >> 8, (pixel & 0x00FF0000) >> 16, (pixel & 0xFF000000) >> 24, (pixel & 0x000000FF))
             for pixel in pixels
     ]
     # Create an image from the pixels
@@ -1124,6 +1120,7 @@ def openBRGAasImage(inputFile):
     image.putdata(rgba8888_pixels)
     return image
 
+#TODO why is it all coming out yellow???
 def writeImagetoBGRAfile(image:Image, outfile:str):
     try:
         dest_file = open(outfile, "wb")
@@ -1134,7 +1131,7 @@ def writeImagetoBGRAfile(image:Image, outfile:str):
         if not pixels:
             logging.error(f"tadpole_functions~writeImagetoBGRAfile: Failed to load image")
             return False
-
+        data = []
         for h in range(image_height):
             for w in range(image_width):
                 pixel = pixels[w, h]
@@ -1145,9 +1142,10 @@ def writeImagetoBGRAfile(image:Image, outfile:str):
                 g = pixel[1]
                 b = pixel[2]
                 a = pixel[3]
-                bgra = (b << 24) | (g << 16) | (r << 8) | a
-                outfile.write(struct.pack('H', bgra))
-        outfile.close()
+                bgra = (b) | (g << 16) | (r << 8) | (a << 24)
+                data.append(struct.pack('>L', bgra))
+        dest_file.write(b''.join(data))
+        dest_file.close()
         return True
     except (OSError, IOError):
         logging.error(f"tadpole_functions~writeImagetoBGRAfile: Failed opening image file {outfile} for conversion")
diff --git a/versioninfo b/versioninfo
index 20a3bab..08b9f56 100644
--- a/versioninfo
+++ b/versioninfo
@@ -1,7 +1,7 @@
 VSVersionInfo(
   ffi=FixedFileInfo(
-    filevers=(1, 3, 2, 0),
-    prodvers=(1, 3, 2, 0),
+    filevers=(1, 3, 3, 0),
+    prodvers=(1, 3, 3, 0),
     mask=0x3f,
     flags=0x0,
     OS=0x4,
@@ -15,12 +15,12 @@ VSVersionInfo(
         u'080904B0',
         [StringStruct(u'CompanyName', u'EricGoldstein'),
         StringStruct(u'FileDescription', u'tadpole'),
-        StringStruct(u'FileVersion', u'1.3.2'),
+        StringStruct(u'FileVersion', u'1.3.3'),
         StringStruct(u'InternalName', u'tadpole'),
         StringStruct(u'LegalCopyright', u'by EricGoldstein 2023'),
         StringStruct(u'OriginalFilename', u'tadpole.exe'),
         StringStruct(u'ProductName', u'tadpole'),
-        StringStruct(u'ProductVersion', u'1.3.2')])
+        StringStruct(u'ProductVersion', u'1.3.3')])
       ]),
     VarFileInfo([VarStruct(u'Translation', [2057, 1200])])
   ]