Example #1
0
        public static void LinkFiles(string rootFolder, string destination, out int exitCode, string[] exclusions, string[] copyInstead)
        {
            exitCode = 1;

            FileInfo[] files = new DirectoryInfo(rootFolder).GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i];

                string lower   = file.Name.ToLower();
                bool   exclude = false;
                for (int j = 0; j < exclusions.Length; j++)
                {
                    string exc = exclusions[j];
                    if (lower.Contains(exc))
                    {
                        // check if the file is i
                        exclude = true;
                        break;
                    }
                }

                if (exclude)
                {
                    continue;
                }

                for (int j = 0; j < copyInstead.Length; j++)
                {
                    string copy = copyInstead[j];
                    if (lower.Contains(copy))
                    {
                        exclude = true;
                        break;
                    }
                }

                string relative = file.FullName.Replace(rootFolder + @"\", "");
                string linkPath = Path.Combine(destination, relative);
                if (exclude)
                {
                    // should copy!
                    File.Copy(file.FullName, linkPath, true);
                }
                else
                {
                    //CmdUtil.MkLinkFile(file.FullName, linkPath, out exitCode);
                    Kernel32Interop.CreateSymbolicLink(linkPath, file.FullName, SymbolicLink.File);
                }
            }
        }
Example #2
0
        public static void LinkDirectory(string root, DirectoryInfo currentDir, string destination, out int exitCode,
                                         string[] dirExclusions, string[] fileExclusions, string[] fileCopyInstead, bool overrideSpecial = false)
        {
            Console.WriteLine($"Symlinking folder {root} to {destination}");

            exitCode = 1;

            bool special = overrideSpecial;

            for (int j = 0; j < dirExclusions.Length; j++)
            {
                string exclusion = dirExclusions[j];
                string fullPath  = Path.Combine(root, exclusion).ToLower();

                if (fullPath.Contains(currentDir.FullName.ToLower()))
                {
                    // special case, one of our subfolders is excluded
                    special = true;
                    break;
                }
            }

            if (special)
            {
                // this folder has a child that cant be symlinked
                Directory.CreateDirectory(destination);
                //CmdUtil.LinkFiles(currentDir.FullName, destination, out exitCode, fileExclusions, fileCopyInstead);
                WinDirectoryUtil.LinkFiles(currentDir.FullName, destination, out exitCode, fileExclusions, fileCopyInstead);


                DirectoryInfo[] children = currentDir.GetDirectories();
                for (int i = 0; i < children.Length; i++)
                {
                    DirectoryInfo child = children[i];
                    LinkDirectory(root, child, Path.Combine(destination, child.Name), out exitCode, dirExclusions, fileExclusions, fileCopyInstead);
                }
            }
            else
            {
                // we symlink this directly
                //CmdUtil.MkLinkDirectory(currentDir.FullName, destination, out exitCode);
                Kernel32Interop.CreateSymbolicLink(destination, currentDir.FullName, SymbolicLink.Directory);
            }
        }
Example #3
0
        private byte[] GetDataFromResource(IntPtr hModule, IntPtr type, IntPtr name)
        {
            // Load the binary data from the specified resource.

            IntPtr hResInfo = Kernel32Interop.FindResource(hModule, name, type);

            if (hResInfo == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            IntPtr hResData = Kernel32Interop.LoadResource(hModule, hResInfo);

            if (hResData == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            IntPtr pResData = Kernel32Interop.LockResource(hResData);

            if (pResData == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            uint size = Kernel32Interop.SizeofResource(hModule, hResInfo);

            if (size == 0)
            {
                throw new Win32Exception();
            }

            byte[] buf = new byte[size];
            Marshal.Copy(pResData, buf, 0, buf.Length);

            return(buf);
        }
Example #4
0
        public static void LinkFiles(string rootFolder, string destination, out int exitCode, string[] exclusions, string[] copyInstead, bool hardLink)
        {
            exitCode = 1;

            FileInfo[] files = new DirectoryInfo(rootFolder).GetFiles();

            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i];

                string lower   = file.Name.ToLower();
                bool   exclude = false;

                if (!string.IsNullOrEmpty(exclusions[0]))
                {
                    for (int j = 0; j < exclusions.Length; j++)
                    {
                        string exc = exclusions[j];
                        if (!string.IsNullOrEmpty(exc) && lower.Contains(exc))
                        {
                            // check if the file is i
                            exclude = true;
                            break;
                        }
                    }
                }

                if (exclude)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(copyInstead[0]))
                {
                    for (int j = 0; j < copyInstead.Length; j++)
                    {
                        string copy = copyInstead[j];
                        if (!string.IsNullOrEmpty(copy) && lower.Contains(copy))
                        {
                            exclude = true;
                            break;
                        }
                    }
                }

                string relative = file.FullName.Replace(rootFolder + @"\", "");
                string linkPath = Path.Combine(destination, relative);
                if (exclude)
                {
                    // should copy!
                    try
                    {
                        File.Copy(file.FullName, linkPath, true);
                    }
                    catch (Exception ex)
                    {
                        CmdUtil.ExecuteCommand(Path.GetDirectoryName(linkPath), out int exitCode2, "copy \"" + file.FullName + "\" \"" + linkPath + "\"");
                    }

                    //CopyFile(file.FullName, linkPath);
                }
                else
                {
                    //CmdUtil.MkLinkFile(file.FullName, linkPath, out exitCode);
                    if (hardLink)
                    {
                        Kernel32Interop.CreateHardLink(linkPath, file.FullName, IntPtr.Zero);
                    }
                    else
                    {
                        Kernel32Interop.CreateSymbolicLink(linkPath, file.FullName, Nucleus.Gaming.Platform.Windows.Interop.SymbolicLink.File);
                    }
                }
            }
        }
Example #5
0
        public static void LinkDirectory(string root, DirectoryInfo currentDir, string destination, out int exitCode,
                                         string[] dirExclusions, string[] fileExclusions, string[] fileCopyInstead, bool hardLink, bool symFolders, bool firstRun = true)
        {
            Console.WriteLine($"Symlinking folder {root} to {destination}");

            exitCode = 1;

            //bool special = overrideSpecial;
            bool special = false;
            bool skip    = false;


            if (!string.IsNullOrEmpty(dirExclusions[0]))
            {
                for (int j = 0; j < dirExclusions.Length; j++)
                {
                    string exclusion = dirExclusions[j];
                    string fullPath;
                    if (exclusion.StartsWith("direxskip"))
                    {
                        fullPath = Path.Combine(root, exclusion.Substring(9).ToLower());
                    }
                    else
                    {
                        fullPath = Path.Combine(root, exclusion).ToLower();
                    }

                    if (!string.IsNullOrEmpty(exclusion) && fullPath.Contains(currentDir.FullName.ToLower()))
                    {
                        if (exclusion.StartsWith("direxskip"))
                        {
                            skip = true;
                            break;
                        }

                        // special case, one of our subfolders is excluded
                        special = true;
                        break;
                    }
                }
            }

            //if (!special)
            //{
            // this folder has a child that cant be symlinked
            //CmdUtil.MkLinkDirectory(currentDir.FullName, destination, out exitCode);
            //Kernel32Interop.CreateSymbolicLink(destination, currentDir.FullName, Nucleus.Gaming.Platform.Windows.Interop.SymbolicLink.Directory);
            if (!skip || firstRun)
            {
                if (symFolders)
                {
                    if (!special && !firstRun)
                    {
                        Kernel32Interop.CreateSymbolicLink(destination, currentDir.FullName, Interop.SymbolicLink.Directory);
                    }
                    else
                    {
                        Directory.CreateDirectory(destination);
                    }
                }
                else
                {
                    Directory.CreateDirectory(destination);
                }

                //CmdUtil.LinkFiles(currentDir.FullName, destination, out exitCode, fileExclusions, fileCopyInstead, true);
                WinDirectoryUtil.LinkFiles(currentDir.FullName, destination, out exitCode, fileExclusions, fileCopyInstead, hardLink);

                DirectoryInfo[] children = currentDir.GetDirectories();
                for (int i = 0; i < children.Length; i++)
                {
                    DirectoryInfo child = children[i];
                    LinkDirectory(root, child, Path.Combine(destination, child.Name), out exitCode, dirExclusions, fileExclusions, fileCopyInstead, hardLink, symFolders, false);
                }
            }
            //}
            //else
            //{
            //    // we symlink this directly
            //    //CmdUtil.MkLinkDirectory(currentDir.FullName, destination, out exitCode);

            //    //Kernel32Interop.CreateSymbolicLink(destination, currentDir.FullName, Nucleus.Gaming.Platform.Windows.Interop.SymbolicLink.Directory);
            //    //System.IO.DriveInfo di = new System.IO.DriveInfo(destination);
            //    //System.IO.DirectoryInfo dirInfo = di.RootDirectory;
            //    //Console.WriteLine("createsymboliclink: " + dirInfo.Attributes.ToString());

            //    //if (symFolders)
            //   // {
            //       // Kernel32Interop.CreateSymbolicLink(destination, currentDir.FullName, Nucleus.Gaming.Platform.Windows.Interop.SymbolicLink.Directory);
            //   // }
            //   // else
            //   // {
            //        Directory.CreateDirectory(destination);
            //   // }
            //}
        }
Example #6
0
        private void Initialize(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            IntPtr hModule = IntPtr.Zero;

            try
            {
                hModule = Kernel32Interop.LoadLibraryEx(fileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
                if (hModule == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }

                FileName = GetFileName(hModule);

                if (String.IsNullOrEmpty(FileName))
                {
                    return;
                }

                // Enumerate the icon resource and build .ico files in memory.

                var tmpData = new List <byte[]>();

                EnumResourceNamesCallback callback = (h, t, name, l) =>
                {
                    // Refer the following URL for the data structures used here:
                    // http://msdn.microsoft.com/en-us/library/ms997538.aspx

                    // RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's.

                    var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name);

                    // Calculate the size of an entire .icon file.

                    int count = BitConverter.ToUInt16(dir, 4);  // GRPICONDIR.idCount
                    int len   = 6 + 16 * count;                 // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count
                    for (int i = 0; i < count; ++i)
                    {
                        len += BitConverter.ToInt32(dir, 6 + 14 * i + 8);   // GRPICONDIRENTRY.dwBytesInRes
                    }
                    using (var dst = new BinaryWriter(new MemoryStream(len)))
                    {
                        // Copy GRPICONDIR to ICONDIR.

                        dst.Write(dir, 0, 6);

                        int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count

                        for (int i = 0; i < count; ++i)
                        {
                            // Load the picture.

                            ushort id  = BitConverter.ToUInt16(dir, 6 + 14 * i + 12);   // GRPICONDIRENTRY.nID
                            var    pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id);

                            // Copy GRPICONDIRENTRY to ICONDIRENTRY.

                            dst.Seek(6 + 16 * i, SeekOrigin.Begin);

                            dst.Write(dir, 6 + 14 * i, 8);  // First 8bytes are identical.
                            dst.Write(pic.Length);          // ICONDIRENTRY.dwBytesInRes
                            dst.Write(picOffset);           // ICONDIRENTRY.dwImageOffset

                            // Copy a picture.

                            dst.Seek(picOffset, SeekOrigin.Begin);
                            dst.Write(pic, 0, pic.Length);

                            picOffset += pic.Length;
                        }

                        tmpData.Add(((MemoryStream)dst.BaseStream).ToArray());
                    }

                    return(true);
                };
                Kernel32Interop.EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero);

                iconData = tmpData.ToArray();
            }
            finally
            {
                if (hModule != IntPtr.Zero)
                {
                    Kernel32Interop.FreeLibrary(hModule);
                }
            }
        }
Example #7
0
        /// <summary>
        /// This function is the heart beat of ACAT.  All animations are handled in this
        /// event.  It unhighlights the currenlty highlighted animation widget and
        /// highlights the next one in the sequence.  If it has reached the end of the
        /// sequence, it wraps around and repeats the animation until the number of
        /// iterations has reached.
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="e">event args</param>
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (_syncObj.IsClosing())
            {
                Log.Debug("Form is closing. Returning" + _rootWidget.UIControl.Name);
                return;
            }

            if (_inTimer)
            {
                Log.Debug("Timer is busy. returning");
                return;
            }

            _inTimer = true;

            try
            {
                Log.Debug("Before tryEnter " + _rootWidget.UIControl.Name + ", threadid: " + Kernel32Interop.GetCurrentThreadId());

                if (!tryEnter(_transitionSync))
                {
                    Log.Debug("_transition sync will block returning");
                    return;
                }

                Log.Debug("After tryEnter" + _rootWidget.UIControl.Name + ", status: " + _syncObj.Status);
                if (_syncObj.IsClosing())
                {
                    Log.Debug("Form is closing. Returning" + _rootWidget.UIControl.Name);
                    return;
                }

                check();

                Log.Debug("CurrentAnimation: " + _currentAnimation.Name +
                          ". Count: " + _currentAnimation.AnimationWidgetList.Count +
                          ". currentWidgetIndex: " + _currentWidgetIndex);

                if (animatedWidgetCount() == 0)
                {
                    Log.Debug("No widgets to animate.");
                    _interpreter.Execute(_currentAnimation.OnEnd);
                    return;
                }

                check();

                var animationWidget = _currentAnimation.AnimationWidgetList[_currentWidgetIndex];

                Log.Debug(_rootWidget.UIControl.Name + ", status: " + _syncObj.Status);

                // if any switch is currently engaged, keep the current widget
                // highlighted until the user releases the switch
                //if (ActuatorManager.Instance.IsSwitchActive())
                if (IsSwitchActive)
                {
                    Log.Debug("Some switch is active. Will try again");
                    return;
                }

                // if there is code associated with an onEnter, execute that
                if (_currentAnimation.OnEnterExecutionNotDone && _currentAnimation.OnEnter != null)
                {
                    _interpreter.Execute(_currentAnimation.OnEnter);
                    _currentAnimation.OnEnterExecutionNotDone = false;
                }

                check();

                Log.Debug(_rootWidget.UIControl.Name + ", status: " + _syncObj.Status);

                // we have reached the end of the iteration. Turn off
                // the widget that was last highlighed and stop the
                // animation sequence
                if (_lastIteration)
                {
                    _lastIteration = false;

                    Widget selectedWidget = (_highlightedWidget != null &&
                                             _highlightedWidget.UIWidget != null) ? _highlightedWidget.UIWidget.Parent : null;

                    _rootWidget.HighlightOff();
                    if (animationWidget.OnHighlightOff.HasCode())
                    {
                        _interpreter.Execute(animationWidget.OnHighlightOff);
                    }

                    if (_currentAnimation.IsFirst)
                    {
                        setPlayerState(PlayerState.Timeout);
                    }

                    if (_timer != null)
                    {
                        _timer.Stop();
                    }
                    else
                    {
                        Log.Debug("Timer is null. returning");
                        return;
                    }

                    check();

                    AuditLog.Audit(new AuditEventAnimationEnd(
                                       _rootWidget.Name,
                                       (selectedWidget != null) ? selectedWidget.Name : string.Empty,
                                       (selectedWidget != null) ? selectedWidget.GetType().Name : string.Empty,
                                       _currentAnimation.Name));

                    // is there an onEnd code that we have to execute
                    _interpreter.Execute(_currentAnimation.OnEnd);

                    return;
                }

                check();

                if (_highlightedWidget != null && _highlightedWidget != animationWidget)
                {
                    Log.Debug(string.Format("Animation: {0}. Turning off . name = {1}. Count: {2}",
                                            _currentAnimation.Name,
                                            _highlightedWidget.UIWidget.Name,
                                            _currentAnimation.AnimationWidgetList.Count));

                    check();

                    _highlightedWidget.UIWidget.HighlightOff();

                    check();

                    if (_highlightedWidget.OnHighlightOff.HasCode())
                    {
                        _interpreter.Execute(_highlightedWidget.OnHighlightOff);
                    }
                }

                check();

                // now turn the highlight on on the next widget in the  sequence
                animationWidget = _currentAnimation.AnimationWidgetList[_currentWidgetIndex];

                Log.Debug("Animation: " + _currentAnimation.Name +
                          ". Turning on " + _currentWidgetIndex +
                          ". name = " + animationWidget.UIWidget.Name);

                check();

                animationWidget.UIWidget.HighlightOn();

                _highlightedWidget = animationWidget;

                // calculate how long to wait before triggering the
                // next timer event.  (this is how long the highlighted
                // widget will stay highlighted)

                int hesitateTime = animationWidget.HesitateTime;

                check();

                if (hesitateTime == 0 && isFirstAnimatedWidget(_currentWidgetIndex))
                {
                    hesitateTime = _currentAnimation.HesitateTime;
                }

                if (_timer != null)
                {
                    _timer.Interval = _currentAnimation.SteppingTime + hesitateTime;
                }
                else
                {
                    Log.Debug("timer is null. returning");
                    return;
                }

                check();

                if (animationWidget.OnHighlightOn.HasCode())
                {
                    _interpreter.Execute(animationWidget.OnHighlightOn);
                }

                check();

                int nextIndex = getNextAnimatedWidget(_currentWidgetIndex);

                // if we have reached the end of the animation sequence, wrap around
                if (nextIndex < 0)
                {
                    _iterationCount++;
                    int iterations = CoreGlobals.AppPreferences.ResolveVariableInt(_currentAnimation.Iterations, 1, 1);
                    if (iterations >= 0 && _iterationCount >= iterations)
                    {
                        _lastIteration = true;
                        return;
                    }

                    _currentWidgetIndex = getFirstAnimatedWidget();
                }
                else
                {
                    _currentWidgetIndex = nextIndex;
                }
            }
            catch (Exception ex)
            {
                Log.Debug("AnimationPlayerexception " + ex);
            }
            finally
            {
                Log.Debug("Before release " + _rootWidget.UIControl.Name);
                release(_transitionSync);
                Log.Debug("After release " + _rootWidget.UIControl.Name);

                Log.Debug("Setting intimer to false " + _rootWidget.UIControl.Name);
                _inTimer = false;

                Log.Debug("Exiting timer " + _rootWidget.UIControl.Name);
            }
        }
Example #8
0
        /// <summary>
        /// Transitions to the specified target animation. Stops the current
        /// animation and starts the new one.
        /// </summary>
        /// <param name="animation">Animation to transition to</param>
        public void Transition(Animation animation)
        {
            if (_syncObj == null)
            {
                Log.Debug("_syncObj is null. returning");
                return;
            }

            try
            {
                _lastIteration = false;

                _timer.Stop();

                Log.Debug("Transition to " + animation.Name);
                setPlayerState(PlayerState.Stopped);

                Log.Debug("Transition : Before Enter " + _rootWidget.UIControl.Name + ", threadid: " + Kernel32Interop.GetCurrentThreadId());
                tryEnterUntilSuccess(_transitionSync);
                Log.Debug("Transition : After Enter " + _rootWidget.UIControl.Name + ", status:  " + _syncObj.Status);

                if (_syncObj.IsClosing())
                {
                    Log.Debug("FORM IS CLOSING. releasing _transitionSync and returning" + _rootWidget.UIControl.Name);
                    release(_transitionSync);
                    return;
                }

                _rootWidget.HighlightOff();

                if (_currentAnimation != null)
                {
                    _currentAnimation.Stop();
                }

                _currentAnimation = animation;
                _currentAnimation.ResolveUIWidgetsReferences(_rootWidget, _variables);
                _currentAnimation.OnEnterExecutionNotDone = true;

                _iterationCount     = 0;
                _currentWidgetIndex = getFirstAnimatedWidget();
                _highlightedWidget  = null;

                Log.Debug("Transition : Before Release " + _rootWidget.UIControl.Name);
                release(_transitionSync);
                Log.Debug("Transition : After Release " + _rootWidget.UIControl.Name);

                Log.Debug("Start new animation " + animation.Name);

                if (!animation.AutoStart && animation.OnStart)
                {
                    animation.OnStart = false;
                    setPlayerState(PlayerState.Timeout);
                }
                else if (_timer != null)
                {
                    _timer.Interval = _currentAnimation.SteppingTime;
                    Log.Debug(_rootWidget.UIControl.Name + ", syncobj.status " + _syncObj.Status);

                    if (_syncObj.Status == SyncLock.StatusValues.None)
                    {
                        timer_Elapsed(null, null);
                        Log.Debug("Starting timer " + _rootWidget.UIControl.Name);
                        _timer.Start();
                    }
                    else
                    {
                        Log.Debug("******** WILL NOT START TIMER!!!" +
                                  _rootWidget.UIControl.Name +
                                  ", syncobj.status " + _syncObj.Status);
                    }

                    setPlayerState(PlayerState.Running);
                }
            }
            catch (Exception ex)
            {
                Log.Debug(ex.ToString());
            }

            Log.Debug("Returning");
        }
Example #9
0
        /// <summary>
        /// Auto-completes a partially entered with with the 'wordselected'. The
        /// 'wordSelected' is typically one that the word prediction engine suggests
        /// and the one that the user selects
        /// </summary>
        /// <param name="wordSelected">the autocomplete word.</param>
        public void AutoCompleteWord(String wordSelected)
        {
            bool isCapitalizedWordToReplace = false;

            try
            {
                using (AgentContext context = Context.AppAgentMgr.ActiveContext())
                {
                    Context.AppAgentMgr.TextChangedNotifications.Hold();

                    int caretPos = context.TextAgent().GetCaretPos();

                    _beforeAutoCompleteCaretPos = caretPos;
                    _autocompleteStartOffset    = -1;

                    int offset;
                    int count;
                    context.TextAgent().GetPrevWordOffset(out offset, out count);
                    Log.Debug("PrevWord offset: " + offset + ", count: " + count);

                    // check if we are just completing the current word or inserting a new word
                    int    insertOrReplaceOffset;
                    String wordToReplace;
                    bool   checkInsert = context.TextAgent().CheckInsertOrReplaceWord(out insertOrReplaceOffset, out wordToReplace);
                    Log.Debug("checkInsert: " + checkInsert + ", insertorreplaceoffset: " + insertOrReplaceOffset +
                              ", caret: " + caretPos + ", caretPos-delprev: " + (caretPos - count));

                    _autoCompletePartialWord = wordToReplace;

                    int wordToReplaceLength = wordToReplace.Length;
                    if (wordToReplaceLength > 0)
                    {
                        isCapitalizedWordToReplace = Char.IsUpper(wordToReplace[0]);
                    }

                    Log.Debug("checkInsert: " + checkInsert + ". inserRepOff: " + insertOrReplaceOffset +
                              ". wordTORep: " + wordToReplace);

                    if (KeyStateTracker.IsStickyShiftOn())
                    {
                        wordSelected = wordSelected.ToUpper();
                    }
                    else if (KeyStateTracker.IsShiftOn())
                    {
                        wordSelected = capitalizeWord(wordSelected);
                        KeyStateTracker.KeyTriggered(wordSelected[0]);
                    }

                    if (checkInsert)
                    {
                        Log.Debug("Inserting [" + wordSelected + "] at offset " + insertOrReplaceOffset);
                        context.TextAgent().Insert(insertOrReplaceOffset, wordSelected);
                    }
                    else
                    {
                        if (count > 0)
                        {
                            wordToReplaceLength = count;
                        }

                        if (wordToReplaceLength > 0 && isCapitalizedWordToReplace &&
                            Char.ToUpper(wordToReplace[0]) == Char.ToUpper(wordSelected[0]))
                        {
                            wordSelected = capitalizeWord(wordSelected);
                        }

                        Log.Debug("Replacing word at offset " + insertOrReplaceOffset + ". Length: " +
                                  wordToReplaceLength + ". with [" + wordSelected + "]");

                        context.TextAgent().Replace(insertOrReplaceOffset, wordToReplaceLength, wordSelected);
                    }

                    _autocompleteStartOffset = insertOrReplaceOffset;

                    _lastAction = LastAction.AutoCompleteWord;

                    postAutoCompleteWord();

                    _autoCompleteCaretPos = context.TextAgent().GetCaretPos();
                    Log.Debug("_autocompleteCursorPos is " + _autoCompleteCaretPos);
                }
            }
            catch (InvalidAgentContextException iace)
            {
                Log.Debug("Agent Context is invalid " + iace);
            }
            catch (Exception ex)
            {
                Log.Debug(ex.ToString());
            }
            finally
            {
                uint threadId = Kernel32Interop.GetCurrentThreadId();
                Log.Debug("Calling TextChangedNotifications.Release AFTER Autocompeting word");

                Context.AppAgentMgr.TextChangedNotifications.Release();

                Log.Debug("Returned from TextChangedNotifications.Release AFTER Autocompeting word " + threadId);
            }
        }