Example #1
0
        private void ReadConfig(string configFile, string output, string flag)
        {
            string file = Path.Combine(@"C:\Users\ganquan.zheng\Desktop\HSL pak\data", configFile);

            Console.WriteLine("begin read : " + configFile);

            if (!File.Exists(file))
            {
                file = Path.Combine(@"C:\Users\ganquan.zheng\Desktop\HSL pak\data\obsolete", configFile);

                if (!File.Exists(file))
                {
                    Console.WriteLine("failed to read config: " + configFile);
                    return;
                }
            }

            string[] lines = File.ReadAllLines(file, encodeBig5);

            Dictionary <string, string> defineMap = new Dictionary <string, string>();

            Dictionary <string, int> properties = new Dictionary <string, int>();

            List <Dictionary <string, List <string> > > characters = new List <Dictionary <string, List <string> > >();

            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].StartsWith("#include", StringComparison.OrdinalIgnoreCase))
                {
                    string[] splits = Regex.Split(lines[i].Trim(), @"\s+");
                    Dictionary <string, string> tempDefineMap = ReadHeadFile(splits[1]);
                    if (tempDefineMap != null)
                    {
                        foreach (var item in tempDefineMap)
                        {
                            //defineMap.Add(item.Key, item.Value);
                        }
                    }
                }
                else if (lines[i].Contains(flag))
                {
                    i++;
                    Dictionary <string, List <string> > character          = new Dictionary <string, List <string> >();
                    Dictionary <string, int>            tempPropertyRecord = new Dictionary <string, int>();
                    while (i < lines.Length)
                    {
                        if (!string.IsNullOrEmpty(lines[i]) &&
                            !InvalidatedLine(lines[i]) &&
                            lines[i].Contains('='))
                        {
                            // split by whitespaces
                            string[] splits = Regex.Split(lines[i].Trim(), @"\s+");
                            // record the max occurrences of a property
                            if (!tempPropertyRecord.ContainsKey(splits[0]))
                            {
                                tempPropertyRecord[splits[0]] = 1;
                            }
                            else
                            {
                                tempPropertyRecord[splits[0]] = tempPropertyRecord[splits[0]] + 1;
                            }
                            if (!properties.ContainsKey(splits[0]))
                            {
                                properties[splits[0]] = 0;
                            }
                            if (properties[splits[0]] < tempPropertyRecord[splits[0]])
                            {
                                properties[splits[0]] = tempPropertyRecord[splits[0]];
                            }
                            if (!character.ContainsKey(splits[0]))
                            {
                                character[splits[0]] = new List <string>();
                            }
                            // parse a line
                            if (splits.Length > 2)
                            {
                                string record = string.Empty;
                                // property = (propertyValue)
                                string propertyValue = splits[2];

                                for (int j = 3; j < splits.Length; j++)
                                {
                                    if (InvalidatedLine(splits[j]))
                                    {
                                        break;
                                    }
                                    propertyValue += splits[j];
                                }

                                if (propertyValue.Contains(","))
                                {
                                    string[] propertyValueSplits = propertyValue.Split(',');
                                    for (int j = 0; j < propertyValueSplits.Length; j++)
                                    {
                                        string key = propertyValueSplits[j];
                                        if (defineMap.ContainsKey(key))
                                        {
                                            key = defineMap[key];
                                        }
                                        if (!string.IsNullOrEmpty(record))
                                        {
                                            record += ",";
                                        }
                                        record += key;
                                    }
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(record))
                                    {
                                        record += ",";
                                    }
                                    if (defineMap.ContainsKey(propertyValue))
                                    {
                                        record += defineMap[propertyValue];
                                    }
                                    else
                                    {
                                        record += propertyValue;
                                    }
                                }
                                if (!string.IsNullOrEmpty(record))
                                {
                                    character[splits[0]].Add(record);
                                }
                            }
                        }
                        i++;
                        if (i >= lines.Length ||
                            Regex.IsMatch(lines[i], "\\[\\S+\\]") // 使用 \S 的原因:一个文件里面的 flag 可能有多个
                            )
                        {
                            i--;
                            break;
                        }
                    }
                    characters.Add(character);
                }
            }

            string[] contents = new string[characters.Count + 3];
            contents[0] = "";
            int column = 0;

            foreach (var item in properties)
            {
                column += item.Value;
            }
            string[] content1array = new string[column];
            int      index         = 0;

            foreach (var item in properties)
            {
                if (item.Value > 1)
                {
                    for (int i = 0; i < item.Value; i++)
                    {
                        content1array[index] = item.Key + i;
                        index++;
                    }
                }
                else
                {
                    content1array[index] = item.Key;
                    index++;
                }
            }
            contents[1] = CSVFileUtil.ToCSVLine(content1array);
            contents[2] = "";

            for (int i = 0; i < characters.Count; i++)
            {
                string[] content_i_array = new string[column];
                int      tempIndex       = 0;
                foreach (var item in properties)
                {
                    if (characters[i].ContainsKey(item.Key))
                    {
                        List <string> content = characters[i][item.Key];

                        for (int j = 0; j < item.Value; j++)
                        {
                            if (j < content.Count)
                            {
                                content_i_array[tempIndex + j] = content[j];
                            }
                            else
                            {
                                content_i_array[tempIndex + j] = string.Empty;
                            }
                        }
                    }
                    tempIndex += item.Value;
                }
                contents[i + 3] = CSVFileUtil.ToCSVLine(content_i_array);
            }

            File.WriteAllLines(output, contents, Encoding.UTF8);

            Console.WriteLine("success read config: " + configFile);
        }
Example #2
0
        public void ReadLanguage()
        {
            string file = @"C:\Users\ganquan.zheng\Desktop\HSL pak\data\RESOURCE.TXT";

            string[] lines = File.ReadAllLines(file, encodeBig5);

            Dictionary <string, string> colorMap = new Dictionary <string, string>();

            colorMap.Add("@0", "<color=#0000FF>"); // blue
            colorMap.Add("@1", "<color=#FFFFFF>"); // white
            colorMap.Add("@2", "<color=#FF0000>"); // red
            colorMap.Add("@3", "<color=#008000>"); // green
            colorMap.Add("@4", "<color=#800080>"); // purple
            colorMap.Add("@5", "<color=#FFFF00>"); // yellow
            colorMap.Add("@6", "<color=#FFA500>"); // orange
            colorMap.Add("@7", "<color=#87CEEB>"); // sky
            colorMap.Add("@8", "<color=#A52A2A>"); // brown
            colorMap.Add("@9", "<color=#FFC0CB>"); // pink

            Regex regex = new Regex("(@0)|(@1)|(@2)|(@3)|(@4)|(@5)|(@6)|(@7)|(@8)|(@9)");

            Dictionary <int, string> map = new Dictionary <int, string>();

            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].StartsWith("item = "))
                {
                    string[] splits = lines[i].Split('=', ',');

                    int id;

                    if (int.TryParse(splits[1], out id) && id > 0)
                    {
                        StringBuilder sb = new StringBuilder(splits[2]);

                        Match match = regex.Match(sb.ToString());

                        bool first = true;

                        while (match.Success)
                        {
                            string result = match.Value;

                            if (colorMap.ContainsKey(result))
                            {
                                if (first)
                                {
                                    sb.Replace(result, colorMap[result], match.Index, match.Length);
                                }
                                else
                                {
                                    sb.Replace(result, "</color>" + colorMap[result], match.Index, match.Length);
                                }
                                first = false;
                            }
                            match = regex.Match(sb.ToString());
                        }
                        if (!first)
                        {
                            sb.Append("</color>");
                        }
                        map.Add(id, sb.ToString());
                    }
                }
            }

            string[] contents = new string[map.Count + 2];

            contents[0] = CSVFileUtil.ToCSVLine(new[] { "ID", "注释", "内容" });
            contents[1] = CSVFileUtil.ToCSVLine(new[] { "int", "", "Content" });

            int index = 2;

            foreach (var item in map)
            {
                contents[index] = CSVFileUtil.ToCSVLine(new[] { item.Key.ToString(), "", item.Value });
                index++;
            }

            File.WriteAllLines("d:/aaa.csv", contents, Encoding.UTF8);
        }