servuo/Scripts/Commands/Generic/Extensions/SortExtension.cs

99 lines
3 KiB
C#
Raw Permalink Normal View History

2013-10-28 07:09:42 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
namespace Server.Commands.Generic
{
public sealed class SortExtension : BaseExtension
{
public static ExtensionInfo ExtInfo = new ExtensionInfo(40, "Order", -1, delegate () { return new SortExtension(); });
2013-10-28 07:09:42 +00:00
private readonly List<OrderInfo> m_Orders;
private IComparer m_Comparer;
public SortExtension()
{
2020-04-16 21:29:55 -04:00
m_Orders = new List<OrderInfo>();
2013-10-28 07:09:42 +00:00
}
2020-04-16 20:21:33 -04:00
public override ExtensionInfo Info => ExtInfo;
2013-10-28 07:09:42 +00:00
public static void Initialize()
{
ExtensionInfo.Register(ExtInfo);
}
public override void Optimize(Mobile from, Type baseType, ref AssemblyEmitter assembly)
{
if (baseType == null)
throw new Exception("The ordering extension may only be used in combination with an object conditional.");
2020-04-16 21:29:55 -04:00
foreach (OrderInfo order in m_Orders)
2013-10-28 07:09:42 +00:00
{
order.Property.BindTo(baseType, PropertyAccess.Read);
order.Property.CheckAccess(from);
}
if (assembly == null)
assembly = new AssemblyEmitter("__dynamic", false);
2020-04-16 21:29:55 -04:00
m_Comparer = SortCompiler.Compile(assembly, baseType, m_Orders.ToArray());
2013-10-28 07:09:42 +00:00
}
public override void Parse(Mobile from, string[] arguments, int offset, int size)
{
if (size < 1)
throw new Exception("Invalid ordering syntax.");
if (Insensitive.Equals(arguments[offset], "by"))
{
++offset;
--size;
if (size < 1)
throw new Exception("Invalid ordering syntax.");
}
int end = offset + size;
while (offset < end)
{
string binding = arguments[offset++];
bool isAscending = true;
if (offset < end)
{
string next = arguments[offset];
switch (next.ToLower())
2013-10-28 07:09:42 +00:00
{
case "+":
case "up":
case "asc":
case "ascending":
isAscending = true;
++offset;
break;
case "-":
case "down":
case "desc":
case "descending":
isAscending = false;
++offset;
break;
}
}
Property property = new Property(binding);
2020-04-16 21:29:55 -04:00
m_Orders.Add(new OrderInfo(property, isAscending));
2013-10-28 07:09:42 +00:00
}
}
public override void Filter(ArrayList list)
{
2020-04-16 21:29:55 -04:00
if (m_Comparer == null)
2013-10-28 07:09:42 +00:00
throw new InvalidOperationException("The extension must first be optimized.");
2020-04-16 21:29:55 -04:00
list.Sort(m_Comparer);
2013-10-28 07:09:42 +00:00
}
}
}