Program.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using BabylonExport.Core.Exporters;
  5. using BabylonExport.Core.Exporters.FBX;
  6. using System.Windows.Forms;
  7. using System.ServiceModel;
  8. using BabylonExport.Interface;
  9. using BabylonExport.Core;
  10. using System.Linq;
  11. namespace BabylonExport
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. string output = "";
  18. try
  19. {
  20. if ((args.Length == 1) && (args[0] == "/service"))
  21. {
  22. var serviceHost = new ServiceHost(typeof(Service), new Uri[] { new Uri("net.pipe://localhost/") });
  23. serviceHost.AddServiceEndpoint(typeof(IService), new NetNamedPipeBinding(), "exportservice");
  24. serviceHost.Open();
  25. Console.WriteLine("Service started. Available in following endpoints");
  26. foreach (var serviceEndpoint in serviceHost.Description.Endpoints)
  27. {
  28. Console.WriteLine(serviceEndpoint.ListenUri.AbsoluteUri);
  29. }
  30. Console.ReadLine();
  31. }
  32. else
  33. {
  34. if (args.Length < 2)
  35. {
  36. DisplayUsage();
  37. return;
  38. }
  39. // Parsing arguments
  40. string input = "";
  41. bool skinned = false;
  42. bool rightToLeft = false;
  43. foreach (var arg in args)
  44. {
  45. var order = arg.Substring(0, 3);
  46. switch (order)
  47. {
  48. case "/i:":
  49. input = arg.Substring(3);
  50. break;
  51. case "/o:":
  52. output = arg.Substring(3);
  53. break;
  54. case "/sk":
  55. skinned = true;
  56. break;
  57. case "/rl":
  58. rightToLeft = true;
  59. break;
  60. default:
  61. DisplayUsage();
  62. return;
  63. }
  64. }
  65. if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(output))
  66. {
  67. DisplayUsage();
  68. return;
  69. }
  70. var extension = Path.GetExtension(input).ToLower();
  71. var outputName = Path.Combine(output, Path.GetFileNameWithoutExtension(input) + ".babylon");
  72. if (!Directory.Exists(output))
  73. {
  74. Directory.CreateDirectory(output);
  75. }
  76. // Browsing exporters
  77. foreach (var type in Assembly.GetAssembly(typeof(NovaExporter)).GetTypes().Where(t => !t.IsAbstract))
  78. {
  79. var interf = type.GetInterface("BabylonExport.Core.IExporter");
  80. if (interf != null)
  81. {
  82. var importer = (IExporter)Activator.CreateInstance(type);
  83. if (!importer.SupportedExtensions.Contains(extension))
  84. {
  85. continue;
  86. }
  87. Console.WriteLine("Using " + type);
  88. // Importation
  89. try
  90. {
  91. importer.OnImportProgressChanged += progress =>
  92. {
  93. Console.CursorLeft = 0;
  94. Console.Write("Generation....{0} %", progress);
  95. };
  96. Console.ForegroundColor = ConsoleColor.Green;
  97. Console.WriteLine("Generation of " + outputName + " started");
  98. Console.WriteLine();
  99. Console.ResetColor();
  100. importer.GenerateBabylonFile(input, outputName, skinned, rightToLeft);
  101. Console.ForegroundColor = ConsoleColor.Green;
  102. Console.WriteLine();
  103. Console.WriteLine();
  104. Console.WriteLine("Generation of " + outputName + " successfull");
  105. Console.ResetColor();
  106. using (var debugFile = new StreamWriter(output + @"\debug.txt", true))
  107. {
  108. debugFile.Write("Generation of " + outputName + " successfull");
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. Console.ForegroundColor = ConsoleColor.Red;
  114. Console.WriteLine();
  115. Console.WriteLine(ex.Message);
  116. Console.ResetColor();
  117. using (var debugFile = new StreamWriter(output + @"\debug.txt", true))
  118. {
  119. debugFile.Write(ex.Message);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. catch (ReflectionTypeLoadException ex)
  127. {
  128. Console.ForegroundColor = ConsoleColor.Red;
  129. Console.WriteLine("Fatal error encountered:");
  130. Console.WriteLine(ex.LoaderExceptions[0].Message);
  131. Console.ResetColor();
  132. if (output != "")
  133. {
  134. using (var debugFile = new StreamWriter(output + @"\debug.txt", true))
  135. {
  136. debugFile.Write(ex.Message);
  137. }
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. Console.ForegroundColor = ConsoleColor.Red;
  143. Console.WriteLine("Fatal error encountered:");
  144. Console.WriteLine(ex.Message);
  145. Console.ResetColor();
  146. if (output != "")
  147. {
  148. using (var debugFile = new StreamWriter(output + @"\debug.txt", true))
  149. {
  150. debugFile.Write(ex.Message);
  151. }
  152. }
  153. }
  154. }
  155. static void DisplayUsage()
  156. {
  157. Console.WriteLine("Babylon Import usage: BabylonImport.exe /i:\"source file\" /o:\"output folder\" [/sk] [/rl]");
  158. }
  159. }
  160. }