<commit_before>﻿/*
 * Author: <NAME> AKA Sarbian, France
 * License: Attribution 4.0 International (CC BY 4.0): http://creativecommons.org/licenses/by/4.0/
 * 
 * Thanks to Nothke for all the feature ideas, testing and feedback
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UnityEngine;

[KSPAddon(KSPAddon.Startup.EveryScene, false)]
internal class PersistentEmitterManager : MonoBehaviour
{
    public static PersistentEmitterManager Instance { get; private set; }

    private static List<PersistentKSPParticleEmitter> persistentEmitters;

    private void Awake()
    {
        PersistentEmitterManager.Instance = this;

        persistentEmitters = new List<PersistentKSPParticleEmitter>();

        GameEvents.onGameSceneLoadRequested.Add(new EventData<GameScenes>.OnEvent(this.OnSceneChange));
    }

    private void OnDestroy()
    {
        GameEvents.onGameSceneLoadRequested.Remove(new EventData<GameScenes>.OnEvent(this.OnSceneChange));
    }

    public static void Add(PersistentKSPParticleEmitter pkpe)
    {
        persistentEmitters.Add(pkpe);
        EffectBehaviour.AddParticleEmitter(pkpe.pe);
    }

    private void OnSceneChange(GameScenes scene)
    {
        for (int i = 0; i < persistentEmitters.Count; i++)
        {
            EffectBehaviour.RemoveParticleEmitter(persistentEmitters[i].pe);
            if (persistentEmitters[i].go.transform.parent == null)
            {
                Destroy(persistentEmitters[i].go);
            }
        }
        persistentEmitters = new List<PersistentKSPParticleEmitter>();
    }

    public void FixedUpdate()
    {
        var persistentEmittersCopy = persistentEmitters.ToArray();
        for (int i = 0; i < persistentEmittersCopy.Length; i++)
        {
            if (persistentEmittersCopy[i].endTime > 0 && persistentEmittersCopy[i].endTime < Time.fixedTime)
            {
                persistentEmittersCopy[i].EmissionStop();
            }

            // If the gameObject is null ( when ? I forgot ... )
            // or the tranform parent is null ( Emitter detached from part so the particle are not removed instantly )
            // then the emitter won't be updated by the effect FixedUpdate Call. So update it here
            if (persistentEmittersCopy[i].go == null 
                || persistentEmittersCopy[i].go.transform == null 
                || persistentEmittersCopy[i].go.transform.parent == null)
            {
                persistentEmittersCopy[i].EmitterOnUpdate(Vector3.zero);

                if (persistentEmittersCopy[i].pe.pe.particles.Count() == 0)
                {
                    EffectBehaviour.RemoveParticleEmitter(persistentEmittersCopy[i].pe);
                    persistentEmitters.Remove(persistentEmittersCopy[i]);
                    if (persistentEmittersCopy[i].go != null)
                    {
                        Destroy(persistentEmittersCopy[i].go);
                    }
                }
            }
        }
    }

    private void Print(string s)
    {
        MonoBehaviour.print(this.GetType().Name + " : " + s);
    }
}<commit_msg>Catch Exceptions tossed by GameObject.transform after parts have been removed by Unity.<commit_after>﻿/*
 * Author: <NAME> AKA Sarbian, France
 * License: Attribution 4.0 International (CC BY 4.0): http://creativecommons.org/licenses/by/4.0/
 * 
 * Thanks to Nothke for all the feature ideas, testing and feedback
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UnityEngine;

[KSPAddon(KSPAddon.Startup.EveryScene, false)]
internal class PersistentEmitterManager : MonoBehaviour
{
    public static PersistentEmitterManager Instance { get; private set; }

    private static List<PersistentKSPParticleEmitter> persistentEmitters;

    private void Awake()
    {
        PersistentEmitterManager.Instance = this;

        persistentEmitters = new List<PersistentKSPParticleEmitter>();

        GameEvents.onGameSceneLoadRequested.Add(new EventData<GameScenes>.OnEvent(this.OnSceneChange));
    }

    private void OnDestroy()
    {
        GameEvents.onGameSceneLoadRequested.Remove(new EventData<GameScenes>.OnEvent(this.OnSceneChange));
    }

    public static void Add(PersistentKSPParticleEmitter pkpe)
    {
        persistentEmitters.Add(pkpe);
        EffectBehaviour.AddParticleEmitter(pkpe.pe);
    }

    private void OnSceneChange(GameScenes scene)
    {
        for (int i = 0; i < persistentEmitters.Count; i++)
        {
            EffectBehaviour.RemoveParticleEmitter(persistentEmitters[i].pe);
            if (persistentEmitters[i].go.transform.parent == null)
            {
                Destroy(persistentEmitters[i].go);
            }
        }
        persistentEmitters = new List<PersistentKSPParticleEmitter>();
    }

    public void FixedUpdate()
    {
        var persistentEmittersCopy = persistentEmitters.ToArray();
        for (int i = 0; i < persistentEmittersCopy.Length; i++)
        {
            if (persistentEmittersCopy[i].endTime > 0 && persistentEmittersCopy[i].endTime < Time.fixedTime)
            {
                persistentEmittersCopy[i].EmissionStop();
            }

            // If the gameObject is null ( when ? I forgot ... )
            // or the tranform parent is null ( Emitter detached from part so the particle are not removed instantly )
            // then the emitter won't be updated by the effect FixedUpdate Call. So update it here
            if (persistentEmittersCopy[i].go == null)
            {
                Transform pecTransform = null;
                
                try
                {
                    pecTransform = persistentEmittersCopy[i].go.transform;
                }
                catch (NullReferenceException)
                {
                    // Destroy the gameobject so avoid exception handling in the future?
                    continue;
                }
                
                if (persistentEmittersCopy[i].go.transform.parent == null)
                {
                    persistentEmittersCopy[i].EmitterOnUpdate(Vector3.zero);

                    if (persistentEmittersCopy[i].pe.pe.particles.Count() == 0)
                    {
                        EffectBehaviour.RemoveParticleEmitter(persistentEmittersCopy[i].pe);
                        persistentEmitters.Remove(persistentEmittersCopy[i]);
                        if (persistentEmittersCopy[i].go != null)
                        {
                            Destroy(persistentEmittersCopy[i].go);
                        }
                    }
                }
            }
        }
    }

    private void Print(string s)
    {
        MonoBehaviour.print(this.GetType().Name + " : " + s);
    }
}
{'fraction_non_alphanumeric': 0.09184574624668826, 'fraction_numerical': 0.0032381513099793936, 'mean_word_length': 2.0914467697907186, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6857666', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1854, 'n_words': 379}