196 lines
5.4 KiB
C#
196 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using MelonLoader;
|
|
using StressLevelZero.Interaction;
|
|
using StressLevelZero.Pool;
|
|
using UnityEngine.Experimental.PlayerLoop;
|
|
using BoneSync.Networking.Messages;
|
|
using BoneSync.Networking;
|
|
using StressLevelZero.Data;
|
|
using System.Collections;
|
|
using StressLevelZero.Props;
|
|
using BoneSync.Patching;
|
|
using StressLevelZero.Props.Weapons;
|
|
using StressLevelZero.AI;
|
|
using PuppetMasta;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
namespace BoneSync.Sync.Components
|
|
{
|
|
|
|
public static class TransformExtensions
|
|
{
|
|
public static string GetPath(this Transform current)
|
|
{
|
|
if (current.parent == null)
|
|
return "/" + current.name;
|
|
return current.parent.GetPath() + "/" + current.name;
|
|
}
|
|
}
|
|
|
|
[RegisterTypeInIl2Cpp]
|
|
public partial class Syncable : MonoBehaviour
|
|
{
|
|
public const int SYNC_FPS = 20;
|
|
|
|
public static Dictionary<GameObject, Syncable> syncablesCache = new Dictionary<GameObject, Syncable>();
|
|
public Syncable(IntPtr intPtr) : base(intPtr) {
|
|
syncablesCache[gameObject] = this;
|
|
}
|
|
|
|
private bool _syncCoroutineRunning;
|
|
|
|
public ulong _ownerId
|
|
{
|
|
private set;
|
|
get;
|
|
}
|
|
private ushort _syncId;
|
|
private float _lastSyncTime;
|
|
|
|
private bool _attemptedRegister;
|
|
|
|
public bool Registered => _syncId != 0;
|
|
public bool isStale => Time.time - _lastSyncTime > 5f;
|
|
public bool isOwner => _ownerId == BoneSync.lobby.GetLocalId();
|
|
|
|
public bool IsHolding()
|
|
{
|
|
if (interactableManager)
|
|
{
|
|
return interactableManager.grabbedHosts.Count > 0;
|
|
}
|
|
if (interactableHost)
|
|
{
|
|
return interactableHost.isAttached;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public InteractableHost interactableHost { private set; get; }
|
|
public InteractableHostManager interactableManager { private set; get; }
|
|
public Poolee poolee { private set; get; }
|
|
|
|
private Prop_Health propHealth;
|
|
private ObjectDestructable objectDestructable;
|
|
|
|
private Rigidbody[] rigidbodies;
|
|
private Transform[] _transforms;
|
|
|
|
private Gun gun;
|
|
private Magazine magazine;
|
|
|
|
private Plug[] plugs;
|
|
|
|
private AIBrain aiBrain;
|
|
private PuppetMaster puppetMaster;
|
|
|
|
private SpawnFragment spawnFragment;
|
|
|
|
|
|
public void OnEnable()
|
|
{
|
|
syncablesCache[gameObject] = this;
|
|
|
|
FindComponents();
|
|
|
|
bool shouldAutoSync = CheckIfShouldAutoSync();
|
|
if (shouldAutoSync && (BoneSync.lobby.IsHost || ClientSpawningAllowed()))
|
|
{
|
|
MelonLogger.Msg("AutoSyncing: " + transform.GetPath());
|
|
RegisterSyncable();
|
|
}
|
|
}
|
|
|
|
public bool CheckIfShouldAutoSync()
|
|
{
|
|
if (poolee && poolee.pool) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public string GetSyncableWorldPath()
|
|
{
|
|
if (poolee && poolee.pool)
|
|
{
|
|
return "";
|
|
}
|
|
if (interactableHost || interactableManager)
|
|
{
|
|
return transform.GetPath().Replace(" ", "[]");
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public void FindComponents()
|
|
{
|
|
ObjectSyncCache.RemoveSyncable(this);
|
|
|
|
interactableManager = GetComponent<InteractableHostManager>();
|
|
interactableHost = GetComponent<InteractableHost>();
|
|
poolee = GetComponent<Poolee>();
|
|
propHealth = GetComponent<Prop_Health>();
|
|
objectDestructable = GetComponent<ObjectDestructable>();
|
|
gun = GetComponent<Gun>();
|
|
magazine = GetComponent<Magazine>();
|
|
plugs = GetComponentsInChildren<Plug>();
|
|
spawnFragment = GetComponent<SpawnFragment>();
|
|
//rigidbodies = GetComponentsInChildren<Rigidbody>();
|
|
UpdateTransformList();
|
|
|
|
ObjectSyncCache.AddSyncable(this);
|
|
}
|
|
|
|
public bool CanBeSynced()
|
|
{
|
|
if (spawnFragment) return false; // if has spawn fragment, don't sync
|
|
FindComponents();
|
|
if (rigidbodies.Length > 0) return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
public void OnDestroy()
|
|
{
|
|
DiscardSyncable();
|
|
//MelonLogger.Msg("Syncable destroyed: " + transform.GetPath());
|
|
}
|
|
|
|
public void DiscardSyncable()
|
|
{
|
|
if (Registered)
|
|
{
|
|
MelonLogger.Warning("Discarding registered syncable: " + transform.GetPath());
|
|
}
|
|
syncablesCache.Remove(gameObject);
|
|
ObjectSyncCache.RemoveSyncable(this);
|
|
Destroy(this);
|
|
}
|
|
|
|
public void OnDisable()
|
|
{
|
|
DiscardSyncable();
|
|
}
|
|
|
|
public void RegisterSyncable()
|
|
{
|
|
if (!BoneSync.lobby.IsConnected()) return;
|
|
if (Registered)
|
|
{
|
|
TryBecomeOwner();
|
|
return;
|
|
}
|
|
if (_attemptedRegister) return;
|
|
if (!CanBeSynced()) return;
|
|
_attemptedRegister = true;
|
|
_SendRegisterSync();
|
|
}
|
|
}
|
|
}
|