SHARE
TWEET

import.rb

polectron Mar 27th, 2020 (edited) 347 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # frozen_string_literal: true
  2.  
  3. require 'zlib'
  4. require 'fileutils'
  5.  
  6. def pbFindScript(a, name)
  7.     a.each do |i|
  8.       next unless i
  9.       return i if i[1] == name
  10.     end
  11.     nil
  12. end
  13.  
  14. def pbAddScriptToGivenFile(scripts, script, sectionname, _fname)
  15.   s = pbFindScript(scripts, sectionname)
  16.   if s
  17.     s[2] = Zlib::Deflate.deflate("#{script}\r\n")
  18.   else
  19.     scripts.insert(scripts.size - 1, [rand(100_000_000), sectionname, Zlib::Deflate.deflate("#{script}\r\n")])
  20.   end
  21.   # sleep(0.001)
  22.   scripts
  23.   end
  24.  
  25. def load_data(fname)
  26.   File.open(fname, 'rb') do |f|
  27.     data = Marshal.load(f)
  28.     for d in data
  29.       d[1].force_encoding("UTF-8")
  30.     end
  31.     return data
  32.   end
  33.   end
  34.  
  35. def save_data(data, fname)
  36.   File.open(fname, 'wb') do |f|
  37.     Marshal.dump(data, f)
  38.   end
  39. end
  40.  
  41. def import_all_scripts
  42.   # msgwindow=Kernel.pbCreateMessageWindow
  43.   # Kernel.pbMessageDisplay(msgwindow,_INTL("Por favor, espera.\\wtnp[0]"))
  44.  
  45.   p 'Importando scripts...'
  46.  
  47.   if !File.directory?("export")
  48.     FileUtils.mkdir_p("export")
  49.   end
  50.  
  51.   datestr = Time.now.strftime("%d_%m_%Y_%H_%M")
  52.  
  53.   FileUtils.cp("Data/Scripts.rxdata", "Data/Scripts_#{datestr}.rxdata")
  54.  
  55.   dir_files = Dir['export/*.rb']
  56.  
  57.   scripts = {
  58.     'scripts' => [],
  59.     'constants' => []
  60.   }
  61.  
  62.   File.open('export/export_info.txt', 'r') do |f|
  63.     content = f.read
  64.     lists = content.split("#-------------------\n")
  65.     lists.each do |files|
  66.       counter = 0
  67.       file_lines = files.split("\n")
  68.       file_lines.each do |file|
  69.         scripts[files.split("\n")[0]].push(file.split(',')) if counter > 0
  70.         counter += 1
  71.       end
  72.     end
  73.   end
  74.  
  75.   begin
  76.     all_scripts = load_data('Data/Scripts.rxdata')
  77.     all_scripts ||= []
  78.   rescue StandardError
  79.     all_scripts = []
  80.   end
  81.  
  82.   dir_files.each do |file|
  83.     next if scripts['constants'].flatten.include?(File.basename(file, '.*'))
  84.  
  85.     File.open(file, 'r') do |f|
  86.       all_scripts = pbAddScriptToGivenFile(all_scripts, f.read, File.basename(file, '.*'), 'Data/Scripts.rxdata')
  87.     end
  88.   end
  89.  
  90.   # save_data(all_scripts,"Data/Scripts.rxdata")
  91.  
  92.   to_delete = []
  93.  
  94.   dir_files_bases = dir_files.map do |f|
  95.     File.basename(f, '.*')
  96.   end
  97.  
  98.   all_scripts.each do |script|
  99.     if !dir_files_bases.include?(script[1]) && !script[1].match(/=+/)
  100.       p "Going to delete #{script[1]}"
  101.       to_delete.push(script)
  102.     end
  103.   end
  104.  
  105.   to_delete.each do |script|
  106.     all_scripts.delete(script)
  107.   end
  108.  
  109.   save_data(all_scripts, 'Data/Scripts.rxdata')
  110.  
  111.   # Kernel.pbMessageDisplay(msgwindow, "ImportaciĆ³n finalizada")
  112.   # Kernel.pbDisposeMessageWindow(msgwindow)
  113.  
  114.   p 'ImportaciĆ³n finalizada.'
  115.   end
  116.  
  117. import_all_scripts
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top