Advertisement
mgis90

mc_stat_save.ahk [OBSOLETE]

Oct 1st, 2014
1,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;UPDATE     2016-05-24  this script is now obsolete, as the launcher has fixed these issues. 1303 hits
  2. ;I leave, however, the last version below
  3. ;-----------------------------------------------------------------------
  4. ;-----------------------------------------------------------------------
  5. ;                                           2014-10-17
  6. ;mc_stat_save.ahk
  7. ;Minecraft stats and achievements rescuing script by Mgis
  8. ;It saves stats for cracked versions of Minecraft
  9. ;[longer description near the end of the file]
  10. ;                           updates at      http://pastebin.com/u/mgis90
  11. ;--- Screenshot --- (both the same)
  12. ;http://imgur.com/4AFvhtN
  13. ;http://pl.tinypic.com/r/2mgupty/8
  14. ;--- Video ---
  15. ;http://youtu.be/LxVBwUBYKK0
  16. ;-----------------------------------------------------------------------
  17. ;thanks for the solution suggested by HiArashi on 2014-08-24 00:33 (at tpb)
  18. ;and @Antaishi asked that question bothering me!
  19. ;[AutoHotkey Version v1.1.13.00 but on older may work anyways]
  20. ;for "TryAgain" [v1.0.44.08+]
  21. ;-----------------------------------------------------------------------
  22. #SingleInstance force
  23. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases they said.
  24. #LTrim  ; Allows continuation sections to be indented. Positional. May be turned off via #LTrim Off
  25. #KeyHistory 0  ; Disable keyboard and mouse event history.
  26. ;#Warn  ;debug mode, not needed for typical user
  27. GoSub,settings
  28. GoSub,TrayIcon  ;it will exit itself regarding to settings
  29. GoSub,java_check
  30.  
  31. ;save the stats
  32. worlds_saved=0
  33. Loop, % dir_saves . "\*",2,0    ;walk through the dir, retrieve folders only
  34. {
  35.     ;here we get world1 world2 ... folders
  36.     worlds_num:=A_Index
  37.     filelist:=Object()      ;list of all stat json files PER WORLD
  38.     max=0                                           ;max FileTimeModified for the next WorldFolder
  39.     Loop, % A_LoopFileFullPath . "\*.json",0,1      ;only files, recurse
  40.     {
  41.         if not InStr(A_LoopFileName,nick)   ;do not list files already named with correct nickname
  42.             filelist.Insert(A_LoopFileLongPath)
  43.         if (A_LoopFileTimeModified > max)
  44.         {
  45.             max:=A_LoopFileTimeModified
  46.             max_path:=A_LoopFileLongPath
  47.             StringTrimRight,max_folder,max_path, % StrLen(A_LoopFileName)   ;warning! contains last backslash
  48.         }
  49.     }   ;after this we have now the freshest file of this world
  50.     ;we change the freshest filename to nick
  51.     ;if filelist contains >=1 entries AND inString of (freshest fileNAME only) there is NO nick
  52.     if (filelist.MaxIndex()>=1) AND ( NOT InStr(max_path,nick,false,StrLen(max_folder)))
  53.     {
  54.         if dbg
  55.             Msgbox,,Moving - %ScriptName%, % "Moving file:`n" . max_path . "`nto`n" .  max_folder . nick . ".json"
  56.         FileMove,%max_path%, % max_folder . nick . ".json",1
  57.         worlds_saved+=1
  58.     } else {    ;there is less than one file OR freshest filename contains nick (that means its ok)
  59.         if dbg
  60.             MsgBox,64,Skipping World - %ScriptName%,Skipping World:`n%A_LoopFileName%`nbecause it seems to not have been played recently
  61.         Continue    ;we really skip the rest of this worlds loop
  62.     }
  63.     if (cleanup_recycle>0)
  64.     {               ;we remove other old files
  65.         if (cleanup_recycle=2)
  66.         {
  67.             if dbg
  68.                 MsgBox,64,Cleanup - %ScriptName%, % "Deleting " . (filelist.MaxIndex()-1) . " files."
  69.             for k, v in filelist
  70.                 FileDelete,%v%
  71.         }
  72.         else if (cleanup_recycle=1)
  73.         {
  74.             if dbg
  75.                 MsgBox,64,Cleanup - %ScriptName%, % "Recycling " . (filelist.MaxIndex()-1) . " files."
  76.             for k, v in filelist
  77.                 FileRecycle,%v%
  78.         }
  79.     }
  80. if dbg
  81.     MsgBox,64,World stats saved - %ScriptName%,World finished:`n%A_LoopFileName%,4
  82. }   ;done world
  83. if (worlds_saved=0)
  84.     TrayTip,%ScriptName%,Nothing to save.,5,1
  85. else
  86.     TrayTip,Saved - %ScriptName%,Saved %worlds_saved% out of %worlds_num% worlds.,3,1
  87. Sleep 3000
  88. if dbg
  89.     MsgBox,64,Saved,All worlds finished! Bye!,2
  90. ExitApp
  91. return  ;autoexec
  92.  
  93. java_check:
  94. ;protects the script from working and possibly arguing with Minecraft for files
  95. if (WaitForJava=2)
  96. {
  97.     ;wait and wake when Minecraft closed, then show a little dialog informing that we save
  98.     Process,WaitClose,javaw.exe     ;wait indefinitely
  99.     if silent
  100.         return
  101.     ;so the user doesnt get the dialog so quickly (missclicks happened to me)
  102.     TrayTip,%ScriptName%,Saving statistics in a few seconds...
  103.     Loop, 15
  104.     {
  105.         ToolTip, % (16-A_Index)
  106.         Sleep 200               ;total sleep =3000
  107.     }
  108.     TrayTip
  109.     ToolTip
  110.     SetTimer, ChangeButtonNames,50
  111.     ;msgbox cancel/try again/continue + 3rd button default
  112.     MsgBox,518,Mc stat save,Script will save stats for %nick%.,6    ;window title here must be unchanged!
  113.     IfMsgBox,Timeout
  114.         return
  115.     IfMsgBox,Continue
  116.         return
  117.     else
  118.         IfMsgBox,TryAgain
  119.         {
  120.             Sleep 180000    ;wait 180 seconds
  121.             Reload
  122.         }
  123.         else            ;(Cancel,closing)   ;escape or closing the window is the same as pressing cancel
  124.             ExitApp
  125. } else if (WaitForJava=1) {
  126.     ;wait one second, if Minecraft process exists then exit script
  127.     Process,WaitClose,javaw.exe,1
  128.     if ErrorLevel
  129.     {
  130.         MsgBox,48,Java found! - %ScriptName%,Whew! I saved you!`nIt seems that there is an instance of JAVAW.EXE running.`nTo prevent damage the script will exit after this message.`n`nMake sure you close Minecraft before running this script.
  131.         ExitApp
  132.     }
  133. }
  134. return ;java_check
  135.  
  136. ChangeButtonNames:
  137. IfWinExist,Mc stat save
  138. {
  139.     WinActivate,Mc stat save
  140.     ControlSetText, Button1,&Exit
  141.     ControlSetText, Button2,&Wait 3min
  142.     ControlSetText, Button3,&Save now
  143.     SetTimer, ChangeButtonNames, off
  144. }  
  145. return
  146.  
  147. TrayIcon:
  148. if not trayicon_change
  149.     return
  150. StringTrimRight,iconpath,A_ScriptFullPath,3
  151. iconpath.="ico"
  152. if trayicon_download        ;retrieve it
  153. {
  154.     if not FileExist(iconpath)      ;download only if it does not exist
  155.     {
  156.         ;ask user first, 35=  32->?icon  +  3->yes/no/cancel
  157.         MsgBox,35,Download icon - %ScriptName%,Do you want to download tray icon from:`n%trayicon_source%`n`n(you may disable this in settings, just edit the line with "trayicon_download=1")
  158.         IfMsgBox,Yes
  159.         {
  160.             MsgBox,,,Ok. Will download now.,5
  161.             UrlDownloadToFile,%trayicon_source%,%iconpath%
  162.             if ErrorLevel
  163.                 MsgBox,48,,There was an error while downloading the icon.`n%ErrorLevel%
  164.             else
  165.             {
  166.                 FileGetSize,iconsize,%iconpath%
  167.                 if (iconsize=trayicon_size)
  168.                     MsgBox,64,Finished downloading - %ScriptName%,Downloaded file size:`n%iconsize% = %trayicon_size%,5
  169.                 else
  170.                     MsgBox,64,Finished downloading - %ScriptName%,Downloaded file size: %iconsize%`nSuggested file size: %trayicon_size%
  171.             }
  172.         }
  173.         IfMsgBox,No
  174.         {
  175.             MsgBox,36,%ScriptName%,Do you want to edit configuration?
  176.             IfMsgBox,Yes
  177.                 Edit
  178.         }
  179.     } ;else does exist
  180. } ;else do not retrieve
  181. if FileExist(iconpath)
  182.     Menu,Tray,Icon,%iconpath%
  183. return ;TrayIcon
  184.  
  185. settings:
  186. if A_AhkVersion < 1.1.0.0
  187. {
  188.     MsgBox,% 16+3+256,You have old ahk!,You have old AutoHotkey!`nYou need to have Ahk_L for this script to work properly.`nPlease visit ahkscript.org`n`nOpen website now?,10
  189.     IfMsgBox,Yes
  190.         Run,www.ahkscript.org
  191. }
  192. ScriptName=Mc stat save
  193. StringTrimRight,file_ini,A_ScriptFullPath, 3
  194. file_ini.="ini"
  195. IniRead,dir_saves,%file_ini%,Settings,dir_saves, % A_AppData . "\.minecraft\saves"
  196. IniRead,nick,%file_ini%,Settings,nick,ERROR
  197. IniRead,AlwaysAskForNick,%file_ini%,Settings,AlwaysAskForNick,0
  198. IniRead,cleanup_recycle,%file_ini%,Settings,cleanup_recycle,0
  199. IniRead,WaitForJava,%file_ini%,Settings,WaitForJava,2
  200. IniRead,silent,%file_ini%,Settings,silent,0
  201. IniRead,dbg,%file_ini%,Settings,dbg,0
  202. IniRead,trayicon_change,%file_ini%,Settings,trayicon_change,1
  203. IniRead,trayicon_download,%file_ini%,Settings,trayicon_download,0
  204. IniRead,trayicon_source,%file_ini%,Settings,trayicon_source,%A_Space%
  205. IniRead,trayicon_size,%file_ini%,Settings,trayicon_size,%A_Space%
  206. if (nick="ERROR") or (StrLen(nick)<2) or AlwaysAskForNick
  207. {
  208.     ;please enter your nick
  209.     ;InputBox, OutputVar [, Title, Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default]
  210.     InputBox,nick,Your Minecraft nick,Please enter Your Minecraft nick:,,250,120,,,,,Player
  211.     if ErrorLevel   ;dismissed nick input
  212.         ExitApp
  213. }
  214. IfNotExist, %file_ini%
  215. {
  216.     ;FileAppend,`;`n, %file_ini%
  217.     FileAppend,`;Descriptions and default values. Delete this Ini to regenerate it.`n, %file_ini%
  218.     FileAppend,`;-----------------------------------------------------------------------`n, %file_ini%
  219.     FileAppend,`;dir_saves=`%AppData`%\.minecraft\saves`n`;`tThis is the directory of your saves folder. No longer you have to configure this! (It will set itself) But you may. Right here. In case you need.`n, %file_ini%
  220.     FileAppend,`;nick=`n`;`tYour Minecraft nickname [text string]`n, %file_ini%
  221.     FileAppend,`;AlwaysAskForNick=0`n`;`tfor multi-nickname situations [0/1]`n, %file_ini%
  222.     FileAppend,`;cleanup_recycle=1`n`;`tAffect other json files beyond the freshest? What to do with files? [0-dont do cleanup 1-recycles 2-deletes]`n, %file_ini%
  223.     FileAppend,`;WaitForJava=2`n`;`tMethod of protection javaw.exe check handling [0-disable check 1-SINGLE CHECK-abort script when java found running 2-wait for java to close and then ask you]`n, %file_ini%
  224.     FileAppend,`;silent=0`n`;`tSaves without asking. Decreases dialogs. [0/1]`n, %file_ini%
  225.     FileAppend,`;dbg=0`n`;`tdebug mode (displays more dialogs you may use it to get familiar with the script)`n, %file_ini%
  226.     FileAppend,`;-----------------------------------------------------------------------`n, %file_ini%
  227.     FileAppend,`;trayicon_change=true`n`;`tchange the tray icon to more minecrafty one? (only if it exists. you may use any icon you want)`n, %file_ini%
  228.     FileAppend,`;uncomment these three lines to download tray icon (and place them under settings, at end of this file) I TAKE NO RESPONSIBILITY FOR ANYTHING (but it works nice for me)`n, %file_ini%
  229.     FileAppend,`;trayicon_download=true`t`;download the tray icon? (will save as ScriptName.ico in the directory of the script) this is DISABLED by default so it does not download anything`n, %file_ini%
  230.     FileAppend,`;trayicon_source=http://www.iconarchive.com/download/i78543/chrisl21/minecraft/Chest.ico`n, %file_ini%
  231.     FileAppend,`;trayicon_size=233286`t`;this is only indicative size`n, %file_ini%
  232.     FileAppend,`;-----------------------------------------------------------------------, %file_ini%
  233.     Sleep 100
  234.     IniWrite, % A_AppData . "\.minecraft\saves",%file_ini%,Settings,dir_saves
  235.     IniWrite,%nick%,%file_ini%,Settings,nick
  236.     IniWrite,%AlwaysAskForNick%,%file_ini%,Settings,AlwaysAskForNick
  237.     Iniwrite,%cleanup_recycle%,%file_ini%,Settings,cleanup_recycle
  238.     IniWrite,%WaitForJava%,%file_ini%,Settings,WaitForJava
  239.     IniWrite,%silent%,%file_ini%,Settings,silent
  240.     IniWrite,%dbg%,%file_ini%,Settings,dbg
  241.     IniWrite,%trayicon_change%,%file_ini%,Settings,trayicon_change
  242.     IniWrite,%trayicon_download%,%file_ini%,Settings,trayicon_download
  243.     IniWrite,%A_Space%,%file_ini%,Settings,trayicon_source
  244.     IniWrite,%A_Space%,%file_ini%,Settings,trayicon_size
  245.     MsgBox,3,Ini not found - %ScriptName%,Default values restored. Script will exit. You can launch me again.`n`nDo you want to edit settings now?
  246.     IfMsgBox,Yes
  247.         Run,%file_ini%
  248.     ExitApp
  249. }
  250. Menu,Tray,NoStandard
  251. Menu,Tray,Add,Edit settings,edit_ini
  252. Menu,Tray,Add
  253. Menu,Tray,Standard
  254. return  ;settings
  255.  
  256. edit_ini:
  257. Run,*edit "%file_ini%"
  258. return ;edit_ini
  259.  
  260. /*
  261. HiArashi on 2014-08-24 00:33 CET:
  262. @Antaishi and everyone else who experience lost achievements and stats problem.
  263. After you quit the game go to .minecraftsaves[your_world_name]stats and change the name of
  264. the file you see there (or the most recent file if you have many of those) to the nick, that you use.
  265. Unfortunately, you have to do it every time you quit the game.
  266. */
  267. /*  ---   longer description   ---
  268. After you play in a World, the freshest file in stats folder (the one
  269. you just played, or are still playing) becomes some kind of long ID number [UUID?],
  270. like this: \stats\0fa4e30e-80a8-43b4-8186-270f8af78b03.json
  271. What this script does: it renames that file to Nick.json
  272. And that file is found by Minecraft the next time you load that World
  273. (it says downloading stats ooooOoo)
  274. Unfortunately this may be only some kind of backward-compatibility of Mc
  275. NOTE: we assume that in \saves\ and its subfolders (worlds) there are no other json files other than stat files in \world\stats\123.json
  276. */
  277. ;---------------------
  278. ;----- changelog -----
  279. ;---------------------
  280. ;times are in CET/CEST, UTC/GMT +2 hours
  281. /*
  282. 15:56 2014-10-01
  283.     fixed deletion of stats of a world which wasnt played in a mc session
  284.     added overwrite flag when moving stats
  285. 23:26 2014-10-01
  286.     note: multi-user players don't have support. Guess they can play on one profile in launcher. I may add a question to select Nickname when script starts. I had this around here... somewhere. Blast that little! Where did she get to?
  287.     reenabled changing button names in dialog
  288.     changed all settings to use true/false instead of 1/0 style
  289.     removed old unused code
  290.     changed stuff
  291.     linked to pastebin
  292.     added minecrafty tray icon support!
  293. 13:17 2014-10-03
  294.     done todo: add some checks that will protect unplayed worlds from getting "Rename Mgis.json --> Mgis.json" because it is an unneeded FileSystem operation!
  295.         it will increase script size but it should be done
  296. 16:26 2014-10-03
  297.     oh no my cat came to me and stepped on Insert and Backspace keys. Luckily I had it saved.
  298.     added skipping world (checking if Mgis.json is the only filename, and if the freshest filename contains Mgis)
  299.     changed one var name to WaitForJava
  300. 01:11 2014-10-04
  301.     moved configuration to Ini
  302.     added WaitForJava=0 which now skips the check entirely
  303.     changed dialogs, added timeout to the save dialog
  304.     added AlwaysAskForNick (see note above at 2014-10-01)
  305.     added world save summary (num/saved)
  306.     added silent mode setting
  307. 2014-10-04
  308.     uploaded a video! http://youtu.be/LxVBwUBYKK0
  309. 2014-10-13
  310.     disabled warn debug mode
  311.     added check for version in settings, added run website
  312. 2014-10-15
  313.     added edit settings to tray menu
  314. 22:15 2014-10-17
  315.     changed conditions of skipping world
  316. 14:21 2016-05-24
  317.     found out that the launcher fixed these issues
  318. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement