first commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Windows.Input;
|
||||
using ClassicAssist.Annotations;
|
||||
|
||||
namespace ClassicAssist.Data.Autoloot
|
||||
{
|
||||
public class AutolootConstraintEntry : INotifyPropertyChanged
|
||||
{
|
||||
private AutolootOperator _operator = AutolootOperator.Equal;
|
||||
private PropertyEntry _property;
|
||||
private int _value;
|
||||
|
||||
public AutolootOperator Operator
|
||||
{
|
||||
get => _operator;
|
||||
set => SetProperty( ref _operator, value );
|
||||
}
|
||||
|
||||
public PropertyEntry Property
|
||||
{
|
||||
get => _property;
|
||||
set => SetProperty( ref _property, value );
|
||||
}
|
||||
|
||||
public int Value
|
||||
{
|
||||
get => _value;
|
||||
set => SetProperty( ref _value, value );
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
[NotifyPropertyChangedInvocator]
|
||||
protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null )
|
||||
{
|
||||
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
|
||||
}
|
||||
|
||||
// ReSharper disable once RedundantAssignment
|
||||
public virtual void SetProperty<T>( ref T obj, T value, [CallerMemberName] string propertyName = "" )
|
||||
{
|
||||
obj = value;
|
||||
OnPropertyChanged( propertyName );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Windows.Input;
|
||||
using ClassicAssist.Annotations;
|
||||
|
||||
namespace ClassicAssist.Data.Autoloot
|
||||
{
|
||||
public class AutolootEntry : INotifyPropertyChanged
|
||||
{
|
||||
private bool _autoloot = true;
|
||||
|
||||
private ObservableCollection<AutolootConstraintEntry> _constraints =
|
||||
new ObservableCollection<AutolootConstraintEntry>();
|
||||
|
||||
private bool _enabled = true;
|
||||
private int _id;
|
||||
private string _name;
|
||||
private bool _rehue;
|
||||
private int _rehueHue = 1153;
|
||||
|
||||
public bool Autoloot
|
||||
{
|
||||
get => _autoloot;
|
||||
set => SetProperty( ref _autoloot, value );
|
||||
}
|
||||
|
||||
public ObservableCollection<AutolootConstraintEntry> Constraints
|
||||
{
|
||||
get => _constraints;
|
||||
set => SetProperty( ref _constraints, value );
|
||||
}
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get => _enabled;
|
||||
set => SetProperty( ref _enabled, value );
|
||||
}
|
||||
|
||||
public int ID
|
||||
{
|
||||
get => _id;
|
||||
set => SetProperty( ref _id, value );
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set => SetProperty( ref _name, value );
|
||||
}
|
||||
|
||||
public bool Rehue
|
||||
{
|
||||
get => _rehue;
|
||||
set => SetProperty( ref _rehue, value );
|
||||
}
|
||||
|
||||
public int RehueHue
|
||||
{
|
||||
get => _rehueHue;
|
||||
set => SetProperty( ref _rehueHue, value );
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Name} - 0x{ID:x}";
|
||||
}
|
||||
|
||||
[NotifyPropertyChangedInvocator]
|
||||
protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null )
|
||||
{
|
||||
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
|
||||
}
|
||||
|
||||
// ReSharper disable once RedundantAssignment
|
||||
public virtual void SetProperty<T>( ref T obj, T value, [CallerMemberName] string propertyName = "" )
|
||||
{
|
||||
obj = value;
|
||||
OnPropertyChanged( propertyName );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using ClassicAssist.UO.Objects;
|
||||
|
||||
namespace ClassicAssist.Data.Autoloot
|
||||
{
|
||||
public static class AutolootHelpers
|
||||
{
|
||||
public static Action<int> SetAutolootContainer { get; set; }
|
||||
|
||||
public static IEnumerable<Item> AutolootFilter( IEnumerable<Item> items, AutolootEntry entry )
|
||||
{
|
||||
return items == null
|
||||
? null
|
||||
: ( from item in items
|
||||
where entry.ID == -1 || item.ID == entry.ID
|
||||
let predicates = ConstraintsToPredicates( entry.Constraints )
|
||||
where !predicates.Any() || CheckPredicates( item, predicates )
|
||||
select item ).ToList();
|
||||
}
|
||||
|
||||
private static bool CheckPredicates( Item item, IEnumerable<Predicate<Item>> predicates )
|
||||
{
|
||||
return predicates.All( predicate => predicate( item ) );
|
||||
}
|
||||
|
||||
public static IEnumerable<Predicate<Item>> ConstraintsToPredicates(
|
||||
IEnumerable<AutolootConstraintEntry> constraints )
|
||||
{
|
||||
List<Predicate<Item>> predicates = new List<Predicate<Item>>();
|
||||
|
||||
foreach ( AutolootConstraintEntry constraint in constraints )
|
||||
{
|
||||
switch ( constraint.Property.ConstraintType )
|
||||
{
|
||||
case PropertyType.Properties:
|
||||
if ( constraint.Operator != AutolootOperator.NotPresent )
|
||||
{
|
||||
predicates.Add( i => i.Properties != null && constraint.Property.Clilocs.Any( cliloc =>
|
||||
i.Properties.Any( p => MatchProperty( p, cliloc,
|
||||
constraint.Property, constraint.Operator,
|
||||
constraint.Value ) ) ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
predicates.Add( i =>
|
||||
i.Properties != null && !constraint.Property.Clilocs.Any( cliloc =>
|
||||
i.Properties.Any( p => p.Cliloc == cliloc ) ) );
|
||||
}
|
||||
|
||||
break;
|
||||
case PropertyType.Object:
|
||||
|
||||
predicates.Add( i =>
|
||||
ItemHasObjectProperty( i, constraint.Property.Name ) && Operation( constraint.Operator,
|
||||
GetItemObjectPropertyValue<int>( i, constraint.Property.Name ), constraint.Value ) );
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
return predicates;
|
||||
}
|
||||
|
||||
public static bool ItemHasObjectProperty( Item item, string propertyName )
|
||||
{
|
||||
PropertyInfo propertyInfo = item.GetType().GetProperty( propertyName );
|
||||
|
||||
if ( propertyInfo == null )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static T GetItemObjectPropertyValue<T>( Item item, string propertyName )
|
||||
{
|
||||
PropertyInfo propertyInfo = item.GetType().GetProperty( propertyName );
|
||||
|
||||
if ( propertyInfo == null )
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
T val = (T) propertyInfo.GetValue( item );
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
public static bool Operation( AutolootOperator @operator, int x, int y )
|
||||
{
|
||||
switch ( @operator )
|
||||
{
|
||||
case AutolootOperator.GreaterThan:
|
||||
return x >= y;
|
||||
case AutolootOperator.LessThan:
|
||||
return x <= y;
|
||||
case AutolootOperator.Equal:
|
||||
return x == y;
|
||||
case AutolootOperator.NotEqual:
|
||||
return x != y;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException( nameof( @operator ), @operator, null );
|
||||
}
|
||||
}
|
||||
|
||||
public static bool MatchProperty( Property property, int cliloc, PropertyEntry constraint,
|
||||
AutolootOperator @operator, int value )
|
||||
{
|
||||
return property.Cliloc == cliloc && ( constraint.ClilocIndex == -1 || Operation( @operator,
|
||||
int.Parse( property.Arguments[constraint.ClilocIndex] ),
|
||||
value ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#region License
|
||||
|
||||
// Copyright (C) 2020 Reetus
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ClassicAssist.Data.Autoloot
|
||||
{
|
||||
public class AutolootManager
|
||||
{
|
||||
private static AutolootManager _instance;
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
public Func<List<AutolootEntry>> GetEntries { get; set; } = () => new List<AutolootEntry>();
|
||||
|
||||
public static AutolootManager GetInstance()
|
||||
{
|
||||
// ReSharper disable once InvertIf
|
||||
if ( _instance == null )
|
||||
{
|
||||
lock ( _lock )
|
||||
{
|
||||
if ( _instance != null )
|
||||
{
|
||||
return _instance;
|
||||
}
|
||||
|
||||
_instance = new AutolootManager();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel;
|
||||
using ClassicAssist.UI.Misc;
|
||||
|
||||
namespace ClassicAssist.Data.Autoloot
|
||||
{
|
||||
[TypeConverter( typeof( EnumDescriptionTypeConverter ) )]
|
||||
public enum AutolootOperator
|
||||
{
|
||||
[Description( "==" )]
|
||||
Equal,
|
||||
|
||||
[Description( "!=" )]
|
||||
NotEqual,
|
||||
|
||||
[Description( ">=" )]
|
||||
GreaterThan,
|
||||
|
||||
[Description( "<=" )]
|
||||
LessThan,
|
||||
|
||||
[Description( "X" )]
|
||||
NotPresent
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Windows.Input;
|
||||
using ClassicAssist.Annotations;
|
||||
|
||||
namespace ClassicAssist.Data.Autoloot
|
||||
{
|
||||
public class PropertyEntry : INotifyPropertyChanged, IComparable<PropertyEntry>
|
||||
{
|
||||
private int _clilocIndex;
|
||||
private int[] _clilocs;
|
||||
private PropertyType _constraintType;
|
||||
private string _name;
|
||||
|
||||
public int ClilocIndex
|
||||
{
|
||||
get => _clilocIndex;
|
||||
set => SetProperty( ref _clilocIndex, value );
|
||||
}
|
||||
|
||||
public int[] Clilocs
|
||||
{
|
||||
get => _clilocs;
|
||||
set => SetProperty( ref _clilocs, value );
|
||||
}
|
||||
|
||||
public PropertyType ConstraintType
|
||||
{
|
||||
get => _constraintType;
|
||||
set => SetProperty( ref _constraintType, value );
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get => _name;
|
||||
set => SetProperty( ref _name, value );
|
||||
}
|
||||
|
||||
public int CompareTo( PropertyEntry other )
|
||||
{
|
||||
if ( ReferenceEquals( this, other ) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( ReferenceEquals( null, other ) )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int nameComparison = string.Compare( _name, other._name, StringComparison.InvariantCultureIgnoreCase );
|
||||
|
||||
return nameComparison;
|
||||
}
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
[NotifyPropertyChangedInvocator]
|
||||
protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null )
|
||||
{
|
||||
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
|
||||
}
|
||||
|
||||
// ReSharper disable once RedundantAssignment
|
||||
public virtual void SetProperty<T>( ref T obj, T value, [CallerMemberName] string propertyName = "" )
|
||||
{
|
||||
obj = value;
|
||||
OnPropertyChanged( propertyName );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace ClassicAssist.Data.Autoloot
|
||||
{
|
||||
public enum PropertyType
|
||||
{
|
||||
Properties,
|
||||
Object
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user