internal void Compare(string file1, string file2, string originalToolPath)
        {
            if (string.IsNullOrEmpty(originalToolPath) || !File.Exists(originalToolPath))
            {
                string path = Environment.GetEnvironmentVariable("VS140COMNTOOLS");
                originalToolPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(path)), "ide\\vsDiffMerge.exe");
                if (!File.Exists(originalToolPath))
                {
                    return;
                }
            }
            string tmpFile1 = Path.Combine(Path.GetTempPath(), "first_" + Path.GetFileName(file1));
            string tmpFile2 = Path.Combine(Path.GetTempPath(), "second_" + Path.GetFileName(file2));

            using (StreamReader reader = new StreamReader(file1))
            {
                MyJObject root = MyJObject.Parse(new JsonTextReader(reader));
                using (StreamWriter sw = new StreamWriter(tmpFile1, false, Encoding.UTF8))
                {
                    root = SortProperties(root);
                    JsonTextWriter jtw = new JsonTextWriter(sw);
                    jtw.Formatting = Formatting.Indented;
                    root.ToString(jtw);
                }
            }
            using (StreamReader reader = new StreamReader(file2))
            {
                MyJObject root = MyJObject.Parse(new JsonTextReader(reader));
                using (StreamWriter sw = new StreamWriter(tmpFile2, false, Encoding.UTF8))
                {
                    root = SortProperties(root);
                    JsonTextWriter jtw = new JsonTextWriter(sw);
                    jtw.Formatting = Formatting.Indented;
                    root.ToString(jtw);
                }
            }

            DateTime dt1Start = new FileInfo(tmpFile1).LastWriteTime;
            DateTime dt2Start = new FileInfo(tmpFile2).LastWriteTime;

            Process p = Process.Start(originalToolPath, string.Concat(tmpFile1, " ", tmpFile2));

            p.WaitForExit();

            DateTime dt1End = new FileInfo(tmpFile1).LastWriteTime;
            DateTime dt2End = new FileInfo(tmpFile2).LastWriteTime;

            if (dt1End > dt1Start)            //il file è stato modificato: riporto le modifiche su quello originario
            {
                File.Copy(tmpFile1, file1, true);
            }
            if (dt2End > dt2Start)            //il file è stato modificato: riporto le modifiche su quello originario
            {
                File.Copy(tmpFile2, file2, true);
            }

            File.Delete(tmpFile1);
            File.Delete(tmpFile2);
        }
        internal static MyJArray Parse(JsonReader reader)
        {
            MyJArray obj = new MyJArray();

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonToken.StartObject:
                    obj.Add(MyJObject.Parse(reader));
                    break;

                case JsonToken.EndArray:
                    return(obj);
                }
            }
            return(obj);
        }
Exemple #3
0
        internal static MyJObject Parse(JsonReader reader)
        {
            if (reader.TokenType == JsonToken.None)
            {
                reader.Read();                //all'inizio mi posiziono sullo start object
            }
            MyJObject   obj         = new MyJObject();
            MyJProperty currentProp = null;

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonToken.PropertyName:
                    currentProp      = new MyJProperty();
                    currentProp.Name = reader.Value.ToString();
                    obj.Add(currentProp);
                    break;

                case JsonToken.StartObject:
                    if (currentProp != null)
                    {
                        currentProp.Value = MyJObject.Parse(reader);
                    }
                    break;

                case JsonToken.EndObject:
                    return(obj);

                case JsonToken.StartArray:
                    currentProp.Value = MyJArray.Parse(reader);
                    break;

                case JsonToken.Comment:
                    currentProp.Comment = reader.Value.ToString();
                    break;

                default:
                    currentProp.Value = reader.Value;
                    break;
                }
            }
            return(obj);
        }
Exemple #4
0
        private void CompactFile(string file, bool allControls)
        {
            Console.WriteLine("Processing " + file);
            bool             updated = false;
            List <MyJObject> removed = new List <MyJObject>();
            MyJObject        root    = null;

            using (StreamReader reader = new StreamReader(file))
            {
                root = MyJObject.Parse(new JsonTextReader(reader));
                bool isTile = root.GetWndObjType() == WndObjType.Tile;
                if (isTile)
                {
                    if (root["x"] != null)
                    {
                        root["x"] = null;
                        updated   = true;
                    }
                    if (root["y"] != null)
                    {
                        root["y"] = null;
                        updated   = true;
                    }
                }
                MyJArray ar = root["items"] as MyJArray;
                if (ar == null)
                {
                    return;
                }
                bool radioException = false;
                if (!Anchored(ar))
                {
                    ar.Sort(new MyJObjectComparer());
                }
                foreach (MyJObject child in ar)
                {
                    //controllo ancorato, sono già passato di qui
                    if (!string.IsNullOrEmpty(child["anchor"] as string))
                    {
                        continue;
                    }
                    string id           = child["id"] as string;
                    string controlClass = child["controlClass"] as string;
                    int    type         = Convert.ToInt32(child.GetWndObjType());
                    if (2 == type && id.Contains("STATIC") && string.IsNullOrEmpty(controlClass))
                    {
                        string text = child["text"] as string;
                        if (string.IsNullOrEmpty(text))
                        {
                            continue;
                        }

                        if (radioException && text.Equals("To", StringComparison.InvariantCultureIgnoreCase))
                        {
                            radioException = false;
                            continue;
                        }

                        int       x   = Convert.ToInt32(child["x"]);
                        int       y   = Convert.ToInt32(child["y"]);
                        MyJObject mjo = Find(child, ar, x, y, allControls);
                        if (mjo != null)
                        {
                            mjo["controlCaption"] = child["text"];
                            mjo["captionFont"]    = child["font"];
                            updated = true;
                            removed.Add(child);
                        }
                    }
                    else if (9 == type)
                    {
                        if (!string.IsNullOrEmpty(id) && id.StartsWith("IDC_STATIC_AREA"))
                        {
                            updated = true;
                            root["hasStaticArea"] = true;
                            removed.Add(child);
                        }
                    }
                    else if (10 == type)
                    {
                        string text = child["text"] as string;
                        if (!string.IsNullOrEmpty(text) && text.Equals("From", StringComparison.InvariantCultureIgnoreCase))
                        {
                            radioException = true;
                        }
                    }
                }
                foreach (var child in removed)
                {
                    ar.Remove(child);
                }

                updated = Anchor(ar) || updated;
            }
            if (updated)
            {
                using (StreamWriter sw = new StreamWriter(file, false, Encoding.UTF8))
                {
                    JsonTextWriter jtw = new JsonTextWriter(sw);
                    jtw.Formatting = Formatting.Indented;
                    root.ToString(jtw);
                    Console.WriteLine("Updated " + file);
                }

                string hjson = Path.ChangeExtension(file, ".hjson");
                if (File.Exists(hjson))
                {
                    string[] lines = File.ReadAllLines(hjson);
                    using (StreamWriter sw = new StreamWriter(hjson, false, Encoding.UTF8))
                    {
                        foreach (string line in lines)
                        {
                            bool found = false;
                            foreach (var child in removed)
                            {
                                string id = child["id"] as string;
                                if (!string.IsNullOrEmpty(id) && Regex.IsMatch(line, "\\s" + id + "\\s"))
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                sw.WriteLine(line);
                            }
                        }

                        Console.WriteLine("Updated " + hjson);
                    }
                }
            }
        }