MinecraftVersions.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from homepagebuilder.interfaces import Logger
  2. import requests
  3. import json
  4. import re
  5. LAUNCHER_MANIFSET_URL = 'https://piston-meta.mojang.com/mc/game/version_manifest.json'
  6. MANIFSET = None
  7. VERSIONS_DICT = None
  8. FULL_VERSIONS = None
  9. LATEST_VERSIONS = None
  10. logger = Logger("VersionScript")
  11. def fetch_manifset():
  12. try:
  13. response = requests.get(LAUNCHER_MANIFSET_URL,timeout=10000)
  14. return json.loads(response.content)
  15. except Exception as e:
  16. logger.warning("加载 MC 版本列表失败")
  17. logger.exception(e)
  18. return {}
  19. def get_manifset():
  20. global MANIFSET
  21. if not MANIFSET:
  22. MANIFSET = fetch_manifset()
  23. return MANIFSET
  24. def init_version_dict():
  25. global LATEST_VERSIONS,FULL_VERSIONS,VERSIONS_DICT
  26. VERSIONS_DICT = {}
  27. LATEST_VERSIONS = {'snapshot':None,'release':None}
  28. manifset = get_manifset()
  29. LATEST_VERSIONS.update(manifset['latest'])
  30. FULL_VERSIONS = manifset.get('versions')
  31. for version in FULL_VERSIONS:
  32. VERSIONS_DICT[version['id']] = version
  33. if get_version_type(LATEST_VERSIONS["snapshot"]) == 'release':
  34. LATEST_VERSIONS["snapshot"] = None
  35. logger.info(f'最新快照版:{LATEST_VERSIONS.get("snapshot")}; 最新正式版:{LATEST_VERSIONS["release"]}')
  36. def get_latest(version_type = 'all',version_rank = -1):
  37. global LATEST_VERSIONS
  38. if not LATEST_VERSIONS:
  39. init_version_dict()
  40. version_rank = int(version_rank)
  41. if version_rank <= 0:
  42. if(version_type == 'all'):
  43. if latest_snapshot := LATEST_VERSIONS.get('snapshot'):
  44. return latest_snapshot
  45. else:
  46. return LATEST_VERSIONS['release']
  47. return LATEST_VERSIONS.get(version_type)
  48. count = version_rank
  49. for version in FULL_VERSIONS:
  50. if version['type'] == version_type:
  51. count -= 1
  52. if count == 0:
  53. return version['id']
  54. return ''
  55. def get_server_jar(version_id):
  56. global VERSIONS_DICT
  57. if not (ver_info := VERSIONS_DICT.get(version_id)):
  58. return None
  59. response = requests.get(ver_info['url'],timeout=10000)
  60. return json.loads(response.content)['downloads']['server']['url']
  61. SNAPSHOT_PATTERN = re.compile(r'^[0-9]{2}[w|W][0-9]{2}[A-Fa-f]$')
  62. PRE_RELEASE_PATTERN = re.compile(r'^.*-pre[0-9]+$')
  63. RELEASE_CANDIDATE_PATTERN = re.compile(r'^.*-rc[0-9]+$')
  64. RELEASE_PATTERN = re.compile(r'^1\.[0-9]+(\.[0-9]+)?$')
  65. def get_version_type(version_id):
  66. '''获取版本类型'''
  67. if re.match(SNAPSHOT_PATTERN,version_id):
  68. return 'snapshot'
  69. if re.match(PRE_RELEASE_PATTERN,version_id):
  70. return 'pre_release'
  71. if re.match(RELEASE_CANDIDATE_PATTERN,version_id):
  72. return 'release_candidate'
  73. if re.match(RELEASE_PATTERN,version_id):
  74. return 'release'
  75. return 'other'