Exemplo n.º 1
0
        private static string CreateData(IniStructure IniData, string comment)
        {       //Iterates through all categories and keys and appends all data to Data
            int CategoryCount = IniData.GetCategories().Length;

            int[]  KeyCountPerCategory = new int[CategoryCount];
            string Data = comment;

            string[] temp = new string[2];          // will contain key-value pair

            for (int i = 0; i < CategoryCount; i++) // Gets keycount per category
            {
                string CategoryName = IniData.GetCategories()[i];
                KeyCountPerCategory[i] = IniData.GetKeys(CategoryName).Length;
            }

            for (int catcounter = 0; catcounter < CategoryCount; catcounter++)
            {
                Data += "\r\n[" + IniData.GetCategoryName(catcounter) + "]\r\n";
                // writes [Category] to Data
                for (int keycounter = 0; keycounter < KeyCountPerCategory[catcounter]; keycounter++)
                {
                    temp[0] = IniData.GetKeyName(catcounter, keycounter);
                    temp[1] = IniData.GetValue(catcounter, keycounter);
                    Data   += temp[0] + "=" + temp[1] + "\r\n";
                    // writes the key-value pair to Data
                }
            }
            return(Data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads an ini file and returns the content as an IniStructure. Returns null if an error occurred.
        /// </summary>
        /// <param name="Filename">The filename to read</param>
        /// <returns></returns>
        public static IniStructure ReadIni(string Filename)
        {
            string Data = ReadFile(Filename);

            if (Data == null)
            {
                return(null);
            }

            IniStructure data = InterpretIni(Data);

            return(data);
        }
Exemplo n.º 3
0
        public static IniStructure InterpretIni(string Data)
        {
            IniStructure IniData = new IniStructure();

            string[] Lines = RemoveAndVerifyIni(DivideToLines(Data));
            // Divides the Data in lines, removes comments and empty lines
            // and verifies if the ini is not corrupted
            // Returns null if it is.
            if (Lines == null)
            {
                return(null);
            }

            if (IsLineACategoryDef(Lines[0]) != LineType.Category)
            {
                return(null);
                // Ini is faulty - does not begin with a categorydef
            }
            string CurrentCategory = "";

            foreach (string line in Lines)
            {
                switch (IsLineACategoryDef(line))
                {
                case LineType.Category:          // the line is a correct category definition
                    string NewCat = line.Substring(1, line.Length - 2);
                    IniData.AddCategory(NewCat); // adds the category to the IniData
                    CurrentCategory = NewCat;
                    break;

                case LineType.NotACategory:     // the line is not a category definition
                    string[] keyvalue = GetDataFromLine(line);
                    IniData.AddValue(CurrentCategory, keyvalue[0], keyvalue[1]);
                    // Adds the key-value to the current category
                    break;

                case LineType.Faulty:     // the line is faulty
                    return(null);
                }
            }
            return(IniData);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Helper helper = Helper.GetHelper();

            helper.RecordLogText("开始准备合并AIType.tab文件");
            string fileName = Path.Combine(Application.StartupPath, @"config.ini");

            if (File.Exists(fileName))
            {
                string       content      = helper.GetFileContent(fileName);
                IniStructure iniStructure = IniStructure.ReadIniWithContent(content);

                string rootPath             = iniStructure.GetValue("General", "RootDirectory");
                string parameterCountString = iniStructure.GetValue("General", "ParameterCount");
                int    parameterCount       = int.Parse(parameterCountString);

                // 需要导出一张旧的AIType表作为中间文件
                fileName = Path.Combine(rootPath, @"settings\AIType.tab");
                content  = helper.GetFileContent(fileName);

                StringBuilder defaultLine = new StringBuilder();
                defaultLine.Append("0\t");

                for (int i = 0; i < parameterCount; i++)
                {
                    defaultLine.Append(string.Format("{0}\t", iniStructure.GetValue("DefaultValue", string.Format("ParameterValue{0}", i))));
                }

                defaultLine.Remove(defaultLine.Length - 1, 1);

                string newContent = CreateNewAITypeContent(content, defaultLine.ToString());
                helper.SaveData(fileName, newContent);

                string        mapCountString = iniStructure.GetValue("General", "MapCount");
                int           mapCount       = int.Parse(mapCountString);
                List <string> mapList        = new List <string>();

                for (int i = 0; i < mapCount; i++)
                {
                    string mapEnable = iniStructure.GetValue("MapList", string.Format("Enable{0}", i));

                    if (mapEnable == "1")
                    {
                        mapList.Add(iniStructure.GetValue("MapList", string.Format("MapName{0}", i)));
                    }
                }

                try
                {
                    Lua    lua     = new Lua(); // lua虚拟机
                    string luaFile = Path.Combine(Application.StartupPath, "exportScript.lua");
                    lua.DoFile(luaFile);

                    LuaFunction function = lua.GetFunction("ExportAIData");
                    string      filePath = Path.Combine(rootPath, @"settings\NpcTemplate.tab");
                    function.Call(filePath, mapList);

                    helper.RecordLogText("AIType.tab合并完成");
                }
                catch (Exception ex)
                {
                    helper.RecordLogText("合并AIType文件时产生异常:" + ex.Message);
                }

                fileName = Path.Combine(Application.StartupPath, "log.txt");
                helper.SaveData(fileName, helper.LogText);
            }
            else
            {
                helper.RecordLogText(string.Format("配置文件{0}不存在", fileName));
            }
        }
Exemplo n.º 5
0
        // add by kuangsihao
        public static IniStructure ReadIniWithContent(string content)
        {
            IniStructure data = InterpretIni(content);

            return(data);
        }
Exemplo n.º 6
0
 private static string CreateData(IniStructure IniData)
 {
     return(CreateData(IniData, ""));
 }
Exemplo n.º 7
0
        /// <summary>
        /// Writes an IniStructure to a file with a comment.
        /// </summary>
        /// <param name="IniData">The contents to write</param>
        /// <param name="Filename">The complete path and name of the file</param>
        /// <param name="comment">Comment to add</param>
        /// <returns></returns>
        public static bool WriteIni(IniStructure IniData, string Filename, string comment)
        {
            string DataToWrite = CreateData(IniData, BuildComment(comment));

            return(WriteFile(Filename, DataToWrite));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Writes an IniStructure to a file without a comment.
        /// </summary>
        /// <param name="IniData">The contents to write</param>
        /// <param name="Filename">The complete path and name of the file</param>
        /// <returns></returns>
        public static bool WriteIni(IniStructure IniData, string Filename)
        {
            string DataToWrite = CreateData(IniData);

            return(WriteFile(Filename, DataToWrite));
        }
Exemplo n.º 9
0
        public static IniStructure InterpretIni(string Data)
        {
            IniStructure IniData = new IniStructure();
            string[] Lines = RemoveAndVerifyIni(DivideToLines(Data));
            // Divides the Data in lines, removes comments and empty lines
            // and verifies if the ini is not corrupted
            // Returns null if it is.
            if (Lines == null)
                return null;

            if (IsLineACategoryDef(Lines[0]) != LineType.Category)
            {
                return null;
                // Ini is faulty - does not begin with a categorydef
            }
            string CurrentCategory = "";
            foreach (string line in Lines)
            {
                switch (IsLineACategoryDef(line))
                {
                    case LineType.Category:	// the line is a correct category definition
                        string NewCat = line.Substring(1, line.Length - 2);
                        IniData.AddCategory(NewCat); // adds the category to the IniData
                        CurrentCategory = NewCat;
                        break;
                    case LineType.NotACategory: // the line is not a category definition
                        string[] keyvalue = GetDataFromLine(line);
                        IniData.AddValue(CurrentCategory, keyvalue[0], keyvalue[1]);
                        // Adds the key-value to the current category
                        break;
                    case LineType.Faulty: // the line is faulty
                        return null;
                }
            }
            return IniData;
        }
Exemplo n.º 10
0
        private static string CreateData(IniStructure IniData, string comment)
        {	//Iterates through all categories and keys and appends all data to Data
            int CategoryCount = IniData.GetCategories().Length;
            int[] KeyCountPerCategory = new int[CategoryCount];
            string Data = comment;
            string[] temp = new string[2]; // will contain key-value pair

            for (int i = 0; i < CategoryCount; i++) // Gets keycount per category
            {
                string CategoryName = IniData.GetCategories()[i];
                KeyCountPerCategory[i] = IniData.GetKeys(CategoryName).Length;
            }

            for (int catcounter = 0; catcounter < CategoryCount; catcounter++)
            {
                Data += "\r\n[" + IniData.GetCategoryName(catcounter) + "]\r\n";
                // writes [Category] to Data
                for (int keycounter = 0; keycounter < KeyCountPerCategory[catcounter]; keycounter++)
                {
                    temp[0] = IniData.GetKeyName(catcounter, keycounter);
                    temp[1] = IniData.GetValue(catcounter, keycounter);
                    Data += temp[0] + "=" + temp[1] + "\r\n";
                    // writes the key-value pair to Data
                }
            }
            return Data;
        }
Exemplo n.º 11
0
 private static string CreateData(IniStructure IniData)
 {
     return CreateData(IniData, "");
 }
Exemplo n.º 12
0
 /// <summary>
 /// Writes an IniStructure to a file without a comment.
 /// </summary>
 /// <param name="IniData">The contents to write</param>
 /// <param name="Filename">The complete path and name of the file</param>
 /// <returns></returns>
 public static bool WriteIni(IniStructure IniData, string Filename)
 {
     string DataToWrite = CreateData(IniData);
     return WriteFile(Filename, DataToWrite);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Writes an IniStructure to a file with a comment.
 /// </summary>
 /// <param name="IniData">The contents to write</param>
 /// <param name="Filename">The complete path and name of the file</param>
 /// <param name="comment">Comment to add</param>
 /// <returns></returns>
 public static bool WriteIni(IniStructure IniData, string Filename, string comment)
 {
     string DataToWrite = CreateData(IniData, BuildComment(comment));
     return WriteFile(Filename, DataToWrite);
 }