This commit is contained in:
RDeck 2026-06-09 08:57:29 -04:00
parent bc0165e3ae
commit ec9c57b6ca
3 changed files with 136 additions and 8 deletions

View File

@ -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<string, string> scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it
string scriptSource = string.Empty;
string tempPath = Path.GetTempPath();
Dictionary<Guid, Dictionary<string, string>>? dctCache = null;
Dictionary<string, Dictionary<string, string>>? dctCache = null;
Dictionary<string, MdlVbsSources> dctFileCache = new(StringComparer.OrdinalIgnoreCase);
Random rn = new();
List<string> 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<string, string> toReturn = new(StringComparer.OrdinalIgnoreCase);
var iKey = SHA1Hash(string.Join('\t', scrInp2.ToArray()));
var toReturn = new Dictionary<string, string>(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<Guid, Dictionary<string, string>> CacheDict { get; set; } = new();
public Dictionary<string, Dictionary<string, string>> CacheDict { get; set; } = new();
}
#endregion

109
ScriptingCS.cs Normal file
View File

@ -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<string, string> scrInp1 = new(StringComparer.OrdinalIgnoreCase);
SortedDictionary<string, string> scrInp2 = new(StringComparer.OrdinalIgnoreCase); //This will have the STRIPUNPRINTABLES applied to it
Dictionary<Guid, Dictionary<string, string>> dctCache = new();
string scriptSource = string.Empty;
string tempPath = Path.GetTempPath();
Random rn = new();
List<string> 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<Guid, Dictionary<string, string>> CacheDict { get; set; } = new();
}
#endregion
}

View File

@ -4,9 +4,9 @@
<TargetFramework>net9.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.2.0.6</AssemblyVersion>
<FileVersion>1.2.0.6</FileVersion>
<Version>1.0.0.6</Version>
<AssemblyVersion>1.2.0.8</AssemblyVersion>
<FileVersion>1.2.0.8</FileVersion>
<Version>1.0.0.8</Version>
</PropertyGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">