From a81d4cff46a01c7499e4b2201d8d0afb46b14cdc Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 18 Apr 2010 12:17:32 +0000 Subject: [PATCH] Split string detection routines into common code. Subversion-branch: /research Subversion-revision: 1888 --- hhe/common.rb | 87 ++++++++++++++++++++++++++++++++++++++++++++-- hhe/dump-strings | 75 ++++----------------------------------- hhe/v1.0/strings.c | 2 +- hhe/v1.2/strings.c | 2 +- hhe/v1.3/strings.c | 2 +- 5 files changed, 94 insertions(+), 74 deletions(-) diff --git a/hhe/common.rb b/hhe/common.rb index 0caf061..f837e8b 100644 --- a/hhe/common.rb +++ b/hhe/common.rb @@ -77,15 +77,96 @@ THING_FLAGS2 = %w{ MF2_DONTDRAW } -def set_config(name) +# HHE seems to stop when it has found this many strings: + +NUM_STRINGS=785 + +def find_config(name) configs = [ Heretic_1_0, Heretic_1_2, Heretic_1_3 ] for config in configs if name == config::NAME - include config - return + return config end end raise "Unknown configuration: #{name}" end + +def set_config(name) + config = find_config(name) + include config +end + +def read_block(file) + result = "" + + 4.times do + break if file.eof? + c = file.readchar + result += sprintf("%c", c) + end + + end_index = result.index(0) + if end_index + result = result[0, end_index] + end + + result +end + +def bad_string?(str) + str.each_byte do |b| + if b >= 0x80 + return true + end + end + + return false +end + +def find_strings(filename, config) + + strings = [] + + File.open(filename) do |file| + current_str = "" + + file.seek(config::STRINGS_OFFSET) + + offset = 0 + start_offset = nil + + while strings.length < NUM_STRINGS + block = read_block(file) + + if start_offset == nil + start_offset = offset + end + + current_str += block + + # End of string? + + if block.length < 4 + + # Extended-ASCII characters cannot be output + # to JSON. + + if bad_string?(current_str) + current_str = nil + end + + strings.push([start_offset, current_str]) + + current_str = "" + start_offset = nil + end + + offset += 4 + end + end + + strings +end + diff --git a/hhe/dump-strings b/hhe/dump-strings index df2eb54..63c3ba6 100755 --- a/hhe/dump-strings +++ b/hhe/dump-strings @@ -3,78 +3,17 @@ require "json" require "common" -# HHE seems to stop when it has found this many strings: +config = find_config(ARGV[1]) +strings = find_strings(ARGV[0], config) -NUM_STRINGS=785 +for string in strings -set_config(ARGV[1]) + string_text = string[1].to_json -def read_block(file) - result = "" - - 4.times do - break if file.eof? - c = file.readchar - result += sprintf("%c", c) - end - - end_index = result.index(0) - if end_index - result = result[0, end_index] - end - - result -end - -def bad_string?(str) - str.each_byte do |b| - if b >= 0x80 - return true - end + if string[1] == nil + string_text = "NULL" end - return false + printf " { %6i, %s },\n", string[0], string_text end -File.open(ARGV[0]) do |file| - current_str = "" - - file.seek(STRINGS_OFFSET) - - offset = 0 - start_offset = nil - num_strings = 0 - - while num_strings < NUM_STRINGS - block = read_block(file) - - if start_offset == nil - start_offset = offset - end - - current_str += block - - # End of string? - - if block.length < 4 - - # Extended-ASCII characters cannot be output - # to JSON. - - if bad_string?(current_str) - current_str = "XXXXX BAD STRING" - end - - printf " { %6i, %s },\n", start_offset, - current_str.to_json - current_str = "" - start_offset = nil - num_strings += 1 - end - - offset += 4 - end -end - - - diff --git a/hhe/v1.0/strings.c b/hhe/v1.0/strings.c index b122c86..3b03591 100644 --- a/hhe/v1.0/strings.c +++ b/hhe/v1.0/strings.c @@ -551,7 +551,7 @@ { 9972, "P_RemoveActivePlat: can't find plat!" }, { 10012, "Too many mace spots." }, { 10036, "" }, - { 10040, "XXXXX BAD STRING" }, + { 10040, NULL }, { 10052, "P_GroupLines: miscounted" }, { 10080, "-timer" }, { 10088, "FLTWAWA1" }, diff --git a/hhe/v1.2/strings.c b/hhe/v1.2/strings.c index 6bcd90c..a1a2921 100644 --- a/hhe/v1.2/strings.c +++ b/hhe/v1.2/strings.c @@ -581,7 +581,7 @@ { 11900, "P_RemoveActivePlat: can't find plat!" }, { 11940, "Too many mace spots." }, { 11964, "" }, - { 11968, "XXXXX BAD STRING" }, + { 11968, NULL }, { 11980, "P_GroupLines: miscounted" }, { 12008, "-timer" }, { 12016, "FLTWAWA1" }, diff --git a/hhe/v1.3/strings.c b/hhe/v1.3/strings.c index 4901838..ed771e0 100644 --- a/hhe/v1.3/strings.c +++ b/hhe/v1.3/strings.c @@ -581,7 +581,7 @@ { 11900, "P_RemoveActivePlat: can't find plat!" }, { 11940, "Too many mace spots." }, { 11964, "" }, - { 11968, "XXXXX BAD STRING" }, + { 11968, NULL }, { 11980, "P_GroupLines: miscounted" }, { 12008, "-timer" }, { 12016, "FLTWAWA1" },