diff --git a/MainScriptingObject.cs b/MainScriptingObject.cs index bbc68f1..df41ac9 100644 --- a/MainScriptingObject.cs +++ b/MainScriptingObject.cs @@ -1,9 +1,9 @@ -using System.Text.RegularExpressions; -using System; -using System.Collections.Generic; -using System.Linq; +//using System; +//using System.Linq; +//using System.Threading.Tasks; +using log4net; +using System.Text.RegularExpressions; using System.Text; -using System.Threading.Tasks; using System.Diagnostics; using System.Security.Cryptography; @@ -15,18 +15,29 @@ public class MainScriptingObject : IDisposable SortedDictionary scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it (so we can cache it) string scriptSource = string.Empty; string tempPath = Path.GetTempPath() + @"TmpVbsScripts" + Path.DirectorySeparatorChar; - Dictionary>? dctCache = null; + Dictionary> dctCache = new(StringComparer.OrdinalIgnoreCase); Dictionary dctFileCache = new();//(StringComparer.OrdinalIgnoreCase); Random rn = new(); List lstFilesToRemove = new(); bool removedOldScripts = false; + ILog? appLog = null; + public void Dispose() { if (dctCache != null) dctCache.Clear(); if (dctFileCache != null) dctFileCache.Clear(); + + if (AllowRemovalOfOldScripts && lstFilesToRemove.Count > 0) + { + foreach (var f in lstFilesToRemove) + { + try { File.Delete(f); } catch { } + } + lstFilesToRemove.Clear(); + } } public bool HadErrors { get; set; } = false; @@ -40,7 +51,7 @@ public class MainScriptingObject : IDisposable set { scriptSource = CleanUserCode(value); - dctCache = new(); + dctCache.Clear(); //= new(StringComparer.); } } @@ -80,7 +91,7 @@ public class MainScriptingObject : IDisposable if (dctFileCache.ContainsKey(theKey)) { scriptSource = dctFileCache[theKey].VbsScriptSource; - dctCache = dctFileCache[theKey].CacheDict; + //dctCache = dctFileCache[theKey].CacheDict; rv = true; } else @@ -88,7 +99,7 @@ public class MainScriptingObject : IDisposable if (!File.Exists(VBScriptPath)) { scriptSource = string.Empty; - dctCache = null; + dctCache.Clear(); rv = false; } else @@ -98,15 +109,26 @@ public class MainScriptingObject : IDisposable scriptSource = sr.ReadToEnd(); z.VbsScriptSource = scriptSource; - z.CacheDict = new(); + //z.CacheDict = new(); dctFileCache[theKey] = z; - dctCache = dctFileCache[theKey].CacheDict; + //dctCache = dctFileCache[theKey].CacheDict; rv = true; } } return rv; } + public MainScriptingObject() + { + var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; + var configFileDirectory = Path.Combine(baseDirectory, "log4net.config"); + + var configFileInfo = new FileInfo(configFileDirectory); + log4net.Config.XmlConfigurator.ConfigureAndWatch(configFileInfo); + + appLog = log4net.LogManager.GetLogger("log4netFileLogger"); + } + public void ClearScriptInputs() { scrInp1.Clear(); @@ -150,16 +172,24 @@ public class MainScriptingObject : IDisposable var scriptFile = Path.Combine(tempPath, tStamp + ".vbs"); var iClearValues = string.Join('\t', scrInp2.ToArray()); var iKey = SHA256Hash(iClearValues); - var toReturn = new Dictionary(StringComparer.OrdinalIgnoreCase); var retVals = string.Empty; var varName = string.Empty; var varVal = string.Empty; + Dictionary toReturn = new(); // new SortedDictionary(StringComparer.OrdinalIgnoreCase); HadErrors = false; lstFilesToRemove.Add(outFile); lstFilesToRemove.Add(scriptFile); - if (UseCache && dctCache != null && dctCache.ContainsKey(iKey)) - toReturn = dctCache[iKey]; + + if (DebugMode) + appLog.DebugFormat("INPUT == {0}", iClearValues); + + if (UseCache && dctCache.ContainsKey(iKey)) + { + toReturn = dctCache[iKey] as Dictionary; + if (DebugMode) + appLog.DebugFormat("OUTPUT FROM CACHE ({0}) == {1}", iKey, string.Join('~', toReturn.ToArray())); //dctCache[iKey].ToArray())); + } else { var genCodeHeader = new StringBuilder(); @@ -180,8 +210,8 @@ public class MainScriptingObject : IDisposable genCodeHeader.AppendLine("'* Revised in 2015 by KeyMotive LLC (Rich Deck) *"); genCodeHeader.AppendLine("'* Refreshed in 2025 by KeyMotive LLC (Rich Deck)*"); genCodeHeader.AppendLine("'*************************************************"); - genCodeHeader.AppendLine("'Non-Crypto Lookup: " + iClearValues); - genCodeHeader.AppendLine("'Crypto Lookup: " + iKey); + //genCodeHeader.AppendLine("'Non-Crypto Lookup: " + iClearValues); + //genCodeHeader.AppendLine("'Crypto Lookup: " + iKey); genCodeHeader.AppendLine("'*************************************************"); genCodeHeader.AppendLine("outFile = \"" + outFile + "\""); @@ -219,7 +249,7 @@ public class MainScriptingObject : IDisposable varVal = string.Format("\"{0}\"", kvp.Value.ToString().Replace("\"", "\"\"")); } - varVal = Regex.Replace(varVal, "(\r\n|\n\r|\r|\n)", "\"\n\""); + varVal = Regex.Replace(varVal, @"(\r\n|\n\r|\r|\n)", "\"\n\""); varName = "\"" + kvp.Key.ToUpper().Replace("\"", "\"\"") + "\""; genCodeHeader.AppendLine(" Case " + varName); genCodeHeader.AppendLine(" retVal = " + varVal); @@ -275,28 +305,43 @@ public class MainScriptingObject : IDisposable retVals = sr.ReadToEnd(); } - toReturn.Clear(); - foreach (var f0 in Regex.Split(retVals, "(\r\n|\n\r|\r|\n)")) + //toReturn = new(StringComparer.OrdinalIgnoreCase); + foreach (var f0 in Regex.Split(retVals, @"(\r\n|\n\r|\r|\n)")) { if (f0.Length >= 3 && f0.IndexOf('\t') > 0) { var nvp = f0.Split('\t'); - nvp[1] = Regex.Replace(nvp[1], "\x7eNewLine\x7e", "\n"); - toReturn[nvp[0]] = nvp[1]; + nvp[1] = Regex.Replace(nvp[1], @"\x7eNewLine\x7e", "\n"); + //toReturn[nvp[0]] = nvp[1]; + toReturn.Add(nvp[0], nvp[1]); } } + //if (DebugMode) + // appLog.DebugFormat("TO-RETURN NOW HAS {0} ITEMS.", toReturn.Count); - if (UseCache && dctCache != null) - dctCache[iKey] = toReturn; + if (UseCache)// && dctCache != null) + { + dctCache.Add(iKey, toReturn);// ?? new Dictionary()); + if (DebugMode) + appLog.DebugFormat("--> VERIFIED FROM CACHE({0}): {1}", iKey, string.Join('~', dctCache[iKey].ToArray())); + //dctCache[iKey] = toReturn; + } + + if (DebugMode) + { + appLog.DebugFormat("OUTPUT (NON-CACHED) == {0}", string.Join('~', toReturn.ToArray())); + } } catch (Exception ex) { + if (DebugMode) + appLog.Error(ex); using (var sw = new StreamWriter(outFile, true)) sw.WriteLine(ex.ToString()); } } - return toReturn; + return new Dictionary(toReturn); } #region "Maint routines" @@ -328,7 +373,7 @@ public class MainScriptingObject : IDisposable string StripUnprintables(string inp) { - var rv = Regex.Replace(inp, "[^\x20-\x7E]", " "); + var rv = Regex.Replace(inp, @"[^\x20-\x7E]", " "); rv = Regex.Replace(rv, " +", " ").Trim(); return rv; } @@ -429,11 +474,11 @@ public class MainScriptingObject : IDisposable { public void Dispose() { - CacheDict.Clear(); + //CacheDict.Clear(); } public string VbsScriptSource { get; set; } = string.Empty; - public Dictionary> CacheDict { get; set; } = new(); + //public Dictionary> CacheDict { get; set; } = new(); } #endregion diff --git a/mScriptableCS25.csproj b/mScriptableCS25.csproj index a5be167..50f8f4b 100644 --- a/mScriptableCS25.csproj +++ b/mScriptableCS25.csproj @@ -1,16 +1,21 @@ - net9.0-windows + net10.0-windows10.0.22000.0 enable enable - 1.2.0.21 - 1.2.0.21 - 1.0.0.21 + 1.2.0.53 + 1.2.0.53 + 1.0.0.53 + 10.0.22000.0 + + + +