| 
									
										
										
										
											2021-07-20 18:23:06 +02:00
										 |  |  | import os | 
					
						
							|  |  |  | import threading | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  | from Utils import local_path, user_path | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  | from worlds.alttp.Rom import Sprite | 
					
						
							| 
									
										
										
										
											2021-07-20 18:23:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def update_sprites_lttp(): | 
					
						
							| 
									
										
										
										
											2021-07-21 09:18:28 +02:00
										 |  |  |     from tkinter import Tk | 
					
						
							|  |  |  |     from LttPAdjuster import get_image_for_sprite | 
					
						
							|  |  |  |     from LttPAdjuster import BackgroundTaskProgress | 
					
						
							| 
									
										
										
										
											2021-12-13 20:24:54 +01:00
										 |  |  |     from LttPAdjuster import BackgroundTaskProgressNullWindow | 
					
						
							| 
									
										
										
										
											2021-07-21 09:18:28 +02:00
										 |  |  |     from LttPAdjuster import update_sprites | 
					
						
							| 
									
										
										
										
											2021-07-20 18:23:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Target directories | 
					
						
							| 
									
										
										
										
											2022-03-31 05:08:15 +02:00
										 |  |  |     input_dir = user_path("data", "sprites", "alttpr") | 
					
						
							|  |  |  |     output_dir = local_path("WebHostLib", "static", "generated")  # TODO: move to user_path | 
					
						
							| 
									
										
										
										
											2021-07-20 18:23:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     os.makedirs(os.path.join(output_dir, "sprites"), exist_ok=True) | 
					
						
							|  |  |  |     # update sprites through gui.py's functions | 
					
						
							|  |  |  |     done = threading.Event() | 
					
						
							| 
									
										
										
										
											2021-12-13 20:24:54 +01:00
										 |  |  |     try: | 
					
						
							|  |  |  |         top = Tk() | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         task = BackgroundTaskProgressNullWindow(update_sprites, lambda successful, resultmessage: done.set()) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         top.withdraw() | 
					
						
							|  |  |  |         task = BackgroundTaskProgress(top, update_sprites, "Updating Sprites", lambda succesful, resultmessage: done.set()) | 
					
						
							| 
									
										
										
										
											2021-07-20 18:23:06 +02:00
										 |  |  |     while not done.isSet(): | 
					
						
							| 
									
										
										
										
											2021-12-13 20:24:54 +01:00
										 |  |  |         task.do_events() | 
					
						
							| 
									
										
										
										
											2021-07-20 18:23:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     spriteData = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for file in os.listdir(input_dir): | 
					
						
							|  |  |  |         sprite = Sprite(os.path.join(input_dir, file)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not sprite.name: | 
					
						
							|  |  |  |             print("Warning:", file, "has no name.") | 
					
						
							|  |  |  |             sprite.name = file.split(".", 1)[0] | 
					
						
							|  |  |  |         if sprite.valid: | 
					
						
							|  |  |  |             with open(os.path.join(output_dir, "sprites", f"{os.path.splitext(file)[0]}.gif"), 'wb') as image: | 
					
						
							|  |  |  |                 image.write(get_image_for_sprite(sprite, True)) | 
					
						
							|  |  |  |             spriteData.append({"file": file, "author": sprite.author_name, "name": sprite.name}) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print(file, "dropped, as it has no valid sprite data.") | 
					
						
							|  |  |  |     spriteData.sort(key=lambda entry: entry["name"]) | 
					
						
							|  |  |  |     with open(f'{output_dir}/spriteData.json', 'w') as file: | 
					
						
							|  |  |  |         json.dump({"sprites": spriteData}, file, indent=1) | 
					
						
							|  |  |  |     return spriteData |