/// <summary>
        /// Adds/subtracts a value from all of the pointers in the script that point to an offset after the script.
        /// </summary>
        /// <param name="delta">The value to add/subtract.</param>
        private void UpdatePointersAfterScript()
        {
            int delta = this.endOffset - this.endInternalOffset;
            //
            ScriptIterator it = new ScriptIterator(this);

            while (!it.IsDone)
            {
                EventActionCommand eac = it.Next();
                int pointer;
                if (eac.Opcode == 0x42 || eac.Opcode == 0x67 || eac.Opcode == 0xE9)
                {
                    if (eac.GetType() == typeof(EventCommand) || eac.Opcode == 0xE9)
                    {
                        pointer = eac.ReadPointerSpecial(0);
                        if (pointer >= (this.endInternalOffset & 0xFFFF) && !eac.PointerChanged[0])
                        {
                            eac.WritePointerSpecial(0, (ushort)(pointer + delta));
                            eac.PointerChanged[0] = true;
                        }
                        pointer = eac.ReadPointerSpecial(1);
                        if (pointer >= (this.endInternalOffset & 0xFFFF) && !eac.PointerChanged[1])
                        {
                            eac.WritePointerSpecial(1, (ushort)(pointer + delta));
                            eac.PointerChanged[1] = true;
                        }
                    }
                    else
                    {
                        pointer = eac.ReadPointer();
                        if (pointer >= (this.endInternalOffset & 0xFFFF) && !eac.PointerChanged[0])
                        {
                            eac.WritePointer((ushort)(pointer + delta));
                            eac.PointerChanged[0] = true;
                        }
                    }
                }
                else
                {
                    pointer = eac.ReadPointer();
                    if (pointer >= (this.endInternalOffset & 0xFFFF) && !eac.PointerChanged[0])
                    {
                        eac.WritePointer((ushort)(pointer + delta));
                        eac.PointerChanged[0] = true;
                    }
                }
            }
        }
        /// <summary>
        /// Updates all of the pointers in the script pointing directly to a given command's offset.
        /// </summary>
        /// <param name="reference">The reference command in the script.</param>
        private void UpdatePointersToCommand(EventActionCommand reference)
        {
            ScriptIterator it = new ScriptIterator(this);

            while (!it.IsDone)
            {
                EventActionCommand eac = it.Next();
                int pointer;
                if (eac.Opcode == 0x42 || eac.Opcode == 0x67 || eac.Opcode == 0xE9)
                {
                    if (eac.GetType() == typeof(EventCommand) || eac.Opcode == 0xE9)
                    {
                        pointer = eac.ReadPointerSpecial(0);
                        if (pointer == (reference.InternalOffset & 0xFFFF) && !eac.PointerChanged[0])
                        {
                            eac.WritePointerSpecial(0, (ushort)(reference.Offset & 0xFFFF));
                            eac.PointerChanged[0] = true;
                        }
                        pointer = eac.ReadPointerSpecial(1);
                        if (pointer == (reference.InternalOffset & 0xFFFF) && !eac.PointerChanged[1])
                        {
                            eac.WritePointerSpecial(1, (ushort)(reference.Offset & 0xFFFF));
                            eac.PointerChanged[1] = true;
                        }
                    }
                    else
                    {
                        pointer = eac.ReadPointer();
                        if (pointer == (reference.InternalOffset & 0xFFFF) && !eac.PointerChanged[0])
                        {
                            eac.WritePointer((ushort)(reference.Offset & 0xFFFF));
                            eac.PointerChanged[0] = true;
                        }
                    }
                }
                else
                {
                    pointer = eac.ReadPointer();
                    if (pointer == (reference.InternalOffset & 0xFFFF) && !eac.PointerChanged[0])
                    {
                        eac.WritePointer((ushort)(reference.Offset & 0xFFFF));
                        eac.PointerChanged[0] = true;
                    }
                }
            }
        }
        public void Refresh()
        {
            if (commands == null)
            {
                return;
            }
            Assemble();
            // refresh offsets
            int offset = baseOffset;

            foreach (EventCommand esc in commands)
            {
                esc.RefreshOffsets(offset);
                offset += esc.Length;
            }
            // update internal pointers
            EventActionCommand eac;
            ScriptIterator     it = new ScriptIterator(this);

            while (!it.IsDone)
            {
                eac = it.Next();
                eac.PointerChanged = new bool[256];
            }
            // if undo/redo, pointers update by raw script change
            if (!Undoing && State.Instance.AutoPointerUpdate)
            {
                UpdatePointersAfterScript();
            }
            it = new ScriptIterator(this);
            while (!it.IsDone)
            {
                eac = it.Next();
                if (!Undoing && State.Instance.AutoPointerUpdate)
                {
                    UpdatePointersToCommand(eac);
                }
                eac.InternalOffset = eac.Offset;
            }
        }