AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Settings API (load/save data to INI files) (https://forums.alliedmods.net/showthread.php?t=243202)

MeRcyLeZZ 07-01-2014 06:47

Settings API (load/save data to INI files)
 
2 Attachment(s)
Settings API

API to load/save settings in a Key+Value format that resembles Windows INI files (http://en.wikipedia.org/wiki/INI_file).

What's new is you can store a variable amount of values in a single key, supporting the use of amxx dyamic arrays.

Use Case:

You want your plugin to use custom sounds and models, then allow users to change them. Previously it would require coding a section into the .sma file, making recompile necessary every time you want to change something. This API allows resources to be edited "externally" through the INI file instead.

Example:

Load admin knife models from file, store them in dynamic array. If section/key is not found, a new entry will be added automatically.
Code:
#define SETTINGS_FILE "plugin_resources.ini" #define MODEL_MAX_LENGTH 64 new const default_model_vknife[] = { "models/v_knife.mdl" } // Default model new Array:g_array_models_admin_vknife public plugin_precache() {     g_array_models_admin_vknife = ArrayCreate( MODEL_MAX_LENGTH, 1 )         if ( !amx_load_setting_string_arr( SETTINGS_FILE, "Admin Models", "V_KNIFE", g_array_models_admin_vknife ) )     {         amx_save_setting_string( SETTINGS_FILE, "Admin Models", "V_KNIFE", default_model_vknife )         ArrayPushString( g_array_models_admin_vknife, default_model_vknife )     } }
File syntax:
Code:

; Add custom admin models here
[Admin Models]
V_KNIFE = models/admin/v_knife_dragon.mdl , models/admin/v_knife_nyan.mdl , models/admin/v_knife3.mdl

Usage:

Download files below into your scripting folder, then do: #include <amx_settings_api>.

NiHiLaNTh 07-01-2014 07:09

Re: Settings API (load/save data to INI files)
 
Awesome, finally you released it. :bee:

meTaLiCroSS 07-01-2014 14:16

Re: Settings API (load/save data to INI files)
 
What's the difference between using "txt" files and "ini" files? Or "cfgs", and others? I don't really know

This would be good as a 3rd party module, for avoiding usage

Arkshine 07-01-2014 15:08

Re: Settings API (load/save data to INI files)
 
I think I will try to import TextParser from SourceMod. It supports INI and Json format. An unified API would be welcomed.

fysiks 07-01-2014 15:09

Re: Settings API (load/save data to INI files)
 
I guess I really don't understand the purpose. It appears to me that it's just a global .ini file that can be used by several plugins at once instead of each plugin using it's own.


Quote:

Originally Posted by meTaLiCroSS (Post 2160421)
What's the difference between using "txt" files and "ini" files? Or "cfgs", and others? I don't really know

A .ini file is a text file with a specific syntax. Take a look at the Wikipedia document linked to in the original post.

joropito 07-01-2014 15:27

Re: Settings API (load/save data to INI files)
 
Quote:

Originally Posted by meTaLiCroSS (Post 2160421)
What's the difference between using "txt" files and "ini" files? Or "cfgs", and others? I don't really know

As fysiks said, it's just a text format to save Key/Values in groups.

Quote:

[section1]
a=a
b=b

[section2]
a=1234
color=blue
Quote:

Originally Posted by fysiks (Post 2160460)
I guess I really don't understand the purpose. It appears to me that it's just a global .ini file that can be used by several plugins at once instead of each plugin using it's own.

If you write an application with a lot of settings to be saved somehow, sometimes INI format it's useful. Just that. Not only to share settings between plugins, you can use this in just one plugin, but a big one like ZP.

CryWolf 07-01-2014 16:57

Re: Settings API (load/save data to INI files)
 
MeRcyLeZZ, Thanks man it's awesome, but one question ZP4.3fix5a is using this method for saving loading ?

I think i will go back from ZP5.0.8 to 4.3Fix5a but not shure, just asked :)

Backstabnoob 07-01-2014 16:58

Re: Settings API (load/save data to INI files)
 
Quote:

Originally Posted by Arkshine (Post 2160459)
I think I will try to import TextParser from SourceMod. It supports INI and Json format. An unified API would be welcomed.

Yes please, I totally need JSON for my current project. Or SMJansson :3

TheDS1337 07-01-2014 19:29

Re: Settings API (load/save data to INI files)
 
Quote:

Originally Posted by CryWolf (Post 2160504)
MeRcyLeZZ, Thanks man it's awesome, but one question ZP4.3fix5a is using this method for saving loading ?

I think i will go back from ZP5.0.8 to 4.3Fix5a but not shure, just asked :)

ZP 5.0 versions using this method, older versions doesn't

meTaLiCroSS 07-01-2014 23:32

Re: Settings API (load/save data to INI files)
 
Quote:

Originally Posted by fysiks (Post 2160460)
I guess I really don't understand the purpose. It appears to me that it's just a global .ini file that can be used by several plugins at once instead of each plugin using it's own.




A .ini file is a text file with a specific syntax. Take a look at the Wikipedia document linked to in the original post.

Yeah, I saw it, but it changes something the "readability" of it? Would be different reading a .txt from a .ini?


All times are GMT -4. The time now is 03:15.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.