Advertisement
Iavra

[Ace] Game.ini Access

Sep 1st, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.54 KB | None | 0 0
  1. #==========================================================================
  2. # Iavra Game.ini Access
  3. # -------------------------------------------------------------------------
  4. # - Purpose:
  5. # Be a simple utility module that can be used to read from and write to the
  6. # Game.ini file.
  7. # -------------------------------------------------------------------------
  8. # - Prerequisites:
  9. # None
  10. # -------------------------------------------------------------------------
  11. # - Terms of Use:
  12. # Free to use for both commercial and non-commercial games. Please give credit.
  13. # -------------------------------------------------------------------------
  14. # - How to Use:
  15. # Place above all scripts, that should take use of it.
  16. #
  17. # Check the presence of the script like this:
  18. #
  19. # if(($imported ||= {})[:iavra_ini_access])
  20. #       # use the script
  21. # else
  22. #       # do something else
  23. # end
  24. #
  25. # Load/write a key in the ini file:
  26. #
  27. # IAVRA::INI.load("section name", "key name", "default value")
  28. # IAVRA::INI.save("section name", "key name", "value to be saved")
  29. # -------------------------------------------------------------------------
  30. # - FAQ:
  31. # None so far
  32. # -------------------------------------------------------------------------
  33. # - Credits:
  34. # Iavra
  35. #==========================================================================
  36.  
  37. ($imported ||= {})[:iavra_ini_access] = true
  38.  
  39. module IAVRA
  40.     module INI
  41.        
  42.         #==========================================================================
  43.         # Path to the ini file. Shouldn't need to be changed.
  44.         #==========================================================================
  45.        
  46.         FILENAME = "./Game.ini"
  47.        
  48.         #==========================================================================
  49.         # The Win API functions used to read and write ini files.
  50.         #==========================================================================
  51.        
  52.         GetPrivateProfileString = Win32API.new('kernel32', 'GetPrivateProfileString', 'ppppip', 'i')
  53.         WritePrivateProfileString = Win32API.new('kernel32', 'WritePrivateProfileString', 'pppp', 'i')
  54.        
  55.         #==========================================================================
  56.         # Wrapper methods for the Win API.
  57.         #==========================================================================
  58.        
  59.         def self.load(section, key, default)
  60.             buffer = [].pack("x256")
  61.             l = GetPrivateProfileString.call(section, key, default, buffer, buffer.size, FILENAME)
  62.             buffer[0, l]
  63.         end
  64.        
  65.         def self.save(section, key, value)
  66.             WritePrivateProfileString.call(section, key, value.to_s, FILENAME)
  67.         end
  68.        
  69.     end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement