mScriptableCS25/ScriptingCS.cs

110 lines
3.1 KiB
C#
Raw Normal View History

2026-06-09 08:57:29 -04:00
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
}