Beispiel #1
0
    private static bool TransferSceneBundles(OVRADBTool adbTool, string absoluteTempPath, string externalSceneCache)
    {
        List <string> bundlesToTransfer = new List <string>();
        List <string> bundlesToDelete   = new List <string>();
        string        manifestFilePath  = externalSceneCache + "/" + BUNDLE_MANAGER_MASTER_BUNDLE;

        string[] pullManifestCommand = { "-d pull", "\"" + manifestFilePath + "\"", "\"" + absoluteTempPath + "\"" };

        string output, error;

        if (adbTool.RunCommand(pullManifestCommand, null, out output, out error) == 0)
        {
            // An existing manifest file was found on device. Load hashes and upload bundles that have changed hashes.
            Debug.Log("[OVRBundleManager] - Scene bundle manifest file found. Decoding changes . . .");

            // Load hashes from remote manifest
            AssetBundle remoteBundle = AssetBundle.LoadFromFile(Path.Combine(absoluteTempPath, BUNDLE_MANAGER_MASTER_BUNDLE));
            if (remoteBundle == null)
            {
                OVRBundleTool.PrintError("Failed to load remote asset bundle manifest file.");
                return(false);
            }
            AssetBundleManifest remoteManifest = remoteBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");

            Dictionary <string, Hash128> remoteBundleToHash = new Dictionary <string, Hash128>();
            if (remoteManifest != null)
            {
                string[] assetBundles = remoteManifest.GetAllAssetBundles();
                foreach (string bundleName in assetBundles)
                {
                    remoteBundleToHash[bundleName] = remoteManifest.GetAssetBundleHash(bundleName);
                }
            }
            remoteBundle.Unload(true);

            // Load hashes from local manifest
            AssetBundle localBundle = AssetBundle.LoadFromFile(BUNDLE_MANAGER_OUTPUT_PATH + "\\" + BUNDLE_MANAGER_MASTER_BUNDLE
                                                               + "\\" + BUNDLE_MANAGER_MASTER_BUNDLE);
            if (localBundle == null)
            {
                OVRBundleTool.PrintError("<color=red>Failed to load local asset bundle manifest file.\n</color>");
                return(false);
            }
            AssetBundleManifest localManifest = localBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");

            if (localManifest != null)
            {
                Hash128 zeroHash = new Hash128(0, 0, 0, 0);

                // Build a list of dirty bundles that will have to be transfered
                string relativeSceneBundlesPath = Path.Combine(BUNDLE_MANAGER_OUTPUT_PATH, BUNDLE_MANAGER_MASTER_BUNDLE);
                bundlesToTransfer.Add(Path.Combine(relativeSceneBundlesPath, BUNDLE_MANAGER_MASTER_BUNDLE));
                string[] assetBundles = localManifest.GetAllAssetBundles();
                foreach (string bundleName in assetBundles)
                {
                    if (!remoteBundleToHash.ContainsKey(bundleName))
                    {
                        bundlesToTransfer.Add(Path.Combine(relativeSceneBundlesPath, bundleName));
                    }
                    else
                    {
                        if (remoteBundleToHash[bundleName] != localManifest.GetAssetBundleHash(bundleName))
                        {
                            bundlesToTransfer.Add(Path.Combine(relativeSceneBundlesPath, bundleName));
                        }
                        remoteBundleToHash[bundleName] = zeroHash;
                    }
                }

                OVRBundleTool.PrintLog(bundlesToTransfer.Count + " dirty bundle(s) will be transfered.\n");
            }
        }
        else
        {
            if (output.Contains("does not exist") || output.Contains("No such file or directory"))
            {
                // Fresh install of asset bundles, transfer all asset bundles
                OVRBundleTool.PrintLog("Manifest file not found. Transfering all bundles . . . ");

                string[] mkdirCommand = { "-d shell", "mkdir -p", "\"" + externalSceneCache + "\"" };
                if (adbTool.RunCommand(mkdirCommand, null, out output, out error) == 0)
                {
                    string absoluteSceneBundlePath = Path.Combine(Path.Combine(Application.dataPath, ".."),
                                                                  Path.Combine(BUNDLE_MANAGER_OUTPUT_PATH, BUNDLE_MANAGER_MASTER_BUNDLE));

                    string[] assetBundlePaths = Directory.GetFiles(absoluteSceneBundlePath);
                    if (assetBundlePaths.Length != 0)
                    {
                        foreach (string path in assetBundlePaths)
                        {
                            if (!path.Contains(".manifest"))
                            {
                                bundlesToTransfer.Add(path);
                            }
                        }
                    }
                    else
                    {
                        OVRBundleTool.PrintError("Failed to locate scene bundles to transfer.");
                        return(false);
                    }
                }
            }
        }

        // If any adb error occured during manifest calculation, print it and return false
        if (!string.IsNullOrEmpty(error) || output.Contains("error"))
        {
            OVRBundleTool.PrintError(string.IsNullOrEmpty(error) ? output : error);
            return(false);
        }

        // Transfer bundles to device
        DateTime transferStart = DateTime.Now;

        foreach (string bundle in bundlesToTransfer)
        {
            string   absoluteBundlePath = Path.Combine(Path.Combine(Application.dataPath, ".."), bundle);
            string[] pushBundleCommand  = { "-d push", "\"" + absoluteBundlePath + "\"", "\"" + externalSceneCache + "\"" };
            adbTool.RunCommandAsync(pushBundleCommand, null);
        }
        Debug.Log("[OVRBundleManager] - Transfer took " + (DateTime.Now - transferStart).TotalSeconds + " seconds.");

        // Delete stale bundles on device
        if (bundlesToDelete.Count > 0)
        {
            foreach (string bundle in bundlesToDelete)
            {
                string   bundlePath          = externalSceneCache + "/" + bundle;
                string[] deleteBundleCommand = { "-d shell", "rm", "\"" + bundlePath + "\"" };
                adbTool.RunCommandAsync(deleteBundleCommand, null);
            }
            OVRBundleTool.PrintLog("Deleted " + bundlesToDelete.Count + " bundle(s) that were stale");
        }

        return(true);
    }
Beispiel #2
0
    public void OnPostprocessBuild(BuildReport report)
    {
#if UNITY_ANDROID
        if (autoIncrementVersion)
        {
            if ((report.summary.options & BuildOptions.Development) == 0)
            {
                PlayerSettings.Android.bundleVersionCode++;
                UnityEngine.Debug.Log("Incrementing version code to " + PlayerSettings.Android.bundleVersionCode);
            }
        }

        bool isExporting = true;
        foreach (var step in report.steps)
        {
            if (step.name.Contains("Compile scripts") ||
                step.name.Contains("Building scenes") ||
                step.name.Contains("Writing asset files") ||
                step.name.Contains("Preparing APK resources") ||
                step.name.Contains("Creating Android manifest") ||
                step.name.Contains("Processing plugins") ||
                step.name.Contains("Exporting project") ||
                step.name.Contains("Building Gradle project"))
            {
                OVRPlugin.SendEvent("build_step_" + step.name.ToLower().Replace(' ', '_'),
                                    step.duration.TotalSeconds.ToString(), "ovrbuild");
#if BUILDSESSION
                UnityEngine.Debug.LogFormat("build_step_" + step.name.ToLower().Replace(' ', '_') + ": {0}", step.duration.TotalSeconds.ToString());
#endif
                if (step.name.Contains("Building Gradle project"))
                {
                    isExporting = false;
                }
            }
        }
        OVRPlugin.AddCustomMetadata("build_step_count", report.steps.Length.ToString());
        if (report.summary.outputPath.Contains("apk"))         // Exclude Gradle Project Output
        {
            var fileInfo = new System.IO.FileInfo(report.summary.outputPath);
            OVRPlugin.AddCustomMetadata("build_output_size", fileInfo.Length.ToString());
        }
#endif
        if (!report.summary.outputPath.Contains("OVRGradleTempExport"))
        {
            OVRPlugin.SendEvent("build_complete", (System.DateTime.Now - buildStartTime).TotalSeconds.ToString(), "ovrbuild");
#if BUILDSESSION
            UnityEngine.Debug.LogFormat("build_complete: {0}", (System.DateTime.Now - buildStartTime).TotalSeconds.ToString());
#endif
        }

#if UNITY_ANDROID
        if (!isExporting)
        {
            // Get the hosts path to Android SDK
            if (adbTool == null)
            {
                adbTool = new OVRADBTool(OVRConfig.Instance.GetAndroidSDKPath(false));
            }

            if (adbTool.isReady)
            {
                // Check to see if there are any ADB devices connected before continuing.
                List <string> devices = adbTool.GetDevices();
                if (devices.Count == 0)
                {
                    return;
                }

                // Clear current logs on device
                Process adbClearProcess;
                adbClearProcess = adbTool.RunCommandAsync(new string[] { "logcat --clear" }, null);

                // Add a timeout if we cannot get a response from adb logcat --clear in time.
                Stopwatch timeout = new Stopwatch();
                timeout.Start();
                while (!adbClearProcess.WaitForExit(100))
                {
                    if (timeout.ElapsedMilliseconds > 2000)
                    {
                        adbClearProcess.Kill();
                        return;
                    }
                }

                // Check if existing ADB process is still running, kill if needed
                if (adbProcess != null && !adbProcess.HasExited)
                {
                    adbProcess.Kill();
                }

                // Begin thread to time upload and install
                var thread = new Thread(delegate()
                {
                    TimeDeploy();
                });
                thread.Start();
            }
        }
#endif
    }