Skip to content
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

Enforce brackets #817

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public static class ItemDirectoryExtensions
public static async Task<IItemAsset?> FindByNameAsync(this IItemDirectory directory, string itemName, bool exact = true)
{
if (exact)
{
return (await directory.GetItemAssetsAsync()).FirstOrDefault(d =>
d.ItemName.Equals(itemName, StringComparison.OrdinalIgnoreCase));
}

return (await directory.GetItemAssetsAsync())
.Where(x => x.ItemName.IndexOf(itemName, StringComparison.OrdinalIgnoreCase) >= 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@ public static class QuaternionMathExtensions
private static void CleanUpAngles(this ref Vector3 vector)
{
while (vector.X >= 360)
{
vector.X -= 360;
}

while (vector.X < 0)
{
vector.X += 360;
}

while (vector.Y >= 360)
{
vector.Y -= 360;
}

while (vector.Y < 0)
{
vector.Y += 360;
}

while (vector.Z >= 360)
{
vector.Z -= 360;
}

while (vector.Z < 0)
{
vector.Z += 360;
}
}

/// <summary>
Expand Down Expand Up @@ -88,7 +100,7 @@ public static Quaternion ToQuaternion(this Vector3 eulerAngles)
var cosYOver2 = (float)Math.Cos(yOver2);
var sinZOver2 = (float)Math.Sin(zOver2);
var cosZOver2 = (float)Math.Cos(zOver2);

return new Quaternion()
{
X = cosYOver2 * sinXOver2 * cosZOver2 + sinYOver2 * cosXOver2 * sinZOver2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public static class VehicleDirectoryExtensions
public static async Task<IVehicleAsset?> FindByNameAsync(this IVehicleDirectory directory, string vehicleName, bool exact = true)
{
if (exact)
{
return (await directory.GetVehicleAssetsAsync()).FirstOrDefault(d =>
d.VehicleName.Equals(vehicleName, StringComparison.OrdinalIgnoreCase));
}

return (await directory.GetVehicleAssetsAsync())
.Where(x => x.VehicleName.IndexOf(vehicleName, StringComparison.OrdinalIgnoreCase) >= 0)
Expand Down
23 changes: 12 additions & 11 deletions framework/OpenMod.API/Persistence/SerializeIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;

namespace OpenMod.API.Persistence;

/// <summary>
/// Attribute to mark any property or field to be ignored
/// This make API independent of any type such as json/xml/yaml
/// </summary>
//todo add this to docs
//todo propagate this to all serializers, note this will be implemented when change to VYaml
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter |
AttributeTargets.ReturnValue)]
public class SerializeIgnoreAttribute : Attribute
namespace OpenMod.API.Persistence
{
/// <summary>
/// Attribute to mark any property or field to be ignored
/// This make API independent of any type such as json/xml/yaml
/// </summary>
//todo add this to docs
//todo propagate this to all serializers, note this will be implemented when change to VYaml
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter |
AttributeTargets.ReturnValue)]
public class SerializeIgnoreAttribute : Attribute
{
}
}
8 changes: 7 additions & 1 deletion framework/OpenMod.Common/Helpers/AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ public static IEnumerable<AssemblyName> GetMissingDependencies(this ReflectionTy
//TypeLoadException is just matching with MissingFileAssemblyVersionRegex
var match = s_MissingFileAssemblyVersionRegex.Match(loaderException.Message);
if (!match.Success)
{
match = s_TypeLoadAssemblyVersionRegex.Match(loaderException.Message);
}

if (!match.Success)
{
continue;

}

var assemblyName = match.Groups["assembly"].Value;
var version = Version.Parse(match.Groups["version"].Value);

if (missingAssemblies.TryGetValue(assemblyName, out var currentVersion) && currentVersion >= version)
{
continue;
}

missingAssemblies[assemblyName] = version;
}
Expand Down
33 changes: 12 additions & 21 deletions framework/OpenMod.Common/Helpers/AssemblyNameEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,25 @@
using System.Reflection;
using System.Text;

namespace OpenMod.Common.Helpers;

/// <summary>
/// Comparer of <see cref="AssemblyName"/> that comparers the name
/// </summary>
public sealed class AssemblyNameEqualityComparer : IEqualityComparer<AssemblyName>
namespace OpenMod.Common.Helpers
{
public static AssemblyNameEqualityComparer Instance { get; } = new();
/// <summary>
/// Comparer of <see cref="AssemblyName"/> that comparers the name
/// </summary>
public sealed class AssemblyNameEqualityComparer : IEqualityComparer<AssemblyName>
{
public static AssemblyNameEqualityComparer Instance { get; } = new();

private AssemblyNameEqualityComparer() { }
private AssemblyNameEqualityComparer() { }

public bool Equals(AssemblyName x, AssemblyName y)
{
if (x == y)
public bool Equals(AssemblyName? x, AssemblyName? y)
{
return true;
return string.Equals(x?.Name, y?.Name, StringComparison.OrdinalIgnoreCase);
}

if (x == null || y == null)
public int GetHashCode(AssemblyName obj)
{
return false;
return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Name);
}

return x.Name.Equals(y.Name, StringComparison.OrdinalIgnoreCase);
}

public int GetHashCode(AssemblyName obj)
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Name);
}
}
14 changes: 13 additions & 1 deletion framework/OpenMod.Common/Helpers/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public static class ReflectionExtensions
var frame = st.GetFrame(i);
var frameMethod = frame.GetMethod();
if (frameMethod == null)
{
continue;
}

// Hot fix for async Task methods:
// If current frame method is called "MoveNext" and parent frame is from "AsyncMethodBuilderCore" type
Expand Down Expand Up @@ -56,10 +58,14 @@ public static class ReflectionExtensions
}

if (skipList.Any(c => c == frameMethod?.DeclaringType))
{
continue;
}

if (skipMethods?.Any(c => c == frameMethod) ?? false)
{
continue;
}

frameTarget = frame;
break;
Expand All @@ -77,7 +83,9 @@ public static class ReflectionExtensions
{
var frame = st.GetFrame(i);
if (skipAssemblies.Any(c => Equals(c, frame.GetMethod()?.DeclaringType?.Assembly)))
{
continue;
}

frameTarget = frame;
}
Expand Down Expand Up @@ -227,7 +235,11 @@ public static bool HasConversionOperator(this Type from, Type to)
throw new ArgumentNullException(nameof(to));
}

UnaryExpression BodyFunction(Expression body) => Expression.Convert(body, to);
UnaryExpression BodyFunction(Expression body)
{
return Expression.Convert(body, to);
}

var inp = Expression.Parameter(from, "inp");
try
{
Expand Down
4 changes: 4 additions & 0 deletions framework/OpenMod.Core/Commands/CommandContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,15 @@ public ICommandContext CreateContext(ICommandActor actor, string[] args, string
foreach (var registration in baseQuery)
{
if (registration.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
{
return registration;
}

if (registration.Aliases != null &&
registration.Aliases.Any(e => e.Equals(name, StringComparison.OrdinalIgnoreCase)))
{
aliasMatch = registration;
}
}

return aliasMatch;
Expand Down
37 changes: 29 additions & 8 deletions framework/OpenMod.Core/Commands/CommandParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ public CommandParameters(ICommandContext commandContext, ICollection<string> arg
protected ICollection<string> RawParameters { get; }

/// <inheritdoc />
public string this[int index] => ToArray()[index];
public string this[int index]
{
get => ToArray()[index];
}

/// <inheritdoc />
public int Length => ToArray().Length;
public int Length
{
get => ToArray().Length;
}

/// <inheritdoc />
public async Task<T> GetAsync<T>(int index) => (T)await GetAsync(index, typeof(T));
public async Task<T> GetAsync<T>(int index)
{
return (T)await GetAsync(index, typeof(T));
}

/// <inheritdoc />
public async Task<object> GetAsync(int index, Type type)
Expand Down Expand Up @@ -156,21 +165,33 @@ public bool TryGet(int index, Type type, out object? value)
}

/// <inheritdoc />
public string[] ToArray() => RawParameters.ToArray();
public string[] ToArray()
{
return RawParameters.ToArray();
}

/// <inheritdoc />
public List<string> ToList() => ToArray().ToList();
public List<string> ToList()
{
return ToArray().ToList();
}

/// <inheritdoc />
public IEnumerator<string> GetEnumerator() => ToList().GetEnumerator();
public IEnumerator<string> GetEnumerator()
{
return ToList().GetEnumerator();
}

/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

/// <inheritdoc />
public int Count
{
get { return RawParameters.Count; }
get => RawParameters.Count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class YamlConfigurationExtensions
/// Ex: supports variables.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddYamlFileEx(this IConfigurationBuilder builder, string path)
Expand All @@ -33,7 +33,7 @@ public static IConfigurationBuilder AddYamlFileEx(this IConfigurationBuilder bui
/// Ex: supports variables.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional">Whether the file is optional.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
Expand All @@ -47,7 +47,7 @@ public static IConfigurationBuilder AddYamlFileEx(this IConfigurationBuilder bui
/// Ex: supports variables.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional">Whether the file is optional.</param>
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
Expand All @@ -62,7 +62,7 @@ public static IConfigurationBuilder AddYamlFileEx(this IConfigurationBuilder bui
/// Ex: supports variables.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="variables">Variables to replace.</param>
/// <param name="optional">Whether the file is optional.</param>
Expand All @@ -79,7 +79,7 @@ public static IConfigurationBuilder AddYamlFileEx(this IConfigurationBuilder bui
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="provider">The <see cref="IFileProvider"/> to use to access the file.</param>
/// <param name="path">Path relative to the base path stored in
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="variables">Variables to replace.</param>
/// <param name="optional">Whether the file is optional.</param>
Expand Down Expand Up @@ -119,6 +119,8 @@ public static IConfigurationBuilder AddYamlFileEx(this IConfigurationBuilder bui
/// <param name="configureSource">Configures the source.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddYamlFileEx(this IConfigurationBuilder builder, Action<YamlConfigurationSourceEx> configureSource)
=> builder.Add(configureSource);
{
return builder.Add(configureSource);
}
}
}