ErrorCode CheckFidelityMessage(SetupConfig config)
        {
            if (config.useAdvancedFidelityParameters)
            {
                return(ErrorCode.Ok);
            }

            if (!MessageUtil.HasQualityLevel <TFidelity>())
            {
                Debug.LogError("Android Performance Tuner is using default fidelity, " +
                               "but Fidelity message doesn't contain level parameter.");
                return(ErrorCode.InvalidFidelity);
            }

            return(ErrorCode.Ok);
        }
        ErrorCode CheckAnnotationMessage(SetupConfig config)
        {
            if (config.useAdvancedAnnotations)
            {
                return(ErrorCode.Ok);
            }
            if (!MessageUtil.HasLoadingState <TAnnotation>())
            {
                Debug.LogError("Android Performance Tuner is using default annotation, " +
                               "but Annotation message doesn't contain loading state parameter.");
                return(ErrorCode.InvalidAnnotation);
            }

            if (!MessageUtil.HasScene <TAnnotation>())
            {
                Debug.LogError("Android Performance Tuner is using default annotation, " +
                               "but Annotation message doesn't contain scene parameter.");
                return(ErrorCode.InvalidAnnotation);
            }

            return(ErrorCode.Ok);
        }
        ErrorCode StartInternal()
        {
            m_SetupConfig = Resources.Load("SetupConfig") as SetupConfig;

            if (m_SetupConfig == null)
            {
                Debug.LogWarning(
                    "SetupConfig can not be loaded, open Google->Android Performance Tuner to setup the plugin.");
                return(ErrorCode.NoSettings);
            }

            if (!m_SetupConfig.pluginEnabled)
            {
                Debug.LogWarning(
                    "Android Performance Tuner plugin is not enabled, open Google->Android Performance Tuner to enable the plugin.");
                return(ErrorCode.TuningforkNotInitialized);
            }

            var annotationStatus = CheckAnnotationMessage(m_SetupConfig);
            var fidelityStatus   = CheckFidelityMessage(m_SetupConfig);

            if (annotationStatus != ErrorCode.Ok)
            {
                return(annotationStatus);
            }
            if (fidelityStatus != ErrorCode.Ok)
            {
                return(fidelityStatus);
            }


            IMessage defaultQualityParameters = null;

            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                defaultQualityParameters = new TFidelity();
                MessageUtil.SetQualityLevel(defaultQualityParameters, QualitySettings.GetQualityLevel());
            }

            var errorCode = m_AdditionalLibraryMethods.Init(Callbacks.FidelityParamsCallbackImpl,
                                                            defaultQualityParameters, m_endPoint);

            if (errorCode != ErrorCode.Ok)
            {
                m_AdditionalLibraryMethods.FreePointers();
                return(errorCode);
            }

            m_OnStop += () => m_AdditionalLibraryMethods.FreePointers();

            CreateSceneObject();
            m_SceneObject.StartCoroutine(CallbacksCheck());

            if (!SwappyIsEnabled())
            {
                EnableUnityFrameTicks();
            }
            if (!m_SetupConfig.useAdvancedAnnotations)
            {
                EnableDefaultAnnotationsMode();
            }
            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                EnableDefaultFidelityMode();
            }

            AddUploadCallback();
            AddAutoFlush();

            return(errorCode);
        }
        ErrorCode StartInternal()
        {
            m_SetupConfig = Resources.Load("SetupConfig") as SetupConfig;

            if (m_SetupConfig == null)
            {
                Debug.LogWarning(
                    "SetupConfig can not be loaded, open Google->Android Performance Tuner to setup the plugin.");
                return(ErrorCode.NoSettings);
            }

            if (!m_SetupConfig.pluginEnabled)
            {
                Debug.LogWarning(
                    "Android Performance Tuner plugin is not enabled, open Google->Android Performance Tuner to enable the plugin.");
                return(ErrorCode.TuningforkNotInitialized);
            }

            var annotationStatus = CheckAnnotationMessage(m_SetupConfig);
            var fidelityStatus   = CheckFidelityMessage(m_SetupConfig);

            if (annotationStatus != ErrorCode.Ok)
            {
                return(annotationStatus);
            }
            if (fidelityStatus != ErrorCode.Ok)
            {
                return(fidelityStatus);
            }


            InitializationSettings settings = new InitializationSettings();

            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                settings.trainingFidelityParams = new TFidelity();
                MessageUtil.SetQualityLevel(settings.trainingFidelityParams, QualitySettings.GetQualityLevel());
            }

            settings.endpointUriOverride = m_endPoint;
            if (m_SetupConfig.mode == TunerMode.Experiments)
            {
                settings.fidelityParamsCallback = Callbacks.FidelityParamsCallbackImpl;
            }
            settings.maxNumMetrics = m_MetricLimits;


            var errorCode = m_AdditionalLibraryMethods.InitWithSettings(settings);

            if (errorCode != ErrorCode.Ok)
            {
                m_AdditionalLibraryMethods.FreePointers();
                return(errorCode);
            }

            m_OnStop += () => m_AdditionalLibraryMethods.FreePointers();

            CreateSceneObject();
            m_SceneObject.StartCoroutine(CallbacksCheck());

            if (!SwappyIsEnabled())
            {
                EnableUnityFrameTicks();
            }
            if (!m_SetupConfig.useAdvancedAnnotations)
            {
                EnableDefaultAnnotationsMode();
            }
            if (!m_SetupConfig.useAdvancedFidelityParameters)
            {
                EnableDefaultFidelityMode();
            }

            AddAutoFlush();
            AddAutoLifecycleUpdate();
            CheckNetworkReachability();

            return(errorCode);
        }