public static void onPostProcessBuild(BuildTarget buildTarget, string targetPath)
        {
            ScaleMonkXml scaleMonkXml = AdsProvidersHelper.ReadAdnetsConfigs();

            if (string.IsNullOrEmpty(scaleMonkXml.ios))
            {
                return;
            }

            configureSwiftBuild(targetPath);
            configureAdnetsAndAppId(buildTarget, targetPath, scaleMonkXml);
            configureSKAdNetworks(buildTarget, targetPath);
        }
        private static void configureAdnetsAndAppId(BuildTarget buildTarget, string targetPath,
                                                    ScaleMonkXml scaleMonkXml)
        {
            if (buildTarget == BuildTarget.iOS)
            {
                var plistPath = Path.Combine(targetPath, "Info.plist");
                var infoPlist = new PlistDocument();
                infoPlist.ReadFromFile(plistPath);

                infoPlist.root.SetString("ScaleMonkApplicationId", scaleMonkXml.ios);

                var adnetsConfigs = scaleMonkXml.adnets;
                foreach (var adnet in adnetsConfigs)
                {
                    if (adnet.configs == null)
                    {
                        continue;
                    }

                    foreach (var config in adnet.configs)
                    {
                        if (config.platform != "ios")
                        {
                            continue;
                        }

                        if (!string.IsNullOrEmpty(config.value))
                        {
                            infoPlist.root.SetString(config.config, config.value.Trim());
                        }
                    }

                    if (adnet.id == "Admob")
                    {
                        disableAdmobSwizzling(infoPlist);
                    }
                }

                infoPlist.WriteToFile(plistPath);
            }
        }
        static ScaleMonkXml ReadScaleMonkFromPath(ScaleMonkXml scaleMonkXmlBase, string path, bool local = false)
        {
            XmlElement  xmlScalemonkNode  = null;
            XmlNodeList xmlAdnetsNodeList = null;

            if (local)
            {
                var localDoc = new XmlDocument();
                localDoc.Load(GetAdnetsXmlPath());
                xmlScalemonkNode = localDoc.DocumentElement;
                var adnetsRoot = xmlScalemonkNode.SelectSingleNode("adnets");
                xmlAdnetsNodeList = xmlScalemonkNode != null?adnetsRoot.SelectNodes("adnet") : null;
            }

            var adnetsDict = new Dictionary <string, AdnetXml>();

            foreach (var adnetBase in scaleMonkXmlBase.adnets)
            {
                adnetsDict[adnetBase.id] = adnetBase;
            }

            var doc = new XmlDocument();

            doc.Load(path);

            var root = doc.DocumentElement;

            if (root == null)
            {
                return(new ScaleMonkXml());
            }

            // read scalemonk attributes to retrieve application ids
            string iOSApplicationID     = xmlScalemonkNode != null ? (xmlScalemonkNode.Attributes["ios"].Value ?? "") : "";
            string androidApplicationID =
                xmlScalemonkNode != null ? (xmlScalemonkNode.Attributes["android"].Value ?? "") : "";

            // read adnets childs
            var nodes = root.SelectSingleNode("adnets").SelectNodes("adnet");

            foreach (XmlNode node in nodes)
            {
                var id   = node.Attributes["id"].Value;
                var name = node.Attributes["name"].Value;
                var ios  = bool.Parse(node.Attributes["ios"] != null
                    ? node.Attributes["ios"].Value ?? "false"
                    : "false");
                var android = bool.Parse(node.Attributes["android"] != null
                    ? node.Attributes["android"].Value ?? "false"
                    : "false");

                var configs      = node.SelectNodes("adnetConfig");
                var adnetConfigs = new List <AdnetConfigXml>();
                foreach (XmlNode config in configs)
                {
                    var configConfig   = config.Attributes["config"].Value;
                    var configPlatform = config.Attributes["platform"].Value;
                    var configName     = config.Attributes["name"].Value;
                    var configValue    = config.Attributes["value"] != null
                        ? config.Attributes["value"].Value ?? string.Empty
                        : string.Empty;
                    adnetConfigs.Add(new AdnetConfigXml(configConfig, configPlatform, configName, configValue));
                }

                AdnetXml currentAdnet = new AdnetXml(id, name, ios, android);
                var      localNode    = xmlAdnetsNodeList != null
                    ? xmlAdnetsNodeList.Cast <XmlNode>().FirstOrDefault(n =>
                                                                        (n.Attributes["id"] != null ? n.Attributes["id"].Value : null) == id)
                    : null;

                if (localNode != null)
                {
                    currentAdnet.android = bool.Parse(localNode.Attributes["android"] != null
                        ? localNode.Attributes["android"].Value ?? "false"
                        : "false");
                    currentAdnet.ios = bool.Parse(localNode.Attributes["ios"] != null
                        ? localNode.Attributes["ios"].Value ?? "false"
                        : "false");
                    currentAdnet.iosVersion = localNode.Attributes["iosVersion"] != null
                        ? localNode.Attributes["iosVersion"].Value
                        : string.Empty;
                    currentAdnet.androidVersion = localNode.Attributes["androidVersion"] != null
                        ? localNode.Attributes["androidVersion"].Value
                        : string.Empty;
                }

                var newConfigs = new List <AdnetConfigXml>();
                if (localNode != null)
                {
                    var savedConfigs = localNode.SelectNodes("adnetConfig");
                    foreach (var newConfig in adnetConfigs)
                    {
                        var curConfig = savedConfigs.Cast <XmlNode>().FirstOrDefault(c =>
                                                                                     (c.Attributes["config"] != null ? c.Attributes["config"].Value : null) ==
                                                                                     newConfig.config &&
                                                                                     (c.Attributes["platform"] != null ? c.Attributes["platform"].Value : null) ==
                                                                                     newConfig.platform);
                        if (curConfig != null)
                        {
                            newConfig.value = curConfig.Attributes["value"].Value;
                        }

                        newConfigs.Add(newConfig);
                    }
                }
                else
                {
                    foreach (var newConfig in adnetConfigs)
                    {
                        newConfigs.Add(newConfig);
                    }
                }

                currentAdnet.configs = newConfigs;
                adnetsDict[id]       = currentAdnet;
            }

            ScaleMonkXml scaleMonkXml = new ScaleMonkXml();

            scaleMonkXml.adnets  = adnetsDict.Values.ToList();
            scaleMonkXml.ios     = iOSApplicationID;
            scaleMonkXml.android = androidApplicationID;
            return(scaleMonkXml);
        }
        static void UpdateNativeDependencies(ScaleMonkXml scaleMonkXml, bool saveOnFocus)
        {
            List <AdnetXml> adnets         = scaleMonkXml.adnets;
            var             doc            = new XmlDocument();
            var             xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            var             root           = doc.DocumentElement;

            doc.InsertBefore(xmlDeclaration, root);
            var dependenciesElement = doc.CreateElement("dependencies");

            bool androidSaved = false, iosSaved = false;

            if (!string.IsNullOrEmpty(scaleMonkXml.android))
            {
                UpdateAndroidDependencies(adnets, doc, dependenciesElement);
                androidSaved = true;
            }

            if (!string.IsNullOrEmpty(scaleMonkXml.ios))
            {
                UpdateIOSDependencies(adnets, doc, dependenciesElement);
                iosSaved = true;
            }

            if (!androidSaved && !iosSaved)
            {
                if (!saveOnFocus)
                {
                    Debug.LogWarning("ScaleMonk Application ID is empty for both platforms, configuration not saved");
                }
                return;
            }

            doc.AppendChild(dependenciesElement);
            if (!saveOnFocus)
            {
                var path = GetDependenciesPath();

                // Make file available to write
                if (File.Exists(path))
                {
                    var pathFileAttributes = File.GetAttributes(path);
                    File.SetAttributes(path, FileAttributes.Normal);
                    doc.Save(path);
                    File.SetAttributes(path, pathFileAttributes);
                }
                else
                {
                    // save file
                    doc.Save(path);
                }


                if (!iosSaved)
                {
                    Debug.Log("ScaleMonk configuration saved only for Android, iOS Application ID is empty");
                }
                else if (!androidSaved)
                {
                    Debug.Log("ScaleMonk configuration saved only for iOS, Android Application ID is empty");
                }
                else
                {
                    Debug.Log("ScaleMonk configuration saved");
                }
            }
        }
        public static void SaveConfig(ScaleMonkXml scaleMonkXml, bool saveOnFocus = false)
        {
            if (scaleMonkXml.adnets == null)
            {
                return;
            }

            var doc            = new XmlDocument();
            var xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            var root           = doc.DocumentElement;

            doc.InsertBefore(xmlDeclaration, root);

            var scaleMonkElement = doc.CreateElement("scalemonk");

            scaleMonkElement.SetAttribute("ios", scaleMonkXml.ios);
            scaleMonkElement.SetAttribute("android", scaleMonkXml.android);
            var adnetsElement = doc.CreateElement("adnets");

            foreach (var adnet in scaleMonkXml.adnets)
            {
                var adnetElement = doc.CreateElement("adnet");
                adnetElement.SetAttribute("id", adnet.id);
                adnetElement.SetAttribute("name", adnet.name);
                adnetElement.SetAttribute("ios", adnet.ios.ToString());
                adnetElement.SetAttribute("iosVersion", adnet.iosVersion);
                adnetElement.SetAttribute("androidVersion", adnet.androidVersion);
                adnetElement.SetAttribute("android", adnet.android.ToString());

                if (adnet.configs != null && adnet.configs.Count > 0)
                {
                    foreach (var config in adnet.configs)
                    {
                        var configElement = doc.CreateElement("adnetConfig");
                        if (!saveOnFocus)
                        {
                            if (
                                ((adnet.ios && config.platform == "ios") ||
                                 (adnet.android && config.platform == "android")) &&
                                string.IsNullOrEmpty(config.value))
                            {
                                Debug.LogErrorFormat("Adnet {0} missing config {1} ({2})", adnet.name, config.name,
                                                     config.config);
                                return;
                            }
                        }

                        configElement.SetAttribute("config", config.config);
                        configElement.SetAttribute("platform", config.platform);
                        configElement.SetAttribute("name", config.name);
                        configElement.SetAttribute("value", config.value);
                        adnetElement.AppendChild(configElement);
                    }
                }

                adnetsElement.AppendChild(adnetElement);
            }

            scaleMonkElement.AppendChild(adnetsElement);
            doc.AppendChild(scaleMonkElement);

            var path = GetAdnetsXmlPath();

            doc.Save(path);

            UpdateNativeDependencies(scaleMonkXml, saveOnFocus);
        }
Example #6
0
 void OnFocus()
 {
     scaleMonkConfig = AdsProvidersHelper.ReadAdnetsConfigs();
 }