UIEditorConverter.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using CommonLang;
  2. using CommonLang.IO;
  3. using CommonLang.Xml;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Xml;
  10. namespace CommonUI.Data
  11. {
  12. public class UIEditorConverter
  13. {
  14. public delegate bool TryConvertToBin(FileInfo xml, FileInfo bin);
  15. public TryConvertToBin TryConvert;
  16. public void ConvertXML(DirectoryInfo dir)
  17. {
  18. foreach (FileInfo sub in dir.GetFiles())
  19. {
  20. ConvertXML(sub);
  21. }
  22. foreach (DirectoryInfo sub in dir.GetDirectories())
  23. {
  24. ConvertXML(sub);
  25. }
  26. }
  27. public void ConvertXML(FileInfo xml_file)
  28. {
  29. if (xml_file.FullName.EndsWith(".gui.xml"))
  30. {
  31. try
  32. {
  33. string bin_name = xml_file.FullName.Substring(0, xml_file.FullName.LastIndexOf(".gui.xml")) + ".gui.bin";
  34. FileInfo bin_file = new FileInfo(bin_name);
  35. if (TryConvert == null || !TryConvert.Invoke(xml_file, bin_file))
  36. {
  37. try
  38. {
  39. Console.Write(string.Format("ConvertXML: {0} -> {1}", xml_file.FullName, xml_file.Name + ".bin"));
  40. var xml = XmlUtil.LoadXML(xml_file.FullName);
  41. if (xml != null && xml.DocumentElement.Name == UIEditorMeta.UERoot_ClassName)
  42. {
  43. var meta = UIEditorMeta.CreateFromXml(xml);
  44. var bin = UIEditorMeta.SaveToBin(meta);
  45. File.WriteAllBytes(bin_file.FullName, bin);
  46. var meta2 = UIEditorMeta.CreateFromBin(bin);
  47. var bin2 = UIEditorMeta.SaveToBin(meta2);
  48. if (!CUtils.ArraysEqual<byte>(bin, bin2))
  49. {
  50. throw new Exception("Bad IO");
  51. }
  52. }
  53. }
  54. finally
  55. {
  56. Console.WriteLine(" OK!");
  57. }
  58. }
  59. }
  60. catch (Exception err)
  61. {
  62. Console.WriteLine(err.Message);
  63. Console.WriteLine(err.StackTrace);
  64. }
  65. }
  66. }
  67. }
  68. }