Advertisement
Narzew

FasterSBG 1.1 Source

Mar 7th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.04 KB | None | 0 0
  1. #encoding: utf-8
  2.  
  3. #========================================================
  4. # Faster SBG v 1.1 by Narzew
  5. # 07.03.2014
  6. # Some rights reserved.
  7. # License : GPL v3 or later
  8. # Please atribute your project to narzew19@yahoo.co.jp
  9. #========================================================
  10.  
  11. require 'find'
  12.  
  13. $fastersbg_version = 1.1
  14. $fastersbg_reldate = "07.03.2014"
  15.  
  16. def sel
  17.     print "**********************************************************\n"
  18. end
  19.  
  20. module FasterSBG
  21.  
  22.     def self.choice(min,max)
  23.         min = min.to_i
  24.         max = max.to_i
  25.         loop do
  26.             y = $stdin.gets.chomp!.to_i
  27.             if y < min || y > max
  28.                 sel
  29.                 print "Bad choice!\n"
  30.                 sel
  31.             else
  32.                 return y.to_i
  33.             end
  34.         end
  35.         return y.to_i
  36.     end
  37.  
  38.     def self.get_total_time(x)
  39.         t = x[0].to_i*3600+x[1].to_i*60+x[2].to_i
  40.         return t
  41.     end
  42.    
  43.     def self.convert_total_time(x)
  44.         hours = x/3600
  45.         minutes = (x%3600)/60
  46.         seconds = ((x%3600)%60)
  47.         return [hours,minutes,seconds]
  48.     end
  49.    
  50.     def self.set_total_time(x)
  51.         timevalue = FasterSBG.convert_total_time(x)
  52.         hours = timevalue.at(0).to_i
  53.         minutes = timevalue.at(1).to_i
  54.         seconds = timevalue.at(2).to_i
  55.         if hours < 0
  56.             par1 = "00"
  57.         elsif hours < 10
  58.             par1 = "0#{hours}"
  59.         else
  60.             par1 = "#{hours}"
  61.         end
  62.         if minutes < 0
  63.             par2 = "00"
  64.         elsif minutes < 10
  65.             par2 = "0#{minutes}"
  66.         else
  67.             par2 = "#{minutes}"
  68.         end
  69.         if seconds < 0
  70.             par3 = "00"
  71.         elsif seconds < 10
  72.             par3 = "0#{seconds}"
  73.         else
  74.             par3 = "#{seconds}"
  75.         end
  76.         return "+#{par1}:#{par2}:#{par3}"
  77.     end
  78.        
  79.    
  80.     def self.change_sbg_speed(file, factor, mode,dir=nil,filename=nil)
  81.         data = lambda{File.open(file,'rb'){|f|return f.read}}.call
  82.         result = ""
  83.         if mode == 0
  84.             result << "# Dose Speed changed using Narzew's FasterSBG v #{$fastersbg_version}\n# Factor: #{factor} faster.\n"
  85.         elsif mode == 1
  86.             result << "# Dose Speed Changed using Narzew's FasterSBG v #{$fastersbg_version}\n# Factor: #{factor} slower.\n"
  87.         end
  88.         result << "# Warning! This is unofficial dose. Use at your own risk!\n# Dose created: #{Time.now.year}-#{Time.now.month}-#{Time.now.day} #{Time.now.hour}:#{Time.now.min}:#{Time.now.sec}\n"
  89.         data.each_line{|x|
  90.             if x[0].to_s != "+"
  91.                 result << x
  92.             else
  93.                 totaltime = 0
  94.                 y = x.gsub("+","")
  95.                 spl1 = y.split("\x20")[0]
  96.                 spl2 = y.split(":")
  97.                 totaltime = FasterSBG.get_total_time(spl2)
  98.                 if mode == 0
  99.                     totaltime = totaltime/factor
  100.                 elsif mode == 1
  101.                     totaltime = totaltime*factor
  102.                 else
  103.                     raise("Bad mode!")
  104.                 end
  105.                 str = FasterSBG.set_total_time(totaltime)
  106.                 x[0..8] = str
  107.                 result << x
  108.             end
  109.         }
  110.         if dir == nil || filename == nil
  111.             if mode == 0
  112.                 File.open(file.gsub(".sbg","-QHU.sbg"),'wb'){|w|w.write(result)}
  113.             elsif mode == 1
  114.                 File.open(file.gsub(".sbg","-SHU.sbg"),'wb'){|w|w.write(result)}
  115.             end
  116.         else
  117.             if mode == 0
  118.                 File.open("#{dir}/"+filename.gsub(".sbg","-QHU.sbg"),'wb'){|w|w.write(result)}
  119.             elsif mode == 1
  120.                 File.open("#{dir}/"+filename.gsub(".sbg","-SHU.sbg"),'wb'){|w|w.write(result)}
  121.             end
  122.         end
  123.         print "#{file} converted.\n"
  124.     end
  125.    
  126.     def self.batch_dir(x,factor, mode)
  127.         Dir.mkdir("Converted Doses") unless Dir.exist?("Converted Doses")
  128.         filenames = []
  129.         Find.find(x).each{|y|
  130.             ext = y.split(".")[-1]
  131.             if ext == "sbg"
  132.                 filenames << y
  133.             end
  134.         }
  135.         filenames.each{|f|
  136.             FasterSBG.change_sbg_speed(f,factor,mode,"Converted Doses", f.gsub("#{x}/",""))
  137.         }
  138.     end
  139.    
  140.     def self.change_sbg_speed_t
  141.         print "Enter .sbg filename.\n"
  142.         name = $stdin.gets.chomp!
  143.         print "Enter factor.\n"
  144.         factor = $stdin.gets.chomp!.to_f
  145.         print "Enter mode:\n"
  146.         print "0 - faster\n"
  147.         print "1 - slower\n"
  148.         mode = FasterSBG.choice(0,1)
  149.         FasterSBG.change_sbg_speed(name,factor,mode)
  150.     end
  151.    
  152.     def self.batch_dir_t
  153.         print "Enter .sbg directory\n"
  154.         dir = $stdin.gets.chomp!
  155.         print "Enter factor.\n"
  156.         factor = $stdin.gets.chomp!.to_f
  157.         print "Enter mode:\n"
  158.         print "0 - faster\n"
  159.         print "1 - slower\n"
  160.         mode = FasterSBG.choice(0,1)
  161.         FasterSBG.batch_dir(dir,factor,mode)
  162.     end
  163.    
  164.     def self.run_interactive
  165.         sel
  166.         print "**Faster SBG\n"
  167.         print "**Tool to change speed of .sbg files\n"
  168.         print "**by Narzew\n"
  169.         print "**v #{$fastersbg_version}\n"
  170.         print "**Release date: #{$fastersbg_reldate}\n"
  171.         print "**All rights reserved.\n"
  172.         print "**Type ruby FasterSBG.rb h for terminal mode help (Linux).\n"
  173.         sel
  174.         print "\n"*2
  175.         sel
  176.         print "1 - change speed of .sbg file\n"
  177.         print "2 - change speed of all .sbg files in a directory\n"
  178.         sel
  179.         choice = FasterSBG.choice(1,2)
  180.         case choice
  181.         when 1 then FasterSBG.change_sbg_speed_t
  182.         when 2 then FasterSBG.batch_dir_t
  183.         end
  184.     end
  185.        
  186. end
  187.  
  188. begin
  189.     if ARGV[0] == nil || ARGV[0] == ""
  190.         FasterSBG.run_interactive
  191.     else
  192.         case ARGV[0].to_s
  193.         when "c" then FasterSBG.change_sbg_speed(ARGV[1].to_s,ARGV[2].to_f,ARGV[3].to_i)
  194.         when "d" then FasterSBG.batch_dir(ARGV[1].to_s, ARGV[2].to_f, ARGV[3].to_i)
  195.         when "h"
  196.             sel
  197.             print "FasterSBG by Narzew v #{$fastersbg_version} Help\n"
  198.             print "FasterSBG.rb conv file factor mode - to convert single .sbg file\n"
  199.             print "FasterSBG.rb dir dir factor mode - to convert whole directory of .sbg's\n"
  200.             print "mode 0 - faster; mode 1 - slower\n"
  201.             sel
  202.         end
  203.     end
  204. rescue => e
  205.     print "Error occured.\nError: #{e}\n"
  206.     $stdin.gets
  207. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement