1+ # This file is responsible for the game's automatic update system through github
2+ import os
3+ import ctypes
4+ import subprocess
5+ import requests
6+ import zipfile
7+ import json
8+
9+ def msgbox (text : str , title : str , style : int , icon : int ): return ctypes .windll .user32 .MessageBoxW (0 , text , title , style | icon )
10+
11+ def checkForUpdates ():
12+ try :
13+ version_url = requests .get ("https://raw.githubusercontent.com/RaiMonteiro/sudoku-python/refs/heads/main/source/r_notes/notes.json" )
14+ online = True
15+ except requests .exceptions .RequestException :
16+ online = False
17+
18+ if online :
19+ if version_url .status_code == 200 : # the file was found
20+ # loads the remote and local json data
21+ remote_data = version_url .json ()
22+ with open ("source/r_notes/notes.json" , mode = "r" , encoding = "utf-8" ) as file : local_data = json .load (file )
23+
24+ if remote_data ["update" ][- 1 ]["version" ] != local_data ["update" ][- 1 ]["version" ]: # compare versions
25+ if msgbox (f"Existe uma nova versão!\n Pretende atualizar o jogo para a versão { remote_data ["update" ][- 1 ]["version" ]} ?" , "Sudoku Updater" , 4 , 0x20 ) == 6 : # ok = 6
26+ if updateFiles (remote_data ["update" ][- 1 ]["version" ]):
27+ try :
28+ exe_path = os .path .join (os .path .dirname (__file__ ), "launcher.exe" )
29+ subprocess .Popen ([exe_path ], cwd = os .path .dirname (exe_path )) # launch the executable
30+ return "UPDATE_SUCESS"
31+
32+ except FileNotFoundError :
33+ msgbox (f"Arquivo não encontrado em { exe_path } ." , f"Sudoku Updater - Erro: FileNotFoundError" , 0 , 0x30 )
34+ return "UPDATE_FAILED"
35+ except PermissionError :
36+ msgbox (f"Sem permissão para executar." , f"Sudoku Updater - Erro: PermissionError" , 0 , 0x30 )
37+ return "UPDATE_FAILED"
38+ except Exception as e :
39+ msgbox (f"Ocorreu um erro ao executar o programa." , f"Sudoku Updater - Erro: { str (e )} " , 0 , 0x30 )
40+ return "UPDATE_FAILED"
41+
42+ else : return "UPDATE_FAILED"
43+ else : return "UPDATE_DECLINED"
44+ else : return "NO_UPDATE"
45+ else :
46+ msgbox (f"Ocorreu um erro ao verficar novas atualizações.\n Por favor tente mais tarde." , f"Sudoku Updater - Erro: { version_url .status_code } " , 0 , 0x30 )
47+ return "OFFLINE"
48+
49+ def updateFiles (version : str ):
50+ zip_url = f"https://github.com/RaiMonteiro/sudoku-python/releases/download/v{ version } /sudoku-update-v{ version } .zip"
51+ response = requests .get (zip_url )
52+
53+ if response .status_code != 200 :
54+ msgbox (f"Ocorreu um erro durante a atualização.\n Por favor tente mais tarde." , f"Sudoku Updater - Erro: { response .status_code } " , 0 , 0x30 )
55+ return False
56+
57+ try :
58+ # Open a local file in binary mode for writing, 'wb' = write binary (required for ZIP files)
59+ with open ("update.zip" , "wb" ) as f : f .write (response .content )
60+
61+ # Open the ZIP file to extract its contents
62+ with zipfile .ZipFile ("update.zip" , 'r' ) as zip_ref : zip_ref .extractall ("." )
63+
64+ os .remove ("update.zip" ) # Clean up downloaded zip
65+ return True
66+ except Exception as e :
67+ msgbox (f"Ocorreu um erro durante a atualização dos ficheiros locais.\n Por favor tente mais tarde." , f"Sudoku Updater - Erro: { str (e )} " , 0 , 0x30 )
68+ return False
0 commit comments