-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Nexus-Mods:main' into dev
- Loading branch information
Showing
89 changed files
with
2,871 additions
and
823 deletions.
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
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
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
18 changes: 18 additions & 0 deletions
18
src/Abstractions/NexusMods.Abstractions.NexusModsLibrary/Attributes/FloatAttribute.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,18 @@ | ||
using NexusMods.MnemonicDB.Abstractions; | ||
using NexusMods.MnemonicDB.Abstractions.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.ElementComparers; | ||
|
||
namespace NexusMods.Abstractions.NexusModsLibrary.Attributes; | ||
|
||
public class FloatAttribute(string ns, string name) : ScalarAttribute<float, float>(ValueTags.Float32, ns, name) | ||
{ | ||
protected override float ToLowLevel(float value) | ||
{ | ||
return value; | ||
} | ||
|
||
protected override float FromLowLevel(float value, ValueTags tags, RegistryId registryId) | ||
{ | ||
return value; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Abstractions/NexusMods.Abstractions.NexusModsLibrary/Attributes/MemoryAttribute.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,24 @@ | ||
using System.Buffers; | ||
using NexusMods.MnemonicDB.Abstractions; | ||
using NexusMods.MnemonicDB.Abstractions.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.ElementComparers; | ||
|
||
namespace NexusMods.Abstractions.NexusModsLibrary.Attributes; | ||
|
||
/// <summary> | ||
/// A hashed blob attribute for <see cref="Memory{T}"/>. | ||
/// </summary> | ||
public class MemoryAttribute(string ns, string name) : HashedBlobAttribute<Memory<byte>>(ns, name) | ||
{ | ||
/// <inheritdoc /> | ||
protected override Memory<byte> FromLowLevel(ReadOnlySpan<byte> value, ValueTags tags, RegistryId registryId) | ||
{ | ||
return new Memory<byte>(value.ToArray()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void WriteValue<TWriter>(Memory<byte> value, TWriter writer) | ||
{ | ||
writer.Write(value.Span); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Abstractions/NexusMods.Abstractions.NexusModsLibrary/CollectionTag.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,23 @@ | ||
using NexusMods.Abstractions.MnemonicDB.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.Models; | ||
|
||
namespace NexusMods.Abstractions.NexusModsLibrary; | ||
|
||
/// <summary> | ||
/// A tag for a collection. | ||
/// </summary> | ||
public partial class CollectionTag : IModelDefinition | ||
{ | ||
private const string Namespace = "NexusMods.Abstractions.NexusModsLibrary.CollectionTag"; | ||
|
||
/// <summary> | ||
/// The name of the collection tag. | ||
/// </summary> | ||
public static readonly StringAttribute Name = new(Namespace, nameof(Name)) { IsIndexed = true }; | ||
|
||
/// <summary> | ||
/// The Nexus mods id of the collection tag. | ||
/// </summary> | ||
public static readonly ULongAttribute NexusId = new(Namespace, nameof(NexusId)) { IsIndexed = true }; | ||
} |
90 changes: 90 additions & 0 deletions
90
src/Abstractions/NexusMods.Abstractions.NexusModsLibrary/GraphQLResolver.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,90 @@ | ||
using NexusMods.MnemonicDB.Abstractions; | ||
using NexusMods.MnemonicDB.Abstractions.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.Models; | ||
using Splat.ModeDetection; | ||
|
||
namespace NexusMods.Abstractions.NexusModsLibrary; | ||
|
||
/// <summary> | ||
/// A helper for upserting entities in the database. When created, you must define a "pimary key" attribute and value, | ||
/// these are used to determine if the entity already exists in the database. If it does, the existing entity is updated, | ||
/// otherwise a new entity is created. | ||
/// | ||
/// For each attribute you want to add to the entity, call the Add method with the attribute and value and any duplicate values | ||
/// will not be added. | ||
/// </summary> | ||
// ReSharper disable once InconsistentNaming | ||
public readonly struct GraphQLResolver(ITransaction Tx, ReadOnlyModel Model) | ||
{ | ||
/// <summary> | ||
/// Create a new resolver using the given primary key attribute and value. | ||
/// </summary> | ||
public static GraphQLResolver Create<THighLevel, TLowLevel>(IDb referenceDb, ITransaction tx, ScalarAttribute<THighLevel, TLowLevel> primaryKeyAttribute, THighLevel primaryKeyValue) where THighLevel : notnull | ||
{ | ||
var existing = referenceDb.Datoms(primaryKeyAttribute, primaryKeyValue); | ||
var exists = existing.Count > 0; | ||
var id = existing.Count == 0 ? tx.TempId() : existing[0].E; | ||
if (!exists) | ||
tx.Add(id, primaryKeyAttribute, primaryKeyValue); | ||
return new GraphQLResolver(tx, new ReadOnlyModel(referenceDb, id)); | ||
} | ||
|
||
/// <summary> | ||
/// The id of the entity, may be temporary if this is a new entity. | ||
/// </summary> | ||
public EntityId Id => Model.Id; | ||
|
||
/// <summary> | ||
/// Add a value to the entity. If the value already exists, it will not be added again. | ||
/// </summary> | ||
public void Add<THighLevel, TLowLevel>(ScalarAttribute<THighLevel, TLowLevel> attribute, THighLevel value) | ||
where THighLevel : notnull | ||
{ | ||
if (attribute.TryGet(Model, out var foundValue)) | ||
{ | ||
// Deduplicate values | ||
if (foundValue.Equals(value)) | ||
return; | ||
} | ||
// Else add the value | ||
Tx.Add(Model.Id, attribute, value); | ||
} | ||
|
||
/// <summary> | ||
/// Add a value to the entity. If the value already exists, it will not be added again. | ||
/// </summary> | ||
public void Add<TOther>(ReferencesAttribute<TOther> attribute, EntityId id) | ||
where TOther : IModelDefinition | ||
{ | ||
if (PartitionId.Temp == id.Partition) | ||
{ | ||
Tx.Add(Model.Id, attribute, id); | ||
return; | ||
} | ||
|
||
if (attribute.Get(Model).Contains(id)) | ||
return; | ||
|
||
// Else add the value | ||
Tx.Add(Model.Id, attribute, id); | ||
} | ||
|
||
/// <summary> | ||
/// Add a value to the entity. If the value already exists, it will not be added again. | ||
/// </summary> | ||
public void Add<TOther>(ReferenceAttribute<TOther> attribute, EntityId id) | ||
where TOther : IModelDefinition | ||
{ | ||
if (PartitionId.Temp == id.Partition) | ||
{ | ||
Tx.Add(Model.Id, attribute, id); | ||
return; | ||
} | ||
|
||
if (attribute.Get(Model).Equals(id)) | ||
return; | ||
|
||
// Else add the value | ||
Tx.Add(Model.Id, attribute, id); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Abstractions/NexusMods.Abstractions.NexusModsLibrary/Models/CollectionMetadata.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,54 @@ | ||
using NexusMods.Abstractions.MnemonicDB.Attributes; | ||
using NexusMods.Abstractions.NexusModsLibrary.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.Attributes; | ||
using NexusMods.MnemonicDB.Abstractions.Models; | ||
|
||
namespace NexusMods.Abstractions.NexusModsLibrary.Models; | ||
|
||
/// <summary> | ||
/// Metadata about a collection on Nexus Mods. | ||
/// </summary> | ||
public partial class CollectionMetadata : IModelDefinition | ||
{ | ||
private const string Namespace = "NexusMods.Library.NexusModsCollectionMetadata"; | ||
|
||
/// <summary> | ||
/// The collection slug. | ||
/// </summary> | ||
public static readonly CollectionsSlugAttribute Slug = new(Namespace, nameof(Slug)) { IsIndexed = true }; | ||
|
||
/// <summary> | ||
/// The name of the collection. | ||
/// </summary> | ||
public static readonly StringAttribute Name = new(Namespace, nameof(Name)); | ||
|
||
/// <summary> | ||
/// The short description of the collection | ||
/// </summary> | ||
public static readonly StringAttribute Summary = new(Namespace, nameof(Summary)); | ||
|
||
/// <summary> | ||
/// The Curating user of the collection. | ||
/// </summary> | ||
public static readonly ReferenceAttribute<User> Author = new(Namespace, nameof(Author)); | ||
|
||
/// <summary> | ||
/// The revisions of the collection. | ||
/// </summary> | ||
public static readonly BackReferenceAttribute<CollectionRevisionMetadata> Revisions = new(CollectionRevisionMetadata.Collection); | ||
|
||
/// <summary> | ||
/// The tags on the collection. | ||
/// </summary> | ||
public static readonly ReferencesAttribute<CollectionTag> Tags = new(Namespace, nameof(Tags)); | ||
|
||
/// <summary> | ||
/// The number of endorsements the collection has. | ||
/// </summary> | ||
public static readonly ULongAttribute Endorsements = new(Namespace, nameof(Endorsements)); | ||
|
||
/// <summary> | ||
/// The collections' image. | ||
/// </summary> | ||
public static readonly MemoryAttribute TileImage = new(Namespace, nameof(TileImage)); | ||
} |
Oops, something went wrong.