private static void CreatConfigFile(string filePath, string writePath, string nameSpace) { var targetPath = Application.dataPath + "/Resources/GameConfig"; if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } var fileName = Path.GetFileNameWithoutExtension(filePath); targetPath = targetPath + "/" + fileName + ".csv"; if (targetPath != csvPath) { File.Copy(csvPath, targetPath, true); } string className = fileName; StreamWriter sw = new StreamWriter(writePath + "/" + className + ".cs"); sw.WriteLine("using UnityEngine;\nusing System.Collections;\nusing ReadyGamerOne.Data;\n"); var ns = string.IsNullOrEmpty(nameSpace) ? "DefaultNamespace" : nameSpace; sw.WriteLine("namespace " + ns + "\n" + "{\n" + "\tpublic class " + className + " : CsvMgr"); sw.WriteLine("\t{"); var csr = new CsvStreamReader(csvPath); for (int colNum = 1; colNum < csr.ColCount + 1; colNum++) { string fieldName = csr[1, colNum]; string fieldType = csr[2, colNum]; sw.WriteLine("\t\t" + "public " + fieldType + " " + fieldName + ";" + ""); } sw.WriteLine("\t}\n" + "}\n"); sw.Flush(); sw.Close(); }
private static void CreatConfigFile(string filePath, string writePath, string nameSpace) { var targetPath = Application.dataPath + "/Resources/ClassFile"; if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } var fileName = Path.GetFileNameWithoutExtension(filePath); targetPath = targetPath + "/" + fileName + ".csv"; if (new FileInfo(targetPath).FullName != new FileInfo(filePath).FullName) { File.Copy(filePath, targetPath, true); } string className = fileName; StreamWriter sw = new StreamWriter(writePath + "/" + className + ".cs"); var parentClass = _dataConfigInfos[fileName].parentName; if (string.IsNullOrEmpty(parentClass)) { sw.Write("using ReadyGamerOne.Data;\n"); parentClass = "CsvMgr"; } sw.WriteLine("using UnityEngine;\nusing System.Collections;\n"); var ns = string.IsNullOrEmpty(nameSpace) ? "DefaultNamespace" : nameSpace; sw.WriteLine("namespace " + ns + "\n" + "{\n\tpublic class " + className + " : " + parentClass); sw.WriteLine("\t{"); var csr = new CsvStreamReader(filePath); sw.WriteLine("\t\tpublic const int " + className + "Count = " + (csr.RowCount - 2) + ";\n"); var index = 0; foreach (var kv in _dataConfigInfos[fileName].fieldInfos) { var fieldName = kv.Value.fieldName; var fieldType = kv.Value.fieldType; if (index == 0) { sw.WriteLine("\t\tpublic override string ID => " + fieldName + ".ToString();\n"); } sw.WriteLine("\t\t" + "public " + fieldType + " " + fieldName + ";" + ""); index++; } var toStringFunc = "\t\tpublic override string ToString()\n" + "\t\t{\n" + "\t\t\tvar ans=\"==《\t" + className + "\t》==\\n\" +\n"; for (int colNum = 1; colNum < csr.ColCount + 1; colNum++) { string fieldName = csr[1, colNum]; string fieldType = csr[2, colNum]; toStringFunc += "\t\t\t\t\t" + "\"" + fieldName + "\" + \"\t\" + " + fieldName + "+\"\\n\" +\n"; } toStringFunc = toStringFunc.Substring(0, toStringFunc.Length - "+\"\\n\" +\n".Length); toStringFunc += ";\n" + "\t\t\treturn ans;\n" + "\n\t\t}\n"; sw.WriteLine(toStringFunc); sw.WriteLine("\t}\n" + "}\n"); sw.Flush(); sw.Close(); }
private static bool ReadDataConfigFile() { _dataConfigInfos.Clear(); string path = null; if (File.Exists(csvDirPath + "/" + DataConfigFileName + ".csv")) { path = csvDirPath + "/" + DataConfigFileName + ".csv"; } else if (File.Exists(csvDirPath + "/" + DataConfigFileName + ".CSV")) { path = csvDirPath + "/" + DataConfigFileName + ".CSV"; } if (string.IsNullOrEmpty(path)) { Debug.Log("没有" + DataConfigFileName + "文件"); return(false); } var csr = new CsvStreamReader(path); for (var i = 1; i < csr.RowCount + 1; i += 2) { var config = new DataConfigInfo(); for (var j = 1; j < csr.ColCount + 1; j++) { var name = csr[i + 0, j]; var type = csr[i + 1, j]; switch (type) { case "className": config.className = name; break; case "parentClassName": config.parentName = name; break; default: if (string.IsNullOrEmpty(name)) { break; } config.fieldInfos.Add( name, new FieldInfo { fieldName = name, fieldType = type }); break; } } if (string.IsNullOrEmpty(config.className)) { return(false); } _dataConfigInfos.Add(config.className, config); } return(true); }
/// <summary> /// 读取数据结构表信息 /// </summary> /// <param name="copyToResource"></param> /// <returns></returns> private static bool ReadDataConfigFile(bool copyToResource = false) { var filePath = ""; if (copyToResource) { var targetPath = Application.dataPath + "/Resources/ClassFile"; if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } if (File.Exists(csvDirPath + "/" + DataConfigFileName + ".CSV")) { filePath = csvDirPath + "/" + DataConfigFileName + ".CSV"; } else if (File.Exists(csvDirPath + "/" + DataConfigFileName + ".csv")) { filePath = csvDirPath + "/" + DataConfigFileName + ".csv"; } else { return(false); } var fileName = Path.GetFileNameWithoutExtension(filePath); targetPath = targetPath + "/" + fileName + ".csv"; if (new FileInfo(targetPath).FullName != new FileInfo(filePath).FullName) { File.Copy(filePath, targetPath, true); } } _dataConfigInfos.Clear(); var csr = new CsvStreamReader(filePath); for (var i = 1; i < csr.RowCount + 1; i += 2) { var config = new DataConfigInfo(); for (var j = 1; j < csr.ColCount + 1; j++) { var name = csr[i + 0, j]; var type = csr[i + 1, j]; switch (type) { case "className": config.className = name; break; case "parentClassName": config.parentName = name; break; case "fileKeys": foreach (var fileKey in name.Split('_')) { config.fileKeys.Add(fileKey.Trim()); } break; default: if (string.IsNullOrEmpty(name)) { break; } config.fieldInfos.Add( name, new FieldInfo { fieldName = name, fieldType = type }); break; } } if (string.IsNullOrEmpty(config.className)) { return(false); } if (config.fileKeys.Count == 0) { config.fileKeys.Add(config.className); } _dataConfigInfos.Add(config.className, config); } return(true); }