22This module is used to forge Jiyu UDP packets.
33"""
44
5+ from __future__ import annotations
6+
57import binascii
68import secrets
79
@@ -242,12 +244,22 @@ def __init__(self, value: int = 0):
242244 raise ValueError ("Value must be a non-negative integer" )
243245 self .value = value
244246
245- def __getattr__ (self , name : str ) -> str :
247+ def __getattr__ (self , name : str ) -> HexStr | HexInt :
246248 try :
247249 if name .startswith ("little_" ):
248- return self .value .to_bytes (int (name [7 :]), "little" ).hex ()
250+ return HexStr ( self .value .to_bytes (int (name [7 :]), "little" ).hex () )
249251 if name .startswith ("big_" ):
250- return self .value .to_bytes (int (name [4 :]), "big" ).hex ()
252+ return HexStr (self .value .to_bytes (int (name [4 :]), "big" ).hex ())
253+ if name .startswith ("add_" ):
254+ return HexInt (self .value + int (name [4 :]))
255+ if name .startswith ("sub_" ):
256+ return HexInt (self .value - int (name [4 :]))
257+ if name .startswith ("mul_" ):
258+ return HexInt (self .value * int (name [4 :]))
259+ if name .startswith ("div_" ):
260+ return HexInt (self .value // int (name [4 :]))
261+ if name .startswith ("mod_" ):
262+ return HexInt (self .value % int (name [4 :]))
251263 except ValueError :
252264 pass
253265
@@ -268,10 +280,18 @@ class HexStr:
268280 def __init__ (self , value : str = "" ):
269281 self .value = str (value )
270282
271- def __getattr__ (self , name : str ) -> str :
283+ def __getattr__ (self , name : str ) -> HexStr | HexInt :
272284 try :
285+ if name == "len" :
286+ return HexInt (len (self .value ))
287+ if name == "hex" :
288+ return HexStr (self .value .encode ("utf-8" ).hex ())
289+ if name == "int" :
290+ return HexInt (int (self .value ))
291+ if name .startswith ("int_" ):
292+ return HexInt (int (self .value , int (name [4 :])))
273293 if name .startswith ("size_" ):
274- return format_data (self .value , int (name [5 :])).hex ()
294+ return HexStr ( format_data (self .value , int (name [5 :])).hex () )
275295 except ValueError :
276296 pass
277297
@@ -295,11 +315,4 @@ def pkg_customize(format_str: str, *args: str) -> bytes:
295315 Returns:
296316 bytes: The packaged command as a byte array, including a header and formatted data.
297317 """
298- user_args = []
299- for arg in args :
300- try :
301- user_args .append (HexInt (int (arg )))
302- except ValueError :
303- user_args .append (HexStr (arg ))
304-
305- return binascii .unhexlify (format_str .format (* user_args , rand16 = rand16 ))
318+ return binascii .unhexlify (format_str .format (* map (HexStr , args ), rand16 = rand16 ))
0 commit comments