void OnFocus() { if (mSettingsList == null) { mSettingsList = ConfigSettings.ReadXmlGroupSettings(); mSelectedGroupId = -1; if (mSettingsList.Count > 0) { mCurGroup = mSettingsList[0]; mSelectedGroupId = 1; } InitGroupOptions(); } mInvalidFiles.Clear(); if (mCurGroup != null) { for (int i = 0, imax = mCurGroup.xmlFiles.Length; i < imax; i++) { string xmlFile = mCurGroup.xmlFiles[i].path; if (!File.Exists(xmlFile)) { mInvalidFiles.Add(xmlFile); } } } }
/// <summary> /// 将一个组中所有的xml数据加到数据库中,运行时将读取原始数据 /// </summary> /// <param name="group">该组的所有信息</param> /// <param name="addedXmls">从组中加到数据库中的所有xml名称</param> /// <returns>组名称,若添加失败或该组已存在,则返回null</returns> public string AddToDatabase(XmlGroupSettings group, IList <string> addedXmls) { if (group == null) { return(null); } if (mGroupXmls.ContainsKey(group.group)) { return(null); } GroupTypeFinder typeFinder = new GroupTypeFinder("Assembly-CSharp", group.group); List <string> typeNames = new List <string>(); int count = group.xmlFiles.Length; string[] xmls = new string[count]; for (int i = 0; i < count; i++) { XmlGroupSettings.XmlData data = group.xmlFiles[i]; string xmlName = Path.GetFileNameWithoutExtension(data.path); if (mDataInfos.ContainsKey(xmlName)) { Debug.LogError(string.Format("Xml '{0}' is already existed !", xmlName)); continue; } int typeIndex = typeNames.IndexOf(data.typeName); if (typeIndex < 0) { typeIndex = typeNames.Count; typeNames.Add(data.typeName); } mDataInfos.Add(xmlName, new DataInfo(data.path, typeFinder, typeIndex)); xmls[i] = xmlName; if (addedXmls != null) { addedXmls.Add(xmlName); } } typeFinder.SetTypes(typeNames.ToArray()); mGroupXmls.Add(group.group, xmls); return(group.group); }
//[MenuItem("FunPlus/Configs/Serialize All Xmls", false, 3)] static void SerializeAllXmls() { List <XmlGroupSettings> groups = ConfigSettings.ReadXmlGroupSettings(); try { List <string> gs = new List <string>(); for (int i = 0, imax = groups.Count; i < imax; i++) { XmlGroupSettings group = groups[i]; XmlSerializer.SerializeXmls(group); gs.Add(group.group); } AssetDatabase.Refresh(); EditorUtility.DisplayDialog("Xml Serializer", "Xmls Serializer !\n" + string.Join("\n", gs.ToArray()), "OK"); } catch (Exception e) { Debug.LogException(e); EditorUtility.DisplayDialog("Xml Serialize Failed !", "See console for more details.\n\n" + e.Message, "OK"); } }
/// <summary> /// 将指定的XmlGroup数据序列化并生成二进制文件 /// </summary> /// <param name="group">该组的所有信息</param> public static void SerializeXmls(XmlGroupSettings group) { FileStream fs = File.OpenWrite(group.group); MemoryStream dataStream = new MemoryStream(); Exception e = null; try { Dictionary <string, int> allTypes = new Dictionary <string, int>(); byte[] bytesContent = Encoding.UTF8.GetBytes(group.group); byte[] bytesLength = BitConverter.GetBytes((ushort)bytesContent.Length); fs.Write(bytesLength, 0, bytesLength.Length); fs.Write(bytesContent, 0, bytesContent.Length); int count = group.xmlFiles.Length; byte[] bytesCount = BitConverter.GetBytes((ushort)count); fs.Write(bytesCount, 0, bytesCount.Length); int[] offsets = new int[count]; long[] offsetsPos = new long[count]; for (int i = 0; i < count; i++) { XmlGroupSettings.XmlData data = group.xmlFiles[i]; string name = Path.GetFileNameWithoutExtension(data.path); byte[] bytes = Encoding.UTF8.GetBytes(name); byte[] bytesLen = BitConverter.GetBytes((ushort)bytes.Length); byte[] bytesOffset = BitConverter.GetBytes((int)dataStream.Position); fs.Write(bytesLen, 0, bytesLen.Length); fs.Write(bytes, 0, bytes.Length); offsets[i] = (int)dataStream.Position; offsetsPos[i] = fs.Position; fs.Write(bytesOffset, 0, bytesOffset.Length); SerializeXml(name, dataStream, data.path, data.typeName, allTypes); } string[] allTypeNames = new string[allTypes.Count]; foreach (KeyValuePair <string, int> kv in allTypes) { allTypeNames[kv.Value] = kv.Key; } byte[] bl = BitConverter.GetBytes((ushort)allTypeNames.Length); fs.Write(bl, 0, bl.Length); for (int i = 0, imax = allTypeNames.Length; i < imax; i++) { bytesContent = Encoding.UTF8.GetBytes(allTypeNames[i]); if (bytesContent.Length > 255) { throw new Exception(string.Format("Type Name '{0}' is too long !", allTypeNames[i])); } fs.WriteByte((byte)bytesContent.Length); fs.Write(bytesContent, 0, bytesContent.Length); } int seg = (int)fs.Position; for (int i = 0; i < count; i++) { byte[] bytesOffset = BitConverter.GetBytes(seg + offsets[i]); fs.Position = offsetsPos[i]; fs.Write(bytesOffset, 0, bytesOffset.Length); } fs.Position = (long)seg; dataStream.SetLength(dataStream.Position); dataStream.Position = 0L; byte[] buffer = new byte[256]; while (true) { int len = dataStream.Read(buffer, 0, buffer.Length); if (len <= 0) { break; } fs.Write(buffer, 0, len); } } catch (Exception ex) { e = ex; } dataStream.Close(); fs.SetLength(fs.Position); fs.Flush(); fs.Close(); if (e != null) { throw e; } }
void OnGUI() { EditorGUI.BeginDisabledGroup(EditorApplication.isCompiling); Color cachedBgColor; bool dirty = false; bool isValid = true; GUILayout.Space(12f); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Xml Group", GUILayout.Width(100f)); int selectedGroupId = EditorGUILayout.Popup(mSelectedGroupId, mGroupNames); if (selectedGroupId == 0) { string groupName = EditorUtilityFix.SaveFilePanel("Set Binary Data Path", ".", "new_xml_group", "dat"); if (!string.IsNullOrEmpty(groupName)) { if (IsGroupNameValid(groupName)) { mCurGroup = new XmlGroupSettings(); mCurGroup.group = groupName; mCurGroup.comments = groupName; mSettingsList.Add(mCurGroup); mSelectedGroupId = InitGroupOptions(); dirty = true; } else { EditorUtility.DisplayDialog("Create New Xmls Group Failed", "A binary file with the same filename has already been set here!", "Got it"); } } } else if (selectedGroupId > 0) { mSelectedGroupId = selectedGroupId; mCurGroup = mSettingsList[mSelectedGroupId - 1]; } EditorGUI.BeginDisabledGroup(mSelectedGroupId <= 0); cachedBgColor = GUI.backgroundColor; GUI.backgroundColor = Color.red; if (GUILayout.Button("Delete", GUILayout.Width(60f))) { mSelectedGroupId = DeleteGroup(mSelectedGroupId); mCurGroup = mSelectedGroupId > 0 ? mSettingsList[mSelectedGroupId - 1] : null; dirty = true; } GUI.backgroundColor = cachedBgColor; EditorGUI.EndDisabledGroup(); EditorGUILayout.EndHorizontal(); if (mCurGroup != null) { GUILayout.Space(8f); EditorGUILayout.BeginHorizontal(GUILayout.Height(20f)); GUILayout.Space(4f); EditorGUILayout.LabelField("Comment", GUILayout.Width(100f)); EditorGUI.BeginChangeCheck(); mCurGroup.comments = EditorGUILayout.TextField(mCurGroup.comments); if (EditorGUI.EndChangeCheck()) { dirty = true; } GUILayout.Space(68f); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(GUILayout.Height(20f)); GUILayout.Space(4f); EditorGUILayout.LabelField("Binary File Path", GUILayout.Width(100f)); EditorGUILayout.LabelField(mCurGroup.group, (GUIStyle)"AS TextArea", GUILayout.Height(20f)); if (GUILayout.Button("Browse", GUILayout.Width(60f))) { string groupPath = EditorUtilityFix.SaveFilePanel("Change Binary Data Path", Path.GetDirectoryName(mCurGroup.group), Path.GetFileNameWithoutExtension(mCurGroup.group), Path.GetExtension(mCurGroup.group).Replace(".", "")); if (!string.IsNullOrEmpty(groupPath) && groupPath != mCurGroup.group) { if (IsGroupNameValid(groupPath)) { if (mCurGroup.comments == mCurGroup.group) { mCurGroup.comments = groupPath; } mCurGroup.group = groupPath; dirty = true; InitGroupOptions(); } else { EditorUtility.DisplayDialog("Change Binary File Path Failed", "A binary file with the same filename has already been set here!", "Got it"); } } } GUILayout.Space(4f); EditorGUILayout.EndHorizontal(); GUILayout.Space(2f); mScrollPos = EditorGUILayout.BeginScrollView(mScrollPos, false, false); EditorGUILayout.LabelField("Xml Files:"); int removeAt = -1; for (int i = 0, imax = mCurGroup.xmlFiles.Length; i < imax; i++) { XmlGroupSettings.XmlData xmlData = mCurGroup.xmlFiles[i]; EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(20f)); GUILayout.Space(12f); //start content cachedBgColor = GUI.backgroundColor; GUI.backgroundColor = (i & 1) == 0 ? new Color(0.6f, 0.6f, 0.7f) : new Color(0.8f, 0.8f, 0.8f); EditorGUILayout.BeginVertical((GUIStyle)"AS TextArea", GUILayout.MinHeight(20f)); GUI.backgroundColor = cachedBgColor; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Comment", GUILayout.Width(60f)); string comments = EditorGUILayout.TextField(xmlData.comments); if (comments != xmlData.comments) { xmlData.comments = comments; dirty = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(20f)); GUILayout.Space(2f); bool invalid = mInvalidFiles.Contains(xmlData.path); cachedBgColor = GUI.backgroundColor; if (invalid) { GUI.backgroundColor = Color.red; } EditorGUILayout.LabelField(new GUIContent(xmlData.path, invalid ? "File Not Found" : null), (GUIStyle)"AS TextArea", GUILayout.Height(20f)); GUI.backgroundColor = cachedBgColor; if (GUILayout.Button("Browse", GUILayout.Width(64))) { string xmlPath = browseXmlFile("Fail to Change Xml File", i); if (!string.IsNullOrEmpty(xmlPath)) { if (xmlData.comments == xmlData.path) { xmlData.comments = xmlPath; } xmlData.path = xmlPath; dirty = true; } } GUILayout.Space(2f); EditorGUILayout.EndHorizontal(); EditorGUI.BeginChangeCheck(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Type", GUILayout.Width(60f)); xmlData.typeName = EditorGUILayout.TextField(xmlData.typeName); if (GUILayout.Button("ProtoGen", GUILayout.Width(64))) { string codePath; string typeName = ProtoGen.GenerateProto(Path.GetDirectoryName(xmlData.codePath), out codePath); if (!string.IsNullOrEmpty(typeName)) { xmlData.typeName = typeName; xmlData.codePath = codePath; dirty = true; AssetDatabase.Refresh(); } } EditorGUILayout.EndHorizontal(); if (EditorGUI.EndChangeCheck()) { dirty = true; } //end content EditorGUILayout.EndVertical(); cachedBgColor = GUI.backgroundColor; GUI.backgroundColor = Color.red; if (GUILayout.Button("Delete", GUILayout.Width(60f))) { removeAt = i; } GUI.backgroundColor = cachedBgColor; GUILayout.Space(4f); EditorGUILayout.EndHorizontal(); GUILayout.Space(2f); } GUILayout.BeginHorizontal(); GUILayout.Space(12f); cachedBgColor = GUI.backgroundColor; GUI.backgroundColor = Color.green; if (GUILayout.Button("Add Xml File")) { string xmlPath = browseXmlFile("Fail to Add Xml File", -1); if (!string.IsNullOrEmpty(xmlPath)) { XmlGroupSettings.XmlData[] xmlFiles = new XmlGroupSettings.XmlData[mCurGroup.xmlFiles.Length + 1]; System.Array.Copy(mCurGroup.xmlFiles, 0, xmlFiles, 0, mCurGroup.xmlFiles.Length); XmlGroupSettings.XmlData newXml = new XmlGroupSettings.XmlData(); newXml.comments = xmlPath; newXml.path = xmlPath; xmlFiles[mCurGroup.xmlFiles.Length] = newXml; mCurGroup.xmlFiles = xmlFiles; dirty = true; } } GUILayout.EndHorizontal(); if (removeAt >= 0) { XmlGroupSettings.XmlData[] xmlFiles = new XmlGroupSettings.XmlData[mCurGroup.xmlFiles.Length - 1]; System.Array.Copy(mCurGroup.xmlFiles, 0, xmlFiles, 0, removeAt); System.Array.Copy(mCurGroup.xmlFiles, removeAt + 1, xmlFiles, removeAt, xmlFiles.Length - removeAt); mCurGroup.xmlFiles = xmlFiles; dirty = true; } GUI.backgroundColor = cachedBgColor; EditorGUILayout.EndScrollView(); GUILayout.Space(8f); EditorGUI.BeginDisabledGroup(!isValid); if (GUILayout.Button("Generate Binary File", GUILayout.Height(32f))) { Generate(); } EditorGUI.EndDisabledGroup(); } GUILayout.Space(4f); if (dirty) { ConfigSettings.WriteXmlGroupSettings(mSettingsList); } EditorGUI.EndDisabledGroup(); }
/// <summary> /// 将一个组中所有的xml数据加到数据库中,运行时将读取原始数据 /// </summary> /// <param name="group">该组的所有信息</param> /// <returns>组名称,若添加失败或该组已存在,则返回null</returns> public string AddToDatabase(XmlGroupSettings group) { return(AddToDatabase(group, null)); }