extract_high_level.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import argparse
  2. import os
  3. import sys
  4. import json
  5. from pathlib import Path
  6. # Add the utils directory to the Python path
  7. # This allows importing modules from the 'utils' subfolder
  8. utils_path = Path(__file__).parent / 'utils'
  9. sys.path.append(str(utils_path))
  10. from yaml_utils import load_unity_yaml, convert_to_plain_python_types
  11. from file_utils import find_files_by_extension, create_guid_to_path_map
  12. from json_utils import write_json
  13. def parse_physics_settings(input_dir, project_mode):
  14. """
  15. Parses the appropriate physics settings file based on the project mode.
  16. """
  17. physics_data = {}
  18. if project_mode == "3D":
  19. asset_path = input_dir / "ProjectSettings" / "DynamicsManager.asset"
  20. if asset_path.is_file():
  21. docs = load_unity_yaml(str(asset_path))
  22. if docs:
  23. settings = convert_to_plain_python_types(docs[0]).get('PhysicsManager', {})
  24. physics_data['gravity'] = settings.get('m_Gravity')
  25. physics_data['sleepThreshold'] = settings.get('m_SleepThreshold')
  26. physics_data['solverType'] = settings.get('m_SolverType')
  27. physics_data['layerCollisionMatrix'] = settings.get('m_LayerCollisionMatrix')
  28. physics_data['autoSimulation'] = settings.get('m_AutoSimulation')
  29. physics_data['autoSyncTransforms'] = settings.get('m_AutoSyncTransforms')
  30. else: # 2D
  31. asset_path = input_dir / "ProjectSettings" / "Physics2DSettings.asset"
  32. if asset_path.is_file():
  33. docs = load_unity_yaml(str(asset_path))
  34. if docs:
  35. settings = convert_to_plain_python_types(docs[0]).get('Physics2DSettings', {})
  36. physics_data['gravity'] = settings.get('m_Gravity')
  37. physics_data['velocityIterations'] = settings.get('m_VelocityIterations')
  38. physics_data['positionIterations'] = settings.get('m_PositionIterations')
  39. physics_data['layerCollisionMatrix'] = settings.get('m_LayerCollisionMatrix')
  40. physics_data['autoSimulation'] = settings.get('m_AutoSimulation')
  41. physics_data['autoSyncTransforms'] = settings.get('m_AutoSyncTransforms')
  42. return physics_data
  43. def parse_project_settings(input_dir, output_dir, indent=None):
  44. """
  45. Parses various project settings files to create a comprehensive manifest.
  46. """
  47. print("\n--- Starting Task 2: Comprehensive Project Settings Parser ---")
  48. manifest_data = {}
  49. guid_map = create_guid_to_path_map(str(input_dir))
  50. # --- ProjectSettings.asset ---
  51. project_settings_path = input_dir / "ProjectSettings" / "ProjectSettings.asset"
  52. if project_settings_path.is_file():
  53. docs = load_unity_yaml(str(project_settings_path))
  54. if docs:
  55. player_settings = convert_to_plain_python_types(docs[0]).get('PlayerSettings', {})
  56. manifest_data['productName'] = player_settings.get('productName')
  57. manifest_data['companyName'] = player_settings.get('companyName')
  58. manifest_data['bundleVersion'] = player_settings.get('bundleVersion')
  59. manifest_data['activeColorSpace'] = player_settings.get('m_ActiveColorSpace')
  60. # --- Mappers for human-readable values ---
  61. scripting_backend_map = {0: "Mono", 1: "IL2CPP"}
  62. api_compatibility_map = {3: ".NET Framework", 6: ".NET Standard 2.1"}
  63. # --- Extract and map platform-specific settings ---
  64. scripting_backends = player_settings.get('scriptingBackend', {})
  65. manifest_data['scriptingBackend'] = {
  66. platform: scripting_backend_map.get(val, f"Unknown ({val})")
  67. for platform, val in scripting_backends.items()
  68. }
  69. api_levels = player_settings.get('apiCompatibilityLevelPerPlatform', {})
  70. manifest_data['apiCompatibilityLevel'] = {
  71. platform: api_compatibility_map.get(val, f"Unknown ({val})")
  72. for platform, val in api_levels.items()
  73. }
  74. # Fallback for older Unity versions that use a single key
  75. if not api_levels and 'apiCompatibilityLevel' in player_settings:
  76. val = player_settings.get('apiCompatibilityLevel')
  77. manifest_data['apiCompatibilityLevel']['Standalone'] = api_compatibility_map.get(val, f"Unknown ({val})")
  78. manifest_data['activeInputHandler'] = player_settings.get('activeInputHandler')
  79. manifest_data['allowUnsafeCode'] = player_settings.get('allowUnsafeCode')
  80. manifest_data['managedStrippingLevel'] = player_settings.get('managedStrippingLevel')
  81. manifest_data['scriptingDefineSymbols'] = player_settings.get('scriptingDefineSymbols')
  82. # --- Deduce configured platforms from various settings ---
  83. configured_platforms = set()
  84. if 'applicationIdentifier' in player_settings:
  85. configured_platforms.update(player_settings['applicationIdentifier'].keys())
  86. if 'scriptingBackend' in player_settings:
  87. configured_platforms.update(player_settings['scriptingBackend'].keys())
  88. manifest_data['configuredPlatforms'] = sorted(list(configured_platforms))
  89. # --- Filter managedStrippingLevel based on configured platforms ---
  90. managed_stripping_level = player_settings.get('managedStrippingLevel', {})
  91. manifest_data['managedStrippingLevel'] = {
  92. platform: level
  93. for platform, level in managed_stripping_level.items()
  94. if platform in manifest_data['configuredPlatforms']
  95. }
  96. # --- Populate all configured platforms for scripting settings ---
  97. default_api_level = player_settings.get('apiCompatibilityLevel')
  98. final_scripting_backends = {}
  99. final_api_levels = {}
  100. for platform in manifest_data['configuredPlatforms']:
  101. # Scripting Backend (Default to Mono if not specified)
  102. backend_val = scripting_backends.get(platform, 0)
  103. final_scripting_backends[platform] = scripting_backend_map.get(backend_val, f"Unknown ({backend_val})")
  104. # API Compatibility Level (Default to project's global setting if not specified)
  105. level_val = api_levels.get(platform, default_api_level)
  106. final_api_levels[platform] = api_compatibility_map.get(level_val, f"Unknown ({level_val})")
  107. manifest_data['scriptingBackend'] = final_scripting_backends
  108. manifest_data['apiCompatibilityLevel'] = final_api_levels
  109. # --- EditorSettings.asset for 2D/3D Mode ---
  110. editor_settings_path = input_dir / "ProjectSettings" / "EditorSettings.asset"
  111. if editor_settings_path.is_file():
  112. docs = load_unity_yaml(str(editor_settings_path))
  113. if docs:
  114. editor_settings = convert_to_plain_python_types(docs[0]).get('EditorSettings', {})
  115. manifest_data['projectMode'] = "2D" if editor_settings.get('m_DefaultBehaviorMode') == 1 else "3D"
  116. # --- GraphicsSettings.asset for Render Pipeline ---
  117. graphics_settings_path = input_dir / "ProjectSettings" / "GraphicsSettings.asset"
  118. manifest_data['renderPipeline'] = 'Built-in'
  119. if graphics_settings_path.is_file():
  120. docs = load_unity_yaml(str(graphics_settings_path))
  121. if docs:
  122. graphics_settings = convert_to_plain_python_types(docs[0]).get('GraphicsSettings', {})
  123. pipeline_ref = graphics_settings.get('m_CustomRenderPipeline') or graphics_settings.get('m_SRPDefaultSettings', {}).get('UnityEngine.Rendering.Universal.UniversalRenderPipeline')
  124. if pipeline_ref and pipeline_ref.get('guid'):
  125. guid = pipeline_ref['guid']
  126. if guid in guid_map:
  127. asset_path = Path(guid_map[guid]).name.upper()
  128. if "URP" in asset_path: manifest_data['renderPipeline'] = 'URP'
  129. elif "HDRP" in asset_path: manifest_data['renderPipeline'] = 'HDRP'
  130. else: manifest_data['renderPipeline'] = 'Scriptable'
  131. # --- TagManager.asset ---
  132. tag_manager_path = input_dir / "ProjectSettings" / "TagManager.asset"
  133. if tag_manager_path.is_file():
  134. docs = load_unity_yaml(str(tag_manager_path))
  135. if docs:
  136. tag_manager = convert_to_plain_python_types(docs[0]).get('TagManager', {})
  137. manifest_data['tags'] = tag_manager.get('tags')
  138. layers_list = tag_manager.get('layers', [])
  139. # Only include layers that have a name, preserving their index
  140. manifest_data['layers'] = {i: name for i, name in enumerate(layers_list) if name}
  141. # --- EditorBuildSettings.asset ---
  142. build_settings_path = input_dir / "ProjectSettings" / "EditorBuildSettings.asset"
  143. if build_settings_path.is_file():
  144. docs = load_unity_yaml(str(build_settings_path))
  145. if docs:
  146. build_settings = convert_to_plain_python_types(docs[0]).get('EditorBuildSettings', {})
  147. manifest_data['buildScenes'] = [
  148. {'path': scene.get('path'), 'enabled': scene.get('enabled') == 1}
  149. for scene in build_settings.get('m_Scenes', [])
  150. ]
  151. # --- TimeManager.asset ---
  152. time_manager_path = input_dir / "ProjectSettings" / "TimeManager.asset"
  153. if time_manager_path.is_file():
  154. docs = load_unity_yaml(str(time_manager_path))
  155. if docs:
  156. time_manager = convert_to_plain_python_types(docs[0]).get('TimeManager', {})
  157. # Cherry-pick only the useful time settings
  158. manifest_data['timeSettings'] = {
  159. 'Fixed Timestep': time_manager.get('Fixed Timestep'),
  160. 'Maximum Allowed Timestep': time_manager.get('Maximum Allowed Timestep'),
  161. 'm_TimeScale': time_manager.get('m_TimeScale'),
  162. 'Maximum Particle Timestep': time_manager.get('Maximum Particle Timestep')
  163. }
  164. # --- Physics Settings ---
  165. manifest_data['physicsSettings'] = parse_physics_settings(input_dir, manifest_data.get('projectMode', '3D'))
  166. # --- Write manifest.json ---
  167. manifest_output_path = output_dir / "manifest.json"
  168. try:
  169. write_json(manifest_data, manifest_output_path, indent=indent)
  170. print(f"Successfully created manifest.json at {manifest_output_path}")
  171. except Exception as e:
  172. print(f"Error writing to {manifest_output_path}. {e}", file=sys.stderr)
  173. def parse_package_manifests(input_dir, output_dir, indent=None):
  174. """
  175. Parses the primary package manifest and creates a clean packages.json file.
  176. """
  177. print("\n--- Starting Task 3: Package Manifest Extractor ---")
  178. manifest_path = input_dir / "Packages" / "manifest.json"
  179. if manifest_path.is_file():
  180. try:
  181. with open(manifest_path, 'r', encoding='utf-8') as f:
  182. packages_data = json.load(f)
  183. packages_output_path = output_dir / "packages.json"
  184. write_json(packages_data, packages_output_path, indent=indent)
  185. print(f"Successfully created packages.json at {packages_output_path}")
  186. except (IOError, json.JSONDecodeError) as e:
  187. print(f"Error processing {manifest_path}: {e}", file=sys.stderr)
  188. else:
  189. print(f"Warning: {manifest_path} not found.")
  190. def main():
  191. """
  192. Main function to run the high-level data extraction process.
  193. """
  194. parser = argparse.ArgumentParser(
  195. description="Extracts high-level summary data from a Unity project."
  196. )
  197. parser.add_argument("--input", type=str, required=True, help="The root directory of the target Unity project.")
  198. parser.add_argument("--output", type=str, required=True, help="The directory where the generated output folder will be saved.")
  199. parser.add_argument("--indent", type=int, default=None, help="Indentation level for JSON output. Defaults to None (compact).")
  200. args = parser.parse_args()
  201. input_dir = Path(args.input)
  202. output_dir = Path(args.output)
  203. if not input_dir.is_dir():
  204. print(f"Error: Input path '{input_dir}' is not a valid directory.", file=sys.stderr)
  205. sys.exit(1)
  206. high_level_output_dir = output_dir / "HighLevel"
  207. try:
  208. high_level_output_dir.mkdir(parents=True, exist_ok=True)
  209. print(f"Output will be saved to: {high_level_output_dir}")
  210. except OSError as e:
  211. print(f"Error: Could not create output directory '{high_level_output_dir}'. {e}", file=sys.stderr)
  212. sys.exit(1)
  213. parse_project_settings(input_dir, high_level_output_dir, indent=args.indent)
  214. parse_package_manifests(input_dir, high_level_output_dir, indent=args.indent)
  215. print("\nHigh-level extraction complete.")
  216. if __name__ == "__main__":
  217. main()