Read from and write to .ini snippet

Started by Zeriab, June 10, 2009, 05:36:52 am

Previous topic - Next topic

Zeriab

June 10, 2009, 05:36:52 am Last Edit: June 10, 2009, 10:52:42 am by Zeriab
Here is a small Utility module which provides method for reading from and writing to .ini files.

module Utility
  #############
  # DLL STUFF #
  #############
  READ_INI         = Win32API.new('kernel32',  'GetPrivateProfileStringA',
                                  %w(p p p p l p), 'l')
  WRITE_INI        = Win32API.new('kernel32',  'WritePrivateProfileStringA',
                                  %w(p p p p), 'l')
  ##
  # Read from system ini
  #
  def self.read_ini(key_name, app_name = 'Game', filename = 'Game.ini',
                    buffer_size = 256, default = '')
    buffer = "\0" * buffer_size
    READ_INI.call(app_name, key_name, default, buffer, buffer_size - 1,
                  ".\\" + filename)
    return buffer.delete("\0")
  end
 
  ##
  # Write to system ini
  #
  def self.write_ini(key_name, value, app_name = 'Game', filename = 'Game.ini')
    return WRITE_INI.call(app_name, key_name, value.to_s, ".\\" + filename)
  end
end


This can for example be used to store configuration information in Game.ini instead of storing it in a separate file. Configuration data the player should be able to change that is.
You can of course also mess around with the already existing data in Game.ini.

If you for example want to read the title of the game you can do that with:
Utility.read_ini('Title')


If you want to save that Windowskin number 3 is used you can do that with:
Utility.write_ini('Windowskin', '3')


Now you may want that setting to be save specific and you should not write save specific details to the .ini file. You can of course, but you really should think it through before doing so.
A global setting is whether to start the game up in fullscreen or not which for example could be done with:
Utility.write_ini('Fullscreen', '1', 'Window')


Notice the new argument I have put there.
Try and see what the effects are in the ini file.
There are also other arguments which I have not covered which you can experiment with.
Just note that the values are strings

*hugs*
- Zeriab

Blizzard

I was just going to say "Why don't you use the API calls?", but then I saw your code. xD *levels up* <3
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zeriab

XD. All I've done is just to wrap the API calls in :P
<3

fugibo

Methinks you should rearrange the arguments, it's too much hassle to specify buffer size etc. when all you want to do is save to a different file :/

Zeriab


fugibo

*levels up nowz*

This is definitely one of the highest-potential scripts for RMXP, solely because it allows for the user to have complete control over any configuration that supports it. The fact that it can be used for global configuration (as opposed to per-save configuration) is also very useful.

*hearts*

Zeriab

Thanks for your comments WcW :3
I don't know what I was thinking with putting the filename after the buffer size >_<

Note that you can change the title of the game if you want. (requires restart)
I agree that the biggest strength is that users can open the ini-file and modify it themselves.

*hugs*