#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# DREAM controller for Music Files by Blizzard
# Version: 1.03b
# Type: Encrpytor / Decryptor / Audio File Player
# uses DREAM v4.0 or higher
# Date: 19.8.2007
# Date v1.0b: 22.8.2007
# Date v1.01b: 27.8.2007
# Date v1.02b: 24.9.2007
# Date v1.03b: 14.12.2007
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
#   
#  This work is protected by the following license:
# #----------------------------------------------------------------------------
# #  
# #  Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported
# #  ( http://creativecommons.org/licenses/by-nc-sa/3.0/ )
# #  
# #  You are free:
# #  
# #  to Share - to copy, distribute and transmit the work
# #  to Remix - to adapt the work
# #  
# #  Under the following conditions:
# #  
# #  Attribution. You must attribute the work in the manner specified by the
# #  author or licensor (but not in any way that suggests that they endorse you
# #  or your use of the work).
# #  
# #  Noncommercial. You may not use this work for commercial purposes.
# #  
# #  Share alike. If you alter, transform, or build upon this work, you may
# #  distribute the resulting work only under the same or similar license to
# #  this one.
# #  
# #  - For any reuse or distribution, you must make clear to others the license
# #    terms of this work. The best way to do this is with a link to this web
# #    page.
# #  
# #  - Any of the above conditions can be waived if you get permission from the
# #    copyright holder.
# #  
# #  - Nothing in this license impairs or restricts the author's moral rights.
# #  
# #----------------------------------------------------------------------------
# 
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=
# 
#   DO TOUCH THE VALUE BELOW! If you change the required version to a lower
#   value your script WILL malfunction, because older DREAM version don't have
#   all the methods and functions required by this script.
Required_DREAM_Version = 4.0
# 
# 
# Compatibility:
# 
#   98% compatible with SDK 1.x. 90% compatible with SDK 2.x. 99% compatible
#   with everything else. WILL corrupt your old savegames. Can cause
#   incompatibility issues with custom CMSes that change music playing methods
#   in Game_System. NEEDS DREAM of the provided version or higher. Files will
#   be saved into "DREAM Encrypted Audio" format with .dea extension.
# 
# 
# Features:
# 
#   - encrypts your Audio files in a way, so you can encrypt them together with
#     Data Files and decrease the chance of music theft
#   - decrypts and plays the .dea files
# 
# new in v1.0b:
#   - rewritten conditions using classic syntax to avoid RGSS conditioning bug
#   - now beta
# 
# new in v1.01b:
#   - compatible with StromTronics CMS v5.0b and higher
# 
# new in v1.02b:
#   - improved coding
# 
# new in v1.03b:
#   - fixed a bbug cause by a typing mistake
# 
# 
# Configuration:
# 
#   If you set ENCRYPT_AUDIO to true the script will encrypt the audio files
#   you have specified in AUDIO_FOR_ENCRYPTION and exit the application. If you
#   have set ENCRYPT_AUDIO to false, you can play the game normally and the
#   script will play the newly created files. All files go into the Data folder
#   organized as if they were in the normal Audio folders.
#   
#   Examples:
#   
#   - old locations and names:
#     Audio/BGM/Outskirts.mp3
#     Audio/BGS/Blizzard.ogg
#     Audio/ME/Victory.mid
#     Audio/SE/Explosion.wav
# 
#   - new locations and names:
#     Data/Audio/BGM/Outskirts.dea
#     Data/Audio/BGS/Blizzard.dea
#     Data/Audio/ME/Victory.dea
#     Data/Audio/SE/Explosion.dea
# 
#   Put all files in the main game folder if you want to encrypt them.
# 
# 
# IMPORTANT NOTES:
# 
#   This script will encrypt audio files and save it into .dea format. A file
#   in .dea format can't be player by any normal media anymore, so be don't
#   delete your old files in case you want to edit them or anything similar.
#   If you use this script without having encrypted all your .dea files, your
#   game will crash.
# 
# 
# If you find any bugs, please report them here:
# http://forum.chaos-project.com
#:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

# turn on to create .dea files
ENCRYPT_AUDIO = false
# include audio file names WITH extension
AUDIO_FOR_ENCRYPTION = ['001-Battle01.mid']

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

# ensures compatiblity
$dream_music = true

#==============================================================================
# DREAM_ext controller
#==============================================================================

module DREAM
  
  def self.self_extract
    data = load_data('Data/DREAM_ext.rb')
    file = File.open("#{File.dirname('Game')}/DREAM_ext.rb", 'wb')
    file.write(data)
    file.close
    require File.expand_path('DREAM_ext.rb')
    self.load_DREAM
  end
  
end

DREAM.self_extract

#==============================================================================
# Audio File Encyption
#==============================================================================

if ENCRYPT_AUDIO
  AUDIO_FOR_ENCRYPTION.each {|filename|
      DREAM.encrypt_audio_file(filename, '.dea file')
      Graphics.update}
  exit
end

#==============================================================================
# Game_System
#==============================================================================

class Game_System
  
  def bgm_play(bgm)
    @playing_bgm = bgm
    if bgm != nil && bgm.name != ''
      DREAM.play_encrypted_audio('Audio/BGM/', bgm, 0)
    else
      Audio.bgm_stop
    end
    Graphics.frame_reset
  end
  
  def bgs_play(bgs)
    @playing_bgs = bgs
    if bgs != nil && bgs.name != ''
      DREAM.play_encrypted_audio('Audio/BGS/', bgs, 1)
    else
      Audio.bgs_stop
    end
    Graphics.frame_reset
  end
  
  def me_play(me)
    if me != nil && me.name != ''
      DREAM.play_encrypted_audio('Audio/ME/', me, 2)
    else
      Audio.me_stop
    end
    Graphics.frame_reset
  end
  
  def se_play(se)
    if se != nil && se.name != ''
      DREAM.play_encrypted_audio('Audio/SE/', se, 3)
    end
  end
  
end
