From 2185cc7a5c83816cd70bb38265ffafb863898189 Mon Sep 17 00:00:00 2001 From: Maruno17 Date: Mon, 7 Sep 2020 21:42:44 +0100 Subject: [PATCH] Updates to script extracting/combining files --- Data/Scripts.rxdata | Bin 563 -> 574 bytes script_dumper.rb | 172 --------------------------------------- scripts_combine.rb | 193 ++++++++++++++++++++++++++++++++++++++++++++ scripts_extract.rb | 193 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 386 insertions(+), 172 deletions(-) delete mode 100644 script_dumper.rb create mode 100644 scripts_combine.rb create mode 100644 scripts_extract.rb diff --git a/Data/Scripts.rxdata b/Data/Scripts.rxdata index 10b905b6ff4d2ce2f301e122388b0275f7eacd7c..6614a254372d8ae9dc0ac47835e6cf6c095b3b00 100644 GIT binary patch delta 39 ucmdnYvX4cQg(I3Rnj@3N;{GOPPbE&@#LPT4D>l~<&y7NgOe}2H3?Tr=o(Vhv delta 27 icmdnTvYADQg(I3Rnj@3N;{GOPB~IVO%)E_~icA1w%?C07 diff --git a/script_dumper.rb b/script_dumper.rb deleted file mode 100644 index 91bff3428..000000000 --- a/script_dumper.rb +++ /dev/null @@ -1,172 +0,0 @@ -require 'zlib' - -class Numeric - def to_digits(n = 3) - str = self.to_s - (n - str.size).times { str = str.prepend("0") } - return str - end -end - -module Scripts - def self.dump(path = "Data/Scripts", rxdata = "Data/Scripts.rxdata") - clear_directory(path) - folder_id = [1, 1] # Can only have two layers of folders - file_id = 1 - level = 0 # 0=main path, 1=subfolder, 2=sub-subfolder - folder_path = path - folder_name = nil - scripts = File.open(rxdata, 'rb') { |f| Marshal.load(f) } - - scripts.each_with_index do |e, i| - _, title, script = e - title = title_to_filename(title).strip - script = Zlib::Inflate.inflate(script).delete("\r") - next if title.empty? && script.empty? - section_name = nil - - if title[/\[\[\s*(.+)\s*\]\]$/] # Make a folder - section_name = $~[1].strip - section_name = "unnamed" if !section_name || section_name.empty? - folder_name = ((i>=scripts.length-2) ? "999" : folder_id[level].to_digits(3)) + "_" + section_name - create_directory(folder_path + "/" + folder_name) - folder_id[level] += 1 - if level < folder_id.length-1 - level += 1 # Go one level deeper - folder_id[level] = 1 # Reset numbering of subfolders - folder_path += "/" + folder_name - folder_name = nil - end - file_id = 1 # Reset numbering of script files - - elsif title.start_with?("=====") # Return to top level directory - level = 0 - folder_path = path - folder_name = nil - end - - # Create script file - next if script.empty? - this_folder = (folder_name) ? folder_path + "/" + folder_name : folder_path - section_name = title.strip if !section_name - section_name = "unnamed" if !section_name || section_name.empty? - file_name = ((i==scripts.length-1) ? "999" : file_id.to_digits(3)) + "_" + section_name + ".rb" - create_script(this_folder + "/" + file_name, script) - file_id += 1 # Increment numbering of script files - end - - # Backup Scripts.rxdata to ScriptsBackup.rxdata - File.open("Data/ScriptsBackup.rxdata", "wb") do |f| - Marshal.dump(scripts, f) - end - - # Replace Scripts.rxdata with ScriptsLoader.rxdata - loader_scripts = File.open("Data/ScriptsLoader.rxdata", 'rb') { |f| Marshal.load(f) } - File.open(rxdata, "wb") do |f| - Marshal.dump(loader_scripts, f) - end - end - - def self.from_folder(path = "Data/Scripts", rxdata = "Data/Scripts.rxdata") - scripts = [] - aggregate_from_folder(path, scripts) - # Save scripts to file - File.open(rxdata, "wb") do |f| - Marshal.dump(scripts, f) - end - end - - def self.aggregate_from_folder(path, scripts, level = 0) - files = [] - folders = [] - Dir.foreach(path) do |f| - next if f == '.' || f == '..' - if File.directory?(path + "/" + f) - folders.push(f) - else - files.push(f) - end - end - files.sort! - files.each do |f| - section_name = filename_to_title(f) - content = File.open(path + "/" + f, "rb") { |f2| f2.read }.gsub(/\n/, "\r\n") - scripts << [rand(999999), section_name, Zlib::Deflate.deflate(content)] - end - folders.sort! - folders.each do |f| - section_name = filename_to_title(f) - scripts << [rand(999999), "==================", Zlib::Deflate.deflate("")] if level==0 - scripts << [rand(999999), "", Zlib::Deflate.deflate("")] if level==1 - scripts << [rand(999999), "[[ " + section_name + " ]]", Zlib::Deflate.deflate("")] - aggregate_from_folder(path + "/" + f, scripts, level+1) - end - end - - def self.filename_to_title(filename) - filename = filename.bytes.pack('U*') - title = "" - if filename[/^[^_]*_(.+)$/] - title = $~[1] - title = title[0..-4] if title.end_with?(".rb") - title = title.strip - end - title = "unnamed" if !title || title.empty? - title.gsub!(/&bs;/, "\\") - title.gsub!(/&fs;/, "/") - title.gsub!(/&cn;/, ":") - title.gsub!(/&as;/, "*") - title.gsub!(/&qm;/, "?") - title.gsub!(/&dq;/, "\"") - title.gsub!(/</, "<") - title.gsub!(/>/, ">") - title.gsub!(/&po;/, "|") - return title - end - - def self.title_to_filename(title) - filename = title.clone - filename.gsub!(/\\/, "&bs;") - filename.gsub!(/\//, "&fs;") - filename.gsub!(/:/, "&cn;") - filename.gsub!(/\*/, "&as;") - filename.gsub!(/\?/, "&qm;") - filename.gsub!(/"/, "&dq;") - filename.gsub!(//, ">") - filename.gsub!(/\|/, "&po;") - return filename - end - - def self.create_script(title, content) - f = File.new(title, "wb") - f.write content - f.close - end - - def self.clear_directory(path, delete_current = false) - Dir.foreach(path) do |f| - next if f == '.' || f == '..' - if File.directory?(path + "/" + f) - clear_directory(path + "/" + f, true) - else - File.delete(path + "/" + f) - end - end - Dir.delete(path) if delete_current - end - - def self.create_directory(path) - paths = path.split('/') - paths.each_with_index do |e, i| - if !File.directory?(paths[0..i].join('/')) - Dir.mkdir(paths[0..i].join('/')) - end - end - end -end - -#Scripts.dump("D:/Desktop/Scripts", "D:/Desktop/Main Essentials/Data/Scripts.rxdata") -#Scripts.from_folder("D:/Desktop/Scripts", "D:/Desktop/Main Essentials/Data/Scripts.rxdata") -Scripts.dump -#Scripts.from_folder \ No newline at end of file diff --git a/scripts_combine.rb b/scripts_combine.rb new file mode 100644 index 000000000..2f3e49df6 --- /dev/null +++ b/scripts_combine.rb @@ -0,0 +1,193 @@ +require 'zlib' + +class Numeric + def to_digits(num = 3) + str = to_s + (num - str.size).times { str = str.prepend("0") } + return str + end +end + +module Scripts + def self.dump(path = "Data/Scripts", rxdata = "Data/Scripts.rxdata") + scripts = File.open(rxdata, 'rb') { |f| Marshal.load(f) } + if scripts.length < 10 + p "Scripts look like they're already extracted. Not doing so again." + return + end + + create_directory(path) + clear_directory(path) + + folder_id = [1, 1] # Can only have two layers of folders + file_id = 1 + level = 0 # 0=main path, 1=subfolder, 2=sub-subfolder + folder_path = path + folder_name = nil + scripts.each_with_index do |e, i| + _, title, script = e + title = title_to_filename(title).strip + script = Zlib::Inflate.inflate(script).delete("\r") + next if title.empty? && script.empty? + + section_name = nil + if title[/\[\[\s*(.+)\s*\]\]$/] # Make a folder + section_name = $~[1].strip + section_name = "unnamed" if !section_name || section_name.empty? + folder_num = (i < scripts.length - 2) ? folder_id[level].to_digits(3) : "999" + folder_name = folder_num + "_" + section_name + create_directory(folder_path + "/" + folder_name) + folder_id[level] += 1 + if level < folder_id.length-1 + level += 1 # Go one level deeper + folder_id[level] = 1 # Reset numbering of subfolders + folder_path += "/" + folder_name + folder_name = nil + end + file_id = 1 # Reset numbering of script files + elsif title.start_with?("=====") # Return to top level directory + level = 0 + folder_path = path + folder_name = nil + end + # Create script file + next if script.empty? + this_folder = folder_path + this_folder += "/" + folder_name if folder_name + section_name ||= title.strip + section_name = "unnamed" if !section_name || section_name.empty? + file_num = (i < scripts.length - 1) ? file_id.to_digits(3) : "999" + file_name = file_num + "_" + section_name + ".rb" + create_script(this_folder + "/" + file_name, script) + file_id += 1 # Increment numbering of script files + end + # Backup Scripts.rxdata to ScriptsBackup.rxdata + File.open("Data/ScriptsBackup.rxdata", "wb") do |f| + Marshal.dump(scripts, f) + end + # Replace Scripts.rxdata with ScriptsLoader.rxdata + createLoaderScripts(rxdata) + end + + def self.createLoaderScripts(rxdata) +# loader_scripts = File.open("Data/ScriptsLoader.rxdata", 'rb') { |f| Marshal.load(f) } +# p loader_scripts + txt = "x\x9C}SM\x8F\xDA0\x10\xBD#\xF1\x1F\x86,Rb-2\xCB\xB1\x95\xE8\x1E\xBAm\xD5S\xAB\x85\e\xA0\xC8$\x13p7\xD8\x91\xED\x94n\t\xFF\xBD\xB6C0\xE9\xD7\xC5\xF2\xCCx\xDE\xCC\xBCy\xBE\x83\xE5\x9Ek\xC8%j\x10\xD2\xC0Q\xAA\x17\xE0\x05\x98=\xC2\x8E\x1D\x10l\x10E\xA6^+\x83\xF9h8\x18\x0Er\xB4Q\xC52\xDC\xB2\xEC%UXIe\x86\x03\x00gz?\xCCa<\xA2W\x93f\xA5\x14\xD8{A\x91e\xFB\x134[\xD38\xBF\x8D\x18\xAA\xEB\xED(\x99\x9E\x92u~O\xCES\x02\xA7huw\x1A\xCF\xCE\e{>\x7FZ,\xD2\xC5\xFB\xE7\xCF_\x97\x8B\xD5xF\x8DL\xF9f5\xDB\x9C\xA3\xB3\xCB\xF7\x87BS+\xE1*\x1FPk\xB6C\xB8\x87h-\xD6\"\xB2\x97P\xF9\x9B\xE4\"\xB1\xFE\x88\f\a(\xF2n \xC5\xB8\xC64\x8C\x85JI\xE5`-\x15\x01\x92j\xFE\x13\xE1\xDD\x1C\xDE<<\xB4\x8D\x7F\xE4%RY\xA1H\xE2k2-\xE5.\x9E@|\x8C\xED\x14\xD0\x14\r\x14\xF4\xA8\xB8\xC1d<\"m\xB3\xD0\x16\x84x\xD9%9\x9E\x8D\x94\xB0\xE5;\n_jS\xD5\x06\xB8\x80>\xA8K\xC5R\xE3\r\x84w\xB99n\x86)%\xCBS\x9D)^\x19\x9D\x16J\x1E\xD2B\x969\xAA\xA4bfO\\Fa\xDB\xD6\x16b\x0E\xAB\x8D\xB7}\\_\xED'\xAEh!\x95[S\x9Bd\x05\xE2&i\v\v\xFCa\x1C1\x05\xCC\xE7\x10\xD3\x18\x9A\xA6\xBB\xD3\xB8}\x92xbr\xAE03R\xBD>z\x14\xB7\x91\xA9[GA\b 10 + p "Scripts.rxdata already has a bunch of scripts in it. Won't consolidate script files." + return + end + + scripts = [] + aggregate_from_folder(path, scripts) + # Save scripts to file + File.open(rxdata, "wb") do |f| + Marshal.dump(scripts, f) + end + end + + def self.aggregate_from_folder(path, scripts, level = 0) + files = [] + folders = [] + Dir.foreach(path) do |f| + next if f == '.' || f == '..' + + if File.directory?(path + "/" + f) + folders.push(f) + else + files.push(f) + end + end + # Aggregate individual script files into Scripts.rxdata + files.sort! + files.each do |f| + section_name = filename_to_title(f) + content = File.open(path + "/" + f, "rb") { |f2| f2.read }.gsub(/\n/, "\r\n") + scripts << [rand(999_999), section_name, Zlib::Deflate.deflate(content)] + end + # Check each subfolder for scripts to aggregate + folders.sort! + folders.each do |f| + section_name = filename_to_title(f) + scripts << [rand(999_999), "==================", Zlib::Deflate.deflate("")] if level == 0 + scripts << [rand(999_999), "", Zlib::Deflate.deflate("")] if level == 1 + scripts << [rand(999_999), "[[ " + section_name + " ]]", Zlib::Deflate.deflate("")] + aggregate_from_folder(path + "/" + f, scripts, level + 1) + end + end + + def self.filename_to_title(filename) + filename = filename.bytes.pack('U*') + title = "" + if filename[/^[^_]*_(.+)$/] + title = $~[1] + title = title[0..-4] if title.end_with?(".rb") + title = title.strip + end + title = "unnamed" if !title || title.empty? + title.gsub!(/&bs;/, "\\") + title.gsub!(/&fs;/, "/") + title.gsub!(/&cn;/, ":") + title.gsub!(/&as;/, "*") + title.gsub!(/&qm;/, "?") + title.gsub!(/&dq;/, "\"") + title.gsub!(/</, "<") + title.gsub!(/>/, ">") + title.gsub!(/&po;/, "|") + return title + end + + def self.title_to_filename(title) + filename = title.clone + filename.gsub!(/\\/, "&bs;") + filename.gsub!(/\//, "&fs;") + filename.gsub!(/:/, "&cn;") + filename.gsub!(/\*/, "&as;") + filename.gsub!(/\?/, "&qm;") + filename.gsub!(/"/, "&dq;") + filename.gsub!(//, ">") + filename.gsub!(/\|/, "&po;") + return filename + end + + def self.create_script(title, content) + f = File.new(title, "wb") + f.write content + f.close + end + + def self.clear_directory(path, delete_current = false) + Dir.foreach(path) do |f| + next if f == '.' || f == '..' + if File.directory?(path + "/" + f) + clear_directory(path + "/" + f, true) + else + File.delete(path + "/" + f) + end + end + Dir.delete(path) if delete_current + end + + def self.create_directory(path) + paths = path.split('/') + paths.each_with_index do |_e, i| + if !File.directory?(paths[0..i].join('/')) + Dir.mkdir(paths[0..i].join('/')) + end + end + end +end + +#Scripts.dump("D:/Desktop/Scripts", "D:/Desktop/Main Essentials/Data/Scripts.rxdata") +#Scripts.from_folder("D:/Desktop/Scripts", "D:/Desktop/Main Essentials/Data/Scripts.rxdata") +#Scripts.dump +Scripts.from_folder diff --git a/scripts_extract.rb b/scripts_extract.rb new file mode 100644 index 000000000..15668f70c --- /dev/null +++ b/scripts_extract.rb @@ -0,0 +1,193 @@ +require 'zlib' + +class Numeric + def to_digits(num = 3) + str = to_s + (num - str.size).times { str = str.prepend("0") } + return str + end +end + +module Scripts + def self.dump(path = "Data/Scripts", rxdata = "Data/Scripts.rxdata") + scripts = File.open(rxdata, 'rb') { |f| Marshal.load(f) } + if scripts.length < 10 + p "Scripts look like they're already extracted. Not doing so again." + return + end + + create_directory(path) + clear_directory(path) + + folder_id = [1, 1] # Can only have two layers of folders + file_id = 1 + level = 0 # 0=main path, 1=subfolder, 2=sub-subfolder + folder_path = path + folder_name = nil + scripts.each_with_index do |e, i| + _, title, script = e + title = title_to_filename(title).strip + script = Zlib::Inflate.inflate(script).delete("\r") + next if title.empty? && script.empty? + + section_name = nil + if title[/\[\[\s*(.+)\s*\]\]$/] # Make a folder + section_name = $~[1].strip + section_name = "unnamed" if !section_name || section_name.empty? + folder_num = (i < scripts.length - 2) ? folder_id[level].to_digits(3) : "999" + folder_name = folder_num + "_" + section_name + create_directory(folder_path + "/" + folder_name) + folder_id[level] += 1 + if level < folder_id.length-1 + level += 1 # Go one level deeper + folder_id[level] = 1 # Reset numbering of subfolders + folder_path += "/" + folder_name + folder_name = nil + end + file_id = 1 # Reset numbering of script files + elsif title.start_with?("=====") # Return to top level directory + level = 0 + folder_path = path + folder_name = nil + end + # Create script file + next if script.empty? + this_folder = folder_path + this_folder += "/" + folder_name if folder_name + section_name ||= title.strip + section_name = "unnamed" if !section_name || section_name.empty? + file_num = (i < scripts.length - 1) ? file_id.to_digits(3) : "999" + file_name = file_num + "_" + section_name + ".rb" + create_script(this_folder + "/" + file_name, script) + file_id += 1 # Increment numbering of script files + end + # Backup Scripts.rxdata to ScriptsBackup.rxdata + File.open("Data/ScriptsBackup.rxdata", "wb") do |f| + Marshal.dump(scripts, f) + end + # Replace Scripts.rxdata with ScriptsLoader.rxdata + createLoaderScripts(rxdata) + end + + def self.createLoaderScripts(rxdata) +# loader_scripts = File.open("Data/ScriptsLoader.rxdata", 'rb') { |f| Marshal.load(f) } +# p loader_scripts + txt = "x\x9C}SM\x8F\xDA0\x10\xBD#\xF1\x1F\x86,Rb-2\xCB\xB1\x95\xE8\x1E\xBAm\xD5S\xAB\x85\e\xA0\xC8$\x13p7\xD8\x91\xED\x94n\t\xFF\xBD\xB6C0\xE9\xD7\xC5\xF2\xCCx\xDE\xCC\xBCy\xBE\x83\xE5\x9Ek\xC8%j\x10\xD2\xC0Q\xAA\x17\xE0\x05\x98=\xC2\x8E\x1D\x10l\x10E\xA6^+\x83\xF9h8\x18\x0Er\xB4Q\xC52\xDC\xB2\xEC%UXIe\x86\x03\x00gz?\xCCa<\xA2W\x93f\xA5\x14\xD8{A\x91e\xFB\x134[\xD38\xBF\x8D\x18\xAA\xEB\xED(\x99\x9E\x92u~O\xCES\x02\xA7huw\x1A\xCF\xCE\e{>\x7FZ,\xD2\xC5\xFB\xE7\xCF_\x97\x8B\xD5xF\x8DL\xF9f5\xDB\x9C\xA3\xB3\xCB\xF7\x87BS+\xE1*\x1FPk\xB6C\xB8\x87h-\xD6\"\xB2\x97P\xF9\x9B\xE4\"\xB1\xFE\x88\f\a(\xF2n \xC5\xB8\xC64\x8C\x85JI\xE5`-\x15\x01\x92j\xFE\x13\xE1\xDD\x1C\xDE<<\xB4\x8D\x7F\xE4%RY\xA1H\xE2k2-\xE5.\x9E@|\x8C\xED\x14\xD0\x14\r\x14\xF4\xA8\xB8\xC1d<\"m\xB3\xD0\x16\x84x\xD9%9\x9E\x8D\x94\xB0\xE5;\n_jS\xD5\x06\xB8\x80>\xA8K\xC5R\xE3\r\x84w\xB99n\x86)%\xCBS\x9D)^\x19\x9D\x16J\x1E\xD2B\x969\xAA\xA4bfO\\Fa\xDB\xD6\x16b\x0E\xAB\x8D\xB7}\\_\xED'\xAEh!\x95[S\x9Bd\x05\xE2&i\v\v\xFCa\x1C1\x05\xCC\xE7\x10\xD3\x18\x9A\xA6\xBB\xD3\xB8}\x92xbr\xAE03R\xBD>z\x14\xB7\x91\xA9[GA\b 10 + p "Scripts.rxdata already has a bunch of scripts in it. Won't consolidate script files." + return + end + + scripts = [] + aggregate_from_folder(path, scripts) + # Save scripts to file + File.open(rxdata, "wb") do |f| + Marshal.dump(scripts, f) + end + end + + def self.aggregate_from_folder(path, scripts, level = 0) + files = [] + folders = [] + Dir.foreach(path) do |f| + next if f == '.' || f == '..' + + if File.directory?(path + "/" + f) + folders.push(f) + else + files.push(f) + end + end + # Aggregate individual script files into Scripts.rxdata + files.sort! + files.each do |f| + section_name = filename_to_title(f) + content = File.open(path + "/" + f, "rb") { |f2| f2.read }.gsub(/\n/, "\r\n") + scripts << [rand(999_999), section_name, Zlib::Deflate.deflate(content)] + end + # Check each subfolder for scripts to aggregate + folders.sort! + folders.each do |f| + section_name = filename_to_title(f) + scripts << [rand(999_999), "==================", Zlib::Deflate.deflate("")] if level == 0 + scripts << [rand(999_999), "", Zlib::Deflate.deflate("")] if level == 1 + scripts << [rand(999_999), "[[ " + section_name + " ]]", Zlib::Deflate.deflate("")] + aggregate_from_folder(path + "/" + f, scripts, level + 1) + end + end + + def self.filename_to_title(filename) + filename = filename.bytes.pack('U*') + title = "" + if filename[/^[^_]*_(.+)$/] + title = $~[1] + title = title[0..-4] if title.end_with?(".rb") + title = title.strip + end + title = "unnamed" if !title || title.empty? + title.gsub!(/&bs;/, "\\") + title.gsub!(/&fs;/, "/") + title.gsub!(/&cn;/, ":") + title.gsub!(/&as;/, "*") + title.gsub!(/&qm;/, "?") + title.gsub!(/&dq;/, "\"") + title.gsub!(/</, "<") + title.gsub!(/>/, ">") + title.gsub!(/&po;/, "|") + return title + end + + def self.title_to_filename(title) + filename = title.clone + filename.gsub!(/\\/, "&bs;") + filename.gsub!(/\//, "&fs;") + filename.gsub!(/:/, "&cn;") + filename.gsub!(/\*/, "&as;") + filename.gsub!(/\?/, "&qm;") + filename.gsub!(/"/, "&dq;") + filename.gsub!(//, ">") + filename.gsub!(/\|/, "&po;") + return filename + end + + def self.create_script(title, content) + f = File.new(title, "wb") + f.write content + f.close + end + + def self.clear_directory(path, delete_current = false) + Dir.foreach(path) do |f| + next if f == '.' || f == '..' + if File.directory?(path + "/" + f) + clear_directory(path + "/" + f, true) + else + File.delete(path + "/" + f) + end + end + Dir.delete(path) if delete_current + end + + def self.create_directory(path) + paths = path.split('/') + paths.each_with_index do |_e, i| + if !File.directory?(paths[0..i].join('/')) + Dir.mkdir(paths[0..i].join('/')) + end + end + end +end + +#Scripts.dump("D:/Desktop/Scripts", "D:/Desktop/Main Essentials/Data/Scripts.rxdata") +#Scripts.from_folder("D:/Desktop/Scripts", "D:/Desktop/Main Essentials/Data/Scripts.rxdata") +Scripts.dump +#Scripts.from_folder