Skip to content

Instantly share code, notes, and snippets.

@valda
Created October 27, 2009 20:48
Show Gist options
  • Save valda/219945 to your computer and use it in GitHub Desktop.
Save valda/219945 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby1.9
require 'rubygems'
require 'hpricot'
require 'pathname'
require 'yaml'
require 'yaml_waml'
require File.join(File.dirname(__FILE__), 'string_table')
orig_dir = Pathname.new(ARGV.shift)
jp_dir = Pathname.new(ARGV.shift)
mod_dir = Pathname.new(ARGV.shift)
diff = Hash.new
Pathname.glob(File.join(jp_dir, '**/*.xml')) do |xmlpath|
orig_dic = Hash.new
relpath = xmlpath.to_s.gsub(/^#{jp_dir}(?:#{Pathname::SEPARATOR_PAT})?/, '')
# 日本語 MOD の xml を読み込み
jp_table = StringTable.new(xmlpath, 'cp932')
# 元 xml を読み込み
orig_table = StringTable.new(orig_dir + relpath, 'iso-8859-1')
# MOD の xml を読み込み
mod_table = StringTable.new(mod_dir + relpath, 'iso-8859-1') rescue nil
jp_table.each do |id, text|
orig_text = orig_table[id]
mod_text = mod_table ? mod_table[id] : nil
if orig_text != text
diff[relpath] ||= Hash.new
diff[relpath][id] ||= Hash.new
diff[relpath][id]['orig'] = orig_text
diff[relpath][id]['mod'] = mod_text if mod_text
if mod_text and mod_text != orig_text
diff[relpath][id]['jp'] = "[FUZZ] #{text}"
else
diff[relpath][id]['jp'] = text
end
end
end
end
puts diff.to_yaml
#!/usr/local/bin/ruby1.9
require 'rubygems'
require 'hpricot'
require 'pathname'
require 'yaml'
require File.join(File.dirname(__FILE__), 'string_table')
orig_dir = Pathname.new(ARGV.shift)
mod_dir = Pathname.new(ARGV.shift)
patch_dir = Pathname.new(ARGV.shift)
merged_dir = Pathname.new(ARGV.shift || 'gamedata_merged')
diff = YAML.load(File.open(patch_dir, 'r:utf-8') { |fh| fh.read })
diff.each_pair do |relpath, dichash|
xmlpath = mod_dir + relpath
xmlpath = orig_dir + relpath unless xmlpath.file?
warn "[ERROR] #{xmlpath} is not exists." unless xmlpath.file?
doc = Hpricot.XML(xmlpath.read)
if xmldecl = doc.at('?xml') and xmldecl.xmldecl?
xmldecl.encoding = 'Shift_JIS'
end
doc.search('//string[@id]') do |string|
id = string.attributes['id']
if dic = dichash[id]
text = dic['jp'] || dic['mod'] || dic['orig']
text = text.sub(/^\[FUZZ\] /, '')
string.at('/text').inner_html = text.encode('cp932', :invalid => :replace, :undef => :replace)
end
end
merged_xmlpath = merged_dir + relpath
merged_xmlpath.dirname.mkpath unless merged_xmlpath.dirname.directory?
merged_xmlpath.open('w') {|fh| fh << doc.to_s }
puts "wrote: #{merged_xmlpath}"
end
class StringTable
def initialize(xmlpath, enc)
@table = Hash.new
unless File.exists?(xmlpath)
raise StandardError, "ERROR: #{xmlpath} is not exists."
end
xml = File.open(xmlpath, "r:#{enc}") { |fh| fh.read }
doc = Hpricot.XML(xml.encode('utf-8', :invalid => :replace, :undef => :replace))
doc.search('//string[@id]') do |string|
id = string.attributes['id']
text = string.at('/text').inner_html
@table[id] = text
end
end
def method_missing(message, *arg, &block)
@table.send(message, *arg, &block)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment