New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UrlReference attempt number 2 #564
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fa39ae0
[UrlReference] Attempt 2 initial changes.
dfkeenan dfb3c6f
[UrlReference] Fix MemberNode.Update exceptions. And tidy/refactor code.
dfkeenan d06dbb6
[UrlReference] Fix conversion errors for collections in GraphNodes.
dfkeenan a43fa4f
[UrlReference] Improved doc coments on UrlReferenceContentManagerExte…
dfkeenan 2024c19
Add more ContentManager methods to the IContentManager interface and …
dfkeenan 5da1482
[UrlReference] Created IUrlReference interface and refactored related…
dfkeenan f00cc0e
[UrlReference] Update samples/templates to use UrlReference.
dfkeenan 3fabe5f
[Samples] Bumping version
xen2 3a7a54c
Merge branch 'master' into UrlReferenceAlt
xen2 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
sources/assets/Xenko.Core.Assets/Serializers/UrlReferenceSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using Xenko.Core; | ||
using Xenko.Core.Annotations; | ||
using Xenko.Core.IO; | ||
using Xenko.Core.Reflection; | ||
using Xenko.Core.Serialization; | ||
using Xenko.Core.Yaml; | ||
using Xenko.Core.Yaml.Events; | ||
using Xenko.Core.Yaml.Serialization; | ||
|
||
namespace Xenko.Core.Assets.Serializers | ||
{ | ||
/// <summary> | ||
/// A Yaml serializer for <see cref="UrlReference"/> | ||
/// </summary> | ||
[YamlSerializerFactory(YamlAssetProfile.Name)] | ||
internal class UrlReferenceSerializer : AssetScalarSerializerBase | ||
{ | ||
public override bool CanVisit(Type type) | ||
{ | ||
return typeof(UrlReference).IsAssignableFrom(type); | ||
} | ||
|
||
public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar) | ||
{ | ||
if (!AssetReference.TryParse(fromScalar.Value, out var guid, out var location)) | ||
{ | ||
throw new YamlException(fromScalar.Start, fromScalar.End, "Unable to decode url reference [{0}]. Expecting format GUID:LOCATION".ToFormat(fromScalar.Value)); | ||
} | ||
|
||
var urlReference = UrlReferenceHelper.CreateReference(context.Descriptor.Type, guid, location.FullPath); | ||
|
||
return urlReference; | ||
} | ||
|
||
public override string ConvertTo(ref ObjectContext objectContext) | ||
{ | ||
if (objectContext.Instance is UrlReference urlReference) | ||
{ | ||
var attachedReference = AttachedReferenceManager.GetAttachedReference(urlReference); | ||
|
||
if (attachedReference != null) | ||
{ | ||
return $"{attachedReference.Id}:{urlReference.Url}"; | ||
} | ||
} | ||
|
||
throw new YamlException($"Unable to extract url reference from object [{objectContext.Instance}]"); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
sources/core/Xenko.Core.Design/TypeConverters/UrlReferenceConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) | ||
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. | ||
|
||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using Xenko.Core.Serialization; | ||
|
||
namespace Xenko.Core.TypeConverters | ||
{ | ||
/// <summary> | ||
/// Defines a type converter for <see cref="UrlReference"/>. | ||
/// </summary> | ||
public class UrlReferenceConverter : BaseConverter | ||
{ | ||
|
||
public UrlReferenceConverter() | ||
{ | ||
|
||
//var type = typeof(Xenko.Core.Serialization.UrlReference); | ||
//Properties = new PropertyDescriptorCollection(new System.ComponentModel.PropertyDescriptor[] | ||
//{ | ||
// new Reflection.PropertyDescriptor(type.GetProperty(nameof(Xenko.Core.Serialization.UrlReference.Url))), | ||
//}); | ||
} | ||
|
||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | ||
{ | ||
return UrlReferenceHelper.IsUrlReferenceType(TypeConverterHelper.GetDestinationType(context)); | ||
} | ||
|
||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||
{ | ||
var attachedReference = AttachedReferenceManager.GetAttachedReference(value); | ||
var destinationType = TypeConverterHelper.GetDestinationType(context); | ||
return UrlReferenceHelper.CreateReference(destinationType, attachedReference.Id, attachedReference.Url); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ces/core/Xenko.Core.Serialization/Serialization/Serializers/UrlReferenceDataSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xenko.Core.Annotations; | ||
using Xenko.Core.Assets; | ||
|
||
namespace Xenko.Core.Serialization.Serializers | ||
{ | ||
/// <summary> | ||
/// Serializer base class for for <see cref="UrlReference"/>. | ||
/// </summary> | ||
public abstract class UrlReferenceDataSerializerBase<T> : DataSerializer<T> | ||
where T: UrlReference | ||
{ | ||
/// <inheritdoc/> | ||
public override void Serialize(ref T urlReference, ArchiveMode mode, [NotNull] SerializationStream stream) | ||
{ | ||
if (mode == ArchiveMode.Serialize) | ||
{ | ||
var attachedReference = AttachedReferenceManager.GetAttachedReference(urlReference); | ||
if(attachedReference == null) | ||
{ | ||
throw new InvalidOperationException("UrlReference does not have an AttachedReference."); | ||
} | ||
|
||
stream.Write(attachedReference.Id); | ||
stream.Write(attachedReference.Url); | ||
} | ||
else | ||
{ | ||
var id = stream.Read<AssetId>(); | ||
var url = stream.ReadString(); | ||
|
||
urlReference = (T)UrlReferenceHelper.CreateReference(typeof(T), id, url); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Serializer for <see cref="UrlReference"/>. | ||
/// </summary> | ||
public sealed class UrlReferenceDataSerializer : UrlReferenceDataSerializerBase<UrlReference> | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Serializer for <see cref="UrlReference{T}"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type of asset.</typeparam> | ||
public sealed class UrlReferenceDataSerializer<T> : UrlReferenceDataSerializerBase<UrlReference<T>> | ||
where T : class | ||
{ | ||
|
||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
sources/core/Xenko.Core.Serialization/Serialization/UrlReference.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Text; | ||
using Xenko.Core.Annotations; | ||
using Xenko.Core.Assets; | ||
using Xenko.Core.Serialization.Contents; | ||
using Xenko.Core.Serialization.Serializers; | ||
|
||
namespace Xenko.Core.Serialization | ||
{ | ||
/// <summary> | ||
/// Represents a Url to an asset. | ||
/// </summary> | ||
[DataContract("urlref", Inherited = true)] | ||
[DataStyle(DataStyle.Compact)] | ||
[ReferenceSerializer] | ||
[DataSerializer(typeof(UrlReferenceDataSerializer))] | ||
public class UrlReference | ||
{ | ||
private string url; | ||
|
||
public UrlReference() | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Create a new <see cref="UrlReference"/> instance. | ||
/// </summary> | ||
/// <param name="url"></param> | ||
public UrlReference(string url) | ||
{ | ||
if (string.IsNullOrWhiteSpace(url)) | ||
{ | ||
throw new ArgumentException($"{nameof(url)} cannot be null or empty.", nameof(url)); | ||
} | ||
|
||
Url = url; | ||
} | ||
|
||
[DataMember(10)] | ||
public string Url | ||
{ | ||
get => url; | ||
internal set | ||
{ | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
throw new ArgumentNullException(nameof(value)); | ||
} | ||
url = value; | ||
} | ||
} | ||
|
||
[DataMemberIgnore] | ||
public bool IsEmpty => string.IsNullOrEmpty(url); | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() | ||
{ | ||
return $"{Url}"; | ||
} | ||
|
||
|
||
} | ||
|
||
/// <summary> | ||
/// Represents a Url to an asset of type <see cref="T"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type off asset.</typeparam> | ||
[DataStyle(DataStyle.Compact)] | ||
[DataSerializer(typeof(UrlReferenceDataSerializer<>), Mode = DataSerializerGenericMode.GenericArguments)] | ||
public sealed class UrlReference<T> : UrlReference | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same goes for this ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please see reply above. |
||
where T : class | ||
{ | ||
public UrlReference() : base() | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Create a new <see cref="UrlReference{T}"/> instance. | ||
/// </summary> | ||
/// <param name="url"></param> | ||
public UrlReference(string url) : base(url) | ||
{ | ||
} | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if it wouldn't be better as a
struct
?Especially since we also have to check for
IsEmpty
so there is two ways to have invalid values currently (either theUrlReference
instance is not set, orIsEmpty
is true). That might be little bit annoying to tests those two things everytime.This would also remove lot of null check as well.
Not sure if all the converters and editor code will like it, but might be worth a try don't you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AttachedReferenceManager
requiresobjects
because it uses aConditionalWeakTable
to attach references. Ideally theUrlReference
types would not have a default constructor so you could not instantiate "empty" ones. But various parts of the asset stuff and UI require a default constructor for them to work. It would require a lot more changes to accomplish this without default constructors.I have created an interface and a base class so the
UrlReference
andUrlReference<T>
types can both besealed
. I have also tidied up a bunch of things:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point about attached reference, makes sense. Thanks!
Wondering, could we do it the other way around and forbid to use null/empty string then?
i.e. if not set in editor,
UrlReference
instance itself is null. If instance is set, it has to have an actual validUrl
value?This implies
UrlReference
wouldn't have an empty constructor anymore too.Note: if annoying/complex, no need to do that now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did have a couple of solutions that didn't expose the default constructor that worked mostly. For example, make the default constructor
internal
and change how instantiation code works. I had a couple of different versions of this that "worked" but I wasn't too happy with the bits I changed.Though I might have just thought of an option....
But even still, doing it this way meant that using collection properties i.e.
List<UrlReference>
don't work because the UI code cannot instantiate an instance without a default constructor. So would either have to leave this feature out or change the UI code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, let's keep it like this for now, thanks.