-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b52298a
commit ca448a5
Showing
14 changed files
with
410 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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; | ||
|
||
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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} |
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
10 changes: 10 additions & 0 deletions
10
...on/PG.StarWarsGame.Components.Localisation/Repository/Content/IReadOnlyTranslationItem.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,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; } | ||
} |
51 changes: 51 additions & 0 deletions
51
...calisation/PG.StarWarsGame.Components.Localisation/Repository/Content/ITranslationItem.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,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; } | ||
} |
17 changes: 17 additions & 0 deletions
17
...ion/PG.StarWarsGame.Components.Localisation/Repository/Content/ITranslationItemContent.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,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; } | ||
} |
8 changes: 8 additions & 0 deletions
8
...lisation/PG.StarWarsGame.Components.Localisation/Repository/Content/ITranslationItemId.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,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 | ||
{ | ||
} |
37 changes: 37 additions & 0 deletions
37
...nents.Localisation/PG.StarWarsGame.Components.Localisation/Repository/ITranslationDiff.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,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); | ||
} |
Oops, something went wrong.