3333A Set of tools to automate the server update process.
3434"""
3535
36- __version__ = '2.2.1 '
36+ __version__ = '2.2.2 '
3737
3838# These variables contain links for the script updating process.
3939
4242GITHUB_RAW = 'https://raw.githubusercontent.com/Owen-Cochell/PaperMC-Update/master/server_update.py'
4343
4444
45- def load_config (config : str ) -> Tuple [str , int ]:
45+ def load_config_old (config : dict ) -> Tuple [str , int ]:
4646 """
4747 Loads configuration data from the given file.
4848
4949 We only load version info if it's in the official format!
5050 We return the version and build number found in the configuration file.
5151
52- :param config: Path to config file
53- :type config: str
54- :return: Tuple contaning version and build data respectively
52+ This function looks for config data in the old format,
53+ which at this time seems to be pre 1.21 (TODO: Confirm)
54+ We preform a check to see if the data looks correct,
55+ the current version string MUST start with 'git-Paper',
56+ otherwise we will raise a value error.
57+
58+ :param config: JSON data to consider
59+ :type config: dict
60+ :return: Tuple containing version and build data respectively
5561 :rtype: Tuple[str, int]
5662 """
5763
58- # Exists and is file, read it
64+ OLD_PREFIX = 'git-Paper' # Old prefix
65+
66+ # Read the data, and attempt to pull some info out of it
5967
60- file = open ( config , 'r' )
68+ current = config [ 'currentVersion' ]
6169
62- data = json . load ( file )
70+ # Ensure string prefix is correct:
6371
64- # Read the data, and attempt to pull some info out of it
72+ if not OLD_PREFIX == current [: len ( OLD_PREFIX )]:
6573
66- current = data ['currentVersion' ]
74+ # Does not match! Must be invalid ...
75+
76+ raise ValueError ("Invalid old config data format!" )
6777
6878 # Splitting the data in two so we can pull some content out:
6979
@@ -82,6 +92,43 @@ def load_config(config: str) -> Tuple[str, int]:
8292 return version , build
8393
8494
95+ def load_config_new (config : dict ) -> Tuple [str , int ]:
96+ """
97+ Loads configuration data from the given file.
98+
99+ We only load version info if it's in the official format!
100+ We return the version and build number found in the configuration file.
101+
102+ This function looks for config data in the new format,
103+ which at this time seems to be post 1.21 (TODO: Confirm)
104+
105+ :param config: JSON data to consider
106+ :type config: dict
107+ :return: Tuple containing version and build data respectively
108+ :rtype: Tuple[str, int]
109+ """
110+
111+ # Read the data, and attempt to pull some info out of it
112+
113+ current = config ['currentVersion' ]
114+
115+ # Splitting the data in two so we can pull some content out:
116+
117+ split = current .split (" " )[0 ].split ("-" )
118+
119+ # Getting build information:
120+
121+ build = int (split [1 ])
122+
123+ # Getting version information:
124+
125+ version = split [0 ]
126+
127+ # Returning version information:
128+
129+ return version , build
130+
131+
85132def upgrade_script (serv : ServerUpdater ):
86133 """
87134 Upgrades this script.
@@ -717,31 +764,55 @@ def load_config(self, config: str) -> Tuple[str, int]:
717764
718765 output ("# Loading configuration data from file [{}] ..." .format (config ))
719766
720- if os .path .isfile (config ):
767+ if not os .path .isfile (config ):
721768
722- try :
769+ print ( "# Unable to load config data from file at [{}] - Not found/Not a file!" . format ( config ))
723770
724- return load_config ( config )
771+ return '0' , 0
725772
726- except JSONDecodeError :
773+ # Load file content
727774
728- # Data not in valid JSON format.
775+ try :
729776
730- output ( "# Failed to load config data - Not in JSON format!" )
777+ # Open file
731778
732- return '0' , 0
779+ with open ( config , 'r' ) as file :
733780
734- except Exception :
781+ # Decode file
735782
736- # Extra weird errors due to formatting issues:
783+ data = json . load ( file )
737784
738- output ( "# Failed to load config data - Strange format, we support official builds only!" )
785+ except JSONDecodeError :
739786
740- return '0' , 0
787+ # Data not in valid JSON format.
741788
742- else :
789+ output ( "# Failed to load config data - Not in JSON format!" )
743790
744- print ("# Unable to load config data from file at [{}] - Not found/Not a file!" .format (config ))
791+ return '0' , 0
792+
793+ try :
794+
795+ # First, try old format...
796+
797+ return load_config_old (data )
798+
799+ except Exception :
800+
801+ # Did not work, try something else ...
802+
803+ pass
804+
805+ # Try new format:
806+
807+ try :
808+
809+ return load_config_new (data )
810+
811+ except Exception :
812+
813+ # Extra weird errors due to formatting issues:
814+
815+ output ("# Failed to load config data - Strange format, we support official builds only!" )
745816
746817 return '0' , 0
747818
0 commit comments