AppRuntimesPackageValidation.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.Build;
  6. using UnityEditor.Build.Reporting;
  7. using UnityEditor.Compilation;
  8. using UnityEditor.SceneManagement;
  9. using UnityEngine;
  10. public static class AppRuntimesPackageValidation
  11. {
  12. public static void Run()
  13. {
  14. var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(AppSettings).Assembly);
  15. Require(package != null && package.name == "come.app.runtimes" && package.displayName == "AppRuntimes", "Package identity");
  16. if (Environment.GetCommandLineArgs().Contains("-appRuntimesPackageOnly"))
  17. {
  18. var dependencies = Newtonsoft.Json.Linq.JObject.Parse(File.ReadAllText("Packages/manifest.json"))["dependencies"];
  19. Require(dependencies.Count() == 1 && dependencies["come.app.runtimes"] != null, "Clean project installs only AppRuntimes");
  20. }
  21. Require(!CompilationPipeline.GetAssemblies().Any(a => a.name.Contains("DOTween")), "No DOTween assembly");
  22. Require(typeof(IAPWrapper).Assembly == typeof(AppRuntimes).Assembly, "IAP wrapper is compiled into AppRuntimes");
  23. foreach (string relative in new[] {
  24. "ThirdParty/FacebookSDK/Plugins/Android/Facebook.Unity.Android.dll",
  25. "ThirdParty/FacebookSDK/Plugins/Android/libs/facebook-android-wrapper-18.1.0.aar",
  26. "ThirdParty/UniWebView/Plugins/Android/UniWebView.aar",
  27. "ThirdParty/ExternalDependencyManager/Editor/1.2.187/Google.JarResolver.dll",
  28. "ThirdParty/ExternalDependencyManager/Editor/1.2.187/Google.IOSResolver.dll",
  29. "Runtime/IAP/IAPAndroidImpl.cs",
  30. "ThirdParty/IAPiOS/IAP/Editor/iOS/FBSDKUnityStoreKit.storekit"
  31. }) Require(File.Exists(Path.Combine(package.resolvedPath, relative)), "Platform SDK retained: " + relative);
  32. Require(!File.Exists(Path.Combine(package.resolvedPath, "Runtime/Resources/AppSettings.asset")), "Templates stay outside runtime Resources");
  33. var loading = Resources.Load<GameObject>("Loading");
  34. Require(loading != null && loading.GetComponentsInChildren<MonoBehaviour>(true).All(c => c != null), "Loading prefab and GUID references");
  35. AppRuntimesSettingsValidation.VerifyStartup();
  36. foreach (string module in new[] { "FBAEMKit", "FBSDKCoreKit_Basics", "FBSDKCoreKit", "FBSDKLoginKit", "FBSDKShareKit", "FBSDKGamingServicesKit" })
  37. {
  38. var importer = AssetImporter.GetAtPath(package.assetPath + "/ThirdParty/FacebookSDK/Plugins/iOS/XCFrameworks/" + module + ".xcframework") as PluginImporter;
  39. Require(importer != null && importer.GetCompatibleWithPlatform(BuildTarget.iOS), "iOS framework importer: " + module);
  40. }
  41. foreach (string source in new[] { "IAPUnityInterface.mm", "IAPSK1.mm", "Swift/IAPSK2.swift" })
  42. {
  43. var importer = AssetImporter.GetAtPath(package.assetPath + "/ThirdParty/IAPiOS/IAP/Editor/iOS/" + source) as PluginImporter;
  44. Require(importer != null && importer.GetCompatibleWithPlatform(BuildTarget.iOS), "IAP iOS importer: " + source);
  45. }
  46. foreach (string file in new[] { "meta.mp4", "meta-logo.png" })
  47. {
  48. Require(File.Exists(AppRuntimesStreamingAssets.GetPath(file)), "StreamingAssets editor path: " + file);
  49. Require(!File.Exists(Path.Combine(Application.streamingAssetsPath, file)), "No host copy of package StreamingAssets: " + file);
  50. }
  51. bool rejectedTraversal = false;
  52. try { AppRuntimesStreamingAssets.GetPath("../package.json"); }
  53. catch (ArgumentException) { rejectedTraversal = true; }
  54. Require(rejectedTraversal, "StreamingAssets path stays relative");
  55. var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
  56. new GameObject("AppRuntimes validation").AddComponent<AppRuntimesValidationStartup>();
  57. new GameObject("IAP validation").AddComponent<IAPWrapper>();
  58. const string scenePath = "Assets/AppRuntimesValidation.unity";
  59. EditorSceneManager.SaveScene(scene, scenePath);
  60. EditorBuildSettings.scenes = new[] { new EditorBuildSettingsScene(scenePath, true) };
  61. PlayerSettings.SetApplicationIdentifier(NamedBuildTarget.iOS, "come.app.runtimes.validation");
  62. PlayerSettings.SetScriptingBackend(NamedBuildTarget.iOS, ScriptingImplementation.IL2CPP);
  63. PlayerSettings.SetManagedStrippingLevel(NamedBuildTarget.iOS, ManagedStrippingLevel.High);
  64. AppRuntimesGupsValidation.BeforeBuild();
  65. bool obfuscationSkipped = false;
  66. Application.LogCallback checkObfuscation = (message, stack, type) => {
  67. if (message.Contains("Obfuscation is deactivated")) obfuscationSkipped = true;
  68. };
  69. BuildReport report;
  70. Application.logMessageReceived += checkObfuscation;
  71. try
  72. {
  73. report = BuildPipeline.BuildPlayer(new BuildPlayerOptions {
  74. scenes = new[] { scenePath }, target = BuildTarget.iOS,
  75. locationPathName = "Build/iOS", options = BuildOptions.None
  76. });
  77. }
  78. finally { Application.logMessageReceived -= checkObfuscation; }
  79. Require(report != null && report.summary.result == BuildResult.Succeeded, "iOS build export");
  80. Require(report.summary.totalErrors == 0, "iOS build has no reported errors");
  81. if (Environment.GetCommandLineArgs().Contains("-appRuntimesPackageOnly"))
  82. Require(!obfuscationSkipped, "GUPS obfuscation was not skipped");
  83. AppRuntimesGupsValidation.AfterBuild();
  84. AppRuntimesXcodeLayoutValidation.Verify("Build/iOS");
  85. var project = new UnityEditor.iOS.Xcode.PBXProject();
  86. project.ReadFromFile("Build/iOS/Unity-iPhone.xcodeproj/project.pbxproj");
  87. var bridge = project.GetRealPathsOfAllFiles(UnityEditor.iOS.Xcode.PBXSourceTree.Source)
  88. .FirstOrDefault(p => Path.GetFileName(p) == "FacebookDynamicCredentials.m");
  89. Require(bridge != null, "Package native bridge exported");
  90. var bridgeGuid = project.FindFileGuidByRealPath(bridge, UnityEditor.iOS.Xcode.PBXSourceTree.Source);
  91. Require(project.GetCompileFlagsForFile(project.GetUnityFrameworkTargetGuid(), bridgeGuid).Contains("-fobjc-arc"), "Package bridge ARC flag");
  92. Require(Directory.GetFiles("Build/iOS/FacebookSDKResources", "PrivacyInfo.xcprivacy", SearchOption.AllDirectories).Length == 5, "Facebook privacy manifests exported");
  93. string projectText = File.ReadAllText("Build/iOS/Unity-iPhone.xcodeproj/project.pbxproj");
  94. foreach (string file in new[] { "IAPUnityInterface.mm", "IAPSK1.mm", "IAPSK2.swift" })
  95. {
  96. string exportedPath = project.GetRealPathsOfAllFiles(UnityEditor.iOS.Xcode.PBXSourceTree.Source)
  97. .SingleOrDefault(p => Path.GetFileName(p) == file);
  98. Require(exportedPath != null && File.Exists(Path.Combine("Build/iOS", exportedPath))
  99. && projectText.Contains(file + " in Sources"), "IAP native source exported and registered for compilation: " + file);
  100. }
  101. foreach (string file in new[] { "meta.mp4", "meta-logo.png" })
  102. {
  103. string exportedPath = "Build/iOS/Data/Raw/" + file;
  104. Require(File.Exists(exportedPath)
  105. && File.ReadAllBytes(exportedPath).SequenceEqual(File.ReadAllBytes(AppRuntimesStreamingAssets.GetPath(file))),
  106. "StreamingAssets exported unchanged: " + file);
  107. Require(!File.Exists(Path.Combine(Application.streamingAssetsPath, file)), "Build leaves host Assets unchanged: " + file);
  108. }
  109. Require(!Directory.GetFiles("Build/iOS/Data/Raw", "*.meta", SearchOption.AllDirectories).Any(), "StreamingAssets metadata excluded from export");
  110. Debug.Log("APP_RUNTIMES_IOS_VALIDATION_PASS");
  111. }
  112. private static void Require(bool condition, string check)
  113. {
  114. if (!condition) throw new Exception("AppRuntimes validation failed: " + check);
  115. Debug.Log("APP_RUNTIMES_CHECK_PASS " + check);
  116. }
  117. }