using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class CreateConfigFile : MonoBehaviour
{
//配置表中的属性
public class FactionNotice
{
public int ID;
public int NoticeType;
public int IconType;
public string Info;
}
[MenuItem("Tools/生成配置表解析文件")]
static void TestData()
{
CreateOne(new FactionNotice());
AssetDatabase.Refresh();
}
static void CreateOne(object data)
{
string className = data.GetType().Name;
var porps = data.GetType().GetFields();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("using UnityEngine;\nusing System;\nusing System.Security;\nusing System.Collections.Generic;\n");
sb.Append("namespace HotFix_Project.Config\n");
sb.Append("{\n");
sb.Append(string.Format("public class {0}Config : ConfigBase\n", className));
sb.Append("{\n");
sb.Append(string.Format("\tpublic class {0}Object\n", className));
sb.Append("\t{\n");
foreach (var pro in porps)
{
sb.Append(string.Format("\t\tpublic {0} {1};\n", pro.FieldType.Name, pro.Name));
}
sb.Append("\t}\n");
// sb.Append("#if !NETFX_CORE\n");
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
string cName = className + "Config";
string pName = className + "Object";
string sName = className.ToLower() + "Obj";
string dicName = className.ToLower() + "Dic";
sb2.Append("\t\t\t\t" + pName + " " + sName + " = new " + pName + "();\n");
foreach (var pro in porps)
{
if (pro.FieldType == typeof(string))
{
sb2.Append("\t\t\t\t" + sName + "." + pro.Name + " = childrenElement.Attribute(\"" + pro.Name + "\");\n");
}
else
{
//sb2.Append("\t\t\t\t" + sName + "." + pro.Name + " = " + pro.FieldType.Name + ".Parse(childrenElement.Attribute(\"" + pro.Name + "\"));\n");
sb2.Append("\t\t\t\t" + pro.FieldType.Name + ".TryParse(childrenElement.Attribute(\"" + pro.Name + "\")" + ", out " + sName + "." + pro.Name + ");\n");
}
}
sb2.Append("\t\t\t\t" + dicName + "[" + sName + ".ID]= " + sName + ";\n");
string fo = "\tpublic override bool Load(SecurityElement element)\n\t{\n\t\tif (element.Tag != \"Items\")\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\tif (element.Children != null)\n\t\t{\n\t\t\tforeach (SecurityElement childrenElement in element.Children)\n\t\t\t{\n" + sb2.ToString() + "\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n";
sb.Append(fo);
string st = "\tpublic " + cName + " ." + pName + " GetConfigElementByID(int id)\n\t{\n\t\t" + pName + " " + sName + "= null;\n\t\t" + dicName + ".TryGetValue(id, out " + sName + ");\n\t\treturn " + sName + ";\n\t}\n";
sb.Append(st);
string st2 = string.Format("\tpublic Dictionary<int, {0}> {1} = new Dictionary<int, {0}>();\n", pName, dicName);
sb.Append(st2);
sb.Append("}\n");
sb.Append("}");
//输出的路径
string path = Application.dataPath + "/../HotFix_Project/Config/" + className + "Config.cs";
using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(file))
{
sw.Write(sb.ToString());
}
}
}
}