示例#1
0
    /// <summary>
    /// Downloads the plugin file for a given network.
    /// </summary>
    /// <param name="network">Network for which to download the current version.</param>
    /// <returns></returns>
    public IEnumerator DownloadPlugin(Network network)
    {
        var path = Path.Combine(Application.temporaryCachePath, network.PluginFileName); // TODO: Maybe delete plugin file after finishing import.

#if UNITY_2017_2_OR_NEWER
        var downloadHandler = new DownloadHandlerFile(path);
#else
        var downloadHandler = new AppLovinDownloadHandler(path);
#endif
        webRequest = new UnityWebRequest(network.DownloadUrl)
        {
            method          = UnityWebRequest.kHttpVerbGET,
            downloadHandler = downloadHandler
        };

#if UNITY_2017_2_OR_NEWER
        var operation = webRequest.SendWebRequest();
#else
        var operation = webRequest.Send();
#endif

        while (!operation.isDone)
        {
            yield return(new WaitForSeconds(0.1f)); // Just wait till webRequest is completed. Our coroutine is pretty rudimentary.

            CallDownloadPluginProgressCallback(network.DisplayName, operation.progress, operation.isDone);
        }


#if UNITY_2017_2_OR_NEWER
        if (webRequest.isNetworkError || webRequest.isHttpError)
#else
        if (webRequest.isError)
#endif
        {
            Debug.LogError(webRequest.error);
        }
        else
        {
            importingNetwork = network;
            AssetDatabase.ImportPackage(path, true);
        }

        webRequest = null;
    }
    [PostProcessBuild(int.MaxValue)] // We want to run Quality Service script last.
    public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath)
    {
        if (!AppLovinSettings.Instance.QualityServiceEnabled)
        {
            return;
        }

        var sdkKey = AppLovinSettings.Instance.SdkKey;

        if (string.IsNullOrEmpty(sdkKey))
        {
            MaxSdkLogger.UserError("Failed to install AppLovin Quality Service plugin. SDK Key is empty. Please enter the AppLovin SDK Key in the Integration Manager.");
            return;
        }

        var outputFilePath = Path.Combine(buildPath, OutputFileName);

        // Check if Quality Service is already installed.
        if (File.Exists(outputFilePath) && Directory.Exists(Path.Combine(buildPath, "AppLovinQualityService")))
        {
            // TODO: Check if there is a way to validate if the SDK key matches the script. Else the pub can't use append when/if they change the SDK Key.
            return;
        }

        // Download the ruby script needed to install Quality Service
#if UNITY_2017_2_OR_NEWER
        var downloadHandler = new DownloadHandlerFile(outputFilePath);
#else
        var downloadHandler = new AppLovinDownloadHandler(path);
#endif
        var postJson      = string.Format("{{\"sdk_key\" : \"{0}\"}}", sdkKey);
        var bodyRaw       = Encoding.UTF8.GetBytes(postJson);
        var uploadHandler = new UploadHandlerRaw(bodyRaw);
        uploadHandler.contentType = "application/json";

        var unityWebRequest = new UnityWebRequest("https://api2.safedk.com/v1/build/ios_setup2")
        {
            method          = UnityWebRequest.kHttpVerbPOST,
            downloadHandler = downloadHandler,
            uploadHandler   = uploadHandler
        };

#if UNITY_2017_2_OR_NEWER
        var operation = unityWebRequest.SendWebRequest();
#else
        var operation = webRequest.Send();
#endif

        // Wait for the download to complete or the request to timeout.
        while (!operation.isDone)
        {
        }

#if UNITY_2017_2_OR_NEWER
        if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
#else
        if (webRequest.isError)
#endif
        {
            MaxSdkLogger.UserError("AppLovin Quality Service installation failed. Failed to download script with error: " + unityWebRequest.error);
            return;
        }

        // Check if Ruby is installed
        var rubyVersion = AppLovinCommandLine.Run("ruby", "--version", buildPath);
        if (rubyVersion.ExitCode != 0)
        {
            MaxSdkLogger.UserError("AppLovin Quality Service installation requires Ruby. Please install Ruby, export it to your system PATH and re-export the project.");
            return;
        }

        // Ruby is installed, run `ruby AppLovinQualityServiceSetup.rb`
        var result = AppLovinCommandLine.Run("ruby", OutputFileName, buildPath);

        // Check if we have an error.
        if (result.ExitCode != 0)
        {
            MaxSdkLogger.UserError("Failed to set up AppLovin Quality Service");
        }

        MaxSdkLogger.UserDebug(result.Message);
    }