Exemple #1
0
        /// <summary>
        /// Replaces BDS version in a data file (the version must be at the
        /// beginning of the file).
        /// </summary>
        public static void ReplaceInDataFile(string fileName, ReplaceDelegate replace)
        {
            string tempFile1 = fileName + ".repl";
            string tempFile2 = fileName + ".backup";

            using (BinaryReader r = new BinaryReader(File.OpenRead(fileName)))
            {
                using (BinaryWriter w = new BinaryWriter(File.Open(tempFile1, FileMode.Create, FileAccess.Write)))
                {
                    BdsVersion currentVersion = new BdsVersion();
                    currentVersion.Read(r);
                    replace(ref currentVersion);
                    currentVersion.Write(w);
                    byte[] buffer = new byte[1024 * 100];
                    for (; ;)
                    {
                        int actRead = r.Read(buffer, 0, buffer.Length);
                        w.Write(buffer, 0, actRead);
                        if (actRead != buffer.Length)
                        {
                            break;
                        }
                    }
                }
            }
            // Now tempFile1 contains the copy of original file with replaced
            // version. Now rename the files and delete the old file.
            File.Move(fileName, tempFile2);
            File.Move(tempFile1, fileName);
            File.Delete(tempFile2);
        }
Exemple #2
0
 /// <summary>
 /// Tags will be replaced with the result of the given string.
 /// </summary>
 public ReplaceRule ReplaceWith(ReplaceSourceContextDelegate inSourceContextDelegate)
 {
     m_ConstantText                 = null;
     m_ReplaceCallback              = null;
     m_ReplaceWithContextCallback   = null;
     m_ReplaceSourceContextCallback = inSourceContextDelegate;
     m_TryReplaceCallback           = null;
     return(this);
 }
Exemple #3
0
        /// <summary>
        /// Adds a new replace rule that replaces with the result of the given callback.
        /// </summary>
        public ReplaceRule AddReplace(string inId, ReplaceDelegate inReplace)
        {
            CheckLocked();

            ReplaceRule rule = FindOrCreateReplaceRule(inId);

            rule.ReplaceWith(inReplace);
            return(rule);
        }
Exemple #4
0
 /// <summary>
 /// Tags will be replaced with the given string.
 /// </summary>
 public ReplaceRule ReplaceWith(string inText)
 {
     m_ConstantText                 = inText;
     m_ReplaceCallback              = null;
     m_ReplaceWithContextCallback   = null;
     m_ReplaceSourceContextCallback = null;
     m_TryReplaceCallback           = null;
     return(this);
 }
Exemple #5
0
 /// <summary>
 /// Closing tags will be replaced with the given string.
 /// </summary>
 public ReplaceRule CloseWith(string inText)
 {
     m_ConstantClosingText                 = inText;
     m_ReplaceClosingCallback              = null;
     m_ReplaceWithContextClosingCallback   = null;
     m_ReplaceSourceContextClosingCallback = null;
     m_TryReplaceCallback = null;
     m_HandleClosing      = true;
     return(this);
 }
Exemple #6
0
 /// <summary>
 /// Closing tags will be replaced with the result of the given string.
 /// </summary>
 public ReplaceRule CloseWith(ReplaceSourceContextDelegate inContextSourceDelegate)
 {
     m_ConstantClosingText                 = null;
     m_ReplaceClosingCallback              = null;
     m_ReplaceWithContextClosingCallback   = null;
     m_ReplaceSourceContextClosingCallback = inContextSourceDelegate;
     m_TryReplaceCallback = null;
     m_HandleClosing      = true;
     return(this);
 }
Exemple #7
0
 /// <summary>
 /// Creates a <see cref="CollectionChangedEventHandler{T}"/> from the specified delegates.
 /// </summary>
 /// <param name="add"> If null is specified, nothing is performed on addition of an element. </param>
 /// <param name="move"> If null is specified, nothing is performed on removal of an element. </param>
 /// <param name="remove"> If null is specified, nothing is performed on removal of an element. </param>
 /// <param name="replace"> If null is specified, the default action is performed, which is to first remove and then add the changes. </param>
 /// <param name="reset"> If null is specified, the default action is performed, which is to remove all elements. </param>
 public static CollectionChangedEventHandler <T> Create <TCollection>(
     AddDelegate <TCollection> add         = null,
     RemoveDelegate <TCollection> remove   = null,
     ReplaceDelegate <TCollection> replace = null,
     MoveDelegate <TCollection> move       = null,
     ResetDelegate <TCollection> reset     = null
     ) where TCollection : INotifyCollectionChanged
 {
     return(new DelegatedCollectionChangedEventHandler <TCollection>(move, add, reset, replace, remove));
 }
Exemple #8
0
    private static string MatchEvaluator(Match match)
    {
        string          placeholder     = match.Groups[1].Value;
        ReplaceDelegate replaceDelegate = null;

        if (!_replaceDelegate.TryGetValue(placeholder, out replaceDelegate))
        {
            return(match.Groups[0].Value);
        }

        return(replaceDelegate());
    }
Exemple #9
0
        /// <summary>
        /// Starts the process.
        /// </summary>
        private void StartProcess()
        {
            // Initialize replacer:
            this.replacer = new Business.Engine.Replacer();
            this.replacer.AddObserver(this);
            this.MarkUnfinished();

            // Start process:
            ReplaceDelegate action   = this.replacer.Replace;
            AsyncCallback   callback = this.Callback;

            action.BeginInvoke(this.ReplaceParameters, callback, action);
        }
Exemple #10
0
 /// <summary>
 /// Tags will be replaced using the given callback.
 /// </summary>
 public ReplaceRule TryReplaceWith(TryReplaceDelegate inTryReplace)
 {
     m_ConstantText                        = null;
     m_ConstantClosingText                 = null;
     m_ReplaceCallback                     = null;
     m_ReplaceClosingCallback              = null;
     m_ReplaceWithContextCallback          = null;
     m_ReplaceWithContextClosingCallback   = null;
     m_ReplaceSourceContextCallback        = null;
     m_ReplaceSourceContextClosingCallback = null;
     m_TryReplaceCallback                  = inTryReplace;
     m_HandleClosing                       = false;
     return(this);
 }
Exemple #11
0
 public DelegatedCollectionChangedEventHandler(
     MoveDelegate <TCollection> move,
     AddDelegate <TCollection> add,
     ResetDelegate <TCollection> reset,
     ReplaceDelegate <TCollection> replace,
     RemoveDelegate <TCollection> remove
     )
 {
     this.move    = move;
     this.add     = add;
     this.reset   = reset;
     this.replace = replace;
     this.remove  = remove;
 }
        /// <summary>
        /// 自定义过滤
        /// </summary>
        public static string Replace(string text, ReplaceDelegate replaceAction)
        {
            var dic    = Find(text);
            var list   = text.ToList();
            var offset = 0;

            foreach (var i in dic)
            {
                list.RemoveRange(i.Key + offset, i.Value);
                var newText = replaceAction(text.Substring(i.Key, i.Value), i.Key, i.Value);
                list.InsertRange(i.Key + offset, newText);
                offset = offset + newText.Length - i.Value;
            }
            return(new string(list.ToArray()));
        }
Exemple #13
0
        private void SetPattern()
        {
            switch (this.Type)
            {
            case FilterType.String:
                StringPattern = StrPattern;
                break;

            case FilterType.Regex:
                if (StrPattern.Length > 0)
                {
                    RegexPattern = new Regex(StrPattern);
                }
                break;

            case FilterType.Function:
                var file = Config.LoadFile(this.FunctionFile, this.ConfigFileDir);
                if (file.Length > 0)
                {
                    this.StrPattern = file;
                }
                if (FunctionName.Length == 0 || StrPattern.Length == 0)
                {
                    // TRANSLATORS: Log message. UserDefinedFilter plugin.
                    Logger.Log(T._("Could not set function."));
                    break;
                }
                JSWrapper.Instance.SetFunction(FunctionName, StrPattern);
                FunctionPattern = (JSArgument arg) => {
                    try {
                        var r = JSWrapper.Instance.ExecuteFunction(FunctionName, arg);
                        return(new JSReturnValue(r));
                    } catch (System.Exception e) {
                        // TRANSLATORS: Log message. UserDefinedFilter plugin.
                        Logger.Log(T._("Could not get JS result."));
                        Logger.DebugLog(e.ToString());
                        return(null);
                    }
                };
                break;

            default:
                break;
            }
        }
Exemple #14
0
        private void SetPattern(SoundConfig parent)
        {
            switch (this.Type)
            {
            case PatternType.String:
                StringPattern = StrPattern;
                break;

            case PatternType.Regex:
                RegexPattern = new Regex(StrPattern);
                break;

            case PatternType.Function:
                var file = Config.LoadFile(this.FunctionFile, parent.ConfigFileDir);
                if (file.Length > 0)
                {
                    this.StrPattern = file;
                }
                if (FunctionName.Length == 0 || StrPattern.Length == 0)
                {
                    // TRANSLATORS: Log message. SoundEffectCommand plugin.
                    Logger.Log(T._("Could not set function."));
                    break;
                }
                JSWrapper.Instance.SetFunction(FunctionName, StrPattern);
                FunctionPattern = (JSArgument arg) => {
                    try {
                        var r = JSWrapper.Instance.ExecuteFunction(FunctionName, arg);
                        return(r as ReplaceResult);
                    } catch (System.Exception e) {
                        // TRANSLATORS: Log message. SoundEffectCommand plugin.
                        Logger.Log(T._("Could not get JS result."));
                        Logger.DebugLog(e.ToString());
                        return(null);
                    }
                };
                break;

            default:
                break;
            }
        }
Exemple #15
0
 /// <summary>
 /// Callbacks the specified result.
 /// </summary>
 /// <param name="result">The result.</param>
 private void Callback(IAsyncResult result)
 {
     try
     {
         // End invoke:
         ReplaceDelegate action = (ReplaceDelegate)result.AsyncState;
         action.EndInvoke(result);
     }
     catch (Exception exc)
     {
         // Log exception:
         this.AddMessage(new LogMessage
         {
             Message = "Error: " + exc.Message,
             Details = exc.ToString()
         });
     }
     finally
     {
         // Mark process as finished:
         this.MarkFinished();
     }
 }
Exemple #16
0
 public void Replace(ReplaceDelegate del)
 {
     Replace(del?.Invoke(this));
 }
Exemple #17
0
 public static void AddReplaceDelegate(string placeholder, ReplaceDelegate replaceDelegate)
 {
     _replaceDelegate.Add(placeholder, replaceDelegate);
 }
Exemple #18
0
        // -----------------------------------
        //              Replace
        // -----------------------------------

        private string doReplace(ReplaceDelegate replacer)
        {
            StringBuilder sb = new StringBuilder();
            //
            int index = 0;
            while (index < this.Items.Count)
            {
                Item item = this.Items[index];
                if (item.Kind == ItemKind.Text)
                {
                    sb.Append(item.CalcText(this.htmlText));
                    index++;
                }
                else if (item.Kind == ItemKind.OpenCloseTag)
                {
                    string textOpen = item.CalcText(this.htmlText);
                    string replaceText = replacer(textOpen, null, null);
                    sb.Append(replaceText);
                    index++;
                }
                else
                {
                    if ((index + 2) >= this.Items.Count) throw new Exception("(index + 2) >= this.textItems.Count");
                    if (item.Kind != ItemKind.OpenTag) throw new Exception("item.Kind != ItemKind.OpenTag");
                    //
                    Item itemContent = this.Items[index + 1];
                    Item itemClose = this.Items[index + 2];
                    if (itemContent.Kind != ItemKind.Content) throw new Exception("itemContent.Kind != ItemKind.Content");
                    if (itemClose.Kind != ItemKind.CloseTag) throw new Exception("itemClose.Kind != ItemKind.CloseTag");
                    //
                    string textOpen = item.CalcText(this.htmlText);
                    string textContent = itemContent.CalcText(this.htmlText);
                    string textClose = itemClose.CalcText(this.htmlText);
                    string replaceText = replacer(textOpen, textContent, textClose);
                    sb.Append(replaceText);
                    //
                    index += 3;
                }
            }
            //
            return sb.ToString();
        }
Exemple #19
0
 public string Replace(ReplaceDelegate replacer)
 {
     return this.doReplace(replacer);
 }
        /// <summary>
        /// 自定义过滤
        /// </summary>
        public static string Replace(string text, ReplaceDelegate replaceAction)
        {

            var dic = Find(text);
            var list = text.ToList();
            var offset = 0;
            foreach (var i in dic)
            {

                list.RemoveRange(i.Key + offset, i.Value);
                var newText = replaceAction(text.Substring(i.Key, i.Value), i.Key, i.Value);
                list.InsertRange(i.Key + offset, newText);

                offset = offset + newText.Length - i.Value;
            }
            return new string(list.ToArray());
        }
 private string ApplyReplaces(string source, ReplaceDelegate[] replaces)
 {
     var result = source;
     ReplaceDelegate lastReplace;
     do
     {
         lastReplace = replaces.FirstOrDefault(d => d(ref result));
     } while (lastReplace != null);
     return result;
 }
Exemple #22
0
			public DelegateReplaceCaptor(ReplaceDelegate replacement, Object id)
			{
				m_replacement = replacement;
				m_id = id;
			}
Exemple #23
0
		public static PegNet.ICaptor Replace(ReplaceDelegate replacement, Object id=null)
		{
			return new DelegateReplaceCaptor(replacement, id);
		}