Esempio n. 1
0
        void Merge(bool export = false)
        {
            main.cfg.merged = source + "|" + destination;

            int mode = comboBoxMode.SelectedIndex;

            Dictionary <string, string> sources = new Dictionary <string, string>();
            Dictionary <string, string> dests   = new Dictionary <string, string>();

            List <string> filesSource = Directory.EnumerateFiles(source, "*_l_" + main.editTranslation + "*.yml", SearchOption.AllDirectories).ToList <string>();

            if (export)
            {
                foreach (string file in filesSource)
                {
                    File.Copy(file, file.Replace("_l_" + main.originalTranslation, "_l_" + main.editTranslation).Replace(source, destination), true);
                }
            }

            List <string> filesDest = Directory.EnumerateFiles(destination, "*_l_" + main.editTranslation + "*.yml", SearchOption.AllDirectories).ToList <string>();

            progressBarMerging.Visible = true;
            progressBarMerging.Maximum = filesSource.Count;

            foreach (string file in filesSource)
            {
                string fn = Path.GetFileName(file);
                if (!sources.ContainsKey(fn))
                {
                    sources.Add(fn, file);
                }
            }

            foreach (string file in filesDest)
            {
                string fn = Path.GetFileName(file);
                if (!dests.ContainsKey(fn))
                {
                    dests.Add(fn, file);
                }
            }

            int filesProcessed = 0;

            string proc = MainForm.appLocalisationStrings["formMerge_processing"];

            foreach (KeyValuePair <string, string> s in sources)
            {
                labelInfo.Text = proc + " " + s.Key + "...";
                labelInfo.Refresh();

                if (dests.ContainsKey(s.Key))
                {
                    Dictionary <string, string> destFields = new Dictionary <string, string>();

                    string[] sourceText = File.ReadAllLines(s.Value);
                    string[] destText   = File.ReadAllLines(dests[s.Key]);

                    int i = 0;

                    string c = "l_" + main.editTranslation + ":";

                    char[] separator = new char[] { ':' };

                    foreach (string dt in destText)
                    {
                        if (dt.Contains(c) || !(dt.Contains("#") || dt.Contains(":")))
                        {
                            continue;
                        }
                        i++;

                        string[] split = dt.Split(separator, 2);

                        if (split.Length > 1)
                        {
                            string key = split[0].TrimStart(' ');
                            if (!destFields.ContainsKey(key))
                            {
                                destFields.Add(key, split[1]);
                            }
                        }
                        else
                        {
                            string sp = split[0].TrimStart(' ');
                            if (!destFields.ContainsKey(sp))
                            {
                                destFields.Add(sp, "");
                            }
                        }
                    }

                    i = 0;
                    foreach (string st in sourceText)
                    {
                        if (st.Contains(c) || !(st.Contains("#") || st.Contains(":")))
                        {
                            continue;
                        }
                        i++;

                        string[] split = st.Split(separator, 2);

                        if (split.Length > 1)
                        {
                            string field = split[0].TrimStart(' ');
                            string text  = split[1];

                            string finaltext = text;

                            switch (mode)
                            {
                            case 1:
                                finaltext = TextUtils.FromChars(text, 2);
                                break;

                            case 2:
                                finaltext = TextUtils.ToChars(text, 2);
                                break;
                            }

                            if (destFields.ContainsKey(field))
                            {
                                destFields[field] = finaltext;
                            }
                            else
                            {
                                destFields.Add(field, finaltext);
                            }
                        }
                        else
                        {
                            string sp = split[0].TrimStart(' ');
                            if (!destFields.ContainsKey(sp))
                            {
                                destFields.Add(sp, "");
                            }
                        }
                    }

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(c);

                    foreach (KeyValuePair <string, string> merged in destFields)
                    {
                        if (merged.Key.StartsWith("#"))
                        {
                            sb.AppendLine(merged.Key);
                        }
                        else
                        {
                            sb.AppendLine(" " + merged.Key + ":" + merged.Value);
                        }
                    }

                    File.WriteAllText(dests[s.Key], sb.ToString(), Encoding.UTF8);
                }

                progressBarMerging.Value = ++filesProcessed;
                progressBarMerging.Refresh();
            }

            labelInfo.Text = MainForm.appLocalisationStrings["formMerge_finish"] + " " + filesProcessed;
        }
Esempio n. 2
0
        void FindInFile(string path)
        {
            int    mode = comboBoxFind.SelectedIndex;//0 - везде, 1 - в тексте, 2 - в именах полей
            string raw  = File.ReadAllText(path);

            char[] trim = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ' };

            int geks = 0;

            if (path.Contains("_" + main.editTranslation))
            {
                geks = main.geks;
            }

            char[] separator = new char[] { ':' };

            using (StringReader sr = new StringReader(raw))
            {
                string line;
                bool   first = true;
                while ((line = sr.ReadLine()) != null)
                {
                    if (first)
                    {
                        first = false;
                        continue;
                    }

                    string[] split = line.Split(separator, 2);

                    if (split.Length < 2)
                    {
                        continue;
                    }

                    line = TextUtils.FromChars(line, geks); // ехал костыль через костыль

                    string source;

                    switch (mode)
                    {
                    case 1:
                        source = line.Remove(0, split[0].Length);
                        break;

                    case 2:
                        source = split[0];
                        break;

                    default:
                        source = line;
                        break;
                    }

                    string q = TextUtils.FromChars(TextUtils.ToChars(textBoxFind.Text));

                    if (Regex.IsMatch(source, q, (checkBoxCase.Checked) ? RegexOptions.None : RegexOptions.IgnoreCase) ||
                        Regex.IsMatch(source, textBoxFind.Text, (checkBoxCase.Checked) ? RegexOptions.None : RegexOptions.IgnoreCase))
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text        = Path.GetFileName(path);
                        item.ToolTipText = path;

                        string field = split[0].TrimStart(' ');

                        item.SubItems.Add(field);
                        item.SubItems.Add(TextUtils.FromChars(split[1].Trim(trim), geks));

                        results.Add(new Result(item.Text, field, item));

                        listViewResults.Items.Add(item);

                        statusLabel.Text = MainForm.appLocalisationStrings["formFind_results"] + ": " + listViewResults.Items.Count;
                    }
                }
            }
        }