Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

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

13 changes: 13 additions & 0 deletions .idea/.idea.PetroglyphTools/.idea/developer-tools.xml

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

8 changes: 8 additions & 0 deletions .idea/.idea.PetroglyphTools/.idea/indexLayout.xml

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

7 changes: 7 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.

23 changes: 14 additions & 9 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<!--
- // Copyright (c) Alamo Engine Tools and contributors. All rights reserved.
- // Licensed under the MIT license. See LICENSE file in the project root for details.
-->

<Project>
<PropertyGroup>
<RepoRootPath>$(MSBuildThisFileDirectory)</RepoRootPath>
Expand All @@ -10,15 +15,15 @@
<Authors>Alamo Engine Tools and Contributors</Authors>
<Copyright>Copyright © 2023 Alamo Engine Tools and contributors. All rights reserved.</Copyright>
<PackageProjectUrl>https://github.com/AlamoEngine-Tools/PetroglyphTools</PackageProjectUrl>
<LicenseFile>$(MSBuildThisFileDirectory)LICENSE</LicenseFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LicenseFile>$(MSBuildThisFileDirectory)LICENSE</LicenseFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/AlamoEngine-Tools/PetroglyphTools</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Company>Alamo Engine Tools</Company>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>aet.png</PackageIcon>
</PropertyGroup>
<PackageIcon>aet.png</PackageIcon>

</PropertyGroup>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<ImplicitUsings>disable</ImplicitUsings>
Expand All @@ -38,8 +43,8 @@
<None Include="$(MSBuildThisFileDirectory)README.md" Pack="true" PackagePath=""/>
</ItemGroup>

<ItemGroup Condition="'$(IsPackable)' == 'true'">
<None Include="$(LicenseFile)" Pack="true" PackagePath=""/>
<None Include="$(MSBuildThisFileDirectory)aet.png" Pack="true" PackagePath=""/>
</ItemGroup>
<ItemGroup Condition="'$(IsPackable)' == 'true'">
<None Include="$(LicenseFile)" Pack="true" PackagePath=""/>
<None Include="$(MSBuildThisFileDirectory)aet.png" Pack="true" PackagePath=""/>
</ItemGroup>
</Project>
44 changes: 44 additions & 0 deletions PG.Commons/PG.Commons.Test/Data/IIdTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Alamo Engine Tools and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

using System.Collections.Generic;
using System.Linq;
using PG.Commons.Data;
using Xunit;

namespace PG.Commons.Test.Data;

// ReSharper disable once InconsistentNaming
public abstract class IIdTestBase<T> where T : IId
{
[Fact]
public void Test_NullIIdCreationReturnsNull()
{
foreach (var nullKeys in GetConfiguredNullIIds()) Assert.Null(nullKeys);
}

[Fact]
public void Test_IIdBehavesAsExpectedWithHash()
{
var keyInitialisers = GetConfiguredValidIIdInitialisers();
var dict = new Dictionary<T, string>();
foreach (var key in keyInitialisers.Select(keyInitialiser => CreateId(keyInitialiser)))
{
Assert.NotNull(key);
dict.Add(key, $"{key}: Hash: {key.GetHashCode()}");
}

foreach (var key in keyInitialisers.Select(keyInitialiser => CreateId(keyInitialiser)))
{
Assert.NotNull(key);
Assert.Contains(key, dict.Keys);
Assert.NotNull(dict[key]);
}
}

protected abstract List<T?> GetConfiguredNullIIds();

protected abstract List<object[]> GetConfiguredValidIIdInitialisers();

protected abstract T CreateId(object[] keyInitialiser);
}
48 changes: 48 additions & 0 deletions PG.Commons/PG.Commons.Test/Data/IIdTestCompletenessTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Alamo Engine Tools and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PG.Commons.Data;
using Xunit;

namespace PG.Commons.Test.Data;

// ReSharper disable once InconsistentNaming
public abstract class IIdTestCompletenessTestBase
{
[Fact]
public void Test_IIdTestPresetForAllIIdsOfPackage()
{
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).ToList();
var iids = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assemblyTypes => assemblyTypes.GetTypes())
.Where(assemblyType => typeof(IId).IsAssignableFrom(assemblyType)
&& assemblyType is { IsClass: true, IsAbstract: false }
&& assemblyType.Namespace != null
&& assemblyType.Namespace.StartsWith(GetConfiguredNamespaceBase()))
.ToList();
var present = new Dictionary<Type, bool>();


foreach (var iid in iids)
{
present.Add(iid, false);
if (types.Any(type => iid.Name + "Test" == type.Name)) present[iid] = true;
}

var o = new StringBuilder().Append("The following IIds have no corresponding test:\n");
var missing = false;
foreach (var kvp in present.Where(kvp => !kvp.Value))
{
o.Append($"\t{kvp.Key.FullName}\n");
missing = true;
}

Assert.False(missing, o.ToString());
}

protected abstract string GetConfiguredNamespaceBase();
}
40 changes: 40 additions & 0 deletions PG.Commons/PG.Commons/Attributes/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;

namespace PG.Commons.Attributes;

/// <summary>
/// The platform a feature/... is supported on.
/// </summary>
[Flags]
public enum Platform
{
/// <summary>
/// Disc version of the game / expansion
/// </summary>
Disc = 0b00001,

/// <summary>
/// Steam version of the game / expansion
/// </summary>
Steam = 0b00010,

/// <summary>
/// Origin version of the game / expansion - yep that abomination exists.
/// </summary>
Origin = 0b00100,

/// <summary>
/// GoG.com version of the game.
/// </summary>
GoG = 0b01000,

/// <summary>
/// MAC version of the game / expansion - yep that exists as well, only the base game though.
/// </summary>
Mac = 0b10000,

/// <summary>
/// Versions that do no longer receive updates.
/// </summary>
Outdated = Disc | Origin | Steam | GoG | Mac
}
24 changes: 24 additions & 0 deletions PG.Commons/PG.Commons/Attributes/PlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace PG.Commons.Attributes;

/// <summary>
/// Simple annotation to attach platform info.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Struct)]
public class PlatformAttribute : Attribute
{
/// <summary>
/// .ctor
/// </summary>
/// <param name="platform"></param>
public PlatformAttribute(Platform platform)
{
Platform = platform;
}

/// <summary>
/// The <see cref="Platform" /> flags.
/// </summary>
public Platform Platform { get; }
}
58 changes: 58 additions & 0 deletions PG.Commons/PG.Commons/Data/DataTransferObjectMapperBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;

namespace PG.Commons.Data;

/// <summary>
/// Container class allowing to map from a data objct to a peer and vice versa.
/// <br />
/// @lgr: check if fully replaceable with AutoMapper
/// </summary>
/// <typeparam name="TDto"></typeparam>
/// <typeparam name="TPeer"></typeparam>
public abstract class DataTransferObjectMapperBase<TDto, TPeer>
{
private readonly DataTransferObjectMappings<TDto, TPeer> _mappings;

/// <summary>
/// .ctor
/// </summary>
protected DataTransferObjectMapperBase()
{
var mappings = new DataTransferObjectMappings<TDto, 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(DataTransferObjectMappings<TDto, 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 ToDto(TPeer source, TDto dataObject)
{
return _mappings.ToDto(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 FromDto(TDto dataObject, TPeer target)
{
return _mappings.FromDto(dataObject ?? throw new ArgumentNullException(nameof(dataObject)),
target);
}
}
69 changes: 69 additions & 0 deletions PG.Commons/PG.Commons/Data/DataTransferObjectMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace PG.Commons.Data;

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

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

return true;
}

// ReSharper disable once HeapView.ClosureAllocation
internal bool FromDto([DisallowNull] TDto dataObject, TPeer target)
{
if (dataObject == null) throw new ArgumentNullException(nameof(dataObject));
try
{
// ReSharper disable once HeapView.DelegateAllocation
Mappings.ForEach(m => m.FromDto(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 DataTransferObjectMappings<TDto, TPeer> With<TValue>(Func<TDto, TValue> dataObjectValueGetter,
Action<TDto, TValue> dataObjectValueSetter,
Func<TPeer, TValue> peerValueGetter, Action<TPeer, TValue> peerValueSetter)
{
// ReSharper disable once HeapView.ObjectAllocation.Evident
Mappings.Add(new DataTransferTransferObjectMapping<TDto, TPeer, TValue>(dataObjectValueGetter,
dataObjectValueSetter,
peerValueGetter, peerValueSetter));
return this;
}
}
Loading