Skip to content

Commit

Permalink
Merge pull request #28 from IRacle1/dev
Browse files Browse the repository at this point in the history
Some Features/Fixes
  • Loading branch information
Folleach authored Oct 2, 2023
2 parents 4e9a2f5 + 33ae468 commit aac1bb1
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 39 deletions.
12 changes: 12 additions & 0 deletions GeometryDashAPI.Tests/TypeDescriptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ public void OverrideVirtualFieldSet_Specific()

block.ZLayer.Should().Be(Layer.B4);
}

[Test]
public void PrivateFieldFromInheritedClass()
{
var input = "1,10,2,20,3,333";
var descriptor = new TypeDescriptor<InheritField>();

var actual = descriptor.Create(input.AsSpan());

actual.X.Should().Be(10);
actual.Y.Should().Be(20);
}
}
5 changes: 2 additions & 3 deletions GeometryDashAPI/Attributes/GamePropertyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ public class GamePropertyAttribute : Attribute
public bool AlwaysSet { get; }
public int KeyOverride { get; set; }
public int Order { get; set; } = int.MaxValue;
public bool IgnoreField = false;

public GamePropertyAttribute(string key, object defaultDefaultValue = null, bool alwaysSet = false)
public GamePropertyAttribute(string key, object defaultValue = null, bool alwaysSet = false)
{
Key = key;
DefaultValue = defaultDefaultValue;
DefaultValue = defaultValue;
AlwaysSet = alwaysSet;
}
}
Expand Down
4 changes: 2 additions & 2 deletions GeometryDashAPI/Crypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static byte[] GZipCompress(byte[] data)
using var memory = new MemoryStream();
using (var destination = new GZipStream(memory, CompressionMode.Compress))
{
using (var memoryStream2 = new MemoryStream(data))
memoryStream2.CopyTo(destination);
using var memoryStream2 = new MemoryStream(data);
memoryStream2.CopyTo(destination);
}
return memory.ToArray();
}
Expand Down
7 changes: 5 additions & 2 deletions GeometryDashAPI/Data/LocalLevels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace GeometryDashAPI.Data
{
public class LocalLevels : GameData, IEnumerable<LevelCreatorModel>
public class LocalLevels : GameData, IReadOnlyCollection<LevelCreatorModel>
{
private List<LevelCreatorModel> levels { get; set; }
private Dictionary<string, Dictionary<int, int>> index;
Expand All @@ -19,6 +19,7 @@ public int BinaryVersion
set => DataPlist["LLM_02"] = value;
}

[Obsolete("Use Count instead", true)]
public int LevelCount => levels.Count;

protected LocalLevels() : base(GameDataType.LocalLevels)
Expand Down Expand Up @@ -104,6 +105,8 @@ public bool Remove(LevelCreatorModel levelInfo)
return true;
}

public int Count => levels.Count;

public IEnumerator<LevelCreatorModel> GetEnumerator()
{
foreach (var level in levels)
Expand Down Expand Up @@ -152,7 +155,7 @@ public static LocalLevels CreateNew()
public void AddLevel(LevelCreatorModel levelInfo)
{
var all = DataPlist["LLM_01"];
for (var i = LevelCount - 1; i >= 0; i--)
for (var i = Count - 1; i >= 0; i--)
all[$"k_{i + 1}"] = all[$"k_{i}"];
all["k_0"] = levelInfo.DataLevel;
LoadLevels();
Expand Down
4 changes: 4 additions & 0 deletions GeometryDashAPI/Levels/GameObjects/Default/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public class Block : GameObject, IBlock
[GameProperty("36", false, Order = Trigger.OrderTriggerBase)] public bool IsTrigger { get; set; }

[GameProperty("6", 0)] public int Rotation { get; set; }

public bool Glow
{
get => !glow;
set => glow = !value;
}

[GameProperty("96", true)] private bool glow = true;
[GameProperty("108", 0)] public int LinkControl { get; set; }
[GameProperty("20", (short)0)] public short EditorL { get; set; }
Expand All @@ -32,11 +34,13 @@ public bool Glow
[GameProperty("57")] [ArraySeparator(".")] public int[] Groups { get; set; }
[GameProperty("67", false)] public bool DontEnter { get; set; }
[GameProperty("25", 2)] public virtual int ZOrder { get; set; } = 2;

public Layer ZLayer
{
get => (Layer)zLayer;
set => zLayer = (short)value;
}

[GameProperty("24", (short)Layer.T1)] protected virtual short zLayer { get; set; } = (int)Layer.T1;
[GameProperty("32", 1f)] public float Scale { get; set; } = 1f;
[GameProperty("34", false)] public bool GroupParent { get; set; }
Expand Down
8 changes: 7 additions & 1 deletion GeometryDashAPI/Levels/GameObjects/Default/Trigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ public abstract class Trigger : Block, ITrigger

[GameProperty("11", false, Order = 100)] public bool TouchTrigger { get; set; } = false;
[GameProperty("62", false, Order = 101)] public bool SpawnTrigger { get; set; } = false;
[GameProperty("87", false, Order = 102)] public virtual bool MultiTrigger { get; set; } = false;
[GameProperty("87", false, Order = 102)] protected bool multiTrigger;

public virtual bool MultiTrigger
{
get => multiTrigger;
set => multiTrigger = value;
}

public Trigger()
{
Expand Down
5 changes: 0 additions & 5 deletions GeometryDashAPI/Levels/GameObjects/Triggers/ShakeTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ public class ShakeTrigger : Trigger
[GameProperty("75", 0f, false, Order = OrderTriggerBase + 2)] public float Strength { get; set; }
[GameProperty("84", 0f, false, Order = OrderTriggerBase + 3)] public float Interval { get; set; }

// robtop 300iq coder
[GameProperty("87", false, Order = 102)]
private bool multiTrigger;

[GameProperty("87", false, Order = 102, IgnoreField = true)]
public override bool MultiTrigger
{
get => !multiTrigger;
Expand Down
5 changes: 0 additions & 5 deletions GeometryDashAPI/Levels/GameObjects/Triggers/TouchTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public class TouchTrigger : Trigger
[GameProperty("82", ToggleMode.None, false, Order = OrderTriggerBase + 3)] public ToggleMode ToggleMode { get; set; } = ToggleMode.None;
[GameProperty("89", false, false, Order = OrderTriggerBase + 4)] public bool DualMode { get; set; } = false;

// robtop 300iq coder
[GameProperty("87", false, Order = 102)]
private bool multiTrigger;

[GameProperty("87", false, Order = 102, IgnoreField = true)]
public override bool MultiTrigger
{
get => !multiTrigger;
Expand Down
26 changes: 12 additions & 14 deletions GeometryDashAPI/Serialization/TypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TypeDescriptor<T> : IDescriptor<T>, ICopyDescriptor<T> where T : IG
{
private readonly SetterInfo<T>[] setters;
private readonly PrinterInfo<T>[] printers;
private readonly Dictionary<string, int> mappings;
private readonly Dictionary<string, int>? mappings;
private readonly string sense;
private readonly Func<T> create;
private readonly bool isStruct;
Expand All @@ -21,7 +21,7 @@ public class TypeDescriptor<T> : IDescriptor<T>, ICopyDescriptor<T> where T : IG
public TypeDescriptor()
{
var type = typeof(T);
create = CreateInstanceExpression<T>(type).Compile();
create = CreateInstanceExpression<T>().Compile();

var senseAttribute = type.GetCustomAttribute<SenseAttribute>();
if (senseAttribute == null)
Expand All @@ -33,7 +33,6 @@ public TypeDescriptor()
var members = GetPropertiesAndFields(type)
.Select(member => (member, attribute: member.GetCustomAttribute<GamePropertyAttribute>()))
.Where(x => x.attribute != null)
.Where(x => !x.attribute.IgnoreField)
.ToArray();
var createSetter = typeof(TypeDescriptorHelper)
.GetMethod(nameof(TypeDescriptorHelper.CreateSetter), BindingFlags.Static | BindingFlags.NonPublic);
Expand Down Expand Up @@ -91,14 +90,13 @@ public T Create(ReadOnlySpan<char> raw)
if (!int.TryParse(key.ToString(), out var index))
#endif
{
var keyString = key.ToString();
if (!mappings.TryGetValue(keyString, out var mapped))
if (mappings == null || !mappings.TryGetValue(key.ToString(), out var mapped))
{
instance.WithoutLoaded.Add($"{key.ToString()}{sense}{value.ToString()}");
continue;
}
if (!TrySet(instance, mapped, value))
instance.WithoutLoaded.Add($"{keyString}{sense}{value.ToString()}");
instance.WithoutLoaded.Add($"{key.ToString()}{sense}{value.ToString()}");
continue;
}
if (!TrySet(instance, baseIndex + index, value))
Expand Down Expand Up @@ -187,19 +185,19 @@ private PrinterInfo<T>[] InitPrinters(IEnumerable<(MemberInfo member, GameProper
.ToArray();
}

private static Expression<Func<TB>> CreateInstanceExpression<TB>(Type type)
private static Expression<Func<TB>> CreateInstanceExpression<TB>()
{
var ctor = Expression.New(type);
var ctor = Expression.New(typeof(TB));
var memberInit = Expression.MemberInit(ctor);

return Expression.Lambda<Func<TB>>(memberInit);
}

private static (SetterInfo<T>[], int baseIndex, Dictionary<string, int> mappings) InitSetters(Type type, IEnumerable<(MemberInfo member, GamePropertyAttribute attribute)> members)
private static (SetterInfo<T>[], int baseIndex, Dictionary<string, int>? mappings) InitSetters(Type type, IEnumerable<(MemberInfo member, GamePropertyAttribute attribute)> members)
{
var keys = new HashSet<string>();
var maxKeyValue = 0;
Dictionary<string, int> mappings = null;
Dictionary<string, int>? mappings = null;
foreach (var (member, attribute) in members)
{
if (int.TryParse(attribute.Key, out var key))
Expand All @@ -212,9 +210,9 @@ private static (SetterInfo<T>[], int baseIndex, Dictionary<string, int> mappings
continue;
}

mappings ??= new Dictionary<string, int>();
if (attribute.KeyOverride == -1)
throw new InvalidOperationException($"Key override for member '{attribute.Key}' in {type.Name} is not set");
mappings ??= new Dictionary<string, int>();
mappings.Add(attribute.Key, attribute.KeyOverride);
}

Expand All @@ -239,7 +237,7 @@ private static IEnumerable<MemberInfo> GetPropertiesAndFields(Type type)
var current = type;
while (current != null && current != typeof(object))
{
foreach (var field in current.GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
foreach (var field in current.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
yield return field;
current = current.BaseType;
}
Expand Down Expand Up @@ -519,12 +517,12 @@ private static Expression<Setter<TInstance>> CreateEnumSetter<TProp, TInstance>(
);
}

private static MethodInfo GetParserMethod<TProp>(out Expression instanceExpression)
private static MethodInfo GetParserMethod<TProp>(out Expression? instanceExpression)
{
return GetParserMethod(typeof(TProp), out instanceExpression);
}

private static MethodInfo GetParserMethod(Type propType, out Expression serializerExp)
private static MethodInfo GetParserMethod(Type propType, out Expression? serializerExp)
{
if (typeof(IGameObject).IsAssignableFrom(propType))
{
Expand Down
14 changes: 12 additions & 2 deletions GeometryDashAPI/Server/Dtos/Account.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GeometryDashAPI.Attributes;
using System;

using GeometryDashAPI.Attributes;
using GeometryDashAPI.Server.Enums;

namespace GeometryDashAPI.Server.Dtos
Expand Down Expand Up @@ -45,7 +47,15 @@ public class Account : GameObject
[GameProperty("45")] public string TwitchId { get; set; }
[GameProperty("46")] public int Diamonds { get; set; }
[GameProperty("48")] public int ExplosionId { get; set; }
[GameProperty("49")] public int Moderator { get; set; }
[GameProperty("49")] public GameModeratorType ModeratorType { get; set; }

[Obsolete("Use ModeratorType instead.")]
public int Moderator
{
get => (int)ModeratorType;
set => ModeratorType = (GameModeratorType)value;
}

[GameProperty("50")] public int CommentHistoryState { get; set; }
}
}
1 change: 1 addition & 0 deletions GeometryDashAPI/Server/Dtos/LevelPreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LevelPreview : GameObject

[GameProperty("3")]
private string description;

public string Description
{
get
Expand Down
8 changes: 8 additions & 0 deletions GeometryDashAPI/Server/Enums/GameModeratorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GeometryDashAPI.Server.Enums;

public enum GameModeratorType
{
Player,
Moderator,
EldedModerator
}
10 changes: 5 additions & 5 deletions GeometryDashAPI/Server/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace GeometryDashAPI.Server
{
public class GameClient : IGameClient
{
private readonly IdentifierQuery identifierQuery;
private readonly OnlineQuery onlineQuery;
private readonly IdentifierQuery? identifierQuery;
private readonly OnlineQuery? onlineQuery;
private readonly Network network;

public GameClient() : this(null)
{
}
public GameClient(IdentifierQuery identifierQuery = null, OnlineQuery onlineQuery = null, Network network = null)

public GameClient(IdentifierQuery? identifierQuery = null, OnlineQuery? onlineQuery = null, Network? network = null)
{
this.identifierQuery = identifierQuery;
this.onlineQuery = onlineQuery;
Expand All @@ -30,7 +30,7 @@ public async Task<ServerResponse<TopResponse>> GetTopAsync(TopType type, int cou
var query = new FlexibleQuery()
.AddToChain(OnlineQuery.Default)
.AddToChain(GetIdentifier())
.AddProperty(new Property("type", type.GetAttributeOfSelected<OriginalNameAttribute>().OriginalName))
.AddProperty(new Property("type", type.GetAttributeOfSelected<OriginalNameAttribute>()!.OriginalName))
.AddProperty(new Property("count", count));
return await Get<TopResponse>("/getGJScores20.php", query);
}
Expand Down
25 changes: 25 additions & 0 deletions TestObjects/InheritFieldFrom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using GeometryDashAPI;
using GeometryDashAPI.Attributes;

namespace TestObjects;

[Sense(",")]
public class InheritFieldFrom : GameObject
{
[GameProperty("1")]
private int x;

[GameProperty("3")]
protected int Id;

public int X => x;
}

[Sense(",")]
public class InheritField : InheritFieldFrom
{
[GameProperty("2")]
private int y;

public int Y => y;
}

0 comments on commit aac1bb1

Please sign in to comment.