Merge pull request #716 from qsb-dev/modded-item-support

Add QSBItem support for OWItems that lack dedicated derived classes
This commit is contained in:
xen-42 2025-03-01 21:05:23 -05:00 committed by GitHub
commit 7d4ad5dac0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 57 additions and 17 deletions

2
.editorconfig Normal file
View File

@ -0,0 +1,2 @@
[*.cs]
indent_style = tab

View File

@ -1,4 +1,5 @@
using Cysharp.Threading.Tasks;
using HarmonyLib;
using OWML.Common;
using QSB.ItemSync.WorldObjects;
using QSB.ItemSync.WorldObjects.Items;
@ -6,6 +7,8 @@ using QSB.ItemSync.WorldObjects.Sockets;
using QSB.Utility;
using QSB.Utility.Deterministic;
using QSB.WorldSync;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;
@ -21,21 +24,66 @@ public class ItemManager : WorldObjectManager
DebugLog.DebugWrite("Building OWItems...", MessageType.Info);
// Items
QSBWorldSync.Init<QSBNomaiConversationStone, NomaiConversationStone>();
QSBWorldSync.Init<QSBScrollItem, ScrollItem>();
QSBWorldSync.Init<QSBSharedStone, SharedStone>();
QSBWorldSync.Init<QSBSimpleLanternItem, SimpleLanternItem>();
QSBWorldSync.Init<QSBSlideReelItem, SlideReelItem>();
QSBWorldSync.Init<QSBWarpCoreItem, WarpCoreItem>();
// dream lantern and vision torch are set up in their own managers
// the rest can just use the generic thing below because they dont have special things using them
// Use the basic QSBItem class for any items that do not require custom code through a derived class (mod compatibility)
// QSB addons can still define their own QSBItem derived classes and they will just get skipped here
var handledItemTypes = new HashSet<Type>(GetHandledItemTypes()); // set cuz we do Contains below
DebugLog.DebugWrite($"Handled OWItem types (the rest will get generic QSBItem support) are: {handledItemTypes.Join()}");
var otherItemlistToInitFrom = QSBWorldSync.GetUnityObjects<OWItem>()
.Where(x => !handledItemTypes.Contains(x.GetType()))
.SortDeterministic();
// could make a subclass for this but i dont care, and would have to filter out from reflection thing below
QSBWorldSync.Init<QSBItem<OWItem>, OWItem>(otherItemlistToInitFrom);
// Sockets
QSBWorldSync.Init<QSBItemSocket, OWItemSocket>();
// other drop targets that don't already have world objects
// breaks if mod adds custom subclass of IItemDropTarget and then makes WorldObject of that but can just fix that when that happens
var listToInitFrom = QSBWorldSync.GetUnityObjects<MonoBehaviour>()
.Where(x => x is IItemDropTarget and not (RaftDock or RaftController or PrisonCellElevator))
.SortDeterministic();
QSBWorldSync.Init<QSBOtherDropTarget, MonoBehaviour>(listToInitFrom);
}
/// <summary>
/// Gets all types that extend QSBItem and returns the list of OWItem types that are already handled by dedicated classes
/// </summary>
private static IEnumerable<Type> GetHandledItemTypes()
{
var assemblies = QSBCore.Addons.Values
.Select(x => x.GetType().Assembly)
.Append(typeof(QSBCore).Assembly);
if (QSBCore.QSBNHAssembly != null)
{
assemblies = assemblies.Append(QSBCore.QSBNHAssembly);
}
// If the class inherits from QSBItem<T>, this will return what T is else null
static Type GetTypeFromQSBItem(Type type)
{
if (type.IsInterface || type.IsAbstract || type.BaseType == null)
{
return null;
}
if (type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(QSBItem<>))
{
return type.BaseType.GetGenericArguments()[0];
}
else
{
return GetTypeFromQSBItem(type.BaseType);
}
}
return assemblies.SelectMany(x => x.GetTypes())
.Select(GetTypeFromQSBItem)
.Where(x => x != null)
.OrderBy(x => x.FullName);
}
}

View File

@ -12,6 +12,8 @@ using UnityEngine;
namespace QSB.ItemSync.WorldObjects.Items;
// not abstract so modded items can use this QSBItem<OWItem>
// could use QSBOtherItem subclass but i dont feel like it
public class QSBItem<T> : WorldObject<T>, IQSBItem
where T : OWItem
{

View File

@ -1,3 +0,0 @@
namespace QSB.ItemSync.WorldObjects.Items;
public class QSBNomaiConversationStone : QSBItem<NomaiConversationStone> { }

View File

@ -1,3 +0,0 @@
namespace QSB.ItemSync.WorldObjects.Items;
public class QSBScrollItem : QSBItem<ScrollItem> { }

View File

@ -1,3 +0,0 @@
namespace QSB.ItemSync.WorldObjects.Items;
public class QSBSharedStone : QSBItem<SharedStone> { }

View File

@ -1,3 +0,0 @@
namespace QSB.ItemSync.WorldObjects.Items;
public class QSBSimpleLanternItem : QSBItem<SimpleLanternItem> { }

View File

@ -3,4 +3,4 @@
public class QSBWarpCoreItem : QSBItem<WarpCoreItem>
{
public bool IsVesselCoreType() => AttachedObject.IsVesselCoreType();
}
}