// UTF-16 (LE BOM) // Requirements: // - Windows 2000 or later (Windows 95/98/ME is not supported) // - Windows Script Host 5.6 or later // (using [Global, Enumerator, RegExp, WScript.Shell.exec]) // msdn.microsoft.com/en-us/library/vstudio/2z6exc9e%28v=vs.100%29.aspx // - beatoraja 0.5.2 or later // - launch4j: http://launch4j.sourceforge.net/ // (exewrap will not work as we expect.) /*jslint browser */ /*global WScript, Enumerator, GetObject */ // Config Start ============================================================== var BEATORAJA = "D:\\bms\\beatoraja\\beatoraja.exe"; var AUTOPLAY = false; // Config End ================================================================ (function (ws) { "use strict"; if (!ws || !Enumerator || parseFloat(ws.Version) < 5.6) { ws.echo("This script requires Windows Script Host 5.6 or later."); ws.quit(1); } var msg = "Drop or Specify a BMS(ON) file."; var args_count = ws.Arguments.count(); if (args_count === 0) { ws.echo(msg); ws.quit(1); } var shell = null; var fso = null; (function () { shell = ws.createObject("WScript.Shell"); fso = ws.createObject("Scripting.FileSystemObject"); }()); if (typeof BEATORAJA === "undefined") { ws.echo("Specify BEATORAJA variable on this JScript."); ws.quit(1); } if (typeof AUTOPLAY === "undefined") { ws.echo("Specify AUTOPLAY variable on this JScript."); ws.quit(1); } if (typeof AUTOPLAY !== "boolean") { ws.echo("Specify AUTOPLAY variable 'true' or 'false'."); ws.quot(1); } if (!fso.fileExists(BEATORAJA)) { ws.echo("Specify full path of beatoraja.exe.\r\n (This is not beatoraja.jar. Use launch4j, exewrap, etc.)"); ws.quit(1); } if (!fso.fileExists(fso.buildPath(fso.getSpecialFolder(1), "taskkill.exe"))) { ws.echo("This script requires 'taskkill' command\r\n (Windows XP Professional or later)."); ws.quit(1); } var CUR = fso.getParentFolderName(ws.ScriptFullName); var i = 0; var arg; var rx = /^(?:BMSON|BMS|BME|BML|PMS)$/i; var type = ""; var file; if (args_count === 1) { file = ws.Arguments.item(0); if (fso.folderExists(file) || !rx.test(fso.getExtensionName(file))) { ws.echo(msg); ws.quit(1); } file = fso.getAbsolutePathName(file); type = AUTOPLAY ? " -a" : ""; } else { while (i < args_count) { arg = ws.Arguments.item(i); if (fso.fileExists(arg) && rx.test(fso.getExtensionName(arg))) { file = fso.getAbsolutePathName(arg); if (type) { break; } } else if (arg.length < 4 && arg.charAt(0) === "-") { switch (arg) { case "-a": case "-p": case "-r1": case "-r2": case "-r3": case "-r4": type = " " + arg; break; } if (type && file) { break; } } i += 1; } } if (!file) { ws.echo(msg); ws.quit(1); } var startTimeMsec = (new Date()).getTime(); var isTarget = function (cmds, cdate) { if (cmds.indexOf(file) === -1 || cmds.indexOf(BEATORAJA) === -1) { return false; } var p = (/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\.(\d+)(.\d+)$/).exec(cdate); var javawStart = (new Date(p[1], parseInt(p[2], 10) - 1, p[3], p[4], p[5], p[6], parseInt(p[7], 10) / 1000)).getTime(); if (javawStart > startTimeMsec) { return true; } }; var tmpJsName = "zzzz_" + startTimeMsec + ".js"; var createTmpJs = function (fname) { var js = fso.createTextFile(fname, true, true); var txt = [ "var PID=WScript.Arguments(0),g=function(pID){", "var i,e=new Enumerator(GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\\\\\.\\\\root\\\\cimv2\")", ".ExecQuery(\"Select * from Win32_PerfRawData_PerfProc_Process where IDProcess=\"+pID));", "while(!e.atEnd()){i=e.item();e.moveNext()}if(i&&i.Name!==\"\"){return {p:i.PercentProcessorTime,t:i.Timestamp_Sys100NS}}},c1=g(PID),c0,cpuUsage;", "if(!c1){WScript.quit()}do{if(!c1){break}c0={p:c1.p,t:c1.t};WScript.sleep(2000);c1=g(PID);cpuUsage=Math.round(((c1.p-c0.p)/(c1.t-c0.t))*100)}", "while(cpuUsage!==0);WScript.sleep(2000);if(g(PID)){", "(WScript.createObject(\"WScript.Shell\")).run(\"cmd /C taskkill /F /PID \"+PID,0,true)}", "(WScript.createObject(\"Scripting.FileSystemObject\")).deleteFile(WScript.ScriptFullName);" ]; js.write(txt.join("")); js.close(); }; var beatoraja = shell.exec("\"" + BEATORAJA + "\"" + type + " \"" + file + "\""); while (beatoraja.Status === 0) { ws.sleep(1000); } var e = new Enumerator(GetObject("winmgmts://.").instancesOf("Win32_Process")); var item; var subJS; while (!e.atEnd()) { item = e.item(); if (item.name === "javaw.exe" && isTarget(item.commandLine, item.creationDate)) { subJS = fso.buildPath(CUR, tmpJsName); createTmpJs(subJS); shell.run("\"" + subJS + "\" " + item.ProcessId, 0, false); break; } e.moveNext(); } }(WScript));