public void Upgrade(AssetMigrationContext context, string dependencyName, PackageVersion currentVersion, PackageVersion targetVersion, YamlMappingNode yamlAssetNode, PackageLoadingAssetFile assetFile) { dynamic asset = new DynamicYamlMapping(yamlAssetNode); // upgrade the asset UpgradeAsset(context, currentVersion, targetVersion, asset, assetFile); SetSerializableVersion(asset, dependencyName, targetVersion); // upgrade its base var baseBranch = asset[Asset.BaseProperty]; if (baseBranch != null) { UpgradeBase(context, dependencyName, currentVersion, targetVersion, baseBranch, assetFile); } // upgrade base parts var basePartsBranch = asset[Asset.BasePartsProperty] as DynamicYamlArray; if (basePartsBranch != null) { foreach (dynamic assetBase in basePartsBranch) { UpgradeBase(context, dependencyName, currentVersion, targetVersion, assetBase, assetFile); } } }
public void Upgrade(AssetMigrationContext context, string dependencyName, PackageVersion currentVersion, PackageVersion targetVersion, YamlMappingNode yamlAssetNode, PackageLoadingAssetFile assetFile) { dynamic asset = new DynamicYamlMapping(yamlAssetNode); // upgrade the asset var baseBranch = asset[Asset.BaseProperty]; var basePartsBranch = asset[Asset.BasePartsProperty] as DynamicYamlArray; // Detect in what kind of override context we are var overrideHint = (baseBranch != null || (basePartsBranch != null && basePartsBranch.Node.Children.Count > 0)) ? OverrideUpgraderHint.Derived : OverrideUpgraderHint.Unknown; // Upgrade the asset UpgradeAsset(context, currentVersion, targetVersion, asset, assetFile, overrideHint); SetSerializableVersion(asset, dependencyName, targetVersion); // Upgrade its base if (baseBranch != null) { UpgradeBase(context, dependencyName, currentVersion, targetVersion, baseBranch, assetFile); } // Upgrade base parts if (basePartsBranch != null) { foreach (dynamic assetBase in basePartsBranch) { UpgradeBase(context, dependencyName, currentVersion, targetVersion, assetBase, assetFile); } } }
public void Test() { var logger = new LoggerResult(); var context = new AssetMigrationContext(null, logger); var files = Directory.EnumerateFiles(@"..\..\samples", "*.xkscene", SearchOption.AllDirectories); foreach (var sceneFile in files) { logger.HasErrors = false; logger.Clear(); Console.WriteLine($"Checking file {sceneFile}"); var file = new PackageLoadingAssetFile(sceneFile, null); var needMigration = AssetMigration.MigrateAssetIfNeeded(context, file, "Xenko"); foreach (var message in logger.Messages) { Console.WriteLine(message); } Assert.False(logger.HasErrors); if (needMigration) { var result = Encoding.UTF8.GetString(file.AssetContent); Console.WriteLine(result); // We cannot load the Package here, as the package can use code/scripts that are only available when you actually compile project assmeblies } } }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { // Introduction of MaterialInstance var material = asset.Type.Material; if (material != null) { asset.Type.MaterialInstance = new YamlMappingNode(); asset.Type.MaterialInstance.Material = material; asset.Type.Material = DynamicYamlEmpty.Default; } var type = asset.Type.Node as YamlMappingNode; if (type != null && type.Tag == "!CubeProceduralModel") { // Size changed from scalar to vector3 var size = asset.Type.Size as DynamicYamlScalar; if (size != null) { var vecSize = new YamlMappingNode { { new YamlScalarNode("X"), new YamlScalarNode(size.Node.Value) }, { new YamlScalarNode("Y"), new YamlScalarNode(size.Node.Value) }, { new YamlScalarNode("Z"), new YamlScalarNode(size.Node.Value) } }; vecSize.Style = YamlStyle.Flow; asset.Type.Size = vecSize; } } }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { if (asset.Source != null) asset.Source = DynamicYamlEmpty.Default; if (asset.SourceHash != null) asset.SourceHash = DynamicYamlEmpty.Default; }
public void Upgrade(AssetMigrationContext context, int currentVersion, int targetVersion, YamlMappingNode yamlAssetNode, PackageLoadingAssetFile assetFile) { dynamic asset = new DynamicYamlMapping(yamlAssetNode); var baseBranch = asset["~Base"]; if (baseBranch != null) asset["~Base"] = DynamicYamlEmpty.Default; SetSerializableVersion(asset, targetVersion); }
private void UpgradeBase(AssetMigrationContext context, string dependencyName, PackageVersion currentVersion, PackageVersion targetVersion, dynamic assetBase, PackageLoadingAssetFile assetFile) { var baseAsset = assetBase[nameof(AssetBase.Asset)]; if (baseAsset != null) { UpgradeAsset(context, currentVersion, targetVersion, baseAsset, assetFile); SetSerializableVersion(baseAsset, dependencyName, targetVersion); } }
public void Upgrade(AssetMigrationContext context, string dependencyName, PackageVersion currentVersion, PackageVersion targetVersion, YamlMappingNode yamlAssetNode, PackageLoadingAssetFile assetFile) { dynamic asset = new DynamicYamlMapping(yamlAssetNode); var baseBranch = asset[Asset.BaseProperty]; if (baseBranch != null) asset[BaseProperty] = DynamicYamlEmpty.Default; AssetUpgraderBase.SetSerializableVersion(asset, dependencyName, targetVersion); }
private void UpgradeBase(AssetMigrationContext context, string dependencyName, PackageVersion currentVersion, PackageVersion targetVersion, dynamic assetBase, PackageLoadingAssetFile assetFile) { var baseAsset = assetBase[nameof(AssetBase.Asset)]; if (baseAsset != null) { UpgradeAsset(context, currentVersion, targetVersion, baseAsset, assetFile, OverrideUpgraderHint.Base); SetSerializableVersion(baseAsset, dependencyName, targetVersion); } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { var rootNode = (YamlNode)asset.Node; var allScalarNodes = rootNode.AllNodes.OfType <YamlScalarNode>().ToList(); var nextIsId = false; var inPublicUIElements = false; foreach (var node in allScalarNodes) { var indexFirstSlash = node.Value.IndexOf('/'); Guid targetGuid = Guid.Empty; if (indexFirstSlash == -1) { Guid.TryParseExact(node.Value, "D", out targetGuid); } else { Guid entityGuid; if (Guid.TryParseExact(node.Value.Substring(0, indexFirstSlash), "D", out entityGuid)) { Guid.TryParseExact(node.Value.Substring(indexFirstSlash + 1), "D", out targetGuid); } } if (targetGuid != Guid.Empty && !nextIsId && !inPublicUIElements) { node.Value = "ref!! " + targetGuid; } else { if (nextIsId && targetGuid == Guid.Empty) { nextIsId = false; } if (inPublicUIElements && node.Value == "Hierarchy") { inPublicUIElements = false; } if (node.Value.Contains("Id")) { nextIsId = true; } if (node.Value == "PublicUIElements") { inPublicUIElements = true; } } } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { var rootPartIds = asset.Hierarchy.RootPartIds; int i = 0; foreach (dynamic rootPartId in rootPartIds) { rootPartIds[i++] = "ref!! " + rootPartId.ToString(); } asset.Hierarchy.RootParts = rootPartIds; asset.Hierarchy.RootPartIds = DynamicYamlEmpty.Default; }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { var hierarchy = asset.Hierarchy; var entities = (DynamicYamlArray)hierarchy.Entities; foreach (dynamic entity in entities) { var components = entity.Components; var modelComponent = components["ModelComponent.Key"]; if (modelComponent != null) modelComponent.RemoveChild("DrawOrder"); } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var hierarchy = asset.Hierarchy; var entities = (DynamicYamlArray)hierarchy.Entities; foreach (dynamic entity in entities) { var components = entity.Components; var spriteComponent = components["SpriteComponent.Key"]; if (spriteComponent != null) spriteComponent.RemoveChild("ExtrusionMethod"); } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { foreach (var modelMaterial in asset.Materials) { var material = modelMaterial.Material; if (material != null) { modelMaterial.MaterialInstance = new YamlMappingNode(); modelMaterial.MaterialInstance.Material = material; modelMaterial.Material = DynamicYamlEmpty.Default; } } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { if (asset.IsDynamic != null) { var isDynamic = (bool)asset.IsDynamic; // There is also SDF type, but old assets don't have it yet asset.AddChild("FontType", isDynamic ? "Dynamic" : "Static"); asset.RemoveChild("IsDynamic"); } }
public void Upgrade(AssetMigrationContext context, int currentVersion, int targetVersion, YamlMappingNode yamlAssetNode, PackageLoadingAssetFile assetFile) { dynamic asset = new DynamicYamlMapping(yamlAssetNode); // upgrade the asset UpgradeAsset(context, currentVersion, targetVersion, asset, assetFile); SetSerializableVersion(asset, targetVersion); // upgrade its base var baseBranch = asset["~Base"]; if (baseBranch != null) { var baseAsset = baseBranch["Asset"]; if (baseAsset != null) UpgradeAsset(context, currentVersion, targetVersion, baseAsset, assetFile); SetSerializableVersion(baseAsset, targetVersion); } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { if (asset.ColliderShapes == null) return; foreach (var colliderShape in asset.ColliderShapes) { if (colliderShape.Node.Tag == "!Box2DColliderShapeDesc") { var size = 2f * DynamicYamlExtensions.ConvertTo<Vector2>(colliderShape.HalfExtent); colliderShape.Size = DynamicYamlExtensions.ConvertFrom(size); colliderShape.HalfExtent = DynamicYamlEmpty.Default; } if (colliderShape.Node.Tag == "!BoxColliderShapeDesc") { var size = 2f * DynamicYamlExtensions.ConvertTo<Vector3>(colliderShape.HalfExtents); colliderShape.Size = DynamicYamlExtensions.ConvertFrom(size); colliderShape.HalfExtents = DynamicYamlEmpty.Default; } if (colliderShape.Node.Tag == "!CapsuleColliderShapeDesc" || colliderShape.Node.Tag == "!CylinderColliderShapeDesc") { var upVector = DynamicYamlExtensions.ConvertTo<Vector3>(colliderShape.UpAxis); if (upVector == Vector3.UnitX) colliderShape.Orientation = ShapeOrientation.UpX; if (upVector == Vector3.UnitZ) colliderShape.Orientation = ShapeOrientation.UpZ; colliderShape.UpAxis = DynamicYamlEmpty.Default; } if (colliderShape.Node.Tag == "!CapsuleColliderShapeDesc" && colliderShape.Height != null) { colliderShape.Length = 2f * (float)colliderShape.Height; colliderShape.Height = DynamicYamlEmpty.Default; } if (colliderShape.Node.Tag == "!CylinderColliderShapeDesc") { colliderShape.Radius = (float)colliderShape.HalfExtents.X; colliderShape.Height = 2f * (float)colliderShape.HalfExtents.Y; colliderShape.HalfExtents = DynamicYamlEmpty.Default; } } }
public void Upgrade(AssetMigrationContext context, string dependencyName, PackageVersion currentVersion, PackageVersion targetVersion, YamlMappingNode yamlAssetNode, PackageLoadingAssetFile assetFile) { dynamic asset = new DynamicYamlMapping(yamlAssetNode); // upgrade the asset UpgradeAsset(context, currentVersion, targetVersion, asset, assetFile); SetSerializableVersion(asset, dependencyName, targetVersion); // upgrade its base var baseBranch = asset["~Base"]; if (baseBranch != null) { var baseAsset = baseBranch["Asset"]; if (baseAsset != null) { UpgradeAsset(context, currentVersion, targetVersion, baseAsset, assetFile); SetSerializableVersion(baseAsset, dependencyName, targetVersion); } } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { var hierarchy = asset.Hierarchy; var entities = (DynamicYamlArray)hierarchy.Entities; foreach (dynamic entity in entities) { var components = entity.Components; var spriteComponent = components["SpriteComponent.Key"]; if (spriteComponent != null) { var provider = spriteComponent.SpriteProvider; var providerAsMap = provider as DynamicYamlMapping; if (providerAsMap != null && providerAsMap.Node.Tag == "!SpriteFromSpriteGroup") { provider.Sheet = provider.SpriteGroup; provider.SpriteGroup = DynamicYamlEmpty.Default; providerAsMap.Node.Tag = "!SpriteFromSheet"; } } } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { var hierarchy = asset.Hierarchy; var entities = hierarchy.Entities; foreach (var entityAndDesign in entities) { var entity = entityAndDesign.Entity; foreach (var component in entity.Components) { var componentTag = component.Node.Tag; if (componentTag != "!SpriteComponent") continue; var provider = component.SpriteProvider; if (provider == null || provider.Node.Tag != "!SpriteFromSheet") continue; component.TransferChild("CurrentFrame", provider, "CurrentFrame"); } } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { var hierarchy = asset.Hierarchy; var entities = hierarchy.Entities; foreach (var entityAndDesign in entities) { var entity = entityAndDesign.Entity; foreach (var component in entity.Components) { var componentTag = component.Node.Tag; if (componentTag != "!UIComponent") continue; // VirtualResolution component.RenameChild("VirtualResolution", "Resolution"); // VirtualResolutionMode component.RenameChild("VirtualResolutionMode", "ResolutionStretch"); } } }
/// <inheritdoc/> protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile, OverrideUpgraderHint overrideHint) { }
protected abstract void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile);
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var proceduralType = asset.Type; if (proceduralType.Node.Tag == "!CapsuleProceduralModel" && proceduralType.Height != null) { proceduralType.Length = 2f * (float)proceduralType.Height; proceduralType.Height = DynamicYamlEmpty.Default; } }
public static bool MigrateAssetIfNeeded(AssetMigrationContext context, PackageLoadingAssetFile loadAsset) { var assetFullPath = loadAsset.FilePath.FullPath; // Determine if asset was Yaml or not var assetFileExtension = Path.GetExtension(assetFullPath); if (assetFileExtension == null) { return(false); } assetFileExtension = assetFileExtension.ToLowerInvariant(); var serializer = AssetSerializer.FindSerializer(assetFileExtension); if (!(serializer is AssetYamlSerializer)) { return(false); } // We've got a Yaml asset, let's get expected and serialized versions var serializedVersion = 0; int expectedVersion; Type assetType; // Read from Yaml file the asset version and its type (to get expected version) // Note: It tries to read as few as possible (SerializedVersion is expected to be right after Id, so it shouldn't try to read further than that) using (var assetStream = loadAsset.OpenStream()) using (var streamReader = new StreamReader(assetStream)) { var yamlEventReader = new EventReader(new Parser(streamReader)); // Skip header yamlEventReader.Expect <StreamStart>(); yamlEventReader.Expect <DocumentStart>(); var mappingStart = yamlEventReader.Expect <MappingStart>(); var yamlSerializerSettings = YamlSerializer.GetSerializerSettings(); var tagTypeRegistry = yamlSerializerSettings.TagTypeRegistry; bool typeAliased; assetType = tagTypeRegistry.TypeFromTag(mappingStart.Tag, out typeAliased); expectedVersion = AssetRegistry.GetCurrentFormatVersion(assetType); Scalar assetKey; while ((assetKey = yamlEventReader.Allow <Scalar>()) != null) { // Only allow Id before SerializedVersion if (assetKey.Value == "Id") { yamlEventReader.Skip(); continue; } if (assetKey.Value == "SerializedVersion") { serializedVersion = Convert.ToInt32(yamlEventReader.Expect <Scalar>().Value, CultureInfo.InvariantCulture); break; } } } if (serializedVersion > expectedVersion) { // Try to open an asset newer than what we support (probably generated by a newer Paradox) throw new InvalidOperationException(string.Format("Asset of type {0} has been serialized with newer version {1}, but only version {2} is supported. Was this asset created with a newer version of Paradox?", assetType, serializedVersion, expectedVersion)); } if (serializedVersion < expectedVersion) { // Perform asset upgrade context.Log.Verbose("{0} needs update, from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion); // transform the stream into string. string assetAsString; using (var assetStream = loadAsset.OpenStream()) using (var assetStreamReader = new StreamReader(assetStream, Encoding.UTF8)) { assetAsString = assetStreamReader.ReadToEnd(); } // Load the asset as a YamlNode object var input = new StringReader(assetAsString); var yamlStream = new YamlStream(); yamlStream.Load(input); var yamlRootNode = (YamlMappingNode)yamlStream.Documents[0].RootNode; // Check if there is any asset updater var assetUpgraders = AssetRegistry.GetAssetUpgraders(assetType); if (assetUpgraders == null) { throw new InvalidOperationException(string.Format("Asset of type {0} should be updated from version {1} to {2}, but no asset migration path was found", assetType, serializedVersion, expectedVersion)); } // Instantiate asset updaters var currentVersion = serializedVersion; while (currentVersion != expectedVersion) { int targetVersion; // This will throw an exception if no upgrader is available for the given version, exiting the loop in case of error. var upgrader = assetUpgraders.GetUpgrader(currentVersion, out targetVersion); upgrader.Upgrade(context, currentVersion, targetVersion, yamlRootNode, loadAsset); currentVersion = targetVersion; } // Make sure asset is updated to latest version YamlNode serializedVersionNode; var newSerializedVersion = 0; if (yamlRootNode.Children.TryGetValue(new YamlScalarNode("SerializedVersion"), out serializedVersionNode)) { newSerializedVersion = Convert.ToInt32(((YamlScalarNode)serializedVersionNode).Value); } if (newSerializedVersion != expectedVersion) { throw new InvalidOperationException(string.Format("Asset of type {0} was migrated, but still its new version {1} doesn't match expected version {2}.", assetType, newSerializedVersion, expectedVersion)); } context.Log.Info("{0} updated from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion); var preferredIndent = YamlSerializer.GetSerializerSettings().PreferredIndent; // Save asset back to disk using (var memoryStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(memoryStream)) { yamlStream.Save(streamWriter, true, preferredIndent); } loadAsset.AssetContent = memoryStream.ToArray(); } return(true); } return(false); }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var proceduralType = asset.Type; if (proceduralType.ScaleUV != null) { var currentScale = (float)proceduralType.ScaleUV; var vecSize = new YamlMappingNode { { new YamlScalarNode("X"), new YamlScalarNode(currentScale.ToString(CultureInfo.InvariantCulture)) }, { new YamlScalarNode("Y"), new YamlScalarNode(currentScale.ToString(CultureInfo.InvariantCulture)) } }; vecSize.Style = YamlStyle.Flow; proceduralType.RemoveChild("ScaleUV"); proceduralType.UvScale = vecSize; } else if (proceduralType.UVScales != null) { var x = (float)proceduralType.UVScales.X; var y = (float)proceduralType.UVScales.Y; var vecSize = new YamlMappingNode { { new YamlScalarNode("X"), new YamlScalarNode(x.ToString(CultureInfo.InvariantCulture)) }, { new YamlScalarNode("Y"), new YamlScalarNode(y.ToString(CultureInfo.InvariantCulture)) } }; vecSize.Style = YamlStyle.Flow; proceduralType.RemoveChild("UVScales"); proceduralType.UvScale = vecSize; } else { var vecSize = new YamlMappingNode { { new YamlScalarNode("X"), new YamlScalarNode("1") }, { new YamlScalarNode("Y"), new YamlScalarNode("1") } }; vecSize.Style = YamlStyle.Flow; proceduralType.UvScale = vecSize; } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { // SerializedVersion format changed during renaming upgrade. However, before this was merged back in master, some asset upgrader still with older version numbers were developed. // However since this upgrader can be reapplied, it is not a problem var proceduralType = asset.Type; if (proceduralType.Node.Tag == "!CapsuleProceduralModel" && currentVersion != PackageVersion.Parse("0.0.6")) { if (proceduralType.Radius == null) { proceduralType.Radius = 0.25f; } } }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var hierarchy = asset.Hierarchy; // Remove from all layers/renderers var layers = (DynamicYamlArray)hierarchy?.SceneSettings?.GraphicsCompositor?.Layers; if (layers != null) { foreach (dynamic layer in layers) { ProcessRenderers((DynamicYamlArray)layer.Renderers); } } var masterRenderers = (DynamicYamlArray)hierarchy?.SceneSettings?.GraphicsCompositor?.Master?.Renderers; ProcessRenderers(masterRenderers); // Remove from editor settings var colorTransforms = hierarchy?.SceneSettings?.EditorSettings?.Mode?.PostProcessingEffects?.ColorTransforms; if (colorTransforms != null) { colorTransforms.RemoveChild("GammaTransform"); // Because the color was stored in linear, we need to store it back to gamma // We also apply a x2 to the color to var color = hierarchy.SceneSettings.EditorSettings.Mode.BackgroundColor; if (color != null) { color["R"] = MathUtil.Clamp(MathUtil.LinearToSRgb((float)color["R"]) * 2.0f, 0.0f, 1.0f); color["G"] = MathUtil.Clamp(MathUtil.LinearToSRgb((float)color["G"]) * 2.0f, 0.0f, 1.0f); color["B"] = MathUtil.Clamp(MathUtil.LinearToSRgb((float)color["B"]) * 2.0f, 0.0f, 1.0f); } } }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var hierarchy = asset.Hierarchy; var entities = (DynamicYamlArray)hierarchy.Entities; foreach (dynamic entity in entities) { var components = entity.Components; var lightComponent = components["LightComponent.Key"]; if (lightComponent != null) { var lightType = lightComponent.Type; if (lightType != null) { var shadow = lightType.Shadow; if (shadow != null) { var size = (OldLightShadowMapSize)(shadow.Size ?? OldLightShadowMapSize.Small); var importance = (OldLightShadowImportance)(shadow.Importance ?? OldLightShadowImportance.Low); // Convert back the old size * importance to the new size var factor = importance == OldLightShadowImportance.High ? 2.0 : importance == OldLightShadowImportance.Medium ? 1.0 : 0.5; factor *= Math.Pow(2.0, (int)size - 2.0); var value = ((int)Math.Log(factor, 2.0)) + 3; var newSize = (LightShadowMapSize)Enum.ToObject(typeof(LightShadowMapSize), value); shadow.Size = newSize; shadow.RemoveChild("Importance"); } } } } }
public static bool MigrateAssetIfNeeded(AssetMigrationContext context, PackageLoadingAssetFile loadAsset, string dependencyName, PackageVersion untilVersion = null) { var assetFullPath = loadAsset.FilePath.FullPath; // Determine if asset was Yaml or not var assetFileExtension = Path.GetExtension(assetFullPath); if (assetFileExtension == null) { return(false); } assetFileExtension = assetFileExtension.ToLowerInvariant(); var serializer = AssetSerializer.FindSerializer(assetFileExtension); if (!(serializer is AssetYamlSerializer)) { return(false); } // We've got a Yaml asset, let's get expected and serialized versions var serializedVersion = PackageVersion.Zero; PackageVersion expectedVersion; Type assetType; // Read from Yaml file the asset version and its type (to get expected version) // Note: It tries to read as few as possible (SerializedVersion is expected to be right after Id, so it shouldn't try to read further than that) using (var assetStream = loadAsset.OpenStream()) using (var streamReader = new StreamReader(assetStream)) { var yamlEventReader = new EventReader(new Parser(streamReader)); // Skip header yamlEventReader.Expect <StreamStart>(); yamlEventReader.Expect <DocumentStart>(); var mappingStart = yamlEventReader.Expect <MappingStart>(); var yamlSerializerSettings = YamlSerializer.GetSerializerSettings(); var tagTypeRegistry = yamlSerializerSettings.TagTypeRegistry; bool typeAliased; assetType = tagTypeRegistry.TypeFromTag(mappingStart.Tag, out typeAliased); var expectedVersions = AssetRegistry.GetCurrentFormatVersions(assetType); expectedVersion = expectedVersions?.FirstOrDefault(x => x.Key == dependencyName).Value ?? PackageVersion.Zero; Scalar assetKey; while ((assetKey = yamlEventReader.Allow <Scalar>()) != null) { // Only allow Id before SerializedVersion if (assetKey.Value == nameof(Asset.Id)) { yamlEventReader.Skip(); } else if (assetKey.Value == nameof(Asset.SerializedVersion)) { // Check for old format: only a scalar var scalarVersion = yamlEventReader.Allow <Scalar>(); if (scalarVersion != null) { serializedVersion = PackageVersion.Parse("0.0." + Convert.ToInt32(scalarVersion.Value, CultureInfo.InvariantCulture)); // Let's update to new format using (var yamlAsset = loadAsset.AsYamlAsset()) { yamlAsset.DynamicRootNode.RemoveChild(nameof(Asset.SerializedVersion)); AssetUpgraderBase.SetSerializableVersion(yamlAsset.DynamicRootNode, dependencyName, serializedVersion); var baseBranch = yamlAsset.DynamicRootNode[Asset.BaseProperty]; if (baseBranch != null) { var baseAsset = baseBranch["Asset"]; if (baseAsset != null) { baseAsset.RemoveChild(nameof(Asset.SerializedVersion)); AssetUpgraderBase.SetSerializableVersion(baseAsset, dependencyName, serializedVersion); } } } } else { // New format: package => version mapping yamlEventReader.Expect <MappingStart>(); while (!yamlEventReader.Accept <MappingEnd>()) { var packageName = yamlEventReader.Expect <Scalar>().Value; var packageVersion = PackageVersion.Parse(yamlEventReader.Expect <Scalar>().Value); // For now, we handle only one dependency at a time if (packageName == dependencyName) { serializedVersion = packageVersion; } } yamlEventReader.Expect <MappingEnd>(); } break; } else { // If anything else than Id or SerializedVersion, let's stop break; } } } if (serializedVersion > expectedVersion) { // Try to open an asset newer than what we support (probably generated by a newer Xenko) throw new InvalidOperationException($"Asset of type {assetType} has been serialized with newer version {serializedVersion}, but only version {expectedVersion} is supported. Was this asset created with a newer version of Xenko?"); } if (serializedVersion < expectedVersion) { // Perform asset upgrade context.Log.Verbose("{0} needs update, from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion); using (var yamlAsset = loadAsset.AsYamlAsset()) { var yamlRootNode = yamlAsset.RootNode; // Check if there is any asset updater var assetUpgraders = AssetRegistry.GetAssetUpgraders(assetType, dependencyName); if (assetUpgraders == null) { throw new InvalidOperationException($"Asset of type {assetType} should be updated from version {serializedVersion} to {expectedVersion}, but no asset migration path was found"); } // Instantiate asset updaters var currentVersion = serializedVersion; while (currentVersion != expectedVersion) { PackageVersion targetVersion; // This will throw an exception if no upgrader is available for the given version, exiting the loop in case of error. var upgrader = assetUpgraders.GetUpgrader(currentVersion, out targetVersion); // Stop if the next version would be higher than what is expected if (untilVersion != null && targetVersion > untilVersion) { break; } upgrader.Upgrade(context, dependencyName, currentVersion, targetVersion, yamlRootNode, loadAsset); currentVersion = targetVersion; } // Make sure asset is updated to latest version YamlNode serializedVersionNode; PackageVersion newSerializedVersion = null; if (yamlRootNode.Children.TryGetValue(new YamlScalarNode(nameof(Asset.SerializedVersion)), out serializedVersionNode)) { var newSerializedVersionForDefaultPackage = ((YamlMappingNode)serializedVersionNode).Children[new YamlScalarNode(dependencyName)]; newSerializedVersion = PackageVersion.Parse(((YamlScalarNode)newSerializedVersionForDefaultPackage).Value); } if (untilVersion == null && newSerializedVersion != expectedVersion) { throw new InvalidOperationException($"Asset of type {assetType} was migrated, but still its new version {newSerializedVersion} doesn't match expected version {expectedVersion}."); } context.Log.Info("{0} updated from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion); } return(true); } return(false); }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { asset.Hierarchy.SceneSettings.EditorSettings.Camera = DynamicYamlEmpty.Default; }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var hierarchy = asset.Hierarchy; var entities = (DynamicYamlArray)hierarchy.Entities; foreach (dynamic entity in entities) { var components = entity.Entity.Components; var physComponent = components["PhysicsComponent.Key"]; if (physComponent == null) continue; foreach (dynamic element in physComponent.Elements) { if (element.Node.Tag == "!RigidbodyElement") { element.NodeName = element.LinkedBoneName; element.RemoveChild("LinkedBoneName"); } } } }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var entities = asset.Hierarchy.Entities; var designEntities = new YamlSequenceNode(); asset.Hierarchy.Entities = designEntities; foreach (var entity in entities) { var designEntity = new YamlMappingNode(); dynamic dynamicDesignEntity = new DynamicYamlMapping(designEntity); dynamicDesignEntity.Entity = entity; designEntities.Add(designEntity); } }
public static bool MigrateAssetIfNeeded(AssetMigrationContext context, PackageLoadingAssetFile loadAsset) { var assetFullPath = loadAsset.FilePath.FullPath; // Determine if asset was Yaml or not var assetFileExtension = Path.GetExtension(assetFullPath); if (assetFileExtension == null) return false; assetFileExtension = assetFileExtension.ToLowerInvariant(); var serializer = AssetSerializer.FindSerializer(assetFileExtension); if (!(serializer is AssetYamlSerializer)) return false; // We've got a Yaml asset, let's get expected and serialized versions var serializedVersion = 0; int expectedVersion; Type assetType; // Read from Yaml file the asset version and its type (to get expected version) // Note: It tries to read as few as possible (SerializedVersion is expected to be right after Id, so it shouldn't try to read further than that) using (var assetStream = loadAsset.OpenStream()) using (var streamReader = new StreamReader(assetStream)) { var yamlEventReader = new EventReader(new Parser(streamReader)); // Skip header yamlEventReader.Expect<StreamStart>(); yamlEventReader.Expect<DocumentStart>(); var mappingStart = yamlEventReader.Expect<MappingStart>(); var yamlSerializerSettings = YamlSerializer.GetSerializerSettings(); var tagTypeRegistry = yamlSerializerSettings.TagTypeRegistry; bool typeAliased; assetType = tagTypeRegistry.TypeFromTag(mappingStart.Tag, out typeAliased); expectedVersion = AssetRegistry.GetCurrentFormatVersion(assetType); Scalar assetKey; while ((assetKey = yamlEventReader.Allow<Scalar>()) != null) { // Only allow Id before SerializedVersion if (assetKey.Value == "Id") { yamlEventReader.Skip(); continue; } if (assetKey.Value == "SerializedVersion") { serializedVersion = Convert.ToInt32(yamlEventReader.Expect<Scalar>().Value, CultureInfo.InvariantCulture); break; } } } if (serializedVersion > expectedVersion) { // Try to open an asset newer than what we support (probably generated by a newer Paradox) throw new InvalidOperationException(string.Format("Asset of type {0} has been serialized with newer version {1}, but only version {2} is supported. Was this asset created with a newer version of Paradox?", assetType, serializedVersion, expectedVersion)); } if (serializedVersion < expectedVersion) { // Perform asset upgrade context.Log.Verbose("{0} needs update, from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion); // transform the stream into string. string assetAsString; using (var assetStream = loadAsset.OpenStream()) using (var assetStreamReader = new StreamReader(assetStream, Encoding.UTF8)) { assetAsString = assetStreamReader.ReadToEnd(); } // Load the asset as a YamlNode object var input = new StringReader(assetAsString); var yamlStream = new YamlStream(); yamlStream.Load(input); var yamlRootNode = (YamlMappingNode)yamlStream.Documents[0].RootNode; // Check if there is any asset updater var assetUpgraders = AssetRegistry.GetAssetUpgraders(assetType); if (assetUpgraders == null) { throw new InvalidOperationException(string.Format("Asset of type {0} should be updated from version {1} to {2}, but no asset migration path was found", assetType, serializedVersion, expectedVersion)); } // Instantiate asset updaters var currentVersion = serializedVersion; while (currentVersion != expectedVersion) { int targetVersion; // This will throw an exception if no upgrader is available for the given version, exiting the loop in case of error. var upgrader = assetUpgraders.GetUpgrader(currentVersion, out targetVersion); upgrader.Upgrade(context, currentVersion, targetVersion, yamlRootNode, loadAsset); currentVersion = targetVersion; } // Make sure asset is updated to latest version YamlNode serializedVersionNode; var newSerializedVersion = 0; if (yamlRootNode.Children.TryGetValue(new YamlScalarNode("SerializedVersion"), out serializedVersionNode)) { newSerializedVersion = Convert.ToInt32(((YamlScalarNode)serializedVersionNode).Value); } if (newSerializedVersion != expectedVersion) { throw new InvalidOperationException(string.Format("Asset of type {0} was migrated, but still its new version {1} doesn't match expected version {2}.", assetType, newSerializedVersion, expectedVersion)); } context.Log.Info("{0} updated from version {1} to version {2}", Path.GetFullPath(assetFullPath), serializedVersion, expectedVersion); var preferredIndent = YamlSerializer.GetSerializerSettings().PreferredIndent; // Save asset back to disk using (var memoryStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(memoryStream)) { yamlStream.Save(streamWriter, true, preferredIndent); } loadAsset.AssetContent = memoryStream.ToArray(); } return true; } return false; }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var proceduralType = asset.Type; if (proceduralType.Diameter != null) { proceduralType.Radius = 0.5f * (float)proceduralType.Diameter; proceduralType.Diameter = DynamicYamlEmpty.Default; } if (proceduralType.Node.Tag == "!TorusProceduralModel" && proceduralType.Thickness != null) { proceduralType.Thickness = 0.5f * (float)proceduralType.Thickness; } }
protected override void UpgradeAsset(AssetMigrationContext context, PackageVersion currentVersion, PackageVersion targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { if (asset.NoPremultiply != null) { asset.IsPremultiplied = !(bool)asset.NoPremultiply; asset.NoPremultiply = DynamicYamlEmpty.Default; } if (asset.IsNotPremultiply != null) { asset.IsPremultiplied = !(bool)asset.IsNotPremultiply; asset.IsNotPremultiply = DynamicYamlEmpty.Default; } }
protected override void UpgradeAsset(AssetMigrationContext context, int currentVersion, int targetVersion, dynamic asset, PackageLoadingAssetFile assetFile) { var hierarchy = asset.Hierarchy; var entities = (DynamicYamlArray)hierarchy.Entities; foreach (dynamic entity in entities) { var components = entity.Components; var physComponent = components["PhysicsComponent.Key"]; if (physComponent != null) { foreach (dynamic element in physComponent.Elements) { var type = element.Type.Node.Value; if (type == "PhantomCollider") { element.Node.Tag = "!TriggerElement"; element.RemoveChild("StepHeight"); } else if (type == "StaticCollider") { element.Node.Tag = "!StaticColliderElement"; element.RemoveChild("StepHeight"); } else if (type == "StaticRigidBody") { element.Node.Tag = "!StaticRigidbodyElement"; element.RemoveChild("StepHeight"); } else if (type == "DynamicRigidBody") { element.Node.Tag = "!DynamicRigidbodyElement"; element.RemoveChild("StepHeight"); } else if (type == "KinematicRigidBody") { element.Node.Tag = "!KinematicRigidbodyElement"; element.RemoveChild("StepHeight"); } else if (type == "CharacterController") { element.Node.Tag = "!CharacterElement"; } element.RemoveChild("Type"); } } } }