Data Encrypter/Decrypter

Data Encrypter/Decrypter
Version: 1.0


Introduction

This is a script to overcome some of RMXP's limitations. It takes the decrypts the data files into an editable format, then encrypts them back into the data files.

Screenshots

Not necessary (It's just a command window on the title background)

Script
Content Hidden Spoiler
#=============================================================================
#  Data Encrypter/Decrypter
#  Version 1.0
#-----------------------------------------------------------------------------
#  Scripted by: Yeyinde
#  Help from: Trickster
#  December 2006
#-----------------------------------------------------------------------------
#  This script decrypts some of the data files in the Data folder, then can
#  remake that decrypted data into new data files.  Because this script will
#  modify the original data files, I suggest you make a backup.
#  Take note on how the data comes out.  That is how the data should look like
#  when, and if, you make changes.
#  Call with $scene = Scene_DataManager.new
#=============================================================================

class Scene_DataManager
  def main
    @command_window = Window_Command.new(160, ['Decrypt', 'Encrypt'])
    @command_window.back_opacity = 160
    @command_window.x = 320 - @command_window.width / 2
    @command_window.y = 288
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.title($data_system.title_name)
    Graphics.transition
    loop do
      Graphics.update
      Input.update
      update
      break if $scene != self
    end
    Graphics.freeze
    @command_window.dispose
    @sprite.dispose
  end
  def update
    @command_window.update
    @sprite.update
    if Input.trigger?(Input::B)
      $game_system.se_play($data_system.cancel_se)
      $scene = Scene_Map.new
      return
    end
    if Input.trigger?(Input::C)
      $game_system.se_play($data_system.decision_se)
      if @command_window.index == 0
        decrypt
      else
        if File.directory?('D_Data')
          encrypt
        else
          print 'Cannot encrypt nothing.  Decrypt data first.'
          return
        end
      end
      $scene = Scene_Map.new
      return
    end
  end
  # Main encrypt method
  def encrypt
    print 'Now encrypting data.'
    encrypt_data('Actors')
    encrypt_data('Classes')
    encrypt_data('Skills')
    encrypt_data('Items')
    encrypt_data('Weapons')
    encrypt_data('Armors')
    encrypt_data('Enemies')
    encrypt_data('States')
    encrypt_data('System', false)
    print 'Data encryption complete.  Now reseting data.'
    $data_actors        = load_data("Data/Actors.rxdata")
    $data_classes       = load_data("Data/Classes.rxdata")
    $data_skills        = load_data("Data/Skills.rxdata")
    $data_items         = load_data("Data/Items.rxdata")
    $data_weapons       = load_data("Data/Weapons.rxdata")
    $data_armors        = load_data("Data/Armors.rxdata")
    $data_enemies       = load_data("Data/Enemies.rxdata")
    $data_states        = load_data("Data/States.rxdata")
    $data_system        = load_data("Data/System.rxdata")
  end
  # Encrypt Data method
  def encrypt_data(data_file, array = true)
    file = File.new('D_Data/' + data_file + '.txt')
    lines = file.readlines
    file.close
    data = array ? [nil] : RPG::System.new
    element = case data_file
              when 'Actors'
                RPG::Actor.new
              when 'Classes'
                RPG::Class.new
              when 'Skills'
                RPG::Skill.new
              when 'Items'
                RPG::Item.new
              when 'Weapons'
                RPG::Weapon.new
              when 'Armors'
                RPG::Armor.new
              when 'Enemies'
                RPG::Enemy.new
              when 'States'
                RPG::State.new
              end
    lines.each_with_index do |line, index|
      name = line.split(' = ')
      if line.size == 1
        data << element if array
        element = case data_file
                  when 'Actors'
                    RPG::Actor.new
                  when 'Classes'
                    RPG::Class.new
                  when 'Skills'
                    RPG::Skill.new
                  when 'Items'
                    RPG::Item.new
                  when 'Weapons'
                    RPG::Weapon.new
                  when 'Armors'
                    RPG::Armor.new
                  when 'Enemies'
                    RPG::Enemy.new
                  when 'States'
                    RPG::State.new
                  end
        next
      end
      var_data = name[1].chomp
      var_data = remake_data(var_data)
      name = name[0]
      data.is_a?(RPG::System) ? data : element
      eval("(data.is_a?(RPG::System) ? data : element).#{name} = var_data")
    end
    save_data(data, 'Data/' + data_file + '.rxdata')
  end
  def remake_data(variable)
    if variable.include?('RPG::AudioFile.new(')
      variable.gsub!('RPG::AudioFile.new(', '')
      variable.gsub!(')', '')
      variable = variable.split(',')
      variable[0].gsub!(/\"/, '')
      variable = RPG::AudioFile.new(variable[0], variable[1].to_i, variable[2].to_i )
    elsif variable.include?('Words{')
      variable.gsub!('Words{', '')
      variable.gsub!('}', '')
      vars = variable.split(',')
      vars.each {|var| var.gsub!(/\"/, '')}
      variable = RPG::System::Words.new
      vars.each do |var|
        var = var.split(':')
        eval("variable.#{var[0]} = '#{var[1]}'")
      end
    elsif variable.include?('Table(')
      variable.gsub!('Table(', '')
      var_data = variable.split(')')
      var_data[0] = var_data[0].split(',')
      var_data[0][0] = var_data[0][0].to_i
      var_data[0][1] = var_data[0][1].to_i if var_data[0][1]
      variable = Table.new(*var_data[0])
      var_data[1].gsub!('[', '')
      var_data[1].gsub!(']', '')
      var_data[1] = var_data[1].split(':')
      for x in 0...var_data[0][0]
        if var_data[0][1]
          var_data[1][x] = var_data[1][x].split(',')
          var_data[1][x].each_with_index do |obj, y|
            variable[x, y] = obj.to_i
          end
        else
          variable[x - 1] = var_data[1][x - 1].to_i
        end
      end
    elsif variable.include?('[')
      variable.gsub!('[', '')
      variable.gsub!(']', '')
      vars = variable.split(',')
      variable = []
      vars.each {|new_variable| variable.push(remake_data(new_variable))}
    elsif variable.include?('Action{')
      variable.gsub!('Action{', '')
      variable.gsub!('}', '')
      vars = variable.split(';')
      vars.each {|var| var.gsub!(/\"/, '')}
      variable = RPG::Enemy::Action.new
      vars.each do |var|
        var = var.split(':')
        eval("variable.#{var[0]} = #{var[1]}.to_i")
      end
    elsif variable.include?('Learning(')
      variable.gsub!('Learning(', '')
      variable.gsub!(')', '')
      var = variable.split(':')
      variable = RPG::Class::Learning.new
      variable.level = var[0].to_i
      variable.skill_id = var[1].to_i
    elsif variable.include?('"')
      variable.gsub!(/\"/, '')
    elsif variable == 'false'
      variable = false
    elsif variable == 'true'
      variable = true
    elsif variable == 'nil'
      variable = nil
    else
      variable = variable.to_i
    end
    return variable
  end
  # Main decrypt method
  def decrypt
    # Check if 'D_Data' is not there, then make it
    unless File.directory?('D_Data')
      Dir.mkdir('D_Data')
    end
    print 'Now decrypting data.'
    decrypt_data('Actors')
    decrypt_data('Classes')
    decrypt_data('Skills')
    decrypt_data('Items')
    decrypt_data('Weapons')
    decrypt_data('Armors')
    decrypt_data('Enemies')
    decrypt_data('States')
    decrypt_data('System')
    print 'Data decryption complete.'
  end
  # Decrypt Data method
  def decrypt_data(data_file)
    data = load_data('Data/' + data_file + '.rxdata')
    file = File.open('D_Data/' + data_file + '.txt', 'w')
    if data.is_a?(Array)
      for i in 0..data.size
        next if data[i].nil?
        puts data_file + ' #' + i.to_s
        variables = data[i].instance_variables
        variables.each do |variable|
          varname = variable.delete('@')
          file.print(varname,' = ')
          object = eval("data[i].#{varname}")
          print_data(object, file)
        end
        file.print("\n")
      end
    else
      variables = data.instance_variables
      variables.each do |variable|
        varname = variable.delete('@')
        next if varname == '_'
        file.print(varname, ' = ')
        object = eval("data.#{varname}")
        print_data(object, file)
      end
    end
    file.close
  end
  def print_data(object, file, newline = true)
    case object
    when Array
      file.print('[')
      object.each_with_index do |element, index|
        print_data(element, file, false)
        file.print(',') unless index == object.size - 1
      end
      file.print(']')
    when Hash
      file.print('{')
      object.each {|k, v| file.print(k, ':', v, ',')}
      file.print('}')
    when Numeric
      file.print(object)
    when String
      file.print('"' + object + '"')
    when TrueClass
      file.print('true')
    when FalseClass
      file.print('false')
    when NilClass
      file.print('nil')
    when RPG::AudioFile
      file.print('RPG::AudioFile.new(')
      file.print('"', object.name, '",', object.volume, ',', object.pitch, ')')
    when Table
      file.print('Table(', object.xsize)
      if object.ysize > 1
        file.print(',', object.ysize)
      end
      file.print(')')
      file.print('[')
      object.xsize.times do |x|
       
        if object.ysize != 1
          object.ysize.times do |y|
            print_data(object[x, y], file, false)
            file.print(',') unless y == object.ysize - 1
          end
        else
          print_data(object[x], file, false)
        end
        file.print(':') unless x == object.xsize - 1
       
      end
      file.print(']')
    when RPG::System::Words
      file.print('Words{')
      file.print('"gold":"', object.gold, '",')
      file.print('"hp":"', object.hp, '",')
      file.print('"sp":"', object.sp, '",')
      file.print('"str":"', object.str, '",')
      file.print('"dex":"', object.dex, '",')
      file.print('"agi":"', object.agi, '",')
      file.print('"int":"', object.int, '",')
      file.print('"atk":"', object.atk, '",')
      file.print('"pdef":"', object.pdef, '",')
      file.print('"mdef":"', object.mdef, '",')
      file.print('"weapon":"', object.weapon, '",')
      file.print('"armor1":"', object.armor1, '",')
      file.print('"armor2":"', object.armor2, '",')
      file.print('"armor3":"', object.armor3, '",')
      file.print('"armor4":"', object.armor4, '",')
      file.print('"attack":"', object.attack, '",')
      file.print('"skill":"', object.skill, '",')
      file.print('"guard":"', object.guard, '",')
      file.print('"item":"', object.item, '",')
      file.print('"equip":"', object.equip, '"}')
    when RPG::Class::Learning
      file.print('Learning(', object.level.to_s, ':', object.skill_id.to_s, ')')
    when RPG::Enemy::Action
      file.print('Action{')
      file.print('"kind":', object.kind, ';')
      file.print('"basic":', object.basic, ';')
      file.print('"skill_id":', object.skill_id, ';')
      file.print('"condition_turn_a":', object.condition_turn_a, ';')
      file.print('"condition_turn_b":', object.condition_turn_b, ';')
      file.print('"condition_hp":', object.condition_hp, ';')
      file.print('"condition_level":', object.condition_level, ';')
      file.print('"condition_switch_id":', object.condition_switch_id, ';')
      file.print('"rating":', object.rating, '}')
    end
    file.print("\n") if newline
  end
end
Expand to see the code.


Instructions

Place this script directly above Main. Call it with $scene = Scene_DataManager.new
If running in test play, this the encryption will not work. Run Game.exe if you want to encrypt the data. This script will only do Actors, Classes, Skills, Items, Weapons, Armor, Enemies, and the System data. Other data is either too complex or too difficult to write down to use.

FAQ



Credits and Thanks

Thanks to Trickster who help fix a few bugs.

Author's Notes

Because this script overwrites the original data files, I strongly recommend that you make a backup of them.
I donÂÂ ´t quite understand the use of this script... Is that made for the scripter to change the rxdata files by hand and encrypt again?

And you have a nice scripting style too, btw.
That's the point, Linkin_T. Because you can change what values you want by hand without being in RMXP, you won't be forced to be stopped by "Must be an integer between 1 and 999".
Good point. I had another idea too, you can *maybe* change the database during the game and save it. And whether the user tries to load a saved file it will load the modified database. Think about that Virtual Database, that SS did, just more effective.

But, to use those encrypt and decrypt methods you need to call the Scene, and in this idea we need to call that encrypt/decrypt methods. What iÂÂ ´m trying to say is... Why not putting encrypt and decrypt as class methods, or make them inside a module, for general use?
Thanks for stealing my idea, IDEA THIEF :P

Quote:
Thanks to Trickster who help fix a few bugs.


more like also told me which files would be easy to decrypt/encrypt

You forgot to mention this

Only works for Actors, Classes, Skills, Items, Weapons, Armor, Enemies, and the System objects. The others were either too hard, too hard to configure after decryption.

Actually this has its uses as Yeyinde Stated Above you will not be stopped by the message "Must be an integer between 1 and 999"
Slow down Trickster :)

I see how this can be useful, my friend Neopilos was talking about this the other day. Just a question - you edit it outside of RPGXP, then try to edit it inside. What happens?
That depends if you've encrypted the data, and what data you've changed. The database might warn you with the "Must be between 1 and 999" deal, or it may just ignore the change like in this screen:
http://i12.photobucket.com/albums/a229/ ... azyEXP.png[/IMG]
...

save_data() method anyone?
It is used, Prexus. save_data(data, 'Data/' + data_file + '.rxdata')
[QUOTE=Prexus;124972]...
save_data() method anyone?[/QUOTE]
Ive said that in the screenshot thread yeah... :thumb:

Oh well, just make a script that allows us to use 1000+ maps. (not that hard actually.. i guess)
Wow... I tested it, 2018 enemies. This script is extremely useful =)
I"m not seeing the changes at all.
witch should you do first decrypt or encrypt?

And another thing is that I want the final level to be 200
but it's still saying integer must be between 1 and 99.
How dose this work because I'm lost.
1. Decrypt the data
2. Edit the final_level of the actor in Actors.txt
3. Change parameters to accommodate the the level difference
4. Encrypt the data (Run Game.exe for this, not testplay)
were would the level difference be located in the editable document?
Depends on what actor you want. All of the editable files, except for System, are in this format:
Data1

Data2

Data3

etc...

Expand to see the code.
I'm getting all kinds of errors in Game_Actor when I change the level maximum to 500 for all of my actors, help please?:)
[QUOTE=Marcel_Makkink;128603]I'm getting all kinds of errors in Game_Actor when I change the level maximum to 500 for all of my actors, help please?:)[/QUOTE]


No, No, No. If you increase the Maximum Level, then you will have to add values to the stat table and exp tables. Your Best Bet is to get my Stats that Level Up script (there is an option to change the maximum level) and go from there

Also tell us what types of errors you are getting
Quote:
This is a script to overcome some of RMXP's limitations.


What are they, exactly? So far, I only know "Must be an integer between 1 and 999" (since I only met that warning)...
The Number of Party members at the start of the game (Though if you add more you can't edit the extra members in the database)

Other Fields have a limit to how long the string can be (as in skill names) and you can override that was as well.

I think that is it
[QUOTE=Trickster;128616] Your Best Bet is to get my Stats that Level Up script (there is an option to change the maximum level) and go from there
[/QUOTE]

TnQ Trickster;)

It still lets me define every exp. needed for every level :O (with RPG Maker's experience table script thingy) , so godly:D.

Edit : Trickster, when I reach the final level of any actor my game gives an error. It's not one of your scripts but its Drago del fato's Ability Point Spend System, although it must have something to do with one of your scripts. It's not a very big problem since no one will ever reach level 251 in my game but anyways.

????? 'Ability Point Spend System' ? 89 ??? ArgumentError ????????

comparison of Bignum with nil failed
Where do you place the %scene = Scene_DataManager
?
In a Script... call, and it's $scene = Scene_DataManager.new
Sorry, but which script do I place the call in then?
Script 'Data Editor' line 237: Errno::EBADF occurred.

Bad file descriptor.

Line: " puts data_file + ' #' + i.to_s"

Both in and out of test play.
If you could also extend this to States that would rock. :D Ofcourse I need it to work first.
EDIT: oh wait... you do appear to do that, but it crashes for me at Enemies.
Could this be used for potentially adding new stats without having to ridiculous round-about methods.

Edit: I get the same edit as the poster above. Exactly the same.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=11586