private void AnchorFriend(MyJObject controlToAnchor, Point pt, List <Point> points, MyJArray ar) { foreach (MyJObject child in ar) { string id = child["id"] as string; if (child != controlToAnchor && !string.IsNullOrEmpty(id) && string.Compare(child["activation"] as string, controlToAnchor["activation"] as string) == 0 && string.IsNullOrEmpty(child["anchor"] as string)) { Int32 xx = Convert.ToInt32(child["x"]); Int32 yy = Convert.ToInt32(child["y"]); if (Math.Abs(yy - pt.Y) <= 2)//(quasi la) stessa coordinata { //devo controllare che non sia associato ad un altro controllo ancorato (tile wide) foreach (Point otherPt in points) { if (!otherPt.Equals(pt) && xx > otherPt.X) //non sono il punto associato al controllo da ancorare, e mi trovo più a destra { continue; } } if (xx >= 327 && pt.X < 327) { continue;//si trovano in aree della tile diverse } child["x"] = null; child["y"] = null; child["anchor"] = controlToAnchor["id"]; } } } }
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); }
private static MyJObject SortProperties(MyJObject original) { var newObject = new MyJObject(); foreach (var prop in original.OrderBy(p => p.Name)) { if (prop.Value is MyJObject) { prop.Value = SortProperties((MyJObject)prop.Value); } else if (prop.Value is MyJArray) { prop.Value = SortArray((MyJArray)prop.Value); } newObject.Add(prop); } return(newObject); }
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); }
private MyJObject Find(MyJObject labelToRemove, MyJArray ar, int x, int y, bool allControls) { List <MyJObject> candidates = new List <MyJObject>(); foreach (MyJObject child in ar) { string id = child["id"] as string; string caption = child["controlCaption"] as string; if (child != labelToRemove && !string.IsNullOrEmpty(id) && !id.StartsWith("IDC_STATIC") && string.IsNullOrEmpty(caption) && string.Compare(child["activation"] as string, labelToRemove["activation"] as string) == 0 && (allControls || !string.IsNullOrEmpty(child["controlClass"] as string))) { Int32 yy = Convert.ToInt32(child["y"]); if (Math.Abs(yy - y) < 5) { candidates.Add(child); } } } int min = int.MaxValue; MyJObject found = null; foreach (MyJObject child in candidates) { int xx = Convert.ToInt32(child["x"]); if (xx < x) //il controllo è a sinistra della label, non può essere la sua { continue; } if ((xx - x) < min) { min = xx - x; found = child; } } return(found); }
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); } } } }