private void SpawnObject(Vector3 position, Quaternion rotation)
        {
            this.Log("Spawning Object " + objectSpawn_.ObjectPrefab.name + " at " + position + " and rotataion " + rotation);
            GameObject spawnedObject = CyanEmuMain.SpawnObject(objectSpawn_.ObjectPrefab, position, rotation);

            spawnedObjects_.Add(spawnedObject);
        }
Ejemplo n.º 2
0
        private static void ExecuteEventForObject(VRC_EventHandler.VrcEvent triggerEvent, GameObject obj, VRC_EventHandler.EventInfo eventInfo, VRC_Trigger.TriggerEvent originalTrigger)
        {
            instance_.Log("Executing Trigger Event: " + triggerEvent.GetEventAsString(obj) + "\n_On Trigger: " + originalTrigger?.GetTriggerEventAsString());

            bool isBufferedExecution = eventInfo.broadcast.IsBufferedBroadcastType() && eventInfo.instagator == null;

            if (eventInfo.broadcast.IsOwnerBroadcastType())
            {
                if (!Networking.IsOwner(obj) || isBufferedExecution)
                {
                    instance_.LogWarning("Not executing as user is not the owner");
                    return;
                }
            }

            if (!isBufferedExecution && eventInfo.broadcast.IsMasterBroadcastType())
            {
                if (!Networking.IsMaster || isBufferedExecution)
                {
                    instance_.LogWarning("Not executing as user is not the master");
                    return;
                }
            }

            if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.ActivateCustomTrigger)
            {
                // Only activates first one
                VRC_Trigger trigger = obj.GetComponent <VRC_Trigger>();
                if (obj.activeInHierarchy && trigger.enabled)
                {
                    trigger.ExecuteCustomTrigger(triggerEvent.ParameterString);
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationBool)
            {
                Animator animator = obj.GetComponent <Animator>();
                bool     value    = animator.GetBool(triggerEvent.ParameterString);
                bool     newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, value);
                animator.SetBool(triggerEvent.ParameterString, newValue);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationInt)
            {
                Animator animator = obj.GetComponent <Animator>();
                animator.SetInteger(triggerEvent.ParameterString, triggerEvent.ParameterInt);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationFloat)
            {
                Animator animator = obj.GetComponent <Animator>();
                animator.SetFloat(triggerEvent.ParameterString, triggerEvent.ParameterFloat);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntAdd)
            {
                Animator animator = obj.GetComponent <Animator>();
                int      value    = animator.GetInteger(triggerEvent.ParameterString);
                animator.SetInteger(triggerEvent.ParameterString, value + triggerEvent.ParameterInt);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntDivide)
            {
                Animator animator = obj.GetComponent <Animator>();
                int      value    = animator.GetInteger(triggerEvent.ParameterString);
                animator.SetInteger(triggerEvent.ParameterString, value / triggerEvent.ParameterInt);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntMultiply)
            {
                Animator animator = obj.GetComponent <Animator>();
                int      value    = animator.GetInteger(triggerEvent.ParameterString);
                animator.SetInteger(triggerEvent.ParameterString, value * triggerEvent.ParameterInt);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntSubtract)
            {
                Animator animator = obj.GetComponent <Animator>();
                int      value    = animator.GetInteger(triggerEvent.ParameterString);
                animator.SetInteger(triggerEvent.ParameterString, value - triggerEvent.ParameterInt);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationTrigger)
            {
                obj.GetComponent <Animator>().SetTrigger(triggerEvent.ParameterString);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.PlayAnimation)
            {
                obj.GetComponent <Animation>().Play(triggerEvent.ParameterString);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AudioTrigger)
            {
                AudioSource[] audioSources = obj.GetComponents <AudioSource>();
                foreach (var source in audioSources)
                {
                    if (source.clip.name == triggerEvent.ParameterString)
                    {
                        source.Play();
                    }
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.DestroyObject)
            {
                Destroy(obj);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SendRPC)
            {
                object[] parameters     = VRC_Serialization.ParameterDecoder(triggerEvent.ParameterBytes);
                Type[]   parameterTypes = new Type[parameters.Length];
                for (int paramIndex = 0; paramIndex < parameters.Length; ++paramIndex)
                {
                    parameterTypes[paramIndex] = parameters[paramIndex].GetType();
                }

                foreach (MonoBehaviour mono in obj.GetComponents <MonoBehaviour>())
                {
                    MethodInfo methodInfo = mono.GetType().GetMethod(triggerEvent.ParameterString, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, Type.DefaultBinder, parameterTypes, null);

                    if (methodInfo != null)
                    {
                        methodInfo.Invoke(mono, parameters);
                    }
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetComponentActive)
            {
                Type type = instance_.GetTypeForComponent(triggerEvent.ParameterString);
                if (type != null)
                {
                    PropertyInfo property = type.GetProperty("enabled");
                    if (property != null)
                    {
                        foreach (Component component in obj.GetComponents(type))
                        {
                            bool value    = (bool)property.GetValue(component, null);
                            bool newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, value);
                            property.SetValue(component, newValue, null);
                        }
                    }
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetGameObjectActive)
            {
                bool newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, obj.activeSelf);
                obj.SetActive(newValue);

                CyanEmuTriggerHelper triggerHelper = obj.GetComponent <CyanEmuTriggerHelper>();
                if (triggerHelper != null && isTriggerGlobalBroadcast_)
                {
                    if (newValue && triggerHelper.HasGlobalOnDisable)
                    {
                        instance_.LogWarning("Posisble OnEnable or OnTimer oversync!");
                    }
                    else if (!newValue && triggerHelper.HasGlobalOnDisable)
                    {
                        instance_.LogWarning("Posisble OnDisable oversync!");
                    }
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetLayer)
            {
                obj.layer = triggerEvent.ParameterInt;
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetMaterial)
            {
                Material mat = VRC_SceneDescriptor.GetMaterial(triggerEvent.ParameterString);
                obj.GetComponent <Renderer>().material = mat;
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetParticlePlaying)
            {
                ParticleSystem system   = obj.GetComponent <ParticleSystem>();
                bool           newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, system.isPlaying);
                if (newValue)
                {
                    system.Play();
                }
                else
                {
                    system.Stop();
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetUIText)
            {
                obj.GetComponent <UnityEngine.UI.Text>().text = triggerEvent.ParameterString;
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SpawnObject)
            {
                GameObject prefab = VRC_SceneDescriptor.GetPrefab(triggerEvent.ParameterString);
                CyanEmuMain.SpawnObject(prefab, obj.transform.position, obj.transform.rotation);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.TeleportPlayer)
            {
                if (isBufferedExecution)
                {
                    instance_.LogWarning("Teleport player actions should not be buffered. Ignoring");
                }
                else
                {
                    if (CyanEmuPlayerController.instance != null)
                    {
                        CyanEmuPlayerController.instance.Teleport(triggerEvent.ParameterObjects[0].transform, triggerEvent.ParameterBoolOp == VRC_EventHandler.VrcBooleanOp.True);
                    }
                    else
                    {
                        instance_.LogWarning("No player container to teleport!");
                    }

                    if (eventInfo.broadcast != VRC_EventHandler.VrcBroadcastType.Local)
                    {
                        instance_.LogWarning("TeleportPlayer action should be set to local!");
                    }
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddForce)
            {
                HandleTriggerPhysicsEvent(triggerEvent, obj);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddVelocity)
            {
                HandleTriggerPhysicsEvent(triggerEvent, obj);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetVelocity)
            {
                HandleTriggerPhysicsEvent(triggerEvent, obj);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddAngularVelocity)
            {
                HandleTriggerPhysicsEvent(triggerEvent, obj);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetAngularVelocity)
            {
                HandleTriggerPhysicsEvent(triggerEvent, obj);
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddDamage)
            {
                HandleTriggerDamageEvent(triggerEvent, obj);

                if (isBufferedExecution)
                {
                    instance_.LogWarning("AddDamage action should not be buffered!");
                }
                else if (eventInfo.broadcast != VRC_EventHandler.VrcBroadcastType.Local)
                {
                    instance_.LogWarning("AddDamage action should be set to local!");
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddHealth)
            {
                HandleTriggerDamageEvent(triggerEvent, obj);

                if (isBufferedExecution)
                {
                    instance_.LogWarning("AddHealth action should not be buffered!");
                }
                else if (eventInfo.broadcast != VRC_EventHandler.VrcBroadcastType.Local)
                {
                    instance_.LogWarning("AddHealth action should be set to local!");
                }
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetWebPanelURI)
            {
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetWebPanelVolume)
            {
            }
            else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.MeshVisibility)
            {
            }
        }