TestFiles.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. namespace SharpGLTF
  7. {
  8. /// <summary>
  9. /// Encapsulates the access to test files.
  10. /// </summary>
  11. static class TestFiles
  12. {
  13. #region lifecycle
  14. static TestFiles()
  15. {
  16. var workingDir = TestContext.CurrentContext.WorkDirectory;
  17. _SchemaDir = System.IO.Path.Combine(workingDir, "glTF-Schema");
  18. _SampleModelsDir = System.IO.Path.Combine(workingDir, "glTF-Sample-Models");
  19. _PollyModelsDir = System.IO.Path.Combine(workingDir, "glTF-Blender-Exporter");
  20. _BabylonJsDir = System.IO.Path.Combine(workingDir, "BabylonJS-MeshesLibrary");
  21. }
  22. public static void DownloadReferenceModels()
  23. {
  24. if (_DonwloadCompleted) return;
  25. _DonwloadCompleted = true;
  26. TestContext.Progress.WriteLine("Downloading reference files... It might take a while, please, wait...");
  27. var dstPath = System.IO.Path.Combine(TestContext.CurrentContext.WorkDirectory, "GeneratedReferenceModels", "v_0_6_1.zip");
  28. _GeneratedModelsDir = DownloadUtils.DownloadFile("https://github.com/KhronosGroup/glTF-Asset-Generator/releases/download/v0.6.1/GeneratedAssets-0.6.1.zip", dstPath);
  29. TestContext.Progress.WriteLine("Checking out test files... It might take a while, please, wait...");
  30. DownloadUtils.SyncronizeGitRepository("https://github.com/KhronosGroup/glTF-Sample-Models.git", _SampleModelsDir);
  31. DownloadUtils.SyncronizeGitRepository("https://github.com/KhronosGroup/glTF-Blender-Exporter.git", _PollyModelsDir);
  32. DownloadUtils.SyncronizeGitRepository("https://github.com/KhronosGroup/glTF.git", _SchemaDir);
  33. DownloadUtils.SyncronizeGitRepository("https://github.com/BabylonJS/MeshesLibrary.git", _BabylonJsDir);
  34. TestContext.Progress.WriteLine("... Download Completed.");
  35. }
  36. #endregion
  37. #region data
  38. private static Boolean _DonwloadCompleted = false;
  39. private static readonly string _SchemaDir;
  40. private static readonly string _SampleModelsDir;
  41. private static readonly string _PollyModelsDir;
  42. private static readonly string _BabylonJsDir;
  43. private static string _GeneratedModelsDir;
  44. #endregion
  45. #region API
  46. public static IReadOnlyList<string> GetSchemaExtensionsModelsPaths()
  47. {
  48. return GetModelPathsInDirectory(_SchemaDir, "extensions", "2.0");
  49. }
  50. public static IEnumerable<(string, bool)> GetReferenceModelPaths(bool useNegative = false)
  51. {
  52. var dirPath = _GeneratedModelsDir;
  53. if (dirPath.EndsWith(".zip")) dirPath = dirPath.Substring(0, dirPath.Length - 4);
  54. var manifestsPath = System.IO.Path.Combine(dirPath, useNegative? "Negative" : "Positive");
  55. var manifests = System.IO.Directory.GetFiles(manifestsPath, "Manifest.json", System.IO.SearchOption.AllDirectories)
  56. .Skip(1)
  57. .ToArray();
  58. foreach (var m in manifests)
  59. {
  60. var d = System.IO.Path.GetDirectoryName(m);
  61. var content = System.IO.File.ReadAllText(m);
  62. var doc = Newtonsoft.Json.Linq.JObject.Parse(content);
  63. var models = doc.SelectToken("models");
  64. foreach(var model in models)
  65. {
  66. var mdlPath = (String)model.SelectToken("fileName");
  67. var loadable = !useNegative;
  68. if (loadable) loadable = (Boolean)model.SelectToken("loadable");
  69. mdlPath = System.IO.Path.Combine(d, mdlPath);
  70. yield return (mdlPath, loadable);
  71. }
  72. }
  73. yield break;
  74. }
  75. public static IReadOnlyList<string> GetSampleModelsPaths()
  76. {
  77. var files = GetModelPathsInDirectory(_SampleModelsDir, "2.0");
  78. return files
  79. .OrderBy(item => item)
  80. .Where(item => !item.Contains("\\glTF-Draco\\"))
  81. .ToList();
  82. }
  83. public static IReadOnlyList<string> GetBabylonJSModelsPaths()
  84. {
  85. var files = GetModelPathsInDirectory(_BabylonJsDir);
  86. return files
  87. .OrderBy(item => item)
  88. .Where(item => !item.Contains("\\AssetGenerator\\0.6\\"))
  89. .Where(item => !item.EndsWith("shaderBall.glb")) // invalid
  90. .ToList();
  91. }
  92. public static string GetPollyFileModelPath()
  93. {
  94. return System.IO.Path.Combine(_PollyModelsDir, "polly", "project_polly.glb");
  95. }
  96. private static IReadOnlyList<string> GetModelPathsInDirectory(params string[] paths)
  97. {
  98. var dirPath = System.IO.Path.Combine(paths);
  99. if (dirPath.EndsWith(".zip")) dirPath = dirPath.Substring(0, dirPath.Length-4);
  100. if (!System.IO.Path.IsPathFullyQualified(dirPath)) throw new ArgumentException(nameof(dirPath));
  101. var gltf = System.IO.Directory.GetFiles(dirPath, "*.gltf", System.IO.SearchOption.AllDirectories);
  102. var glbb = System.IO.Directory.GetFiles(dirPath, "*.glb", System.IO.SearchOption.AllDirectories);
  103. return gltf.Concat(glbb).ToList();
  104. }
  105. #endregion
  106. }
  107. }