Advertisement
ForeverZer0

[RMXP] RunTime Script Caller

May 21st, 2011
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.35 KB | None | 0 0
  1. #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. # Run-Time Script Caller
  3. # Author: ForeverZer0
  4. # Version: 1.0
  5. # Date: 11.27.2010
  6. #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  7. #
  8. # Introduction:
  9. #
  10. #  This script will allow you to open a small box while the game is running and
  11. #  type script calls into, which will execute when the Enter button is pressed.
  12. #
  13. # Feature:
  14. #
  15. #  - Simple to use.
  16. #  - Built in rescue to keep game from crashing if the script call is written
  17. #    wrong, etc. Instead it shows the error and continues on.
  18. #  - Did I mention you can make up script calls and change things at run-time.
  19. #
  20. # Instructions:
  21. #
  22. #  - Place script anywhere.
  23. #  - Configure call button below (F7 by default)
  24. #
  25. #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  26.  
  27. #===============================================================================
  28. # ** ScriptEditor
  29. #===============================================================================
  30.  
  31. class ScriptEditor
  32.  
  33.   CALL_BUTTON = Input::F7
  34.   # Set the button to call the script box.
  35.  
  36.   def initialize
  37.     # Get game window title from Game.ini
  38.     ini = Win32API.new('kernel32', 'GetPrivateProfileString','PPPPLP', 'L')
  39.     @title = "\0" * 256
  40.     ini.call('Game', 'Title', '', @title, 256, '.\\Game.ini')
  41.     @title.delete!("\0")
  42.     # Set game window to an instance variable, using the title we found.
  43.     @main = Win32API.new('user32', 'FindWindowA', 'PP', 'L').call('RGSS Player', @title)
  44.     # Set variables to call for creating showing, and destroying the window.
  45.     @create_window = Win32API.new('user32','CreateWindowEx','lpplllllllll','l')
  46.     @show_window = Win32API.new('user32','ShowWindow',%w(l l),'l')
  47.     @destroy_window = Win32API.new('user32','DestroyWindow','p','l')
  48.     # Set variables to get the window size, position, text, etc, etc.
  49.     @window_text = Win32API.new('user32','GetWindowText',%w(n p n ),'l')
  50.     @metrics = Win32API.new('user32', 'GetSystemMetrics', 'I', 'I')
  51.     @set_window_pos = Win32API.new('user32', 'SetWindowPos', 'LLIIIII', 'I')
  52.     @window_rect = Win32API.new('user32','GetWindowRect',%w(l p),'i')
  53.     # Define the coordinates to display the window.
  54.     @x = (@metrics.call(0) - 576) / 2
  55.     @y = (@metrics.call(1) - 22) / 2
  56.     # Updates the client area of the window.
  57.     @update_window = Win32API.new('user32','UpdateWindow','p','i')
  58.     # Set a button that will register when button is pressed.
  59.     @input = Win32API.new('user32','GetAsyncKeyState','i','i')
  60.   end  
  61.  
  62.   def destroy_window
  63.     # Disposes the created box for typing text, and sets variable to nil.
  64.     @destroy_window.call(@script_window)
  65.     @script_window = nil
  66.   end
  67.    
  68.   def show_window
  69.     # End method now if window is already there.
  70.     if @script_window != nil
  71.       return
  72.     end
  73.     # Create text box for adding text into, using window dimensions.
  74.     @script_window = @create_window.call(768, 'Edit', '', 0x86000000, @x, @y, 576, 22, @main, 0, 0, 0)
  75.     # Set the 'visibility' of the window.
  76.     @show_window.call(@script_window, 1)
  77.     # Begin the loop for the text box.
  78.     start_script_update
  79.   end  
  80.  
  81.   def start_script_update
  82.     # Enter update loop.
  83.     loop {
  84.       # Update the Graphics, and the window. Breaks when Enter button is pressed.
  85.       Graphics.update
  86.       @update_window.call(@script_window)
  87.       break if @input.call(0x0D) & 0x01 == 1
  88.     }
  89.     # Get the text from the window.
  90.     text = "\0" * 256
  91.     @window_text.call(@script_window, text, 0x3E80)
  92.     text.delete!("\0")
  93.     # Evaluate the text, simply displaying a message if it throws an error.
  94.     begin
  95.       eval(text)
  96.     rescue#'start_script_update'
  97.       # Simply write the type of the error.
  98.       message = $!.message.gsub("(eval):1:in `start_script_update'") { '' }
  99.       print("#{$!.class}\r\n\r\n#{message}")
  100.     end
  101.     destroy_window
  102.   end
  103. end
  104.  
  105. if $DEBUG
  106.  
  107.   $editor = ScriptEditor.new
  108.   # Create in instance of the class so it is ready to call.
  109.  
  110.   module Input
  111.    
  112.     class << self
  113.       alias zer0_script_editor_upd update
  114.     end
  115.    
  116.     def self.update
  117.       # Alias the Input.update method to check if call button is pressed.
  118.       $editor.show_window if self.trigger?(ScriptEditor::CALL_BUTTON)
  119.       zer0_script_editor_upd
  120.     end
  121.   end
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement