From ec9c57b6cae1a6a4f9a35c77db1aa2fb70f1234e Mon Sep 17 00:00:00 2001 From: RDeck Date: Tue, 9 Jun 2026 08:57:29 -0400 Subject: [PATCH] ! --- MainScriptingObject.cs | 29 +++++++++-- ScriptingCS.cs | 109 +++++++++++++++++++++++++++++++++++++++++ mScriptableCS25.csproj | 6 +-- 3 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 ScriptingCS.cs diff --git a/MainScriptingObject.cs b/MainScriptingObject.cs index 6d3dba7..cf0e332 100644 --- a/MainScriptingObject.cs +++ b/MainScriptingObject.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; +using System.Security.Cryptography; namespace mScriptableCS25; @@ -14,7 +15,7 @@ public class MainScriptingObject : IDisposable SortedDictionary scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it string scriptSource = string.Empty; string tempPath = Path.GetTempPath(); - Dictionary>? dctCache = null; + Dictionary>? dctCache = null; Dictionary dctFileCache = new(StringComparer.OrdinalIgnoreCase); Random rn = new(); List lstFilesToRemove = new(); @@ -141,8 +142,8 @@ public class MainScriptingObject : IDisposable var tStamp = string.Format("TmpVbsScript-{0:yyyyMMddHHmmssfff}-{1}", DateTime.Now, Guid.NewGuid().ToString("N").Substring(0, 12)); var outFile = Path.Combine(tempPath, tStamp + ".txt"); var scriptFile = Path.Combine(tempPath, tStamp + ".vbs"); - var iKey = ToGuid(string.Join('\t', scrInp2.ToArray())); - Dictionary toReturn = new(StringComparer.OrdinalIgnoreCase); + var iKey = SHA1Hash(string.Join('\t', scrInp2.ToArray())); + var toReturn = new Dictionary(StringComparer.OrdinalIgnoreCase); var retVals = string.Empty; var varName = string.Empty; var varVal = string.Empty; @@ -170,6 +171,7 @@ public class MainScriptingObject : IDisposable genCodeHeader.AppendLine("'* as long as you give credit to me (Don) for *"); genCodeHeader.AppendLine("'* mScriptable. *"); genCodeHeader.AppendLine("'* Revised in 2015 by KeyMotive LLC (Rich Deck) *"); + genCodeHeader.AppendLine("'* Refreshed in 2025 by KeyMotive LLC (Rich Deck)*"); genCodeHeader.AppendLine("'*************************************************"); genCodeHeader.AppendLine("outFile = \"" + outFile + "\""); @@ -287,7 +289,6 @@ public class MainScriptingObject : IDisposable return toReturn; } - #region "Maint routines" string CleanUserCode(string ScriptCode) { @@ -337,6 +338,24 @@ public class MainScriptingObject : IDisposable return gx; } + internal string SHA1Hash(string strToHash) + { + var rv = new StringBuilder(); + + using (var sha = SHA1.Create()) + { + byte[] bytes = Encoding.UTF8.GetBytes(strToHash); + byte[] hash = sha.ComputeHash(bytes); + + foreach (byte x in hash) + { + rv.AppendFormat("{0:x2}", x); + } + } + + return rv.ToString(); + } + #endregion #region "Models and Such" @@ -348,7 +367,7 @@ public class MainScriptingObject : IDisposable } public string VbsScriptSource { get; set; } = string.Empty; - public Dictionary> CacheDict { get; set; } = new(); + public Dictionary> CacheDict { get; set; } = new(); } #endregion diff --git a/ScriptingCS.cs b/ScriptingCS.cs new file mode 100644 index 0000000..3a899ed --- /dev/null +++ b/ScriptingCS.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace mScriptableCS25; + +internal class ScriptingCS : IDisposable +{ + SortedDictionary scrInp1 = new(StringComparer.OrdinalIgnoreCase); + SortedDictionary scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it + Dictionary> dctCache = new(); + string scriptSource = string.Empty; + string tempPath = Path.GetTempPath(); + Random rn = new(); + List lstFilesToRemove = new(); + bool removedOldScripts = false; + + public void Dispose() + { + scrInp1.Clear(); + scrInp2.Clear(); + dctCache.Clear(); + } + + public bool HadErrors { get; internal set; } = false; + public bool DebugMode { get; set; } = false; + public bool AllowRemovalOfOldScripts { get; set; } = true; + + public string ScriptSource + { + get { return scriptSource; } + set + { + scriptSource = CleanUserCode(value); + dctCache.Clear(); + } + } + + #region "Maint routines" + string CleanUserCode(string ScriptCode) + { + var workCode = ScriptCode; + Regex? re = null; + //string inpVal_re = @"Function\s+inpVal\s*?\([^)]+\)(.*?)End\s+Function"; + //string iv_re = @"Function\s+iv\s*?\([^)]+\)(.*?)End\s+Function"; + //string return_re = @"Sub\s+return\s*?\([^)]+\)(.*?)End\s+Sub"; + string inpVal_re = @"string\s+inpVal\s*?\(\)(.*?)\{(?s:.*?)}"; + string iv_re = @"string\s+iv\s*?\(\)(.*?)\{(?s:.*?)}"; + string return_re = @"Sub\s+return\s*?\([^)]+\)(.*?)End\s+Sub"; + + //remove conflicting "inpVal" functions + re = new Regex(inpVal_re, RegexOptions.Singleline | RegexOptions.IgnoreCase); + if (re.IsMatch(workCode)) + workCode = re.Replace(workCode, ""); + + //remove conflicting "iv" functions + re = new Regex(iv_re, RegexOptions.Singleline | RegexOptions.IgnoreCase); + if (re.IsMatch(workCode)) + workCode = re.Replace(workCode, ""); + + //remove conflicting "return" subroutine + re = new Regex(return_re, RegexOptions.Singleline | RegexOptions.IgnoreCase); + if (re.IsMatch(workCode)) + workCode = re.Replace(workCode, ""); + + return workCode; + } + + string StripUnprintables(string inp) + { + var rv = Regex.Replace(inp, "[^\x20-\x7E]", " "); + rv = Regex.Replace(rv, " +", " ").Trim(); + return rv; + } + + Guid ToGuid(string strToHash) + { + var gx = Guid.Empty; + using (var mD = System.Security.Cryptography.MD5.Create()) + { + var stringBuilder = new StringBuilder(); + byte[] array = mD.ComputeHash(Encoding.UTF8.GetBytes(strToHash)); + for (int i = 0; i < array.Length; i++) + stringBuilder.Append(array[i].ToString("X2")); + + gx = Guid.Parse(stringBuilder.ToString()); + } + return gx; + } + + #endregion + + #region "Models and Such" + internal class MdlVbsSources : IDisposable + { + public void Dispose() + { + CacheDict.Clear(); + } + + public string CSScriptSource { get; set; } = string.Empty; + public Dictionary> CacheDict { get; set; } = new(); + } + #endregion + +} diff --git a/mScriptableCS25.csproj b/mScriptableCS25.csproj index 3ecb777..8aa33e7 100644 --- a/mScriptableCS25.csproj +++ b/mScriptableCS25.csproj @@ -4,9 +4,9 @@ net9.0-windows enable enable - 1.2.0.6 - 1.2.0.6 - 1.0.0.6 + 1.2.0.8 + 1.2.0.8 + 1.0.0.8