Weird issue with the cache - it's fixed now!

This commit is contained in:
RDeck 2026-06-09 13:25:15 -04:00
parent 79340a9197
commit 60da5c74ef
2 changed files with 81 additions and 31 deletions

View File

@ -1,9 +1,9 @@
using System.Text.RegularExpressions; //using System;
using System; //using System.Linq;
using System.Collections.Generic; //using System.Threading.Tasks;
using System.Linq; using log4net;
using System.Text.RegularExpressions;
using System.Text; using System.Text;
using System.Threading.Tasks;
using System.Diagnostics; using System.Diagnostics;
using System.Security.Cryptography; using System.Security.Cryptography;
@ -15,18 +15,29 @@ public class MainScriptingObject : IDisposable
SortedDictionary<string, string> scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it (so we can cache it) SortedDictionary<string, string> scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it (so we can cache it)
string scriptSource = string.Empty; string scriptSource = string.Empty;
string tempPath = Path.GetTempPath() + @"TmpVbsScripts" + Path.DirectorySeparatorChar; string tempPath = Path.GetTempPath() + @"TmpVbsScripts" + Path.DirectorySeparatorChar;
Dictionary<string, Dictionary<string, string>>? dctCache = null; Dictionary<string, Dictionary<string, string>> dctCache = new(StringComparer.OrdinalIgnoreCase);
Dictionary<string, MdlVbsSources> dctFileCache = new();//(StringComparer.OrdinalIgnoreCase); Dictionary<string, MdlVbsSources> dctFileCache = new();//(StringComparer.OrdinalIgnoreCase);
Random rn = new(); Random rn = new();
List<string> lstFilesToRemove = new(); List<string> lstFilesToRemove = new();
bool removedOldScripts = false; bool removedOldScripts = false;
ILog? appLog = null;
public void Dispose() public void Dispose()
{ {
if (dctCache != null) if (dctCache != null)
dctCache.Clear(); dctCache.Clear();
if (dctFileCache != null) if (dctFileCache != null)
dctFileCache.Clear(); 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; public bool HadErrors { get; set; } = false;
@ -40,7 +51,7 @@ public class MainScriptingObject : IDisposable
set set
{ {
scriptSource = CleanUserCode(value); scriptSource = CleanUserCode(value);
dctCache = new(); dctCache.Clear(); //= new(StringComparer.);
} }
} }
@ -80,7 +91,7 @@ public class MainScriptingObject : IDisposable
if (dctFileCache.ContainsKey(theKey)) if (dctFileCache.ContainsKey(theKey))
{ {
scriptSource = dctFileCache[theKey].VbsScriptSource; scriptSource = dctFileCache[theKey].VbsScriptSource;
dctCache = dctFileCache[theKey].CacheDict; //dctCache = dctFileCache[theKey].CacheDict;
rv = true; rv = true;
} }
else else
@ -88,7 +99,7 @@ public class MainScriptingObject : IDisposable
if (!File.Exists(VBScriptPath)) if (!File.Exists(VBScriptPath))
{ {
scriptSource = string.Empty; scriptSource = string.Empty;
dctCache = null; dctCache.Clear();
rv = false; rv = false;
} }
else else
@ -98,15 +109,26 @@ public class MainScriptingObject : IDisposable
scriptSource = sr.ReadToEnd(); scriptSource = sr.ReadToEnd();
z.VbsScriptSource = scriptSource; z.VbsScriptSource = scriptSource;
z.CacheDict = new(); //z.CacheDict = new();
dctFileCache[theKey] = z; dctFileCache[theKey] = z;
dctCache = dctFileCache[theKey].CacheDict; //dctCache = dctFileCache[theKey].CacheDict;
rv = true; rv = true;
} }
} }
return rv; 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() public void ClearScriptInputs()
{ {
scrInp1.Clear(); scrInp1.Clear();
@ -150,16 +172,24 @@ public class MainScriptingObject : IDisposable
var scriptFile = Path.Combine(tempPath, tStamp + ".vbs"); var scriptFile = Path.Combine(tempPath, tStamp + ".vbs");
var iClearValues = string.Join('\t', scrInp2.ToArray()); var iClearValues = string.Join('\t', scrInp2.ToArray());
var iKey = SHA256Hash(iClearValues); var iKey = SHA256Hash(iClearValues);
var toReturn = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var retVals = string.Empty; var retVals = string.Empty;
var varName = string.Empty; var varName = string.Empty;
var varVal = string.Empty; var varVal = string.Empty;
Dictionary<string, string> toReturn = new(); // new SortedDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
HadErrors = false; HadErrors = false;
lstFilesToRemove.Add(outFile); lstFilesToRemove.Add(outFile);
lstFilesToRemove.Add(scriptFile); 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<string,string>;
if (DebugMode)
appLog.DebugFormat("OUTPUT FROM CACHE ({0}) == {1}", iKey, string.Join('~', toReturn.ToArray())); //dctCache[iKey].ToArray()));
}
else else
{ {
var genCodeHeader = new StringBuilder(); var genCodeHeader = new StringBuilder();
@ -180,8 +210,8 @@ public class MainScriptingObject : IDisposable
genCodeHeader.AppendLine("'* Revised in 2015 by KeyMotive LLC (Rich Deck) *"); genCodeHeader.AppendLine("'* Revised in 2015 by KeyMotive LLC (Rich Deck) *");
genCodeHeader.AppendLine("'* Refreshed in 2025 by KeyMotive LLC (Rich Deck)*"); genCodeHeader.AppendLine("'* Refreshed in 2025 by KeyMotive LLC (Rich Deck)*");
genCodeHeader.AppendLine("'*************************************************"); genCodeHeader.AppendLine("'*************************************************");
genCodeHeader.AppendLine("'Non-Crypto Lookup: " + iClearValues); //genCodeHeader.AppendLine("'Non-Crypto Lookup: " + iClearValues);
genCodeHeader.AppendLine("'Crypto Lookup: " + iKey); //genCodeHeader.AppendLine("'Crypto Lookup: " + iKey);
genCodeHeader.AppendLine("'*************************************************"); genCodeHeader.AppendLine("'*************************************************");
genCodeHeader.AppendLine("outFile = \"" + outFile + "\""); genCodeHeader.AppendLine("outFile = \"" + outFile + "\"");
@ -219,7 +249,7 @@ public class MainScriptingObject : IDisposable
varVal = string.Format("\"{0}\"", kvp.Value.ToString().Replace("\"", "\"\"")); 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("\"", "\"\"") + "\""; varName = "\"" + kvp.Key.ToUpper().Replace("\"", "\"\"") + "\"";
genCodeHeader.AppendLine(" Case " + varName); genCodeHeader.AppendLine(" Case " + varName);
genCodeHeader.AppendLine(" retVal = " + varVal); genCodeHeader.AppendLine(" retVal = " + varVal);
@ -275,28 +305,43 @@ public class MainScriptingObject : IDisposable
retVals = sr.ReadToEnd(); retVals = sr.ReadToEnd();
} }
toReturn.Clear(); //toReturn = new(StringComparer.OrdinalIgnoreCase);
foreach (var f0 in Regex.Split(retVals, "(\r\n|\n\r|\r|\n)")) foreach (var f0 in Regex.Split(retVals, @"(\r\n|\n\r|\r|\n)"))
{ {
if (f0.Length >= 3 && f0.IndexOf('\t') > 0) if (f0.Length >= 3 && f0.IndexOf('\t') > 0)
{ {
var nvp = f0.Split('\t'); var nvp = f0.Split('\t');
nvp[1] = Regex.Replace(nvp[1], "\x7eNewLine\x7e", "\n"); nvp[1] = Regex.Replace(nvp[1], @"\x7eNewLine\x7e", "\n");
toReturn[nvp[0]] = nvp[1]; //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) if (UseCache)// && dctCache != null)
dctCache[iKey] = toReturn; {
dctCache.Add(iKey, toReturn);// ?? new Dictionary<string, string>());
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) catch (Exception ex)
{ {
if (DebugMode)
appLog.Error(ex);
using (var sw = new StreamWriter(outFile, true)) using (var sw = new StreamWriter(outFile, true))
sw.WriteLine(ex.ToString()); sw.WriteLine(ex.ToString());
} }
} }
return toReturn; return new Dictionary<string, string>(toReturn);
} }
#region "Maint routines" #region "Maint routines"
@ -328,7 +373,7 @@ public class MainScriptingObject : IDisposable
string StripUnprintables(string inp) string StripUnprintables(string inp)
{ {
var rv = Regex.Replace(inp, "[^\x20-\x7E]", " "); var rv = Regex.Replace(inp, @"[^\x20-\x7E]", " ");
rv = Regex.Replace(rv, " +", " ").Trim(); rv = Regex.Replace(rv, " +", " ").Trim();
return rv; return rv;
} }
@ -429,11 +474,11 @@ public class MainScriptingObject : IDisposable
{ {
public void Dispose() public void Dispose()
{ {
CacheDict.Clear(); //CacheDict.Clear();
} }
public string VbsScriptSource { get; set; } = string.Empty; public string VbsScriptSource { get; set; } = string.Empty;
public Dictionary<string, Dictionary<string, string>> CacheDict { get; set; } = new(); //public Dictionary<string, Dictionary<string, string>> CacheDict { get; set; } = new();
} }
#endregion #endregion

View File

@ -1,16 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net9.0-windows</TargetFramework> <TargetFramework>net10.0-windows10.0.22000.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AssemblyVersion>1.2.0.21</AssemblyVersion> <AssemblyVersion>1.2.0.53</AssemblyVersion>
<FileVersion>1.2.0.21</FileVersion> <FileVersion>1.2.0.53</FileVersion>
<Version>1.0.0.21</Version> <Version>1.0.0.53</Version>
<SupportedOSPlatformVersion>10.0.22000.0</SupportedOSPlatformVersion>
</PropertyGroup> </PropertyGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent"> <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="if /I &quot;$(ConfigurationName)&quot; == &quot;Release&quot; c:\Misc\AutoVersionIncrement.exe &quot;$(ProjectDir)&quot;" /> <Exec Command="if /I &quot;$(ConfigurationName)&quot; == &quot;Release&quot; c:\Misc\AutoVersionIncrement.exe &quot;$(ProjectDir)&quot;" />
</Target> </Target>
<ItemGroup>
<PackageReference Include="log4net" Version="3.3.1" />
</ItemGroup>
</Project> </Project>