Skip to content

Commit

Permalink
Base Interface definition
Browse files Browse the repository at this point in the history
  • Loading branch information
gruenwaldlk committed Oct 4, 2024
1 parent b52298a commit ca448a5
Show file tree
Hide file tree
Showing 14 changed files with 410 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .idea/.idea.PetroglyphTools/.idea/projectSettingsUpdater.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.PetroglyphTools/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions PG.Commons/PG.Commons/Data/DataObjectMapperBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;

namespace PG.Commons.Data;

/// <summary>
/// Container class allowing to map from a data objct to a peer and vice versa
/// </summary>
/// <typeparam name="TDataObject"></typeparam>
/// <typeparam name="TPeer"></typeparam>
public abstract class DataObjectMapperBase<TDataObject, TPeer>
{
private readonly DataObjectMappings<TDataObject, TPeer> _mappings;

/// <summary>
/// .ctor
/// </summary>
protected DataObjectMapperBase()
{
var mappings = new DataObjectMappings<TDataObject, TPeer>();
// ReSharper disable once VirtualMemberCallInConstructor
InitMappings(mappings);
_mappings = mappings;
}

/// <summary>
/// Method is overridden by subclasses to initialize the mappings.
/// </summary>
/// <param name="mappings"></param>
protected abstract void InitMappings(DataObjectMappings<TDataObject, TPeer> mappings);


/// <summary>
/// Applies the mapping from the source to the data object.
/// </summary>
/// <param name="source"></param>
/// <param name="dataObject"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
protected bool ToDataObject(TPeer source, TDataObject dataObject)
{
return _mappings.ToDataObject(source ?? throw new ArgumentNullException(nameof(source)), dataObject);
}

/// <summary>
/// Applies the mapping from the data object to the target.
/// </summary>
/// <param name="dataObject"></param>
/// <param name="target"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
protected bool FromDataObject(TDataObject dataObject, TPeer target)
{
return _mappings.FromDataObject(dataObject ?? throw new ArgumentNullException(nameof(dataObject)),
target);
}
}
33 changes: 33 additions & 0 deletions PG.Commons/PG.Commons/Data/DataObjectMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace PG.Commons.Data;

internal class DataObjectMapping<TDataObject, TPeer, TValue>(
Func<TDataObject, TValue> dataObjectValueGetter,
Action<TDataObject, TValue> dataObjectValueSetter,
Func<TPeer, TValue> peerValueGetter,
Action<TPeer, TValue> peerValueSetter)
: IDataObjectMapping<TDataObject, TPeer>
{
private Func<TDataObject, TValue> DataObjectValueGetter { get; } = dataObjectValueGetter;
private Action<TDataObject, TValue> DataObjectValueSetter { get; } = dataObjectValueSetter;
private Func<TPeer, TValue> PeerValueGetter { get; } = peerValueGetter;
private Action<TPeer, TValue> PeerValueSetter { get; } = peerValueSetter;

public void ToDataObject(TPeer source, TDataObject dataObject)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (dataObject == null) throw new ArgumentNullException(nameof(dataObject));

var value = PeerValueGetter.Invoke(source);
DataObjectValueSetter.Invoke(dataObject, value);
}

public void FromDataObject(TDataObject dataObject, TPeer target)
{
if (target == null) throw new ArgumentNullException(nameof(target));
if (dataObject == null) throw new ArgumentNullException(nameof(dataObject));
var value = DataObjectValueGetter.Invoke(dataObject);
PeerValueSetter.Invoke(target, value);
}
}
68 changes: 68 additions & 0 deletions PG.Commons/PG.Commons/Data/DataObjectMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace PG.Commons.Data;

/// <summary>
/// Data mappings.
/// </summary>
/// <typeparam name="TDataObject"></typeparam>
/// <typeparam name="TPeer"></typeparam>
public class DataObjectMappings<TDataObject, TPeer>
{
private List<IDataObjectMapping<TDataObject, TPeer>> Mappings { get; } = new();

// ReSharper disable once HeapView.ClosureAllocation
internal bool ToDataObject([DisallowNull] TPeer source, TDataObject dataObject)
{
if (source == null) throw new ArgumentNullException(nameof(source));
try
{
// ReSharper disable once HeapView.DelegateAllocation
Mappings.ForEach(m => m.ToDataObject(source, dataObject));
}
catch (Exception)
{
return false;
}

return true;
}

// ReSharper disable once HeapView.ClosureAllocation
internal bool FromDataObject([DisallowNull] TDataObject dataObject, TPeer target)
{
if (dataObject == null) throw new ArgumentNullException(nameof(dataObject));
try
{
// ReSharper disable once HeapView.DelegateAllocation
Mappings.ForEach(m => m.FromDataObject(dataObject, target));
}
catch (Exception)
{
return false;
}

return true;
}

/// <summary>
/// Mapping builder.
/// </summary>
/// <param name="dataObjectValueGetter"></param>
/// <param name="dataObjectValueSetter"></param>
/// <param name="peerValueGetter"></param>
/// <param name="peerValueSetter"></param>
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
public DataObjectMappings<TDataObject, TPeer> With<TValue>(Func<TDataObject, TValue> dataObjectValueGetter,
Action<TDataObject, TValue> dataObjectValueSetter,
Func<TPeer, TValue> peerValueGetter, Action<TPeer, TValue> peerValueSetter)
{
// ReSharper disable once HeapView.ObjectAllocation.Evident
Mappings.Add(new DataObjectMapping<TDataObject, TPeer, TValue>(dataObjectValueGetter, dataObjectValueSetter,
peerValueGetter, peerValueSetter));
return this;
}
}
8 changes: 8 additions & 0 deletions PG.Commons/PG.Commons/Data/IDataObjectMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PG.Commons.Data;

internal interface IDataObjectMapping<in TDataObject, in TPeer>
{
internal void ToDataObject(TPeer source, TDataObject dataObject);

internal void FromDataObject(TDataObject dataObject, TPeer target);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// Licensed under the MIT license. See LICENSE file in the project root for details.

using System;
using System.Diagnostics.CodeAnalysis;

namespace PG.StarWarsGame.Components.Localisation.Attributes;

/// <summary>
/// Marks the default (fallback) language.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
[ExcludeFromCodeCoverage]
public class DefaultLanguageAttribute : Attribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PG.Commons\PG.Commons\PG.Commons.csproj"/>
<ProjectReference Include="..\..\PG.StarWarsGame.Files.DAT\PG.StarWarsGame.Files.DAT\PG.StarWarsGame.Files.DAT.csproj"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace PG.StarWarsGame.Components.Localisation.Repository.Content;

/// <summary>
/// Readonly representation of an <see cref="ITranslationItem" />
/// </summary>
public interface IReadOnlyTranslationItem : ITranslationItem
{
/// <inheritdoc cref="ITranslationItem.Content" />
new ITranslationItemContent Content { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace PG.StarWarsGame.Components.Localisation.Repository.Content;

/// <summary>
/// The basic representation of a translation item.
/// </summary>
public interface ITranslationItem
{
/// <summary>
/// The source of the translation item.
/// </summary>
enum TranslationItemSource
{
/// <summary>
/// A translation item available in the base game.
/// </summary>
BaseGame,

/// <summary>
/// A translation item added by the expansion
/// </summary>
Expansion,

/// <summary>
/// A translation item added by a mod.
/// </summary>
Mod
}

/// <summary>
/// ID
/// </summary>
ITranslationItemId ItemId { get; }

/// <summary>
/// The acutal translation content.
/// </summary>
ITranslationItemContent Content { get; set; }

/// <summary>
/// The source of the item.
/// </summary>
TranslationItemSource Source { get; }

/// <summary>
/// If an item is originally present in the <see cref="TranslationItemSource.BaseGame" /> or the
/// <see cref="TranslationItemSource.Expansion" />, but also contained in either the
/// <see cref="TranslationItemSource.Expansion" /> or the <see cref="TranslationItemSource.Mod" /> with a different
/// value the item counts as overwritten.
/// </summary>
bool Overwritten { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace PG.StarWarsGame.Components.Localisation.Repository.Content;

/// <summary>
/// The raw content of a translation item.
/// </summary>
public interface ITranslationItemContent
{
/// <summary>
/// The string key, usually mapped to <see cref="PG.StarWarsGame.Files.DAT.Data.DatStringEntry.Key" />
/// </summary>
string Key { get; set; }

/// <summary>
/// The string value, usually mapped to <see cref="PG.StarWarsGame.Files.DAT.Data.DatStringEntry.Value" />
/// </summary>
string Value { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace PG.StarWarsGame.Components.Localisation.Repository.Content;

/// <summary>
/// The unique item ID, could be the string key, or any other unique ID.
/// </summary>
public interface ITranslationItemId
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Immutable;
using PG.StarWarsGame.Components.Localisation.Languages;
using PG.StarWarsGame.Components.Localisation.Repository.Content;

namespace PG.StarWarsGame.Components.Localisation.Repository;

/// <summary>
/// A Diff between all language contents of a given <see cref="ITranslationRepository" />.
/// </summary>
public interface ITranslationDiff
{
/// <summary>
/// The base language for the diff.
/// </summary>
IAlamoLanguageDefinition DiffBase { get; }

/// <summary>
/// All languages present in the diff.
/// </summary>
IImmutableSet<IAlamoLanguageDefinition> Languages { get; }

/// <summary>
/// Collects all <see cref="ITranslationItemId" />s that are missing from a language when compared to the
/// <see cref="DiffBase" />
/// </summary>
/// <param name="language"></param>
/// <returns></returns>
IImmutableSet<ITranslationItemId> GetMissingItemIdsForLanguage(IAlamoLanguageDefinition language);

/// <summary>
/// Collects all <see cref="ITranslationItemId" />s that are still present for a language when compared to the
/// <see cref="DiffBase" />
/// </summary>
/// <param name="language"></param>
/// <returns></returns>
IImmutableSet<ITranslationItemId> GetDanglingItemIdsForLanguage(IAlamoLanguageDefinition language);
}
Loading

0 comments on commit ca448a5

Please sign in to comment.