Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
fix: changed the way to add rotation config.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Mar 21, 2024
1 parent baad8e5 commit 3f0d7e7
Show file tree
Hide file tree
Showing 18 changed files with 228 additions and 272 deletions.
2 changes: 1 addition & 1 deletion RotationSolver.Basic/Actions/ActionTargetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private readonly IEnumerable<BattleChara> GetCanTargets(bool skipStatusProvideCh
return objs.Where(CanUseTo).Where(InViewTarget).Where(action.Setting.CanTarget);
}

private readonly IEnumerable<BattleChara> GetCanAffects(bool skipStatusProvideCheck, TargetType type)
private readonly List<BattleChara> GetCanAffects(bool skipStatusProvideCheck, TargetType type)
{
if (EffectRange == 0) return [];

Expand Down
19 changes: 19 additions & 0 deletions RotationSolver.Basic/Attributes/RotationConfigAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace RotationSolver.Basic.Attributes;

/// <summary>
///
/// </summary>
/// <param name="type"></param>
[AttributeUsage(AttributeTargets.Property)]
public class RotationConfigAttribute(CombatType type) : Attribute
{
/// <summary>
/// The type of this config.
/// </summary>
public CombatType Type => type;

/// <summary>
/// The display name for this config.
/// </summary>
public string Name { get; set; } = string.Empty;
}
2 changes: 1 addition & 1 deletion RotationSolver.Basic/Configuration/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ public const string
private readonly Dictionary<uint, ItemConfig> _rotationItemConfig = [];

[JobChoiceConfig]
private readonly Dictionary<string, string> _rotationsConfigurations = [];
private readonly Dictionary<string, string> _rotationConfigurations = [];

public Dictionary<uint, string> DutyRotationChoice { get; set; } = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,110 +9,4 @@ public interface IRotationConfigSet : IEnumerable<IRotationConfig>
/// The configs.
/// </summary>
HashSet<IRotationConfig> Configs { get; }

/// <summary>
/// Set the float.
/// </summary>
/// <param name="unit">tye unit type</param>
/// <param name="type">the combat type</param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="displayName"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="speed"></param>
IRotationConfigSet SetFloat(ConfigUnitType unit, CombatType type, string name, float value, string displayName, float min = 0, float max = 1, float speed = 0.002f);

/// <summary>
/// Set the string.
/// </summary>
/// <param name="type">the combat type</param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="displayName"></param>
/// <returns></returns>
IRotationConfigSet SetString(CombatType type, string name, string value, string displayName);

/// <summary>
///
/// </summary>
/// <param name="type">the combat type</param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="displayName"></param>
/// <returns></returns>
IRotationConfigSet SetBool(CombatType type, string name, bool value, string displayName);

/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="displayName"></param>
/// <param name="items"></param>
/// <returns></returns>
IRotationConfigSet SetCombo(CombatType type, string name, int value, string displayName, params string[] items);

/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="displayName"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="speed"></param>
/// <returns></returns>
IRotationConfigSet SetInt(CombatType type, string name, int value, string displayName, int min = 0, int max = 10, int speed = 1);

/// <summary>
/// Set the value.
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void SetValue(string name, string value);

/// <summary>
/// Get the combo.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
int GetCombo(string name);

/// <summary>
/// Get the bool.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
bool GetBool(string name);

/// <summary>
/// Get the float.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
float GetFloat(string name);

/// <summary>
/// Get the int.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
int GetInt(string name);

/// <summary>
/// Get the raw string value in the saved dictionary, is not readable.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
string GetString(string name);

/// <summary>
/// Get the readable string for display.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
string GetDisplayString(string name);
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,85 @@
namespace RotationSolver.Basic.Configuration.RotationConfig;
using ECommons.DalamudServices;

internal abstract class RotationConfigBase(string name, string value, string displayName, CombatType type)
namespace RotationSolver.Basic.Configuration.RotationConfig;

internal abstract class RotationConfigBase
: IRotationConfig
{
public string Name { get; } = name;
public string DefaultValue { get; } = value;
public string DisplayName { get; } = displayName;
public CombatType Type { get; } = type;
readonly PropertyInfo _property;
readonly ICustomRotation _rotation;
public string Name { get; }
public string DefaultValue { get; }
public string DisplayName { get; }
public CombatType Type { get; }

public string Value
{
get
{
if (!Service.Config.RotationsConfigurations.TryGetValue(Name, out var config)) return DefaultValue;
if (!Service.Config.RotationConfigurations.TryGetValue(Name, out var config)) return DefaultValue;
return config;
}
set
{
Service.Config.RotationsConfigurations[Name] = value;
Service.Config.RotationConfigurations[Name] = value;
SetValue(value);
}
}

protected RotationConfigBase(ICustomRotation rotation, PropertyInfo property)
{
_property = property;
_rotation = rotation;

Name = property.Name;
DefaultValue = property.GetValue(rotation)?.ToString() ?? string.Empty;
var attr = property.GetCustomAttribute<RotationConfigAttribute>();
if (attr != null)
{
DisplayName = attr.Name;
Type = attr.Type;
}
else
{
DisplayName = Name;
Type = CombatType.None;
}

//Set Up
if (Service.Config.RotationConfigurations.TryGetValue(Name, out var value))
{
SetValue(value);
}
}

private void SetValue(string value)
{
var type = _property.PropertyType;
if (type == null) return;

try
{
_property.SetValue(_rotation, ChangeType(value, type));
}
catch (Exception ex)
{
Svc.Log.Error(ex, "Failed to convert type.");
_property.SetValue(_rotation, ChangeType(DefaultValue, type));
}
}

private static object ChangeType(string value, Type type)
{
if (type.IsEnum)
{
return Enum.Parse(type, value);
}
else if(type == typeof(bool))
{
return bool.Parse(value);
}

return Convert.ChangeType(value, type);
}

public virtual bool DoCommand(IRotationConfigSet set, string str) => str.StartsWith(Name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace RotationSolver.Basic.Configuration.RotationConfig;

internal class RotationConfigBoolean(string name, bool value, string displayName, CombatType type)
: RotationConfigBase(name, value.ToString(), displayName, type)
internal class RotationConfigBoolean(ICustomRotation rotation, PropertyInfo property)
: RotationConfigBase(rotation, property)
{
public override bool DoCommand(IRotationConfigSet set, string str)
{
Expand All @@ -11,11 +11,11 @@ public override bool DoCommand(IRotationConfigSet set, string str)

if (bool.TryParse(numStr, out _))
{
set.SetValue(Name, numStr.ToString());
Value = numStr.ToString();
}
else
{
set.SetValue(Name, (!set.GetBool(Name)).ToString());
Value = (!bool.Parse(Value)).ToString();
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
namespace RotationSolver.Basic.Configuration.RotationConfig;
using Dalamud.Utility;

internal class RotationConfigCombo(string name, int value, string displayName, string[] items, CombatType type)
: RotationConfigBase(name, value.ToString(), displayName, type)
namespace RotationSolver.Basic.Configuration.RotationConfig;

internal class RotationConfigCombo: RotationConfigBase
{
public string[] Items { get; set; } = items;
public string[] Items { get; set; }

public RotationConfigCombo(ICustomRotation rotation, PropertyInfo property)
:base(rotation, property)
{
var names = new List<string>();
foreach (Enum v in Enum.GetValues(property.PropertyType))
{
names.Add(v.GetAttribute<DescriptionAttribute>()?.Description ?? v.ToString());
}
Items = [.. names];
}

public override string ToString()
{
Expand All @@ -19,7 +31,7 @@ public override bool DoCommand(IRotationConfigSet set, string str)
string numStr = str[Name.Length..].Trim();
var length = Items.Length;

int nextId = (set.GetCombo(Name) + 1) % length;
int nextId = (int.Parse(Value) + 1) % length;
if (int.TryParse(numStr, out int num))
{
nextId = num % length;
Expand All @@ -35,7 +47,7 @@ public override bool DoCommand(IRotationConfigSet set, string str)
}
}

set.SetValue(Name, nextId.ToString());
Value = nextId.ToString();
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Diagnostics.CodeAnalysis;

namespace RotationSolver.Basic.Configuration.RotationConfig
namespace RotationSolver.Basic.Configuration.RotationConfig;

internal class RotationConfigComparer : IEqualityComparer<IRotationConfig>
{
internal class RotationConfigComparer : IEqualityComparer<IRotationConfig>
public bool Equals(IRotationConfig? x, IRotationConfig? y)
{
public bool Equals(IRotationConfig? x, IRotationConfig? y)
{
if (x == null || y == null) return false;
return x.Name.Equals(y.Name);
}

public int GetHashCode([DisallowNull] IRotationConfig obj) => obj.Name.GetHashCode();
if (x == null || y == null) return false;
return x.Name.Equals(y.Name);
}

public int GetHashCode([DisallowNull] IRotationConfig obj) => obj.Name.GetHashCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ internal class RotationConfigFloat : RotationConfigBase

public ConfigUnitType UnitType { get; set; }


public RotationConfigFloat(string name, float value, string displayName, float min, float max, float speed, ConfigUnitType unitType, CombatType type) : base(name, value.ToString(), displayName, type)
public RotationConfigFloat(ICustomRotation rotation, PropertyInfo property)
: base(rotation, property)
{
Min = min;
Max = max;
Speed = speed;
UnitType = unitType;
var attr = property.GetCustomAttribute<RangeAttribute>();
if(attr != null)
{
Min = attr.MinValue;
Max = attr.MaxValue;
Speed = attr.Speed;
UnitType = attr.UnitType;
}
else
{
Min = 0.0f;
Max = 1.0f;
Speed = 0.005f;
UnitType = ConfigUnitType.Percent;
}
}

public override bool DoCommand(IRotationConfigSet set, string str)
Expand All @@ -23,7 +34,7 @@ public override bool DoCommand(IRotationConfigSet set, string str)

if (float.TryParse(numStr, out _))
{
set.SetValue(Name, numStr.ToString());
Value = numStr.ToString();
}
return true;
}
Expand Down
Loading

0 comments on commit 3f0d7e7

Please sign in to comment.