From 78184271913ec0530ee8feaaf23663affa892762 Mon Sep 17 00:00:00 2001
From: Jonius7 <jhgenius01@hotmail.com>
Date: Sun, 2 Jun 2024 21:23:50 +1000
Subject: [PATCH] Release 5.14 now compatible with Millennium, auto-check Steam
 updates

---
 backend.py                        | 44 +++++++++++++++++++++++++------
 docs/Changelog - old_glory GUI.md |  4 ++-
 get_md5_hash.py                   |  9 -------
 manager.py                        |  1 +
 old_glory.py                      | 19 +++++++++----
 old_glory_data.json               |  1 +
 tests/tests_backend.py            |  2 +-
 7 files changed, 56 insertions(+), 24 deletions(-)
 delete mode 100644 get_md5_hash.py

diff --git a/backend.py b/backend.py
index 69fd07e..e6a607c 100644
--- a/backend.py
+++ b/backend.py
@@ -12,8 +12,8 @@
 from datetime import datetime, timezone
 import requests
 from requests_oauthlib import OAuth1Session
-from hashlib import sha1
-import time
+import hashlib
+from pathlib import Path
 
 ##########################################
 ### CONSTANTS
@@ -294,22 +294,22 @@ def help():
     #for item in l:
         
     
-def get_file_hash(filepath):
+def get_git_file_hash(filepath, encoding="UTF-8"):
     '''
     UTILITY: Returns the git SHA hash of a file.
     '''
     try:
         #exclude .png for filehash
         if os.path.isfile(filepath) and os.path.splitext(filepath)[1] != ".png":
-            with open(filepath, 'r', encoding="UTF-8") as f, \
-                open(filepath + ".temp", 'w', encoding="UTF-8", newline='\n') as f1:
+            with open(filepath, 'r', encoding=encoding) as f, \
+                open(filepath + ".temp", 'w', encoding=encoding, newline='\n') as f1:
                 f1.writelines(f.readlines())
             f.close()
             f1.close()
 
             filesize_bytes = os.path.getsize(filepath + ".temp")
 
-            s = sha1()
+            s = hashlib.sha1()
             s.update(b"blob %u\0" % filesize_bytes)
             
             with open(filepath + ".temp", 'rb') as g:
@@ -324,6 +324,30 @@ def get_file_hash(filepath):
         print("Unable to get hash of file: " + filepath, file=sys.stderr)
         print_traceback()
 
+def get_md5_file_hash(filepath):
+    '''
+    UTILITY: Returns the MD5 hash of a file.
+    '''
+    try:
+        #exclude .png for filehash
+        if os.path.isfile(filepath):
+            with open(filepath, "rb") as f:
+                file_hash = hashlib.md5()
+                while chunk := f.read(8192):
+                    file_hash.update(chunk)
+            return file_hash.hexdigest()
+    except:
+        print("Unable to get hash of file: " + filepath, file=sys.stderr)
+        print_traceback()
+        
+def get_path_with_wildcard(filepath = r"C:\Program Files (x86)\0E Games\Steam\package",
+                           search_term = "steamui_websrc_all.zip.vz.*"):
+    '''
+    UTILITY: Returns the filepath (filename) of file using a search wildcard
+        defaults to Steam\package steamui_websrc_all.zip.vz.* file
+    '''
+    return next(Path(filepath).glob(search_term))
+
 ### Check CSS Patched
 def is_css_patched(filename="2.css"):
     '''
@@ -1306,6 +1330,10 @@ def update_json_last_patched_date(json_data):
     json_data["lastPatchedDate"] = get_remote_datetime()
     #print(json_data)
     write_json_data(json_data)
+    
+def update_steamui_websrc_hash(json_data):
+    json_data["steamui_websrc_all.zip.vz_hash"] = get_md5_file_hash(get_path_with_wildcard())
+    write_json_data(json_data)
 
 ### file management functions as part of auto-update
 ### file_dates - dictionary of filenames with their dates
@@ -1336,7 +1364,7 @@ def hash_compare_small_update_files(file_dates, json_data):
                         #print(local_filepath)
                         if os.path.exists(local_filepath) and os.path.isfile(local_filepath):
                             #if local hash != remote hash
-                            if (get_file_hash(local_filepath) != filedata["sha"] and
+                            if (get_git_file_hash(local_filepath) != filedata["sha"] and
                                  local_filepath != "scss/libraryroot.custom.scss" and
                                  local_filepath != "scss/_custom_module1.scss" and
                                  local_filepath != "scss/_custom_module2.scss"):                
@@ -1353,7 +1381,7 @@ def hash_compare_small_update_files(file_dates, json_data):
                                 dir_filepath = local_filepath + "/" + dir_filedata["name"]
                                 if (os.path.exists(dir_filepath) and
                                 os.path.isfile(dir_filepath)):
-                                    if (get_file_hash(dir_filepath) != filedata["sha"] and
+                                    if (get_git_file_hash(dir_filepath) != filedata["sha"] and
                                     os.path.splitext(dir_filepath)[1] != ".png"):
                                         print("New Version | " + dir_filepath)
                                         updatetype_files.append(dir_filepath)
diff --git a/docs/Changelog - old_glory GUI.md b/docs/Changelog - old_glory GUI.md
index 4203f55..673f524 100644
--- a/docs/Changelog - old_glory GUI.md	
+++ b/docs/Changelog - old_glory GUI.md	
@@ -1,6 +1,8 @@
 ## SteamUI-OldGlory GUI Changelog
 
-1.1.2		
+1.1.3		Now auto-checks if there is a new Steam Update and notifies accordingly
+
+1.1.2			*(5.14-pre1)* New Install Location dropdown (now compatible with Millennium)
 
 **1.1.1.1**	    **(Release 5.13)**
 
diff --git a/get_md5_hash.py b/get_md5_hash.py
deleted file mode 100644
index c637ef7..0000000
--- a/get_md5_hash.py
+++ /dev/null
@@ -1,9 +0,0 @@
-import hashlib
-
-with open(r"C:\Program Files (x86)\0E Games\Steam\package\steamui_websrc_all.zip.vz.996bf6091ef8a0822f336e6fc912471ab0fee08d_23943287", "rb") as f:
-    file_hash = hashlib.md5()
-    while chunk := f.read(8192):
-        file_hash.update(chunk)
-
-print(file_hash.digest())
-print(file_hash.hexdigest())  # to get a printable str instead of bytes
\ No newline at end of file
diff --git a/manager.py b/manager.py
index 795d548..001aa16 100644
--- a/manager.py
+++ b/manager.py
@@ -57,6 +57,7 @@ def install_click(event, page, controller):
             ### To be changed in a later version
             backend.write_js_fixes(controller.js_config, controller.special_js_config)
             backend.write_css_configurables(controller.css_config)
+            backend.update_steamui_websrc_hash(controller.json_data)
             
             #applying settings
             change_javascript = check_if_css_requires_javascript(page, controller, settings)
diff --git a/old_glory.py b/old_glory.py
index 866c188..b511475 100644
--- a/old_glory.py
+++ b/old_glory.py
@@ -25,8 +25,8 @@
 
 class OldGloryApp(tk.Tk):
     def __init__(self, *args, **kwargs):
-        self.version = "1.1.2"
-        self.release = "5.14-pre1"
+        self.version = "1.1.3"
+        self.release = "5.14"
       
         ### Window Frame
         tk.Tk.__init__(self, *args, **kwargs)
@@ -71,8 +71,10 @@ def __init__(self, *args, **kwargs):
         self.show_frame("StartPage")
 
         ### Run Update Checks with show frame
-        thread = Thread(target = self.update_check, args = ())
-        thread.start()
+        thread1 = Thread(target = self.steam_update_check, args = ())
+        thread1.start()
+        thread2 = Thread(target = self.update_check, args = ())
+        thread2.start()
         
     def set_window_dimensions(self, width, height):
         self.windowW = width
@@ -130,6 +132,12 @@ def get_frame(self, name):
                     return frame
     '''
     
+    def steam_update_check(self):
+        pass
+        self.current_file_hash = backend.get_md5_file_hash(backend.get_path_with_wildcard())
+        if self.current_file_hash != self.json_data["steamui_websrc_all.zip.vz_hash"]:
+            print("New Steam Update detected. If the Steam window does not appear, try using the Remake JS button in Settings.", file=sys.stderr)
+    
     #init text log
     def update_check(self):        
         ### Check if CSS Patched
@@ -1544,7 +1552,8 @@ def reset_all_tweaks(event, controller):
     backend.clear_js_working_files()
 
 def remake_js(event, controller):
-    backend.clear_js_working_files()    
+    controller.json_data["steamui_websrc_all.zip.vz_hash"] = controller.current_file_hash
+    backend.clear_js_working_files() 
     thread = Thread(target = run_js_tweaker, args = (controller, 1,))
     thread.start()
     #thread.join()
diff --git a/old_glory_data.json b/old_glory_data.json
index 272d1ab..99fae86 100644
--- a/old_glory_data.json
+++ b/old_glory_data.json
@@ -17,6 +17,7 @@
 	"jsModifFile": "sp.modif.js",
 	"jsPatchedFile": "sq.js",
 	"jsOriginalFile": "sp.js.original",
+	"steamui_websrc_all.zip.vz_hash": "543ddfbd73d1bb57720f1545974908c7",
 	"lastPatchedDate": "2024-05-30T14:41:52Z",
 	"themes": {
 		"steam-library": {
diff --git a/tests/tests_backend.py b/tests/tests_backend.py
index b9f215d..fcab4c7 100644
--- a/tests/tests_backend.py
+++ b/tests/tests_backend.py
@@ -72,7 +72,7 @@ def test_filename_too_long_for_html(self):
 
     def test_get_function_performance(self):
         start_time = time.time()
-        print(backend.get_file_hash("../scss/_sidebar.scss"))
+        print(backend.get_git_file_hash("../scss/_sidebar.scss"))
         print("--- %s seconds ---" % (time.time() - start_time))