public static string RemoveDirtFromCode(string code) { string[] script = ESCode.SplitCode(code); StringBuilder result = new StringBuilder(); for (int i = 0; i < script.Length; i++) { result.AppendLine(RemoveDirtFromString(script[i])); } return(result.ToString()); }
public static bool ReadToken(Stream s, ESCode code) { string str = ""; while (true) { char c = s.ReadChar(); if (c == '\0') { return(false); } if (c == ' ') { break; } if (c == ';') { return(true); //прочитано сука } str += c; } if (str == "string") { var token = new EVariableDef(EVariableType.E_STRING); token.Read(s); Variables.SetVariableObject(token.name, (string)token.value); } if (str == "bool") { var token = new EVariableDef(EVariableType.E_BOOL); token.Read(s); Variables.SetVariableObject(token.name, (bool)token.value); } return(ReadToken(s, code)); }
public static string CheckForImports(IntPtr resourceUpdateHandle, string scriptContent, string mname, string mainPath) { string result = scriptContent; var h = resourceUpdateHandle; string[] scriptLines = ESCode.SplitCode(scriptContent); foreach (var line in scriptLines) { if (line.StartsWith("Import ")) { FileInfo importScriptFile = new FileInfo(line.Remove(0, "Import ".Length)); if (!importScriptFile.Exists) { importScriptFile = new FileInfo(Path.Combine(mainPath, line.Remove(0, "Import ".Length))); } if (!importScriptFile.Exists) { EConsole.WriteLine($"[ERROR] File {importScriptFile.FullName} not found ({line})", true, ConsoleColor.Red); continue; } if (importScriptFile.Extension.ToLower() != ".es" && importScriptFile.Extension.ToLower() != ".escript" && importScriptFile.Extension.ToLower() != ".esh") { EConsole.WriteLine($"Import: Unsupported extension: {importScriptFile.Extension}", true, ConsoleColor.Yellow); continue; } string importName = "import" + Cmd.Process("RandomString 16"); result = result.Replace(line, "Import " + importName); EConsole.WriteLine("Processing Import " + importScriptFile.Name + "..."); string importScriptContent; using (StreamReader r = new StreamReader(importScriptFile.FullName)) { importScriptContent = r.ReadToEnd(); } importScriptContent = GlobalVars.RemoveDirtFromCode(importScriptContent); importScriptContent = CheckForImports(h, importScriptContent, mname, mainPath); //importScriptContent = "BASE64:" + GlobalVars.Base64Encode(importScriptContent); byte[] bytes = Encoding.ASCII.GetBytes(importScriptContent); // Get language identifier System.Globalization.CultureInfo currentCulture = System.Globalization.CultureInfo.CurrentCulture; int pid = ((ushort)currentCulture.LCID) & 0x3ff; int sid = ((ushort)currentCulture.LCID) >> 10; ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid)); // Get pointer to data GCHandle scriptHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned); if (GlobalVars.UpdateResource(h, (IntPtr)10, importName, languageID, scriptHandle.AddrOfPinnedObject(), (uint)bytes.Length)) { EConsole.WriteLine("[" + importName + "(" + importScriptFile.Name + ")] 0x" + scriptHandle.AddrOfPinnedObject().ToString("X4") + " (" + bytes.Length + ") -> " + mname); } else { throw new Exception($"Can't insert resource ({importScriptFile.Name}): " + Marshal.GetLastWin32Error()); } } } return(result); }
//public static void PrintResult(string result, string prefix = "null") //{ // Cmd.Process("PrintResult {#" + result + "}||" + prefix, null, true); //} public static void Init(string[] args) { EConsole.ForegroundColor = ScriptColor; new ESCode(ESCode.SplitCode(ReadResource("escript.Functions.es"))); Debug("escript.Functions.es imported"); if (args.Contains <string>("-ignoreRunasRestart")) { GlobalVars.IgnoreRunAsAdmin = true; } if (args.Contains <string>("/help") || args.Contains <string>("/?") || args.Contains <string>("--h") || args.Contains <string>("-help")) { Cmd.Process("ShowConsole"); EConsole.ForegroundColor = ConsoleColor.Gray; EConsole.WriteLine("ESCRIPT <Script Path> [/help] [/convert] [-ignoreRunasRestart] [-console] [-debug] [-cmd] [-close] [-install]"); EConsole.WriteLine(""); EConsole.WriteLine("/help\t\t\tShow this article"); EConsole.WriteLine("-ignoreRunAsRestart\tIgnore 'Restart 1'"); EConsole.WriteLine("-console\t\tUse ConsoleMessageBox instead of Windows Forms"); EConsole.WriteLine("-debug\t\t\tStart with debug mode"); EConsole.WriteLine("-cmd\t\t\tStart command interpretator"); EConsole.WriteLine("-install\t\tStart installation"); EConsole.WriteLine(" "); EConsole.WriteLine("<Script Path> /convert <Out Path> <Icon Path>\tConvert .es to .exe"); EConsole.WriteLine("Press any key..."); EConsole.ReadKey(); Environment.Exit(100); } if (args.Contains <string>("/convert")) { try { FileInfo s = new FileInfo(args[0]); if (!File.Exists(s.FullName)) { throw new Exception("Script " + args[0] + " not found"); } string outPath = ""; if (s.Name.Contains(".es")) { outPath = s.Name.Replace(".es", ".exe"); } else { outPath = s.Name + ".exe"; } try { if (args[2].Length >= 1) { outPath = args[2]; } } catch { } string iconArg = ""; try { FileInfo ii = new FileInfo(args[3]); if (File.Exists(ii.FullName)) { iconArg = ii.FullName; } } catch { } FileInfo outFile = new FileInfo(outPath); bool anykey = false; if (args.Contains("-anykey")) { anykey = true; } CompileScript(s.FullName, outFile.FullName, iconArg, anykey); Environment.Exit(0); } catch (Exception ex) { Cmd.Process("ShowConsole"); EConsole.ForegroundColor = ConsoleColor.Gray; EConsole.WriteLine("ERROR: " + ex.Message); EConsole.WriteLine(""); EConsole.WriteLine("Use: ESCRIPT <Script Path> /convert (<Compiled File Path>) (<Icon Path>)"); EConsole.WriteLine("Examples:"); EConsole.WriteLine("escript \"MyScript.es\" /convert\twill be converted in MyScript.exe"); EConsole.WriteLine("escript \"MyScript.es\" /convert \"C:\\Programs\\MyScript.exe\""); EConsole.WriteLine("escript \"MyScript.es\" /convert \"C:\\Programs\\MyScript.exe\" \"myicon.ico\""); EConsole.WriteLine(""); EConsole.WriteLine("If you want to insert icon, you must set <Compiled File Path>"); EConsole.ForegroundColor = ConsoleColor.Gray; EConsole.WriteLine("Press any key to close this window..."); EConsole.ReadKey(); Environment.Exit(404); } } //Cmd.Process("SetStatus ESCRIPT Initializing||Please wait..."); //Cmd.Process("sleep 2000"); //Cmd.Process("HideStatus"); if (!args.Contains <string>("-ignoreLib")) { foreach (var f in GlobalVars.GetAboutMe().Directory.EnumerateFiles()) { if (f.Name.Contains("ESLib")) { Cmd.Process("Import " + f.FullName); } } } Debug("Environment formed", ConsoleColor.DarkGreen); #if !IsCore IntPtr resourceInfo = GlobalVars.FindResource(IntPtr.Zero, "script", (IntPtr)10); Debug("SCRIPT Resource: 0x" + resourceInfo.ToString("X4")); if (resourceInfo != IntPtr.Zero) { uint size = GlobalVars.SizeofResource(IntPtr.Zero, resourceInfo); IntPtr pt = GlobalVars.LoadResource(IntPtr.Zero, resourceInfo); GlobalVars.IsCompiledScript = true; Variables.Set("isCompiledScript", "1"); byte[] bPtr = new byte[size]; Marshal.Copy(pt, bPtr, 0, (int)size); string code = Encoding.ASCII.GetString(bPtr); Debug("SCRIPT:\r\n" + code); ESCode script = new ESCode(ESCode.SplitCode(code)); script.RunScript(); Break(); } #endif if (args.Contains <string>("-close")) { if (GlobalVars.IsCompiledScript) { throw new Exception("Can't install compiled version. Please, use clean ESCRIPT or remove script information from resources manually."); } foreach (var p in Process.GetProcesses()) { try { if (p.ProcessName.ToLower() == "escript" && p.Id != Process.GetCurrentProcess().Id) { p.Kill(); } if (p.ProcessName.ToLower() == "escript-update" && p.Id != Process.GetCurrentProcess().Id) { p.Kill(); } } catch { } } } if (args.Contains <string>("-install")) { if (GlobalVars.IsCompiledScript) { throw new Exception("Can't install compiled version. Please, use clean ESCRIPT or remove script information from resources manually."); } string InstallScript = "InstallScript.es"; WriteResourceToFile("escript.InstallScript.es", InstallScript); ESCode script = new ESCode(InstallScript); script.RunScript(); Environment.Exit(0); } if (args.Contains <string>("-assoc")) { if (GlobalVars.IsCompiledScript) { throw new Exception("Can't install compiled version. Please, use clean ESCRIPT or remove script information from resources manually."); } string me = System.Reflection.Assembly.GetExecutingAssembly().Location; #if !IsCore //Cmd.Process("ShowConsole"); EConsole.WriteLine("Associating ESCRIPT files..."); try { FileAssociation.AssociateESCRIPT("ESCRIPT File", me, ".es"); FileAssociation.AssociateESCRIPT("ESCRIPT File", me, ".escript"); FileAssociation.AssociateESCRIPT("ESCRIPT Header File", me, ".esh", "escriptheader", false); #if !IsCore FileAssociation.SHChangeNotify(FileAssociation.SHCNE_ASSOCCHANGED, FileAssociation.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero); #endif } catch (Exception ex) { EConsole.WriteLine("ERROR: " + ex.Message); Environment.Exit(21); } EConsole.WriteLine("Creating a script on the desktop..."); try { string desktop = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ESCRIPT.es"); using (StreamWriter w = new StreamWriter(desktop)) { w.WriteLine("start {GetProgramPath}"); w.WriteLine("Exit"); } desktop = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ESCRIPT Folder.es"); WriteResourceToFile("escript.ExampleFolder.es", desktop); } catch (Exception ex) { EConsole.WriteLine("ERROR: " + ex.Message); Environment.Exit(21); } #else EConsole.ForegroundColor = ConsoleColor.Red; EConsole.WriteLine("WARNING: ESCRIPT Core cannot be installed"); #endif EConsole.ForegroundColor = ConsoleColor.Green; EConsole.WriteLine("ESCRIPT was installed!"); //Thread.Sleep(2000); EConsole.WriteLine(""); Environment.Exit(0); } try { if (GlobalVars.UsingAPI) { return; } if ((args.Length <= 0 && !GlobalVars.UsingAPI) || args.Contains <string>("-cmd")) { CommandLine(); } else if (File.Exists(args[0])) { ESCode script = new ESCode(args[0]); script.RunScript(); } else if (!File.Exists(args[0])) { CommandLine(); } // if (args.Contains<string>("-installNext")) // { // foreach (var p in Process.GetProcesses()) // { // if (p.ProcessName.ToLower() == "escript" && p.Id != Process.GetCurrentProcess().Id) p.Kill(); // } // string me = System.Reflection.Assembly.GetExecutingAssembly().Location; // string destination = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ESCRIPT"); // FileInfo aboutme = GetAboutMe(); // if (aboutme.DirectoryName == destination && aboutme.Name.ToLower().StartsWith("escript.exe")) // { // EConsole.WriteLine("You can't do this. Use escript-install.exe or UpdateProgram method."); // } // else // { // EConsole.WriteLine("Installing ESCRIPT..."); //#if !IsCore // if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),"ESCRIPT"))) Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "ESCRIPT")); // File.Copy(me, Path.Combine(destination, "escript.exe"), true); //#else // EConsole.WriteLine("Can't install ESCRIPT Core"); //#endif // new Process() { StartInfo = { FileName = Path.Combine(destination, "escript.exe"), Arguments = "-close -assoc" } }.Start(); // Cmd.Process("HideConsole", null, null); // Environment.Exit(0); // } // } } catch (Exception ex) { BachokMessage(ex); } Break(); }