generateNeoforge.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import json
  2. import os
  3. import sys
  4. from meta.common import ensure_component_dir, polymc_path, upstream_path, static_path
  5. from meta.common.mojang import MINECRAFT_COMPONENT
  6. from meta.common.neoforge import NEOFORGE_COMPONENT, INSTALLER_MANIFEST_DIR, VERSION_MANIFEST_DIR, DERIVED_INDEX_FILE
  7. from meta.model import MetaVersion, Dependency, Library, GradleSpecifier, MojangLibraryDownloads, MojangArtifact, \
  8. MetaPackage
  9. from meta.model.mojang import MojangVersion
  10. from meta.model.neoforge import NeoForgeEntry, NeoForgeInstallerProfile
  11. PMC_DIR = polymc_path()
  12. UPSTREAM_DIR = upstream_path()
  13. STATIC_DIR = static_path()
  14. ensure_component_dir(NEOFORGE_COMPONENT)
  15. def eprint(*args, **kwargs):
  16. print(*args, file=sys.stderr, **kwargs)
  17. def version_from_installer(installer: MojangVersion, profile: NeoForgeInstallerProfile,
  18. entry: NeoForgeEntry) -> MetaVersion:
  19. v = MetaVersion(name="NeoForge", version=entry.sane_version(), uid=NEOFORGE_COMPONENT)
  20. v.requires = [Dependency(uid=MINECRAFT_COMPONENT, equals=entry.mc_version)]
  21. v.main_class = "io.github.zekerzhayard.forgewrapper.installer.Main"
  22. # FIXME: Add the size and hash here
  23. v.maven_files = []
  24. # load the locally cached installer file info and use it to add the installer entry in the json
  25. installer_lib = Library(
  26. name=GradleSpecifier("net.neoforged", "forge" if entry.mc_version == "1.20.1" else "neoforge",
  27. entry.version, "installer"))
  28. installer_lib.downloads = MojangLibraryDownloads()
  29. installer_lib.downloads.artifact = MojangArtifact(
  30. url=entry.installer_url(),
  31. sha1=entry.installer_sha1,
  32. size=entry.installer_size)
  33. v.maven_files.append(installer_lib)
  34. for upstream_lib in profile.libraries:
  35. forge_lib = Library.parse_obj(upstream_lib.dict())
  36. if forge_lib.name.is_log4j():
  37. continue
  38. v.maven_files.append(forge_lib)
  39. v.libraries = []
  40. wrapper_lib = Library(name=GradleSpecifier("io.github.zekerzhayard", "ForgeWrapper", "1.6.0"))
  41. wrapper_lib.downloads = MojangLibraryDownloads()
  42. wrapper_lib.downloads.artifact = MojangArtifact(
  43. url="https://github.com/ZekerZhayard/ForgeWrapper/releases/download/1.6.0/ForgeWrapper-1.6.0.jar",
  44. sha1="035a51fe6439792a61507630d89382f621da0f1f",
  45. size=28679
  46. )
  47. v.libraries.append(wrapper_lib)
  48. for upstream_lib in installer.libraries:
  49. forge_lib = Library.parse_obj(upstream_lib.dict())
  50. if forge_lib.name.is_log4j():
  51. continue
  52. v.libraries.append(forge_lib)
  53. v.release_time = installer.release_time
  54. v.order = 5
  55. mc_args = "--username ${auth_player_name} --version ${version_name} --gameDir ${game_directory} " \
  56. "--assetsDir ${assets_root} --assetIndex ${assets_index_name} --uuid ${auth_uuid} " \
  57. "--accessToken ${auth_access_token} --userType ${user_type} --versionType ${version_type}"
  58. for arg in installer.arguments.game:
  59. mc_args += f" {arg}"
  60. v.minecraft_arguments = mc_args
  61. return v
  62. def main():
  63. # load the locally cached version list
  64. with open(os.path.join(UPSTREAM_DIR, DERIVED_INDEX_FILE), 'r') as f:
  65. entries = [NeoForgeEntry.from_obj(e) for e in json.load(f)]
  66. recommended_versions = []
  67. for entry in entries:
  68. if not os.path.isfile(os.path.join(PMC_DIR, MINECRAFT_COMPONENT, f"{entry.mc_version}.json")):
  69. eprint(f"Skipping NeoForge {entry.sane_version()} with no corresponding Minecraft version {entry.mc_version}")
  70. continue
  71. if entry.latest:
  72. recommended_versions.append(entry.sane_version())
  73. # Path for new-style build system based installers
  74. profile_filepath = os.path.join(UPSTREAM_DIR, INSTALLER_MANIFEST_DIR, f"{entry.sane_version()}.json")
  75. installer_version_filepath = os.path.join(UPSTREAM_DIR, VERSION_MANIFEST_DIR, f"{entry.sane_version()}.json")
  76. if not os.path.isfile(profile_filepath):
  77. eprint(f"Skipping {entry.sane_version()} with missing profile json")
  78. continue
  79. eprint(f"Processing NeoForge {entry.sane_version()}")
  80. profile = NeoForgeInstallerProfile.parse_file(profile_filepath)
  81. installer = MojangVersion.parse_file(installer_version_filepath)
  82. v = version_from_installer(installer, profile, entry)
  83. v.write(os.path.join(PMC_DIR, NEOFORGE_COMPONENT, f"{v.version}.json"))
  84. recommended_versions.sort()
  85. print('Recommended versions:', recommended_versions)
  86. package = MetaPackage(uid=NEOFORGE_COMPONENT, name="NeoForge", project_url="https://neoforged.net/")
  87. package.recommended = recommended_versions
  88. package.write(os.path.join(PMC_DIR, NEOFORGE_COMPONENT, "package.json"))
  89. if __name__ == '__main__':
  90. main()