Exemple #1
0
        // initial version copied from https://github.com/yonstorm/ProjectRider-Unity/blob/develop/Assets/Plugins/Editor/ProjectRider/ProjectValidator.cs
        private static bool UpdateDebugSettings()
        {
            var workspaceFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), ".idea"), ".idea." + Path.GetFileNameWithoutExtension(Rider.SlnFile)), ".idea"), "workspace.xml");

            if (!File.Exists(workspaceFile))
            {
                // TODO: write workspace settings from a template to be able to write debug settings before Rider is started for the first time.
                Rider.Log(workspaceFile + " doesn't exist.");
                return(true);
            }

            var document          = XDocument.Load(workspaceFile);
            var runManagerElement = (from elem in document.Descendants()
                                     where elem.Attribute("name") != null && elem.Attribute("name").Value.Equals("RunManager")
                                     select elem).FirstOrDefault();

            if (runManagerElement == null)
            {
                var projectElement = document.Element("project");
                if (projectElement == null)
                {
                    return(false);
                }

                runManagerElement = new XElement("component", new XAttribute("name", "RunManager"));
                projectElement.Add(runManagerElement);
            }

            var editorConfigElem = (from elem in runManagerElement.Descendants()
                                    where elem.Attribute("name") != null && elem.Attribute("name").Value.Equals("UnityEditor-generated")
                                    select elem).FirstOrDefault();

            var currentDebugPort = GetDebugPort();

            if (editorConfigElem == null)
            {
                editorConfigElem = new XElement("configuration");
                var defaultAttr     = new XAttribute("default", false);
                var nameAttr        = new XAttribute("name", "UnityEditor-generated");
                var typeAttr        = new XAttribute("type", "ConnectRemote");
                var factoryNameAttr = new XAttribute("factoryName", "Mono remote");
                var showStdErrAttr  = new XAttribute("show_console_on_std_err", false);
                var showStdOutAttr  = new XAttribute("show_console_on_std_out", true);
                var portAttr        = new XAttribute("b", currentDebugPort);
                var addressAttr     = new XAttribute("a", "127.0.0.1");

                editorConfigElem.Add(defaultAttr, nameAttr, typeAttr, factoryNameAttr, showStdErrAttr, showStdOutAttr,
                                     portAttr, addressAttr);

                var optionAdress = new XElement("option");
                optionAdress.Add(new XAttribute("address", "127.0.0.1"));
                var optionPort = new XElement("option");
                optionPort.Add(new XAttribute("port", currentDebugPort.ToString()));

                editorConfigElem.Add(optionAdress, optionPort);

                runManagerElement.SetAttributeValue("selected", "Mono remote.UnityEditor-generated");
                runManagerElement.Add(editorConfigElem);
            }
            else
            {
                editorConfigElem.Attribute("b").Value = currentDebugPort.ToString();
                var el = editorConfigElem.Descendants("option").Single(a => a.Attribute("name").Value == "port");
                el.Attribute("value").SetValue(currentDebugPort.ToString());
            }

            document.Save(workspaceFile);

            // Rider doesn't like it small... :/
            var lines = File.ReadAllLines(workspaceFile);

            lines[0] = lines[0].Replace("utf-8", "UTF-8");
            File.WriteAllLines(workspaceFile, lines);

            return(true);
        }