2020-09-29 21:34:46 +01:00
using OWML.Common ;
2021-02-25 14:53:34 +00:00
using QSB.ItemSync.WorldObjects ;
2020-12-31 12:10:55 +00:00
using QSB.OrbSync.WorldObjects ;
2020-09-06 09:07:31 +01:00
using QSB.TransformSync ;
2020-09-29 21:34:46 +01:00
using QSB.Utility ;
2020-09-06 09:07:31 +01:00
using System ;
2020-09-04 20:57:35 +01:00
using System.Collections.Generic ;
2020-08-13 19:25:12 +02:00
using System.Linq ;
2020-09-06 09:07:31 +01:00
using System.Reflection ;
2020-12-23 22:43:05 +00:00
using UnityEngine ;
2020-08-13 19:25:12 +02:00
namespace QSB.WorldSync
{
2020-12-11 13:14:58 +00:00
public static class QSBWorldSync
2020-12-02 21:29:53 +00:00
{
2020-12-14 21:41:56 +01:00
public static List < NomaiOrbTransformSync > OrbSyncList { get ; } = new List < NomaiOrbTransformSync > ( ) ;
public static List < NomaiInterfaceOrb > OldOrbList { get ; set ; } = new List < NomaiInterfaceOrb > ( ) ;
public static List < CharacterDialogueTree > OldDialogueTrees { get ; set ; } = new List < CharacterDialogueTree > ( ) ;
2020-12-11 13:14:58 +00:00
public static Dictionary < string , bool > DialogueConditions { get ; } = new Dictionary < string , bool > ( ) ;
2020-12-19 10:56:25 +00:00
public static List < FactReveal > ShipLogFacts { get ; } = new List < FactReveal > ( ) ;
2020-09-22 21:11:29 +01:00
2020-12-24 09:41:38 +01:00
private static readonly List < IWorldObject > WorldObjects = new List < IWorldObject > ( ) ;
2020-12-14 21:41:56 +01:00
2020-12-24 10:06:17 +00:00
public static IEnumerable < TWorldObject > GetWorldObjects < TWorldObject > ( )
= > WorldObjects . OfType < TWorldObject > ( ) ;
2020-08-13 19:25:12 +02:00
2021-02-24 10:45:25 +00:00
public static TWorldObject GetWorldFromId < TWorldObject > ( int id )
2021-02-25 22:45:32 +00:00
{
if ( id < 0 | | id > = GetWorldObjects < TWorldObject > ( ) . Count ( ) )
{
DebugLog . DebugWrite ( $"Warning - Tried to find {typeof(TWorldObject).Name} id {id}. Count is {GetWorldObjects<TWorldObject>().Count()}." , MessageType . Warning ) ;
return default ;
}
return GetWorldObjects < TWorldObject > ( ) . ToList ( ) [ id ] ;
}
2020-09-06 09:07:31 +01:00
2021-02-24 10:45:25 +00:00
public static TWorldObject GetWorldFromUnity < TWorldObject , TUnityObject > ( TUnityObject unityObject )
2021-02-18 10:34:35 +00:00
where TWorldObject : WorldObject < TUnityObject >
where TUnityObject : MonoBehaviour
{
var allWorldObjects = GetWorldObjects < TWorldObject > ( ) ;
2021-02-18 15:36:11 +00:00
if ( allWorldObjects . Count ( ) = = 0 )
{
DebugLog . ToConsole ( $"Error - No worldobjects exist of type {typeof(TWorldObject).Name}!" , MessageType . Error ) ;
return null ;
}
2021-02-18 10:34:35 +00:00
var correctWorldObject = allWorldObjects . First ( x = > x . AttachedObject = = unityObject ) ;
return correctWorldObject ;
}
2021-02-24 10:45:25 +00:00
public static int GetIdFromUnity < TWorldObject , TUnityObject > ( TUnityObject unityObject )
where TWorldObject : WorldObject < TUnityObject >
where TUnityObject : MonoBehaviour
= > GetWorldFromUnity < TWorldObject , TUnityObject > ( unityObject ) . ObjectId ;
public static int GetIdFromTypeSubset < TTypeSubset > ( TTypeSubset typeSubset )
2021-02-25 22:45:32 +00:00
{
var index = GetWorldObjects < TTypeSubset > ( ) . ToList ( ) . IndexOf ( typeSubset ) ;
if ( index = = - 1 )
{
DebugLog . ToConsole ( $"Warning - {(typeSubset as IWorldObject).Name} doesn't exist in list of {typeof(TTypeSubset).Name} !" , MessageType . Warning ) ;
}
return index ;
}
2021-02-24 10:45:25 +00:00
2020-12-24 09:41:38 +01:00
public static void RemoveWorldObjects < TWorldObject > ( )
2021-02-09 21:35:31 +00:00
{
var itemsToRemove = WorldObjects . Where ( x = > x is TWorldObject ) ;
foreach ( var item in itemsToRemove )
{
2021-02-19 21:25:17 +00:00
try
{
item . OnRemoval ( ) ;
}
catch ( Exception e )
{
DebugLog . ToConsole ( $"Error - Exception in OnRemoval() for {item.GetType()}. Message : {e.Message}, Stack trace : {e.StackTrace}" , MessageType . Error ) ;
}
2021-02-09 21:35:31 +00:00
}
2021-02-14 09:43:36 +00:00
DebugLog . DebugWrite ( $"Removing {typeof(TWorldObject).Name} : {WorldObjects.Count(x => x is TWorldObject)} instances." ) ;
2021-02-09 21:35:31 +00:00
WorldObjects . RemoveAll ( x = > x is TWorldObject ) ;
}
2020-09-17 20:14:55 +01:00
2020-12-24 09:06:23 +01:00
public static List < TUnityObject > Init < TWorldObject , TUnityObject > ( )
where TWorldObject : WorldObject < TUnityObject >
2021-01-29 15:29:50 +00:00
where TUnityObject : MonoBehaviour
2020-12-02 21:29:53 +00:00
{
2021-02-14 09:43:36 +00:00
RemoveWorldObjects < TWorldObject > ( ) ;
2020-12-24 09:06:23 +01:00
var list = Resources . FindObjectsOfTypeAll < TUnityObject > ( ) . ToList ( ) ;
2021-02-10 19:34:41 +00:00
DebugLog . DebugWrite ( $"{typeof(TWorldObject).Name} init : {list.Count} instances." , MessageType . Info ) ;
2020-12-23 22:43:05 +00:00
for ( var id = 0 ; id < list . Count ; id + + )
2020-12-02 21:29:53 +00:00
{
2021-02-24 10:45:25 +00:00
var obj = CreateWorldObject < TWorldObject > ( ) ;
2020-12-23 22:43:05 +00:00
obj . Init ( list [ id ] , id ) ;
2020-12-02 21:29:53 +00:00
}
2020-12-23 22:43:05 +00:00
return list ;
2020-12-02 21:29:53 +00:00
}
2020-09-06 09:07:31 +01:00
2020-12-24 09:12:47 +01:00
private static TWorldObject CreateWorldObject < TWorldObject > ( )
2020-12-24 09:41:38 +01:00
where TWorldObject : IWorldObject
2020-12-24 09:12:47 +01:00
{
var worldObject = ( TWorldObject ) Activator . CreateInstance ( typeof ( TWorldObject ) ) ;
WorldObjects . Add ( worldObject ) ;
return worldObject ;
}
2020-12-02 21:29:53 +00:00
public static void RaiseEvent ( object instance , string eventName )
{
if ( ! ( instance . GetType ( )
. GetField ( eventName , BindingFlags . Instance | BindingFlags . NonPublic ) ?
. GetValue ( instance ) is MulticastDelegate multiDelegate ) )
{
return ;
}
var delegateList = multiDelegate . GetInvocationList ( ) . ToList ( ) ;
2020-12-06 17:00:23 +00:00
foreach ( var del in delegateList )
{
del . DynamicInvoke ( instance ) ;
}
2020-12-02 21:29:53 +00:00
}
2020-12-11 13:14:58 +00:00
2020-12-23 22:43:05 +00:00
public static void HandleSlotStateChange ( NomaiInterfaceSlot slot , NomaiInterfaceOrb affectingOrb , bool state )
{
var slotList = GetWorldObjects < QSBOrbSlot > ( ) . ToList ( ) ;
if ( ! slotList . Any ( ) )
{
return ;
}
2021-02-19 21:25:17 +00:00
var qsbSlot = slotList . FirstOrDefault ( x = > x . AttachedObject = = slot ) ;
if ( qsbSlot = = null )
{
DebugLog . ToConsole ( $"Error - No QSBOrbSlot found for {slot.name}!" , MessageType . Error ) ;
return ;
}
2020-12-23 22:43:05 +00:00
var orbSync = OrbSyncList . First ( x = > x . AttachedOrb = = affectingOrb ) ;
if ( orbSync . HasAuthority )
{
qsbSlot . HandleEvent ( state , OldOrbList . IndexOf ( affectingOrb ) ) ;
}
}
2020-12-11 22:42:21 +00:00
public static void SetDialogueCondition ( string name , bool state )
2020-12-11 13:14:58 +00:00
{
2020-12-14 16:24:52 +00:00
if ( ! QSBCore . IsServer )
2020-12-11 13:14:58 +00:00
{
2020-12-19 18:44:27 +00:00
DebugLog . ToConsole ( "Warning - Cannot write to condition dict when not server!" , MessageType . Warning ) ;
2020-12-11 13:14:58 +00:00
return ;
}
DialogueConditions [ name ] = state ;
}
2020-12-19 10:56:25 +00:00
public static void AddFactReveal ( string id , bool saveGame , bool showNotification )
{
if ( ! QSBCore . IsServer )
{
2020-12-19 18:44:27 +00:00
DebugLog . ToConsole ( "Warning - Cannot write to fact list when not server!" , MessageType . Warning ) ;
2020-12-19 10:56:25 +00:00
return ;
}
if ( ShipLogFacts . Any ( x = > x . Id = = id ) )
{
2021-01-18 12:33:07 +00:00
DebugLog . ToConsole ( $"Warning - Fact id {id} already in list!" , MessageType . Warning ) ;
2020-12-19 10:56:25 +00:00
return ;
}
ShipLogFacts . Add ( new FactReveal
{
Id = id ,
SaveGame = saveGame ,
ShowNotification = showNotification
} ) ;
}
2020-12-02 21:29:53 +00:00
}
2020-12-03 08:28:05 +00:00
}