Advertisement
aveyo

SetDotaKey

Sep 16th, 2017
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 12.34 KB | None | 0 0
  1. goto="HERO" /*
  2. :"HERO"
  3. @echo off &mode 99,11 &color 4F &echo.
  4. ::
  5. set "name=DotaAlt"
  6. set "key=CTRL"
  7. ::
  8. title SetDotaKey '%name%' to '%key%' by AveYo v322
  9. echo  Please close the game before running this script!
  10. echo  - determine game path and userdata
  11. echo  - load \Steam\userdata\[yourfriendsid]\570\remote\cfg\dotakeys_personal.lst
  12. echo  - add/change "%name%" key to "%key%"
  13. call :set_dota
  14. echo  Last used Steam profile: %STEAMDATA%
  15. set "dotakeys_personal=%STEAMDATA%\570\remote\cfg\dotakeys_personal.lst"
  16. :: powershell openfiledialog snippet                                                             changes: ps dialog instead of mshta
  17. set "o=$directory='%STEAMPATH%\userdata'; $filter='(*.lst)|*.lst';"
  18. set "f=[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"); $ofd=New-Object System.Windows.Forms"  
  19. set "d=.OpenFileDialog; $ofd.filter=$filter; $ofd.initialDirectory=$directory; [void]$ofd.ShowDialog(); $ofd.filename"
  20. set "ps_openfiledialog=%o:"=\"%%f:"=\"%%d:"=\"%"
  21. if not exist "%dotakeys_personal%" for /f %%q in ('powershell -c "%ps_openfiledialog%"') do set "dotakeys_personal=%%q"                                                                                                                                                                                                                    
  22. if not exist "%dotakeys_personal%" call :end ! Try \Steam\userdata\[yourfriendsid]\570\remote\cfg\dotakeys_personal.lst &goto :eof
  23. :: load, parse and update dotakeys_personal.lst
  24. cscript //E:JScript //nologo "%~f0" SetDotaKey "%STEAMDATA%\570\remote\cfg\dotakeys_personal.lst" %name% %key%
  25. call :end Done!
  26. goto :eof
  27. ::----------------------------------------------------------------------------------------------------------------------------------
  28. :"Batch_Utility_functions"
  29. ::----------------------------------------------------------------------------------------------------------------------------------
  30. :set_dota outputs %STEAMPATH% %STEAMAPPS% %STEAMDATA% %DOTA%                       ||:i AveYo:" Override detection below if needed "
  31. set "STEAMPATH=C:\Steam" &set "DOTA=C:\Games\steamapps\common\dota 2 beta"                    
  32. if not exist "%STEAMPATH%\Steam.exe" call :reg_query "HKCU\SOFTWARE\Valve\Steam" "SteamPath" STEAMPATH
  33. if not exist "%STEAMPATH%\Steam.exe" call :end ! Cannot find SteamPath in registry
  34. if exist "%DOTA%\game\dota\maps\dota.vpk" set "STEAMAPPS=%DOTA:\common\dota 2 beta=%" &goto :eof
  35. for %%s in ("%STEAMPATH%") do set "STEAMPATH=%%~dpns" &set "libfilter=LibraryFolders { TimeNextStatsReport ContentStatsID }"
  36. if not exist "%STEAMPATH%\SteamApps\libraryfolders.vdf" call :end ! Cannot find "%STEAMPATH%\SteamApps\libraryfolders.vdf"
  37. for /f usebackq^ delims^=^"^ tokens^=4 %%s in (`findstr /v "%libfilter%" "%STEAMPATH%\SteamApps\libraryfolders.vdf"`) do (
  38. if exist "%%s\steamapps\appmanifest_570.acf" if exist "%%s\steamapps\common\dota 2 beta\game\dota\maps\dota.vpk" set "libfs=%%s" )
  39. set "STEAMAPPS=%STEAMPATH%\steamapps" &if defined libfs set "STEAMAPPS=%libfs:\\=\%\steamapps"
  40. if not exist "%STEAMAPPS%\common\dota 2 beta\game\dota\maps\dota.vpk" call :end ! Cannot find "%STEAMAPPS%\common\dota 2 beta"
  41. set "DOTA=%STEAMAPPS%\common\dota 2 beta" &cd /d "%STEAMAPPS%\common\dota 2 beta\game\dota"
  42. call :reg_query "HKCU\SOFTWARE\Valve\Steam\ActiveProcess" "ActiveUser" STEAMUSER &set /a "STEAMID=STEAMUSER" >nul 2>nul
  43. if defined STEAMID if exist "%STEAMPATH%\userdata\%STEAMID%\config\localconfig.vdf" set "STEAMDATA=%STEAMPATH%\userdata\%STEAMID%"
  44. if not defined STEAMDATA for /f delims^=^ eol^= %%b in ('dir /a:-d /b /o:d /t:w cache_*.soc 2^>nul') do set "usercache=%%~nb"
  45. if not defined STEAMDATA if defined usercache set "STEAMDATA=%steampath%\userdata\%usercache:cache_=%"
  46. if not exist "%STEAMDATA%\570\remote\cfg\dotakeys_personal.lst" call :end ! Cannot find your dotakeys definition file !
  47. goto :eof
  48. :reg_query %1:KeyName %2:ValueName %3:OutputVariable %4:other_options[example: "/t REG_DWORD"]
  49. setlocal &for /f "skip=2 delims=" %%s in ('reg query "%~1" /v "%~2" /z 2^>nul') do set "rq=%%s" &call set "rv=%%rq:*)    =%%"
  50. endlocal &call set "%~3=%rv%" &goto :eof                         ||:i AveYo - Usage:" call :reg_query "HKCU\MyKey" "MyValue" MyVar "
  51. :end %1:Message
  52. if "%~1"=="!" ( color c0 &echo !ERROR%* &timeout /t 16 &color &exit ) else echo  %* &timeout /t 8 &color &exit
  53. ::----------------------------------------------------------------------------------------------------------------------------------
  54. exit /b :End_of_Batch_engine_parsing - only JS sections below this line
  55. ::--------------------------------------------------------------------------------------------------------------------------------*/
  56.                           //:i Switch syntax highlighter in your text editor from BAT to JS
  57. //----------------------------------------------------------------------------------------------------------------------------------
  58. // SetDotaKey function
  59. //----------------------------------------------------------------------------------------------------------------------------------
  60. SetDotaKey = function(fn, id, key) {
  61.   var file_src = path.normalize(fn), file_read = fs.readFileSync(file_src, DEF_ENCODING), vdf = ValveDataFormat();
  62.   var file_parse = vdf.parse(file_read), dotakeys = file_parse.KeyBindings.Keys; dotakeys[id]={Mode: '0',Key: key};
  63.   fs.writeFileSync(fn, vdf.stringify(file_parse,true), DEF_ENCODING); console.log(' SetDotaKey',id,key);
  64. }
  65. //----------------------------------------------------------------------------------------------------------------------------------
  66. // ValveDataFormat hybrid parser by AveYo, 2016                                                   VDF test on 20.1 MB items_game.txt
  67. // loosely based on vdf-parser by Rossen Popov, 2014-2016                                                           node.js  cscript                
  68. // featuring auto-renaming duplicate keys, saving comments, grabing lame one-line "key" { "k" "v" }         parse:  1.329s   9.285s
  69. // greatly improved cscript performance - it's not that bad overall but still lags behind node.js       stringify:  0.922s   3.439s    
  70. //----------------------------------------------------------------------------------------------------------------------------------
  71. function ValveDataFormat() {
  72.   var jscript = (typeof ScriptEngine=='function' && ScriptEngine()=='JScript');
  73.   var order = !jscript, dups = false, comments = false, newline = '\n', empty = (jscript) ? '' : !REG3XP0!>undefined;
  74.   return {
  75.     parse: function(txt, flag) {
  76.       var obj = {}, stack = [obj], expect_bracket = false, i = 0; comments = flag || false;
  77.       if (/\r\n/.test(txt)) {newline = '\r\n'} else newline = '\n';
  78.       var m, regex =/[^"\r\n]*(\/\/.*)|"([^"]*)"[ \t]+"([^"]*\\"[^"]*\\"[^"]*|[^"]*)"|"([^"]*)"|({)|(})/g;    
  79.       while ((m = regex.exec(txt)) !== null) {
  80.         //lf='\n'; console.log(' cmnt:',m[1],lf ,'key:',m[2],lf ,'val:',m[3],lf ,'add:',m[4],lf ,'open:',m[5],lf ,'close:',m[6],lf);
  81.         if (comments && m[1] !== empty) {
  82.           key = '\x10' + i++; stack[stack.length-1][key] = m[1];                                  // AveYo: optionally save comments
  83.         } else if (m[4] !== empty) {          
  84.           key = m[4]; if (expect_bracket) { console.log('VDF.parse: invalid bracket near '+ m[0]); return this.stringify(obj,true) }
  85.           if (order && key == ''+~~key) {key = '\x11' + key;}  // AveYo: prepend nr. keys with \x11 to keep order in node.js
  86.           if (stack[stack.length-1][key] === undefined) {
  87.             stack[stack.length-1][key] = {};
  88.           } else {
  89.             key += '\x12' + i++; stack[stack.length-1][key] = {}; dups = true; // AveYo: rename duplicate key obj with \x12 + i
  90.           }
  91.           stack.push(stack[stack.length-1][key]); expect_bracket = true;      
  92.         } else if (m[2] !== empty) {
  93.           key = m[2]; if (expect_bracket) { console.log('VDF.parse: invalid bracket near '+ m[0]); return this.stringify(obj,true) }          
  94.           if (order && key == ''+~~key) key = '\x11' + key;    // AveYo: prepend nr. keys with \x11 to keep order in node.js
  95.           if (stack[stack.length-1][key] !== undefined) { key += '\x12' + i++; dups = true }// AveYo: rename duplicate k-v pair
  96.           stack[stack.length-1][key] = m[3]||'';
  97.         } else if (m[5] !== empty) {
  98.           expect_bracket = false; continue; // one level deeper
  99.         } else if (m[6] !== empty) {
  100.           stack.pop(); continue; // one level back
  101.         }
  102.       }
  103.       if (stack.length != 1) { console.log('VDF.parse: open parentheses somewhere'); return this.stringify(obj,true) }
  104.       return obj; // stack[0];
  105.     },
  106.     stringify: function(obj, pretty, nl) {
  107.       if (typeof obj != 'object') { console.log('VDF.stringify: Input not an object'); return obj }
  108.       pretty = ( typeof pretty == 'boolean' && pretty) ? true : !! nl = nl || newline || '\n';
  109.       return this.dump(obj, pretty, nl, 0);
  110.     },
  111.     dump: function(obj, pretty, nl, level) {
  112.       if (typeof obj != 'object') { console.log('VDF.stringify: Key not string or object'); return obj}
  113.       var indent = '\t', buf = '', idt = '', i = 0;
  114.       if (pretty) for (; i < level; i++) idt += indent;
  115.       for (var key in obj) {
  116.         if (typeof obj[key] == 'object')  {
  117.           buf += idt +'"'+ this.redup(key) +'"'+ nl + idt +'{'+ nl + this.dump(obj[key], pretty, nl, level+1) + idt +'}'+ nl;
  118.         } else {
  119.           if (comments && key.indexOf('\x10') !== -1) { buf += idt + obj[key] + nl; continue } // AveYo: restore comments (optional)
  120.           buf += idt +'"'+ this.redup(key) +'"'+ indent + indent +'"'+ obj[key] +'"'+ nl;
  121.         }
  122.       };
  123.       return buf;
  124.     },
  125.     redup: function(key) {
  126.       if (order && key.indexOf('\x11')!== -1) key = key.split('\x11')[1]; // AveYo: restore number keys in node.js
  127.       if (dups && key.indexOf('\x12') !== -1) key = key.split('\x12')[0]; // AveYo: restore duplicate key names
  128.       return key;
  129.     },
  130.     nr: function(key) {return (!jscript && key.indexOf('\x11') == -1) ? '\x11' + key : class="re0">key} //check number key: vdf.nr('nr');
  131.   }
  132. } // End of ValveDataFormat
  133. //----------------------------------------------------------------------------------------------------------------------------------
  134. // JScript Engine by AveYo - can call specific functions as the first script argument    
  135. // a subset of Hybrid Node.js / JScript Engine used in 'No-Bling DOTA mod builder.bat'                
  136. //----------------------------------------------------------------------------------------------------------------------------------
  137. jscript = true, engine = 'JScript', w = WScript, argc = w.Arguments.Count(), argv=[], run='';
  138. if (argc > 0) { run = w.Arguments(0); for (var i=1;i<argc;i++) argv.push( '"'+ w.Arguments(i).replace(/[\\\/]+/g,'\\\\') +'"') }
  139. process={}; process.argv=[ScriptEngine(),w.ScriptFullName]; for (var i=0;i<argc;i++) process.argv[i+2] = w.Arguments(i);
  140. path={}; path.join = function(f,n){return fso.BuildPath(f,n)}; path.normalize = function(f){return fso.GetAbsolutePathName(f)};  
  141. path.basename = function(f){return fso.GetBaseName(f)}; path.dirname=function(f){return fso.GetParentFolderName(f)};path.sep='\\';
  142. console={}; console.log=function(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) { w.echo(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u) };
  143. fs={}; fso=new ActiveXObject("Scripting.FileSystemObject"); ado=new ActiveXObject('ADODB.Stream'); DEF_ENCODING='Windows-1252';
  144. FileExists = function(f) { return fso.FileExists(f) }; PathExists = function(f) { return fso.FolderExists(f) };
  145. fs.readFileSync = function(fn, charset) {
  146.   var data=''; ado.Mode=3; ado.Type=2; ado.Charset=charset || 'Windows-1252'; ado.Open(); ado.LoadFromFile(fn);
  147.   while (!ado.EOS) data += ado.ReadText(131072); ado.Close(); return data;
  148. }
  149. fs.writeFileSync = function(fn, data, encoding) {
  150.   ado.Mode=3; ado.Type=2; ado.Charset=encoding || 'Windows-1252'; ado.Open();
  151.   ado.WriteText(data); ado.SaveToFile(fn, 2); ado.Close(); return 0;
  152. }
  153. //----------------------------------------------------------------------------------------------------------------------------------
  154. // Auto-run: if first script argument is a function name - call it, passing the next arguments
  155. //----------------------------------------------------------------------------------------------------------------------------------
  156. if (run && !/[^A-Z0-9$_]/i.test(run)) new Function('if(typeof '+run+'=="function"){'+run+'('+argv+');}')();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement