/// <summary>
/// Organizes the item attributes of all item categories in the inventory system database.
/// </summary>
/// <param name="database">The inventory system database.</param>
public static void OrganizeItemAttributesOfAllItemCategory(InventorySystemDatabase database)
{
List<ItemCategory> organizedItemCategories = new List<ItemCategory>();
while (organizedItemCategories.Count != database.ItemCategories.Length)
{
foreach (var itemCategory in database.ItemCategories)
{
if (organizedItemCategories.Contains(itemCategory))
{
continue;
}
bool hasUnorganized = false;
var parents = itemCategory.ParentsReadOnly;
if (parents.Count == 0)
{
organizedItemCategories.Add(itemCategory);
continue;
}
foreach (var itemCategoryParent in parents)
{
if (!organizedItemCategories.Contains(itemCategoryParent))
{
hasUnorganized = true;
break;
}
}
if (hasUnorganized)
{
continue;
}
organizedItemCategories.Add(itemCategory);
List<AttributeBase> parentItemAttributes = new List<AttributeBase>();
List<AttributeBase> parentItemDefinitionAttributes = new List<AttributeBase>();
List<AttributeBase> parentItemCategoryAttributes = new List<AttributeBase>();
foreach (var parent in parents)
{
foreach (var itemAttribute in parent.ItemAttributeCollection)
{
bool exist = false;
foreach (var parentItemAttribute in parentItemAttributes)
{
if (itemAttribute.Name == parentItemAttribute.Name)
{
exist = true;
}
}
if (!exist)
{
parentItemAttributes.Add(itemAttribute);
}
}
foreach (var itemDefinitionAttribute in parent.ItemDefinitionAttributeCollection)
{
bool exist = false;
foreach (var parentItemDefinitionAttribute in parentItemDefinitionAttributes)
{
if (itemDefinitionAttribute.Name == parentItemDefinitionAttribute.Name)
{
exist = true;
}
}
if (!exist)
{
parentItemDefinitionAttributes.Add(itemDefinitionAttribute);
}
}
foreach (var itemCategoryAttribute in parent.ItemCategoryAttributeCollection)
{
bool exist = false;
foreach (var parentItemCategoryAttribute in parentItemCategoryAttributes)
{
if (itemCategoryAttribute.Name == parentItemCategoryAttribute.Name)
{
exist = true;
}
}
if (!exist)
{
parentItemCategoryAttributes.Add(itemCategoryAttribute);
}
}
}
bool dirty = false;
if (parentItemAttributes.Count != 0 &&
OrganizeItemAttributes(parentItemAttributes, itemCategory.ItemAttributeCollection.Attributes))
{
dirty = true;
}
if (parentItemDefinitionAttributes.Count != 0 &&
OrganizeItemAttributes(parentItemDefinitionAttributes, itemCategory.ItemDefinitionAttributeCollection.Attributes))
{
dirty = true;
}
if (parentItemCategoryAttributes.Count != 0 &&
OrganizeItemAttributes(parentItemCategoryAttributes, itemCategory.ItemCategoryAttributeCollection.Attributes))
{
dirty = true;
}
if (dirty)
{
Debug.Log($"Organizing item attributes of item category: {itemCategory.name}.");
//SetItemCategoryDirty(itemCategory, true);
}
}
}
}
/// <summary>
/// Organizes the item attributes of the parent order according to the target order.
/// </summary>
/// <param name="parentOrder">The parent order to follow.</param>
/// <param name="target">The target order to organize.</param>
/// <returns>Returns true if the target order was modified, false otherwise.</returns>
private static bool OrganizeItemAttributes(List<AttributeBase> parentOrder, ResizableArray<AttributeBase> target)
{
int targetCount = 0;
bool dirty = false;
for (int i = 0; i < parentOrder.Count; i++)
{
var parentAttribute = parentOrder[i];
for (int j = 0; j < target.Count; j++)
{
var atribute = target[j];
if (parentAttribute.Name == atribute.Name)
{
if (j != targetCount)
{
target.RemoveAt(j);
target.Insert(targetCount, atribute);
dirty = true;
}
targetCount++;
}
}
}
return dirty;
}