public void InjectTexturesCorrectly() { AssetPath gltfPath = new AssetPath(core.finalDownloadedPath, "MyHash", "model/myModel.gltf"); AssetPath texturePath = new AssetPath(core.finalDownloadedPath, "MyHash2", "model/texture.png"); AssetPath texturePath2 = new AssetPath(core.finalDownloadedPath, "MyHash3", "model/invalid-texture.png"); PersistentAssetCache.ImageCacheByUri.Clear(); PersistentAssetCache.StreamCacheByUri.Clear(); core.RetrieveAndInjectTexture(gltfPath, texturePath); string content1 = "Test"; env.file.WriteAllText(texturePath.finalPath, content1); core.RetrieveAndInjectTexture(gltfPath, texturePath); core.RetrieveAndInjectTexture(gltfPath, texturePath2); string id1 = $"texture.png@{gltfPath.finalPath}"; string id2 = $"invalid-texture.png@{gltfPath.finalPath}"; //NOTE(Brian): Check if streams exists and are added correctly Assert.IsTrue(PersistentAssetCache.HasImage(id1), $"id1:{id1} doesn't exist?"); Assert.IsFalse(PersistentAssetCache.HasImage(id2), $"Second file with {id2} shouldn't be injected because it doesn't exist!"); Assert.IsNotNull(PersistentAssetCache.GetImage(id1), "First image don't exist!"); //NOTE(Brian): Read image and validate content var image1 = PersistentAssetCache.GetImage(id1); }
public void InjectBuffersCorrectly() { AssetPath gltfPath = new AssetPath(core.finalDownloadedPath, "MyHash", "models/myModel.gltf"); AssetPath bufferPath = new AssetPath(core.finalDownloadedPath, "MyHash2", "models/anims/anim1.bin"); AssetPath bufferPath2 = new AssetPath(core.finalDownloadedPath, "MyHash3", "models/misc.bin"); AssetPath bufferPath3 = new AssetPath(core.finalDownloadedPath, "MyHash4", "missing-file.bin"); PersistentAssetCache.ImageCacheByUri.Clear(); PersistentAssetCache.StreamCacheByUri.Clear(); string content1 = "Test"; string content2 = "Test2"; env.file.WriteAllText(bufferPath.finalPath, content1); env.file.WriteAllText(bufferPath2.finalPath, content2); core.RetrieveAndInjectBuffer(gltfPath, bufferPath); core.RetrieveAndInjectBuffer(gltfPath, bufferPath2); core.RetrieveAndInjectBuffer(gltfPath, bufferPath3); char ds = Path.DirectorySeparatorChar; string id1 = $"anims{ds}anim1.bin@{gltfPath.finalPath}"; string id2 = $"misc.bin@{gltfPath.finalPath}"; string id3 = $"..{ds}missing-file.bin@{gltfPath.finalPath}"; //NOTE(Brian): Check if streams exists and are added correctly Assert.IsTrue(PersistentAssetCache.HasBuffer(id1)); Assert.IsTrue(PersistentAssetCache.HasBuffer(id2)); Assert.IsFalse(PersistentAssetCache.HasBuffer(id3), "Third file shouldn't be injected because it doesn't exist!"); Assert.IsNotNull(PersistentAssetCache.GetBuffer(id1), "First stream don't exist!"); Assert.IsNotNull(PersistentAssetCache.GetBuffer(id2), "Second stream don't exist!"); //NOTE(Brian): Read stream and validate content var buffer1 = PersistentAssetCache.GetBuffer(id1); var buffer2 = PersistentAssetCache.GetBuffer(id2); byte[] chars = new byte[100]; buffer1.stream.Read(chars, 0, 100); string bufferText = UTF8Encoding.UTF8.GetString(chars).TrimEnd('\0'); Assert.AreEqual(content1, bufferText, "First stream has invalid content!"); buffer2.stream.Read(chars, 0, 100); bufferText = UTF8Encoding.UTF8.GetString(chars).TrimEnd('\0'); Assert.AreEqual(content2, bufferText, "Second stream has invalid content!"); buffer1.stream.Dispose(); buffer2.stream.Dispose(); }
/// <summary> /// Load dumped buffers and put them in PersistentAssetCache so the GLTFSceneImporter /// can pick them up. /// </summary> /// <param name="gltfPath">GLTF path of the gltf that will pick up the references</param> /// <param name="bufferPath">Buffer path of the texture to be injected</param> internal void RetrieveAndInjectBuffer(AssetPath gltfPath, AssetPath bufferPath) { string finalPath = bufferPath.finalPath; if (!env.file.Exists(finalPath)) { return; } Stream stream = env.file.OpenRead(finalPath); string relativePath = ABConverter.PathUtils.GetRelativePathTo(gltfPath.file, bufferPath.file); // NOTE(Brian): This cache will be used by the GLTF importer when seeking streams. This way the importer will // consume the asset bundle dependencies instead of trying to create new streams. PersistentAssetCache.AddBuffer(relativePath, gltfPath.finalPath, stream); }
/// <summary> /// Load dumped textures and put them in PersistentAssetCache so the GLTFSceneImporter /// can pick them up. /// </summary> /// <param name="gltfPath">GLTF path of the gltf that will pick up the references</param> /// <param name="texturePath">Texture path of the texture to be injected</param> internal void RetrieveAndInjectTexture(AssetPath gltfPath, AssetPath texturePath) { string finalPath = texturePath.finalPath; if (!env.file.Exists(finalPath)) { return; } Texture2D t2d = env.assetDatabase.LoadAssetAtPath <Texture2D>(finalPath); if (t2d == null) { return; } string relativePath = ABConverter.PathUtils.GetRelativePathTo(gltfPath.file, texturePath.file); //NOTE(Brian): This cache will be used by the GLTF importer when seeking textures. This way the importer will // consume the asset bundle dependencies instead of trying to create new textures. PersistentAssetCache.AddImage(relativePath, gltfPath.finalPath, t2d); }
public override void Cleanup() { OnCleanup?.Invoke(); PersistentAssetCache.RemoveImage(texture); Object.Destroy(texture); }