/// <summary> /// Remove a file (or directory with all sub files) from an existing zip archive. /// </summary> /// <param name="zipFile">The zip archive to remove a file from.</param> /// <param name="files"> /// An array of files to remove from the zip archive. /// A single file or directory may also be deleted. /// Any directories will be recursively removed from the zip. /// </param> /// <returns>An error message or "".</returns> public static Primitive Remove(Primitive zipFile, Primitive files) { try { using (ZipFile zip = ZipFile.Read(zipFile)) { if (SBArray.IsArray(files)) { Primitive indices = SBArray.GetAllIndices(files); int count = SBArray.GetItemCount(indices); for (int i = 1; i <= count; i++) { RemoveFromArchive(zip, files[indices[i]]); } } else { RemoveFromArchive(zip, files); } zip.Save(); } return(""); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return(Utilities.GetCurrentMethod() + " " + ex.Message); } }
private static string getFilter(Primitive extension) { string filter = ""; if (((string)extension).Contains("|")) { filter = extension; } else if (SBArray.IsArray(extension)) { Primitive indices = SBArray.GetAllIndices(extension); int count = SBArray.GetItemCount(indices); string types = ""; for (int i = 1; i <= count; i++) { types += "*." + extension[indices[i]]; if (i < count) { types += ";"; } } filter = "File Type (" + types + ") |" + types; } else { filter = "File Type (*." + extension + ") |*." + extension; } return(filter); }
/// <summary> /// Compress files to a zip archive. /// </summary> /// <param name="zipFile">The zip archive file to create.</param> /// <param name="files"> /// An array of files to append to the zip archive. /// A single file or directory may also be set. /// Any directories will be recursively added to the zip. /// Any white space in files or directories will be replaced with "_". /// </param> /// <returns>An error message or "".</returns> public static Primitive Zip(Primitive zipFile, Primitive files) { try { using (Package zip = ZipPackage.Open(zipFile, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { if (SBArray.IsArray(files)) { Primitive indices = SBArray.GetAllIndices(files); int count = SBArray.GetItemCount(indices); for (int i = 1; i <= count; i++) { AddToArchive(zip, files[indices[i]], ""); } } else { AddToArchive(zip, files, ""); } zip.Close(); } return(""); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return(Utilities.GetCurrentMethod() + " " + ex.Message); } }
/// <summary> /// Set a list of files to the clipboard. /// </summary> /// <param name="fileList">An array (or single file) of file names (full path).</param> /// <returns>"SUCCESS" or "FAILED".</returns> public static Primitive SetFileList(Primitive fileList) { try { CB_files = new StringCollection(); if (SBArray.IsArray(fileList)) { Primitive indices = SBArray.GetAllIndices(fileList); for (int i = 1; i <= SBArray.GetItemCount(indices); i++) { CB_files.Add(fileList[indices[i]]); } } else { CB_files.Add(fileList); } Thread thread = new Thread(CB_SetFileList); thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA thread.Start(); thread.Join(); //Wait for the thread to end return("SUCCESS"); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return("FAILED"); } }
/// <summary> /// Split a variable into an array delimiated by a separator. /// </summary> /// <param name="text">A text string to split.</param> /// <param name="separator">A separator string (e.g. " "), or an array of separator strings.</param> /// <returns>A result array of deliminated texts.</returns> public static Primitive Split(Primitive text, Primitive separator) { try { string[] separators; if (SBArray.IsArray(separator)) { int count = SBArray.GetItemCount(separator); separators = new string[count]; Primitive indices = SBArray.GetAllIndices(separator); for (int i = 0; i < count; i++) { separators[i] = separator[indices[i + 1]]; } } else { separators = new string[] { separator }; } string[] splitText = ((string)text).Split(separators, System.StringSplitOptions.RemoveEmptyEntries); string result = ""; for (int i = 0; i < splitText.Length; i++) { result += (i + 1).ToString() + "=" + Utilities.ArrayParse(splitText[i]) + ";"; } return(Utilities.CreateArrayMap(result)); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return(""); } }
private static Object[] GetArguments(Primitive args, MethodInfo methodInfo) { Object[] arguments = new Object[] { }; if (SBArray.IsArray(args)) { Primitive indices = SBArray.GetAllIndices(args); int count = SBArray.GetItemCount(indices); arguments = new Object[count]; for (int i = 1; i <= count; i++) { arguments[i - 1] = ObjectCast(args[indices[i]], methodInfo.GetParameters()[i - 1].ParameterType); } } else if (args != "") { arguments = new Object[1]; arguments[0] = ObjectCast(args, methodInfo.GetParameters()[0].ParameterType); } return(arguments); }
private static bool isCondition() { string varName = FormDebug.conditionName.ToLower(); Primitive varValue = FormDebug.conditionValue; varValue = Text.ConvertToLowerCase(varValue); if (varName == "" || varValue == "") { return(false); } try { if (applicationThread.ThreadState != System.Threading.ThreadState.Running) { applicationThread.Suspend(); } StackTrace stackTrace = new StackTrace(applicationThread, false); StackFrame frame = stackTrace.GetFrame(stackTrace.FrameCount - 1); MethodBase method = frame.GetMethod(); Type type = method.DeclaringType; FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic); for (int i = 0; i < fields.Length; i++) { Primitive var = (Primitive)(fields[i].GetValue(null)); var = Text.ConvertToLowerCase(var); int pos = varName.IndexOf("["); string variable = pos < 0 ? varName : varName.Substring(0, pos); if (fields[i].Name.ToLower() == variable) { string arrayName = fields[i].Name.ToLower(); if (SBArray.IsArray(var)) { Primitive Indices1 = SBArray.GetAllIndices(var); for (int j = 1; j <= SBArray.GetItemCount(Indices1); j++) { Primitive var1 = var[Indices1[j]]; arrayName = ((string)(fields[i].Name + "[" + Indices1[j] + "]")).ToLower(); if (SBArray.IsArray(var1)) { Primitive Indices2 = SBArray.GetAllIndices(var1); for (int k = 1; k <= SBArray.GetItemCount(Indices2); k++) { Primitive var2 = var1[Indices2[k]]; arrayName = ((string)(fields[i].Name + "[" + Indices1[j] + "]" + "[" + Indices2[k] + "]")).ToLower(); if (SBArray.IsArray(var2)) { Primitive Indices3 = SBArray.GetAllIndices(var2); for (int l = 1; l <= SBArray.GetItemCount(Indices3); l++) { Primitive var3 = var2[Indices3[l]]; arrayName = ((string)(fields[i].Name + "[" + Indices1[j] + "]" + "[" + Indices2[k] + "]" + "[" + Indices3[l] + "]")).ToLower(); if (arrayName.StartsWith(varName) && checkCondition(var3, varValue)) { return(true); } } } else { if (arrayName.StartsWith(varName) && checkCondition(var2, varValue)) { return(true); } } } } else { if (arrayName.StartsWith(varName) && checkCondition(var1, varValue)) { return(true); } } } } else { if (checkCondition(var, varValue)) { return(true); } } } } return(false); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); if (Utilities.bShowErrors) { TextWindow.WriteLine("ThreadState " + applicationThread.ThreadState.ToString()); } return(false); } }
private void AddVariables(StackTrace stackTrace) { StackFrame frame = stackTrace.GetFrame(stackTrace.FrameCount - 1); MethodBase method = frame.GetMethod(); Type type = method.DeclaringType; FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic); List <varSort> fieldsSort = new List <varSort>(); for (int i = 0; i < fields.Length; i++) { fieldsSort.Add(new varSort(i, fields[i].Name)); } fieldsSort.Sort(); // 3D array is enough (could make it infinite but messy the way the tree is defined) foreach (varSort variable in fieldsSort) { int i = variable.I; Primitive var = (Primitive)fields[i].GetValue(null); if (SBArray.IsArray(var)) { treeView1.Nodes[1].Nodes.Add(variable.Name).Tag = new vars(i, new string[] { }); Primitive Indices = SBArray.GetAllIndices(var); for (int j = 1; j <= SBArray.GetItemCount(Indices); j++) { Primitive var1 = var[Indices[j]]; if (SBArray.IsArray(var1)) { treeView1.Nodes[1].Nodes[treeView1.Nodes[1].Nodes.Count - 1].Nodes.Add(Indices[j]).Tag = new vars(i, new string[] { Indices[j] }); Primitive Indices1 = SBArray.GetAllIndices(var1); for (int k = 1; k <= SBArray.GetItemCount(Indices1); k++) { Primitive var2 = var1[Indices1[k]]; if (SBArray.IsArray(var2)) { treeView1.Nodes[1].Nodes[treeView1.Nodes[1].Nodes.Count - 1].Nodes[j - 1].Nodes.Add(Indices1[k]).Tag = new vars(i, new string[] { Indices[j], Indices1[k] }); Primitive Indices2 = SBArray.GetAllIndices(var2); for (int l = 1; l <= SBArray.GetItemCount(Indices2); l++) { Primitive var3 = var2[Indices2[l]]; treeView1.Nodes[1].Nodes[treeView1.Nodes[1].Nodes.Count - 1].Nodes[j - 1].Nodes[k - 1].Nodes.Add(Indices2[l] + " : " + var3).Tag = new vars(i, new string[] { Indices[j], Indices1[k], Indices2[l] }); } } else { treeView1.Nodes[1].Nodes[treeView1.Nodes[1].Nodes.Count - 1].Nodes[j - 1].Nodes.Add(Indices1[k] + " : " + var2).Tag = new vars(i, new string[] { Indices[j], Indices1[k] }); } } } else { treeView1.Nodes[1].Nodes[treeView1.Nodes[1].Nodes.Count - 1].Nodes.Add(Indices[j] + " : " + var1).Tag = new vars(i, new string[] { Indices[j] }); } } } else { treeView1.Nodes[0].Nodes.Add(variable.Name + " : " + var).Tag = new vars(i, new string[] { }); } } }
/// <summary> /// Call any extension method asynchronously. /// See example LDCallAsync. /// If dll, extension, obj and arguments are all "", then method may be a subroutine in your SmallBasic program. /// </summary> /// <param name="dll">The extension dll (e.g. "LitDev.dll" or "SmallBasicLibrary.dll").</param> /// <param name="extension">The extension namespace (usually the same as the dll name, e.g. "LitDev" or "MicroSoft.SmallBasic.Library" for SmallBasicLibrary.dll).</param> /// <param name="obj">The extension object name.</param> /// <param name="method">The extension method name.</param> /// <param name="arguments">An array of arguments or "" for none. A single argument doesn't have to be in an array.</param> /// <returns>"PENDING" or an error message on failure.</returns> public static Primitive CallAsync(Primitive dll, Primitive extension, Primitive obj, Primitive method, Primitive arguments) { try { Type type; string callName = ""; if (dll == "" && extension == "" && obj == "" && arguments == "") { type = mainModule; } else { string path = Path.GetDirectoryName(entryAssembly.Location) + "\\" + dll; if (!System.IO.File.Exists(path)) { return(dll + " dll not found"); } Assembly assembly = Assembly.LoadFrom(path); type = assembly.GetType(extension + "." + obj, false, true); if (null == type) { return(extension + "." + obj + " extension not found"); } callName += extension + "." + obj + "."; } MethodInfo methodInfo = type.GetMethod((string)method, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase); if (null == methodInfo) { return(method + " method not found"); } int numArgs = 0; Object[] args = null; callName += method + "("; if (SBArray.IsArray(arguments)) { numArgs = SBArray.GetItemCount(arguments); args = new Object[numArgs]; Primitive indices = SBArray.GetAllIndices(arguments); for (int i = 1; i <= numArgs; i++) { args[i - 1] = arguments[indices[i]]; callName += arguments[indices[i]]; if (i < numArgs) { callName += ","; } } } else if (arguments != "") { args = new Object[1] { arguments }; callName += arguments; } callName += ")"; Thread thread = new Thread(new ParameterizedThreadStart(DoCall)); thread.Start(new Object[] { methodInfo, args, callName }); //while (!thread.IsAlive) Thread.Sleep(1); //delay to let async call get started return("PENDING"); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); return(ex.Message); } }
private static CompilerParameters SetReferences(Primitive assemblies, string dllName, bool bJS = false) { CompilerParameters compilerParams = new CompilerParameters(); compilerParams.TreatWarningsAsErrors = false; compilerParams.GenerateExecutable = false; if (!bJS) { compilerParams.CompilerOptions = "/optimize"; } if (dllName.Length == 0) { compilerParams.GenerateInMemory = true; } else { compilerParams.GenerateInMemory = false; if (!dllName.EndsWith(".dll")) { dllName += ".dll"; } compilerParams.OutputAssembly = dllName; if (!bJS) { compilerParams.CompilerOptions += " /doc:" + '"' + Path.ChangeExtension(dllName, ".xml") + '"'; } } loadedAssemblies.Clear(); AddReferences(Assembly.GetExecutingAssembly(), compilerParams); if (!bJS) { foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { AddReferences(assembly, compilerParams); } } if (SBArray.IsArray(assemblies)) { Primitive indices = SBArray.GetAllIndices(assemblies); for (int i = 1; i <= SBArray.GetItemCount(indices); i++) { try { AddReference(Assembly.ReflectionOnlyLoadFrom(assemblies[indices[i]]), compilerParams); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); } } } else if (assemblies != "") { try { AddReference(Assembly.ReflectionOnlyLoadFrom(assemblies), compilerParams); } catch (Exception ex) { Utilities.OnError(Utilities.GetCurrentMethod(), ex); } } if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Resources")) { string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "\\Resources"); foreach (string file in files) { compilerParams.EmbeddedResources.Add(file); } } return(compilerParams); }