Advertisement
Iavra

[Ace] Game Localization - Database Module

May 28th, 2015
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.98 KB | None | 0 0
  1. #==============================================================================
  2. # Iavra Game Localization - Database Module 1.00
  3. # -----------------------------------------------------------------------------
  4. # Description:
  5. # Allows the localization of database entries. Supports the following entries:
  6. # - Actors, classes, skills, items, weapons, armors, enemies, states
  7. # - System, terms, vocab
  8. # - map display names
  9. # -----------------------------------------------------------------------------
  10. # Prerequisites:
  11. # Iavra Game Localization - Core Engine
  12. # -----------------------------------------------------------------------------
  13. # How to Use:
  14. # Language files are built after the following formats:
  15. #
  16. # - Actors, classes, skills, items, weapons, armors, enemies, states
  17. #
  18. # {
  19. #   :actors => {
  20. #     1 => {
  21. #       :name => "Name of actor 1"
  22. #     }
  23. #   }
  24. # }
  25. #
  26. # - System, terms, vocab:
  27. #
  28. # {
  29. #   :system => {
  30. #     :game_title => "Game title"
  31. #   }
  32. # }
  33. #
  34. # - Map display names:
  35. #
  36. # {
  37. #   :maps => {
  38. #     1 => "Name of map 1"
  39. #   }
  40. # }
  41. #
  42. # If an entry is not localized, it will be set to its default value, instead.
  43. # The default value is whatever was set in VX Ace.
  44. # -----------------------------------------------------------------------------
  45. # Terms of Use:
  46. # Free to use for both commercial and non-commercial games. Please give credit.
  47. # -----------------------------------------------------------------------------
  48. # Credits:
  49. # Iavra
  50. # -----------------------------------------------------------------------------
  51. # Changelog:
  52. # - 1.00: Release version.
  53. #==============================================================================
  54.  
  55. ($imported ||= {})[:iavra_i18n_database] = true
  56.  
  57. fail "This module needs the core engine to function." unless $imported[:iavra_i18n_core]
  58.  
  59. #==============================================================================
  60. # ▼ IAVRA::I18N::DATABASE
  61. #==============================================================================
  62.  
  63. module IAVRA
  64.     module I18N
  65.         module DATABASE
  66.            
  67.             #========================================================================
  68.             # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  69.             #========================================================================
  70.            
  71.             #========================================================================
  72.             # Since actors are cached, we have to localize the values directly inside
  73.             # of $game_actors. Setting this to true will revert those entries back
  74.             # to default before saving and re-localize them afterwards, leaving no
  75.             # traces in your savefiles.
  76.             #========================================================================
  77.            
  78.             CLEAN_ACTORS = false
  79.            
  80.             #========================================================================
  81.             # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  82.             #========================================================================
  83.            
  84.             #========================================================================
  85.             # The attributes of $data_actors, $data_classes, $data_skills, $data_items,
  86.             # $data_weapons, $data_armors, $data_enemies and $data_states, that should
  87.             # be localized.
  88.             #========================================================================
  89.            
  90.             CONFIG_DATA = {
  91.                 :actors => {
  92.                     :@name => :name,
  93.                     :@nickname => :nickname,
  94.                     :@description => :description
  95.                 },
  96.                 :classes => {
  97.                     :@name => :name
  98.                 },
  99.                 :skills => {
  100.                     :@name => :name,
  101.                     :@description => :description,
  102.                     :@message1 => :message1,
  103.                     :@message2 => :message2
  104.                 },
  105.                 :items => {
  106.                     :@name => :name,
  107.                     :@description => :description
  108.                 },
  109.                 :weapons => {
  110.                     :@name => :name,
  111.                     :@description => :description
  112.                 },
  113.                 :armors => {
  114.                     :@name => :name,
  115.                     :@description => :description
  116.                 },
  117.                 :enemies => {
  118.                     :@name => :name
  119.                 },
  120.                 :states => {
  121.                     :@name => :name,
  122.                     :@description => :description,
  123.                     :@message1 => :message1,
  124.                     :@message2 => :message2,
  125.                     :@message3 => :message3,
  126.                     :@message4 => :message4
  127.                 }
  128.             }
  129.            
  130.             #========================================================================
  131.             # The attributes of $data_system, that should be localized.
  132.             #========================================================================
  133.            
  134.             CONFIG_SYSTEM_CAT = :system
  135.             CONFIG_SYSTEM = {
  136.                 :@game_title => :game_title,
  137.                 :@currency_unit => :currency_unit,
  138.                 :@elements => :elements,
  139.                 :@skill_types => :skill_types,
  140.                 :@weapon_types => :weapon_types,
  141.                 :@armor_types => :armor_types
  142.             }
  143.            
  144.             #========================================================================
  145.             # The attributes of $data_system.terms, that should be localized.
  146.             #========================================================================
  147.            
  148.             CONFIG_TERMS_CAT = :terms
  149.             CONFIG_TERMS = {
  150.                 :@basic => {
  151.                     0 => :basic_level,
  152.                     1 => :basic_level_short,
  153.                     2 => :basic_hp,
  154.                     3 => :basic_hp_short,
  155.                     4 => :basic_mp,
  156.                     5 => :basic_mp_short,
  157.                     6 => :basic_tp,
  158.                     7 => :basic_tp_short
  159.                 },
  160.                 :@params => {
  161.                     0 => :param_mhp,
  162.                     1 => :param_mmp,
  163.                     2 => :param_atk,
  164.                     3 => :param_def,
  165.                     4 => :param_matk,
  166.                     5 => :param_mdef,
  167.                     6 => :param_agi,
  168.                     7 => :param_luck
  169.                 },
  170.                 :@etypes => {
  171.                     0 => :etype_weapon,
  172.                     1 => :etype_shield,
  173.                     2 => :etype_head,
  174.                     3 => :etype_body,
  175.                     4 => :etype_accessory
  176.                 },
  177.                 :@commands => {
  178.                     0 => :command_fight,
  179.                     1 => :command_escape,
  180.                     2 => :command_attack,
  181.                     3 => :command_guard,
  182.                     4 => :command_item,
  183.                     5 => :command_skill,
  184.                     6 => :command_equip,
  185.                     7 => :command_status,
  186.                     8 => :command_formation,
  187.                     9 => :command_save,
  188.                     10 => :command_game_end,  
  189.                     12 => :command_weapon,
  190.                     13 => :command_armor,
  191.                     14 => :command_key_item,
  192.                     15 => :command_change_equip,
  193.                     16 => :command_optimize_equip,
  194.                     17 => :command_clear_equip,
  195.                     18 => :command_new_game,
  196.                     19 => :command_continue,
  197.                     20 => :command_shutdown,
  198.                     21 => :command_to_title,
  199.                     22 => :command_cancel
  200.                 }
  201.             }
  202.            
  203.             #========================================================================
  204.             # The constants inside the Vocab module, that should be localized.
  205.             #========================================================================
  206.            
  207.             CONFIG_VOCAB_CAT = :vocab
  208.             CONFIG_VOCAB = {
  209.                 :ShopBuy => :shop_buy,
  210.                 :ShopSell => :shop_sell,
  211.                 :ShopCancel => :shop_cancel,
  212.                 :Possession => :possession,
  213.                 :ExpTotal => :exp_total,
  214.                 :ExpNext => :exp_next,
  215.                 :SaveMessage => :save_message,
  216.                 :LoadMessage => :load_message,
  217.                 :File => :file,
  218.                 :PartyName => :party_name,
  219.                 :Emerge => :emerge,
  220.                 :Preemptive => :preemptive,
  221.                 :Surprise => :surprise,
  222.                 :EscapeStart => :escape_start,
  223.                 :EscapeFailure => :escape_failure,
  224.                 :Victory => :victory,
  225.                 :Defeat => :defeat,
  226.                 :ObtainExp => :obtain_exp,
  227.                 :ObtainGold => :obtain_gold,
  228.                 :ObtainItem => :obtain_item,
  229.                 :LevelUp => :level_up,
  230.                 :ObtainSkill => :obtain_skill,
  231.                 :UseItem => :use_item,
  232.                 :CriticalToEnemy => :critical_to_enemy,
  233.                 :CriticalToActor => :critical_to_actor,
  234.                 :ActorDamage => :actor_damage,
  235.                 :ActorRecovery => :actor_recovery,
  236.                 :ActorGain => :actor_gain,
  237.                 :ActorLoss => :actor_loss,
  238.                 :ActorDrain => :actor_drain,
  239.                 :ActorNoDamage => :actor_no_damage,
  240.                 :ActorNoHit => :actor_no_hit,
  241.                 :EnemyDamage => :enemy_damage,
  242.                 :EnemyRecovery => :enemy_recovery,
  243.                 :EnemyGain => :enemy_gain,
  244.                 :EnemyLoss => :enemy_loss,
  245.                 :EnemyDrain => :enemy_drain,
  246.                 :EnemyNoDamage => :enemy_no_damage,
  247.                 :EnemyNoHit => :enemy_no_hit,
  248.                 :Evasion => :evasion,
  249.                 :MagicEvasion => :magic_evasion,
  250.                 :MagicReflection => :magic_reflection,
  251.                 :CounterAttack => :counter_attack,
  252.                 :Substitute => :substitute,
  253.                 :BuffAdd => :buff_add,
  254.                 :DebuffAdd => :debuff_add,
  255.                 :BuffRemove => :buff_remove,
  256.                 :ActionFailure => :action_failure
  257.             }
  258.             #========================================================================
  259.             # The category for map ids whose display names should be localized.
  260.             #========================================================================
  261.            
  262.             CONFIG_MAPS_CAT = :maps
  263.            
  264.             #========================================================================
  265.             # Creates backups of the values values to use if no localized value can
  266.             # be found.
  267.             #========================================================================
  268.            
  269.             def self.initialize_base_values
  270.                 CONFIG_DATA.each{|name, attrs|
  271.                     eval("$data_#{name}").compact.each{|data|
  272.                         base = Hash[attrs.keys.map{|attr| [attr, data.instance_variable_get(attr)]}]
  273.                         data.iavra_i18n_database_base = base
  274.                     }
  275.                 }
  276.                
  277.                 base = {}
  278.                 CONFIG_SYSTEM.each{|k, v|
  279.                     base[k] = $data_system.instance_variable_get(k) || ""
  280.                 }
  281.                 $data_system.iavra_i18n_database_base = base   
  282.                
  283.                 base = Hash[CONFIG_TERMS.keys.map{|name| [name, $data_system.terms.instance_variable_get(name)]}]
  284.                 $data_system.terms.iavra_i18n_database_base = base
  285.                
  286.                 @vocab_base = Hash[CONFIG_VOCAB.keys.map{|symbol| [symbol, Vocab.const_get(symbol)]}]
  287.             end
  288.            
  289.             #========================================================================
  290.             # Updates all localized values whenever the current language is changed.
  291.             #========================================================================
  292.            
  293.             def self.update_data
  294.                 CONFIG_DATA.each{|name, attrs|
  295.                     eval("$data_#{name}").compact.each{|data|
  296.                         attrs.each{|attr, symbol|
  297.                             value = (IAVRA::I18N[name][data.id] || {})[symbol] || data.iavra_i18n_database_base[attr]
  298.                             data.instance_variable_set(attr, value)
  299.                         }
  300.                     }
  301.                 }
  302.             end
  303.            
  304.             def self.update_system
  305.                 CONFIG_SYSTEM.each{|k, v|
  306.                     base = $data_system.iavra_i18n_database_base[k].clone
  307.                     config = IAVRA::I18N[CONFIG_SYSTEM_CAT][v]
  308.                     if(base.is_a?(Array))
  309.                         base = base.map.with_index{|data, id| (config || {})[id] || data}
  310.                     else
  311.                         base = config || base
  312.                     end
  313.                     $data_system.instance_variable_set(k, base)
  314.                 }
  315.             end
  316.            
  317.             def self.update_terms              
  318.                 CONFIG_TERMS.each{|name, attrs|
  319.                     value = $data_system.terms.instance_variable_get(name).each_index.map{|id|
  320.                         IAVRA::I18N[CONFIG_TERMS_CAT][attrs[id]] || $data_system.terms.iavra_i18n_database_base[name][id]
  321.                     }
  322.                     $data_system.terms.instance_variable_set(name, value)
  323.                 }
  324.             end
  325.            
  326.             def self.update_vocab
  327.                 CONFIG_VOCAB.each{|name, symbol|
  328.                     Vocab.send(:remove_const, name) if Vocab.const_defined?(name)
  329.                     Vocab.const_set(name, IAVRA::I18N[CONFIG_VOCAB_CAT][symbol] || @vocab_base[name])
  330.                 }
  331.             end
  332.            
  333.             def self.update_actors
  334.                 $data_actors.each_with_index{|actor, id|
  335.                     next if $game_actors[id].nil?
  336.                     (CONFIG_DATA[:actors] || {}).each {|k, v|
  337.                         $game_actors[id].instance_variable_set(k, actor.instance_variable_get(k))
  338.                     }
  339.                 } unless $game_actors.nil?
  340.             end
  341.            
  342.         end
  343.        
  344.         #==========================================================================
  345.         # Registering our initialize and update methods with the core engine.
  346.         #==========================================================================
  347.        
  348.         class << self
  349.             alias :iavra_i18n_database_initialize :initialize
  350.             alias :iavra_i18n_database_update :update
  351.         end
  352.        
  353.         def self.initialize
  354.             iavra_i18n_database_initialize
  355.             DATABASE.initialize_base_values
  356.         end
  357.        
  358.         def self.update
  359.             iavra_i18n_database_update
  360.             DATABASE.update_data
  361.             DATABASE.update_system
  362.             DATABASE.update_terms
  363.             DATABASE.update_vocab
  364.             DATABASE.update_actors
  365.         end
  366.        
  367.     end
  368. end
  369.  
  370. #==============================================================================
  371. # ▼ RPG::BaseItem, RPG::System, RPG::System::Terms
  372. #==============================================================================
  373.  
  374. ["RPG::BaseItem", "RPG::System", "RPG::System::Terms"].each{|cls| eval %Q{
  375.     class #{cls}; attr_accessor :iavra_i18n_database_base; end
  376. }}
  377.  
  378. #==============================================================================
  379. # ▼ DataManager
  380. #==============================================================================
  381.  
  382. module DataManager
  383.    
  384.     class << self
  385.         alias :iavra_i18n_database_extract_save_contents :extract_save_contents
  386.     end
  387.    
  388.     #============================================================================
  389.     # If the language is changed before a game is loaded, the change wouldn't
  390.     # carry over to $game_actors. This call fixes that.
  391.     #============================================================================
  392.    
  393.     def self.extract_save_contents(*args)
  394.         iavra_i18n_database_extract_save_contents(*args)
  395.         IAVRA::I18N::DATABASE.update_actors
  396.     end
  397.    
  398.     #============================================================================
  399.     # If CLEAN_ACTORS is set to true, this will revert the localizing done to
  400.     # $game_actors before saving and immediately relocalize it afterwards.
  401.     #============================================================================
  402.    
  403.     if(IAVRA::I18N::DATABASE::CLEAN_ACTORS)
  404.        
  405.         class << self
  406.             alias :iavra_i18n_database_save_game :save_game
  407.         end
  408.        
  409.         def self.save_game(*args)
  410.             $data_actors.each_with_index{|actor, id|
  411.                 next if $game_actors[id].nil?
  412.                 (IAVRA::I18N::DATABASE::CONFIG_DATA[:actors] || {}).each {|k, v|
  413.                     $game_actors[id].instance_variable_set(k, actor.iavra_i18n_database_base[k])
  414.                 }
  415.             }
  416.             iavra_i18n_database_save_game(*args)
  417.             IAVRA::I18N::DATABASE.update_actors
  418.         end
  419.        
  420.     end
  421.    
  422. end
  423.  
  424. #==============================================================================
  425. # ▼ Game_Map
  426. #==============================================================================
  427.  
  428. class Game_Map
  429.    
  430.     alias :iavra_i18n_database_display_name :display_name
  431.    
  432.     def display_name
  433.     IAVRA::I18N[IAVRA::I18N::DATABASE::CONFIG_MAPS_CAT][@map_id] || iavra_i18n_database_display_name
  434.   end
  435.    
  436. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement