/////////////////////////////////////////////////////////



        // private functions

        private IFSMConstructor CreateConstructor(Type constructorType, object[] initObjects)

        {
            IFSMConstructor ret = null;



            // No parameters in the constructor

            ConstructorInfo constructor = constructorType.GetConstructor(new Type[] {});

            ret = (IFSMConstructor)constructor.Invoke(new object[] {});

            ret.Init(initObjects);

            return(ret);
        }
        private void AddConstructor_Internal(string szChapterName, Type constructorType, object[] initObjects)

        {
            if (_RunningState == RunningState.Running)

            {
                throw new FSMLibraryAlreadyRunningException("FSM Library is already running!");
            }

            if (_RunningState == RunningState.Paused)

            {
                throw new FSMLibraryNotRunningException("FSM Library is paused!");
            }

            IFSMConstructor constructor = CreateConstructor(constructorType, initObjects);

            if (!string.IsNullOrEmpty(szChapterName))

            {
                constructor.Chapter = szChapterName;
            }



            if (_FSMChapters.ContainsKey(constructor.ChapterHash))

            {
                throw new StateExistsException("Constructor \"" + constructor.Chapter + "\" already exists!");
            }



            if (!_FSMChapters.ContainsKey(constructor.ChapterHash))

            {
                AddChapter(constructor.ChapterHash);
            }

            _Constructors.Add(constructor);
        }
        private void PriorityUpdate_Internal()

        {
            if (_RunningState == RunningState.Suspended)

            {
                throw new FSMLibraryNotRunningException("FSM Library isnt running!");
            }

            if (_RunningState == RunningState.Paused)

            {
                throw new FSMLibraryNotRunningException("FSM Library is paused!");
            }



            // remove all stopped FSMs

            if (_ConstructorToStop.Count > 0)

            {
                foreach (ConstructorEndData ced in _ConstructorToStop)

                {
                    IFSMConstructor constructor = CreateConstructor(ced.ConstructorType, null);



                    if (!string.IsNullOrEmpty(ced.ChapterNameOverride))
                    {
                        constructor.Chapter = ced.ChapterNameOverride;
                    }

                    GetChapter(constructor.ChapterHash).StopChapter();

                    constructor.Destroy();



                    _FSMChapters.Remove(constructor.ChapterHash);
                }

                _ConstructorToStop.Clear();
            }



            // process all added FSMs

            if (_ConstructorToStart.Count > 0)

            {
                foreach (ConstructorStartData data in _ConstructorToStart)

                {
                    IFSMConstructor constructor = CreateConstructor(data.ConstructorType, data.Passthrough);

                    if (!string.IsNullOrEmpty(data.ChapterNameOverride))
                    {
                        constructor.Chapter = data.ChapterNameOverride;
                    }

                    constructor.Build();



                    FSMChapter chapt = GetChapter(constructor.ChapterHash);

                    object[] finalParams;

                    if (data.Passthrough != null && data.Passthrough.Length > 0)

                    {
                        finalParams = new object[data.Passthrough.Length + 1];

                        Array.Copy(data.Passthrough, 0, finalParams, 1, data.Passthrough.Length);
                    }

                    else

                    {
                        finalParams = new object[] { chapt };
                    }

                    chapt.StartChapter(finalParams);
                }

                _ConstructorToStart.Clear();
            }



            foreach (FSMChapter chapter in _FSMChapters.Values)

            {
                chapter.PriorityUpdate();
            }
        }