本文整理汇总了C#中EventReader.Skip方法的典型用法代码示例。如果您正苦于以下问题:C# EventReader.Skip方法的具体用法?C# EventReader.Skip怎么用?C# EventReader.Skip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EventReader
的用法示例。
在下文中一共展示了EventReader.Skip方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: NodeIsNull
bool INodeDeserializer.Deserialize(EventReader reader, Type expectedType, Func<EventReader, Type, object> nestedObjectDeserializer, out object value)
{
value = null;
var evt = reader.Peek<NodeEvent>();
var isNull = evt != null
&& NodeIsNull(evt);
if (isNull)
{
reader.Skip();
}
return isNull;
}
示例2: MigrateAssetIfNeeded
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
//.........这里部分代码省略.........
示例3: MigrateAssetIfNeeded
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 = AssetFileSerializer.FindSerializer(assetFileExtension);
if (!(serializer is YamlAssetSerializer))
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 tagTypeRegistry = AssetYamlSerializer.Default.GetSerializerSettings().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["~Base"];
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;
}
}
//.........这里部分代码省略.........