Skip to content

Instantly share code, notes, and snippets.

@julik
Created March 10, 2009 04:38
Show Gist options
  • Save julik/76732 to your computer and use it in GitHub Desktop.
Save julik/76732 to your computer and use it in GitHub Desktop.
require 'strscan'
class SloppyParser < StringScanner
def skip_until(pattern)
pattern.is_a?(String) ? super(/#{Regexp.escape(pattern)}/m) : super(pattern)
end
def scan_until(pattern)
pattern.is_a?(String) ? super(/#{Regexp.escape(pattern)}/m) : super(pattern)
end
end
class ValueAt
attr_accessor :value, :frame
def self.from_string(str)
v = new
v.value, v.frame = str.scan(/(.+)@(\d+)/).to_a.flatten
v
end
def reverse_float
1 - @value.to_f
end
end
class CurveParser < SloppyParser
BLOCK_ENDS = /\d,|\)/
attr_reader :values
def initialize(with_curve_arg)
@values = []
super(with_curve_arg.to_s)
# Skip the interpolation
skip_until '('
# Skip the first defining parameter whatever that might be
skip_until ','
loop do
break unless (value_at = scan_until(BLOCK_ENDS))
@values << ValueAt.from_string(value_at)
end
end
end
class TrackerParser < SloppyParser
attr_reader :x_curve, :y_curve, :c_curve, :name
def initialize(with_tracker_args)
super(with_tracker_args)
# Name me!
@name = scan_until(/(\w+) /)
# All the tracker arguments
17.times { skip_until ',' } # input data
# Grab the curves
@x_curve, @y_curve, @c_curve = (0..2).map{ CurveParser.new(scan_until('),')).values }
end
def curves
[@x_curve, @y_curve, @c_curve]
end
end
class TrackParser < SloppyParser
def initialize(with_tracker_block)
# scan until the first " - name of the track
# scan values
# discard the 8 box determinators
8.times { skip_until ',' }
# discard the box animation
2.times { skip_until ',' }
end
end
# Read the Shake file
script_file = File.read("E036_tracks.shk")
out_to = File.open("dumped_tracks.2dt", 'w')
# Grab all tracker blocks
TRACKER_PATTERN = /((\w+) = Tracker\(([^;]+))/m
script_file.scan(TRACKER_PATTERN).each_with_index do | tracker_text_block, idx |
parser = TrackerParser.new(tracker_text_block.to_s)
tracker_name = parser.name
x, y, c = TrackerParser.new(tracker_text_block.to_s).curves
out_to.puts "\n"
out_to.puts tracker_name.inspect # "autoquotes"
out_to.puts x.length
x.each_with_index do | value_at, k |
begin
out_to.puts "%s %.3f %.3f %.3f" % [value_at.frame.to_i - 1, value_at.value, y[k].value, c[k].reverse_float]
rescue NoMethodError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment