Program.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.IO;
  8. using Newtonsoft.Json;
  9. using BabylonExport.Core;
  10. namespace BabylonBinaryConverter
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. string srcFilename = "";
  17. string dstPath = "";
  18. bool formatted = false;
  19. try
  20. {
  21. if (args.Length < 2)
  22. {
  23. DisplayUsage();
  24. return;
  25. }
  26. foreach (var arg in args)
  27. {
  28. if(arg.ToLower() == "/formatted")
  29. {
  30. formatted = true;
  31. }
  32. else if (arg.Substring(0, 3).ToLower() == "/i:")
  33. {
  34. srcFilename = arg.Substring(3);
  35. }
  36. else if (arg.Substring(0, 3).ToLower() == "/o:")
  37. {
  38. dstPath = arg.Substring(3);
  39. }
  40. else
  41. {
  42. DisplayUsage();
  43. }
  44. }
  45. if (string.IsNullOrEmpty(srcFilename) || string.IsNullOrEmpty(dstPath))
  46. {
  47. DisplayUsage();
  48. return;
  49. }
  50. string srcPath = Path.GetDirectoryName(srcFilename);
  51. string dstFilename = "";
  52. if (!srcFilename.Contains(".incremental.babylon"))
  53. dstFilename = Path.Combine(dstPath, Path.GetFileNameWithoutExtension(srcFilename) + ".incremental.babylon");
  54. else
  55. dstFilename = Path.Combine(dstPath, Path.GetFileName(srcFilename));
  56. if (!Directory.Exists(dstPath))
  57. {
  58. Directory.CreateDirectory(dstPath);
  59. }
  60. Console.WriteLine("Converting file " + WebUtility.UrlDecode(srcFilename) + " to binary in folder " + dstPath);
  61. ParseBabylonSceneFileAsJson(srcPath, srcFilename, dstPath, dstFilename, formatted);
  62. using (var debugFile = new StreamWriter(dstPath + @"\debug.txt", true))
  63. {
  64. debugFile.Write("Generation of " + dstFilename + " successfull");
  65. }
  66. }
  67. catch (Exception ex)
  68. {
  69. Console.ForegroundColor = ConsoleColor.Red;
  70. Console.WriteLine();
  71. Console.WriteLine(ex.Message);
  72. Console.ResetColor();
  73. using (var debugFile = new StreamWriter(dstPath + @"\debug.txt", true))
  74. {
  75. debugFile.Write(ex);
  76. }
  77. }
  78. }
  79. static void ParseBabylonSceneFileAsJson(string srcPath, string srcFilename, string dstPath, string dstFilename, bool formatted)
  80. {
  81. try
  82. {
  83. BabylonLodScene scene = JsonConvert.DeserializeObject<BabylonLodScene>(File.ReadAllText(WebUtility.UrlDecode(srcFilename)));
  84. scene.Convert(srcPath, dstPath);
  85. File.WriteAllText(WebUtility.UrlDecode(dstFilename), JsonConvert.SerializeObject(scene, (formatted ? Formatting.Indented : Formatting.None)));
  86. }
  87. catch (Exception ex)
  88. {
  89. Console.ForegroundColor = ConsoleColor.Red;
  90. Console.WriteLine();
  91. Console.WriteLine(ex.Message);
  92. Console.ForegroundColor = ConsoleColor.DarkCyan;
  93. Console.WriteLine(ex);
  94. Console.ResetColor();
  95. }
  96. }
  97. static void DisplayUsage()
  98. {
  99. Console.WriteLine("Babylon binary converter usage: BabylonBinaryConverter.exe /i:\"source file\" /o:\"output folder\" /formatted");
  100. Console.WriteLine(" /formatted to write formatted Json. The default is compressed.");
  101. }
  102. }
  103. }