Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #encoding: utf-8
- #========================================================
- # Faster SBG v 1.1 by Narzew
- # 07.03.2014
- # Some rights reserved.
- # License : GPL v3 or later
- # Please atribute your project to narzew19@yahoo.co.jp
- #========================================================
- require 'find'
- $fastersbg_version = 1.1
- $fastersbg_reldate = "07.03.2014"
- def sel
- print "**********************************************************\n"
- end
- module FasterSBG
- def self.choice(min,max)
- min = min.to_i
- max = max.to_i
- loop do
- y = $stdin.gets.chomp!.to_i
- if y < min || y > max
- sel
- print "Bad choice!\n"
- sel
- else
- return y.to_i
- end
- end
- return y.to_i
- end
- def self.get_total_time(x)
- t = x[0].to_i*3600+x[1].to_i*60+x[2].to_i
- return t
- end
- def self.convert_total_time(x)
- hours = x/3600
- minutes = (x%3600)/60
- seconds = ((x%3600)%60)
- return [hours,minutes,seconds]
- end
- def self.set_total_time(x)
- timevalue = FasterSBG.convert_total_time(x)
- hours = timevalue.at(0).to_i
- minutes = timevalue.at(1).to_i
- seconds = timevalue.at(2).to_i
- if hours < 0
- par1 = "00"
- elsif hours < 10
- par1 = "0#{hours}"
- else
- par1 = "#{hours}"
- end
- if minutes < 0
- par2 = "00"
- elsif minutes < 10
- par2 = "0#{minutes}"
- else
- par2 = "#{minutes}"
- end
- if seconds < 0
- par3 = "00"
- elsif seconds < 10
- par3 = "0#{seconds}"
- else
- par3 = "#{seconds}"
- end
- return "+#{par1}:#{par2}:#{par3}"
- end
- def self.change_sbg_speed(file, factor, mode,dir=nil,filename=nil)
- data = lambda{File.open(file,'rb'){|f|return f.read}}.call
- result = ""
- if mode == 0
- result << "# Dose Speed changed using Narzew's FasterSBG v #{$fastersbg_version}\n# Factor: #{factor} faster.\n"
- elsif mode == 1
- result << "# Dose Speed Changed using Narzew's FasterSBG v #{$fastersbg_version}\n# Factor: #{factor} slower.\n"
- end
- 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"
- data.each_line{|x|
- if x[0].to_s != "+"
- result << x
- else
- totaltime = 0
- y = x.gsub("+","")
- spl1 = y.split("\x20")[0]
- spl2 = y.split(":")
- totaltime = FasterSBG.get_total_time(spl2)
- if mode == 0
- totaltime = totaltime/factor
- elsif mode == 1
- totaltime = totaltime*factor
- else
- raise("Bad mode!")
- end
- str = FasterSBG.set_total_time(totaltime)
- x[0..8] = str
- result << x
- end
- }
- if dir == nil || filename == nil
- if mode == 0
- File.open(file.gsub(".sbg","-QHU.sbg"),'wb'){|w|w.write(result)}
- elsif mode == 1
- File.open(file.gsub(".sbg","-SHU.sbg"),'wb'){|w|w.write(result)}
- end
- else
- if mode == 0
- File.open("#{dir}/"+filename.gsub(".sbg","-QHU.sbg"),'wb'){|w|w.write(result)}
- elsif mode == 1
- File.open("#{dir}/"+filename.gsub(".sbg","-SHU.sbg"),'wb'){|w|w.write(result)}
- end
- end
- print "#{file} converted.\n"
- end
- def self.batch_dir(x,factor, mode)
- Dir.mkdir("Converted Doses") unless Dir.exist?("Converted Doses")
- filenames = []
- Find.find(x).each{|y|
- ext = y.split(".")[-1]
- if ext == "sbg"
- filenames << y
- end
- }
- filenames.each{|f|
- FasterSBG.change_sbg_speed(f,factor,mode,"Converted Doses", f.gsub("#{x}/",""))
- }
- end
- def self.change_sbg_speed_t
- print "Enter .sbg filename.\n"
- name = $stdin.gets.chomp!
- print "Enter factor.\n"
- factor = $stdin.gets.chomp!.to_f
- print "Enter mode:\n"
- print "0 - faster\n"
- print "1 - slower\n"
- mode = FasterSBG.choice(0,1)
- FasterSBG.change_sbg_speed(name,factor,mode)
- end
- def self.batch_dir_t
- print "Enter .sbg directory\n"
- dir = $stdin.gets.chomp!
- print "Enter factor.\n"
- factor = $stdin.gets.chomp!.to_f
- print "Enter mode:\n"
- print "0 - faster\n"
- print "1 - slower\n"
- mode = FasterSBG.choice(0,1)
- FasterSBG.batch_dir(dir,factor,mode)
- end
- def self.run_interactive
- sel
- print "**Faster SBG\n"
- print "**Tool to change speed of .sbg files\n"
- print "**by Narzew\n"
- print "**v #{$fastersbg_version}\n"
- print "**Release date: #{$fastersbg_reldate}\n"
- print "**All rights reserved.\n"
- print "**Type ruby FasterSBG.rb h for terminal mode help (Linux).\n"
- sel
- print "\n"*2
- sel
- print "1 - change speed of .sbg file\n"
- print "2 - change speed of all .sbg files in a directory\n"
- sel
- choice = FasterSBG.choice(1,2)
- case choice
- when 1 then FasterSBG.change_sbg_speed_t
- when 2 then FasterSBG.batch_dir_t
- end
- end
- end
- begin
- if ARGV[0] == nil || ARGV[0] == ""
- FasterSBG.run_interactive
- else
- case ARGV[0].to_s
- when "c" then FasterSBG.change_sbg_speed(ARGV[1].to_s,ARGV[2].to_f,ARGV[3].to_i)
- when "d" then FasterSBG.batch_dir(ARGV[1].to_s, ARGV[2].to_f, ARGV[3].to_i)
- when "h"
- sel
- print "FasterSBG by Narzew v #{$fastersbg_version} Help\n"
- print "FasterSBG.rb conv file factor mode - to convert single .sbg file\n"
- print "FasterSBG.rb dir dir factor mode - to convert whole directory of .sbg's\n"
- print "mode 0 - faster; mode 1 - slower\n"
- sel
- end
- end
- rescue => e
- print "Error occured.\nError: #{e}\n"
- $stdin.gets
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement