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); }
/// <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); }
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); }
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; }
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; }
private static string CreateData(IniStructure IniData) { return CreateData(IniData,""); }
/// <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); }
/// <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); }
public bool Export(SqlConnection Conn, string RootDir) { bool bResult = true; StringBuilder sb = new StringBuilder(); sb.Append("LinkID\tMapID\tNpcID\tKind\tx\ty\tz\tNpcName\r\n"); String sql = "Select * from CareerGuide"; DataTable tbl = Helper.GetDataTable(sql, Conn); //按地图划分 sql = "Select MapID from CareerGuide where MapID > 0 group by MapID"; DataTable tblMapID = Helper.GetDataTable(sql, Conn); ArrayList alMapID = new ArrayList(); foreach (DataRow dr in tblMapID.Rows) { alMapID.Add((object)dr["MapID"].ToString()); DataRow[] rows = tbl.Select("MapID = " + dr["MapID"].ToString()); sql = "select Name from MapList where ID = " + dr["MapID"].ToString(); DataTable dt = Helper.GetDataTable(sql, Conn); String strMapName = dt.Rows[0][0].ToString(); if (strMapName == "测试1") { strMapName = RootDir + "\\data\\source\\maps\\测试\\" + strMapName + ".Map.Logical"; } else { strMapName = RootDir + "\\data\\source\\maps\\" + strMapName + "\\" + strMapName + ".Map.Logical"; } String strRetVal; String strContent = Helper.FileToString(strMapName); Misc.IniStructure m_inis = new Misc.IniStructure(); m_inis = Misc.IniStructure.ReadIniWithContent(strContent); //遍历Logical文件中npc strRetVal = m_inis.GetValue("MAIN", "NumNPC"); int nNPCCount = Int32.Parse(strRetVal); for (int nIndex = 0; nNPCCount > nIndex; nIndex++) { String strSectionName = "NPC" + nIndex.ToString(); strRetVal = m_inis.GetValue(strSectionName, "nTempleteID"); int nTempleteID = Int32.Parse(strRetVal); foreach (DataRow row in rows) { if (nTempleteID == Int32.Parse(row["NpcID"].ToString())) { strRetVal = m_inis.GetValue(strSectionName, "nX"); row["x"] = strRetVal.ToString(); strRetVal = m_inis.GetValue(strSectionName, "nY"); row["y"] = strRetVal.ToString(); strRetVal = m_inis.GetValue(strSectionName, "nZ"); row["z"] = strRetVal.ToString(); } } } } foreach (DataRow row in tbl.Rows) { sb.Append(row["LinkID"].ToString() + "\t"); sb.Append(row["MapID"].ToString() + "\t"); sb.Append(row["NpcID"].ToString() + "\t"); sb.Append(row["Kind"].ToString() + "\t"); sb.Append(row["x"].ToString() + "\t"); sb.Append(row["y"].ToString() + "\t"); sb.Append(row["z"].ToString() + "\t"); sb.Append(row["NpcName"].ToString() + "\r\n"); if (row["NpcID"].ToString() != "0" && (row["x"].ToString() == "" || row["y"].ToString() == "" || row["z"].ToString() == "")) { Console.Write("NPC:" + row["NpcID"].ToString() + "未找到对应坐标!\n"); } } sql = "Select filename from sys_export_table_cfg where tablename = 'CareerGuide'"; tbl = Helper.GetDataTable(sql, Conn); String strFileName = tbl.Rows[0][0].ToString(); strFileName = RootDir + strFileName; Helper.StringToFile(sb.ToString(), strFileName, Encoding.GetEncoding("gb2312")); return(bResult); }
public bool Export(SqlConnection Conn, string RootDir) { bool bResult = true; StringBuilder sb = new StringBuilder(); sb.Append("LinkID\tMapID\tNpcID\tKind\tx\ty\tz\tNpcName\r\n"); String sql = "Select * from CareerGuide"; DataTable tbl = Helper.GetDataTable(sql, Conn); //按地图划分 sql = "Select MapID from CareerGuide where MapID > 0 group by MapID"; DataTable tblMapID = Helper.GetDataTable(sql, Conn); ArrayList alMapID = new ArrayList(); foreach (DataRow dr in tblMapID.Rows) { alMapID.Add((object)dr["MapID"].ToString()); DataRow[] rows = tbl.Select("MapID = " + dr["MapID"].ToString()); sql = "select Name from MapList where ID = " + dr["MapID"].ToString(); DataTable dt = Helper.GetDataTable(sql, Conn); String strMapName = dt.Rows[0][0].ToString(); if (strMapName == "测试1") strMapName = RootDir + "\\data\\source\\maps\\测试\\" + strMapName + ".Map.Logical"; else strMapName = RootDir + "\\data\\source\\maps\\" + strMapName + "\\" + strMapName + ".Map.Logical"; String strRetVal; String strContent = Helper.FileToString(strMapName); Misc.IniStructure m_inis = new Misc.IniStructure(); m_inis = Misc.IniStructure.ReadIniWithContent(strContent); //遍历Logical文件中npc strRetVal = m_inis.GetValue("MAIN", "NumNPC"); int nNPCCount = Int32.Parse(strRetVal); for (int nIndex = 0; nNPCCount > nIndex; nIndex++) { String strSectionName = "NPC" + nIndex.ToString(); strRetVal = m_inis.GetValue(strSectionName, "nTempleteID"); int nTempleteID = Int32.Parse(strRetVal); foreach (DataRow row in rows) { if (nTempleteID == Int32.Parse(row["NpcID"].ToString())) { strRetVal = m_inis.GetValue(strSectionName, "nX"); row["x"] = strRetVal.ToString(); strRetVal = m_inis.GetValue(strSectionName, "nY"); row["y"] = strRetVal.ToString(); strRetVal = m_inis.GetValue(strSectionName, "nZ"); row["z"] = strRetVal.ToString(); } } } } foreach (DataRow row in tbl.Rows) { sb.Append(row["LinkID"].ToString() + "\t"); sb.Append(row["MapID"].ToString() + "\t"); sb.Append(row["NpcID"].ToString() + "\t"); sb.Append(row["Kind"].ToString() + "\t"); sb.Append(row["x"].ToString() + "\t"); sb.Append(row["y"].ToString() + "\t"); sb.Append(row["z"].ToString() + "\t"); sb.Append(row["NpcName"].ToString() + "\r\n"); if (row["NpcID"].ToString() != "0" && (row["x"].ToString() == "" || row["y"].ToString() == "" || row["z"].ToString() == "")) { Console.Write("NPC:" + row["NpcID"].ToString() + "未找到对应坐标!\n"); } } sql = "Select filename from sys_export_table_cfg where tablename = 'CareerGuide'"; tbl = Helper.GetDataTable(sql, Conn); String strFileName = tbl.Rows[0][0].ToString(); strFileName = RootDir + strFileName; Helper.StringToFile(sb.ToString(), strFileName, Encoding.GetEncoding("gb2312")); return bResult; }
// add by kuangsihao public static IniStructure ReadIniWithContent(string content) { IniStructure data = InterpretIni(content); return(data); }
private static string CreateData(IniStructure IniData) { return(CreateData(IniData, "")); }
/// <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)); }
/// <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)); }