public static void GenerateAndCompare(string title, string outputFilePath, string referenceFilePath, Asset asset) { Console.WriteLine(title + @"- from file " + outputFilePath); Console.WriteLine(@"---------------------------------------"); AssetFileSerializer.Save(outputFilePath, asset); var left = File.ReadAllText(outputFilePath).Trim(); Console.WriteLine(left); var right = File.ReadAllText(referenceFilePath).Trim(); Assert.That(left, Is.EqualTo(right)); }
public static void SerializeAndCompare(AssetItem assetItem, AssetPropertyGraph graph, string expectedYaml, bool isDerived) { assetItem.Asset.Id = isDerived ? DerivedId : BaseId; Assert.Equal(isDerived, assetItem.Asset.Archetype != null); if (isDerived) { assetItem.Asset.Archetype = new AssetReference(BaseId, assetItem.Asset.Archetype?.Location); } graph.PrepareForSave(null, assetItem); var stream = new MemoryStream(); AssetFileSerializer.Save(stream, assetItem.Asset, assetItem.YamlMetadata, null); stream.Position = 0; var streamReader = new StreamReader(stream); var yaml = streamReader.ReadToEnd(); Assert.Equal(expectedYaml, yaml); }
public void TestSerialization() { var originAsset = CreateOriginAsset(); using (var stream = new MemoryStream()) { AssetFileSerializer.Save(stream, originAsset); stream.Position = 0; var serializedVersion = Encoding.UTF8.GetString(stream.ToArray()); Console.WriteLine(serializedVersion); stream.Position = 0; var newAsset = AssetFileSerializer.Load <PrefabAsset>(stream, "Prefab.xkprefab").Asset; CheckAsset(originAsset, newAsset); } }
public void TestUpgrade(MyUpgradedAsset asset, bool needMigration) { var loadingFilePath = new PackageLoadingAssetFile(Path.Combine(DirectoryTestBase, "TestUpgrade\\Asset1.xkobj"), DirectoryTestBase); var outputFilePath = loadingFilePath.FilePath.FullPath; AssetFileSerializer.Save(outputFilePath, asset, null); var logger = new LoggerResult(); var context = new AssetMigrationContext(null, loadingFilePath.ToReference(), loadingFilePath.FilePath.ToWindowsPath(), logger); Assert.AreEqual(AssetMigration.MigrateAssetIfNeeded(context, loadingFilePath, "TestPackage"), needMigration); if (needMigration) { using (var fileStream = new FileStream(outputFilePath, FileMode.Truncate)) fileStream.Write(loadingFilePath.AssetContent, 0, loadingFilePath.AssetContent.Length); } Console.WriteLine(File.ReadAllText(outputFilePath).Trim()); var upgradedAsset = AssetFileSerializer.Load <MyUpgradedAsset>(outputFilePath).Asset; AssertUpgrade(upgradedAsset); }
static void ImportFile(string path, string relativePath, string output) { string[] arr = relativePath.Split('.'); string name = Path.GetFileNameWithoutExtension(relativePath); switch (arr[arr.Length - 1]) { case "jpg": case "jpeg": case "dds": case "tiff": case "tif": case "psd": case "tga": case "bmp": case "gif": case "png": break; if ((name.ToLower().EndsWith("bump") && !name.ToLower().StartsWith("bump")) || (name.ToLower().EndsWith("normal") && !name.ToLower().StartsWith("normal"))) { TextureAsset tex = NormalMapTextureFactory.Create(); tex.Source = new UFile(relativePath); if (!overwrite && File.Exists($"{output}/{name}{TextureAsset.FileExtension}")) { Console.WriteLine($"{output}/{name}{TextureAsset.FileExtension} exists - not writing."); break; } else { AssetFileSerializer.Save($"{output}/{name}{TextureAsset.FileExtension}", tex, null); } } else { TextureAsset tex = ColorTextureFactory.Create(); tex.Source = new UFile(relativePath); if (!overwrite && File.Exists($"{output}/{name}{TextureAsset.FileExtension}")) { Console.WriteLine($"{output}/{name}{TextureAsset.FileExtension} exists - not writing."); break; } AssetFileSerializer.Save($"{output}/{name}{TextureAsset.FileExtension}", tex, null); MaterialAsset mat = DiffuseMaterialFactory.Create(); ((ComputeTextureColor)((MaterialDiffuseMapFeature)mat.Attributes.Diffuse).DiffuseMap).Texture = GetTexture(tex); AssetFileSerializer.Save($"{output}/{name}{MaterialAsset.FileExtension}", mat, null); } break; case "dae": case "3ds": case "obj": case "blend": case "x": case "md2": case "md3": case "dxf": case "fbx": if (!overwrite && File.Exists($"{output}/{name}{ModelAsset.FileExtension.Split(';')[0]}")) { Console.WriteLine($"{output}/{name}{ModelAsset.FileExtension.Split(';')[0]} exists - not writing."); break; } ModelAsset model = DefaultAssetFactory <ModelAsset> .Create(); model.Source = new UFile(relativePath); Scene scene = ctx.ImportFile(path, PostProcessSteps.None); Dictionary <string, Stride.Rendering.Material> materials = new Dictionary <string, Stride.Rendering.Material>(); for (int i = 0; i < scene.MaterialCount; i++) { if (materials.ContainsKey(scene.Materials[i].Name)) { model.Materials.Add(new ModelMaterial() { Name = scene.Materials[i].Name, MaterialInstance = new MaterialInstance(materials[scene.Materials[i].Name]) }); continue; } if (!overwrite && File.Exists($"{output}/{scene.Materials[i].Name}{MaterialAsset.FileExtension}")) { Console.WriteLine($"{output}/{scene.Materials[i].Name}{MaterialAsset.FileExtension} exists - not writing parent mesh."); break; } MaterialAsset materialAsset = DiffuseMaterialFactory.Create(); // set diffuse (if possible) if (scene.Materials[i].HasTextureDiffuse) { string diffPath = Path.GetRelativePath(output, Path.Combine(Path.GetDirectoryName(path), scene.Materials[i].TextureDiffuse.FilePath)); TextureAsset asset = ImportTexture(diffPath, TextureType.Diffuse); if (!overwrite && File.Exists($"{output}/{asset.Source.GetFileNameWithoutExtension()}{TextureAsset.FileExtension}")) { Console.WriteLine($"{output}/{asset.Source.GetFileNameWithoutExtension()}{TextureAsset.FileExtension} exists - not writing parent mesh."); break; } AssetFileSerializer.Save($"{output}/{asset.Source.GetFileNameWithoutExtension()}{TextureAsset.FileExtension}", asset, null); Texture tex = GetTexture(asset); SetTexture(materialAsset, tex, TextureType.Diffuse); } // normals if (scene.Materials[i].HasTextureNormal) { string normPath = Path.GetRelativePath(output, Path.Combine(Path.GetDirectoryName(path), scene.Materials[i].TextureNormal.FilePath)); TextureAsset asset = ImportTexture(normPath, TextureType.Normals); if (!overwrite && File.Exists($"{output}/{asset.Source.GetFileNameWithoutExtension()}{TextureAsset.FileExtension}")) { Console.WriteLine($"{output}/{asset.Source.GetFileNameWithoutExtension()}{TextureAsset.FileExtension} exists - not writing parent mesh."); break; } AssetFileSerializer.Save($"{output}/{asset.Source.GetFileNameWithoutExtension()}{TextureAsset.FileExtension}", asset, null); Texture tex = GetTexture(asset); SetTexture(materialAsset, tex, TextureType.Normals); } AssetFileSerializer.Save($"{output}/{scene.Materials[i].Name}{MaterialAsset.FileExtension}", materialAsset, null); Stride.Rendering.Material material = GetMaterial(materialAsset, scene.Materials[i].Name); model.Materials.Add(new ModelMaterial() { Name = scene.Materials[i].Name, MaterialInstance = new MaterialInstance(material) }); materials.Add(scene.Materials[i].Name, material); } AssetFileSerializer.Save($"{output}/{name}{ModelAsset.FileExtension.Split(';')[0]}", model, null); break; default: Console.WriteLine($"The file extension \".{arr[arr.Length - 1]}\" is not supported. ({path})"); break; } }