1 /** 2 Helper class to handle INI-style files. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.configfile; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.classdb; 24 import godot.reference; 25 /** 26 Helper class to handle INI-style files. 27 28 This helper class can be used to store $(D Variant) values on the filesystem using INI-style formatting. The stored values are identified by a section and a key: 29 30 31 $(D section) 32 some_key=42 33 string_example="Hello World!" 34 a_vector=Vector3( 1, 0, 2 ) 35 36 37 The stored data can be saved to or parsed from a file, though ConfigFile objects can also be used directly without accessing the filesystem. 38 The following example shows how to create a simple $(D ConfigFile) and save it on disk: 39 40 41 # Create new ConfigFile object. 42 var config = ConfigFile.new() 43 44 # Store some values. 45 config.set_value("Player1", "player_name", "Steve") 46 config.set_value("Player1", "best_score", 10) 47 config.set_value("Player2", "player_name", "V3geta") 48 config.set_value("Player2", "best_score", 9001) 49 50 # Save it to a file (overwrite if already exists). 51 config.save("user://scores.cfg") 52 53 54 This example shows how the above file could be loaded: 55 56 57 var score_data = {} 58 var config = ConfigFile.new() 59 60 # Load data from a file. 61 var err = config.load("user://scores.cfg") 62 63 # If the file didn't load, ignore it. 64 if err != OK: 65 return 66 67 # Iterate over all sections. 68 for player in config.get_sections(): 69 # Fetch the data for each section. 70 var player_name = config.get_value(player, "player_name") 71 var player_score = config.get_value(player, "best_score") 72 score_data$(D player_name) = player_score 73 74 75 Keep in mind that section and property names can't contain spaces. Anything after a space will be ignored on save and on load. 76 ConfigFiles can also contain manually written comment lines starting with a semicolon (`;`). Those lines will be ignored when parsing the file. Note that comments will be lost when saving the ConfigFile. This can still be useful for dedicated server configuration files, which are typically never overwritten without explicit user action. 77 */ 78 @GodotBaseClass struct ConfigFile 79 { 80 package(godot) enum string _GODOT_internal_name = "ConfigFile"; 81 public: 82 @nogc nothrow: 83 union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; } 84 alias _GODOT_base this; 85 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 86 package(godot) __gshared bool _classBindingInitialized = false; 87 package(godot) static struct GDNativeClassBinding 88 { 89 __gshared: 90 @GodotName("clear") GodotMethod!(void) clear; 91 @GodotName("erase_section") GodotMethod!(void, String) eraseSection; 92 @GodotName("erase_section_key") GodotMethod!(void, String, String) eraseSectionKey; 93 @GodotName("get_section_keys") GodotMethod!(PoolStringArray, String) getSectionKeys; 94 @GodotName("get_sections") GodotMethod!(PoolStringArray) getSections; 95 @GodotName("get_value") GodotMethod!(Variant, String, String, Variant) getValue; 96 @GodotName("has_section") GodotMethod!(bool, String) hasSection; 97 @GodotName("has_section_key") GodotMethod!(bool, String, String) hasSectionKey; 98 @GodotName("load") GodotMethod!(GodotError, String) load; 99 @GodotName("load_encrypted") GodotMethod!(GodotError, String, PoolByteArray) loadEncrypted; 100 @GodotName("load_encrypted_pass") GodotMethod!(GodotError, String, String) loadEncryptedPass; 101 @GodotName("parse") GodotMethod!(GodotError, String) parse; 102 @GodotName("save") GodotMethod!(GodotError, String) save; 103 @GodotName("save_encrypted") GodotMethod!(GodotError, String, PoolByteArray) saveEncrypted; 104 @GodotName("save_encrypted_pass") GodotMethod!(GodotError, String, String) saveEncryptedPass; 105 @GodotName("set_value") GodotMethod!(void, String, String, Variant) setValue; 106 } 107 /// 108 pragma(inline, true) bool opEquals(in ConfigFile other) const 109 { return _godot_object.ptr is other._godot_object.ptr; } 110 /// 111 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 112 { _godot_object.ptr = n; return null; } 113 /// 114 pragma(inline, true) bool opEquals(typeof(null) n) const 115 { return _godot_object.ptr is n; } 116 /// 117 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 118 mixin baseCasts; 119 /// Construct a new instance of ConfigFile. 120 /// Note: use `memnew!ConfigFile` instead. 121 static ConfigFile _new() 122 { 123 static godot_class_constructor constructor; 124 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("ConfigFile"); 125 if(constructor is null) return typeof(this).init; 126 return cast(ConfigFile)(constructor()); 127 } 128 @disable new(size_t s); 129 /** 130 131 */ 132 void clear() 133 { 134 checkClassBinding!(typeof(this))(); 135 ptrcall!(void)(GDNativeClassBinding.clear, _godot_object); 136 } 137 /** 138 Deletes the specified section along with all the key-value pairs inside. Raises an error if the section does not exist. 139 */ 140 void eraseSection(in String section) 141 { 142 checkClassBinding!(typeof(this))(); 143 ptrcall!(void)(GDNativeClassBinding.eraseSection, _godot_object, section); 144 } 145 /** 146 Deletes the specified key in a section. Raises an error if either the section or the key do not exist. 147 */ 148 void eraseSectionKey(in String section, in String key) 149 { 150 checkClassBinding!(typeof(this))(); 151 ptrcall!(void)(GDNativeClassBinding.eraseSectionKey, _godot_object, section, key); 152 } 153 /** 154 Returns an array of all defined key identifiers in the specified section. Raises an error and returns an empty array if the section does not exist. 155 */ 156 PoolStringArray getSectionKeys(in String section) const 157 { 158 checkClassBinding!(typeof(this))(); 159 return ptrcall!(PoolStringArray)(GDNativeClassBinding.getSectionKeys, _godot_object, section); 160 } 161 /** 162 Returns an array of all defined section identifiers. 163 */ 164 PoolStringArray getSections() const 165 { 166 checkClassBinding!(typeof(this))(); 167 return ptrcall!(PoolStringArray)(GDNativeClassBinding.getSections, _godot_object); 168 } 169 /** 170 Returns the current value for the specified section and key. If either the section or the key do not exist, the method returns the fallback `default` value. If `default` is not specified or set to `null`, an error is also raised. 171 */ 172 Variant getValue(VariantArg2)(in String section, in String key, in VariantArg2 _default = Variant.nil) const 173 { 174 checkClassBinding!(typeof(this))(); 175 return ptrcall!(Variant)(GDNativeClassBinding.getValue, _godot_object, section, key, _default); 176 } 177 /** 178 Returns `true` if the specified section exists. 179 */ 180 bool hasSection(in String section) const 181 { 182 checkClassBinding!(typeof(this))(); 183 return ptrcall!(bool)(GDNativeClassBinding.hasSection, _godot_object, section); 184 } 185 /** 186 Returns `true` if the specified section-key pair exists. 187 */ 188 bool hasSectionKey(in String section, in String key) const 189 { 190 checkClassBinding!(typeof(this))(); 191 return ptrcall!(bool)(GDNativeClassBinding.hasSectionKey, _godot_object, section, key); 192 } 193 /** 194 Loads the config file specified as a parameter. The file's contents are parsed and loaded in the $(D ConfigFile) object which the method was called on. 195 Returns one of the $(D error) code constants (`OK` on success). 196 */ 197 GodotError load(in String path) 198 { 199 checkClassBinding!(typeof(this))(); 200 return ptrcall!(GodotError)(GDNativeClassBinding.load, _godot_object, path); 201 } 202 /** 203 Loads the encrypted config file specified as a parameter, using the provided `key` to decrypt it. The file's contents are parsed and loaded in the $(D ConfigFile) object which the method was called on. 204 Returns one of the $(D error) code constants (`OK` on success). 205 */ 206 GodotError loadEncrypted(in String path, in PoolByteArray key) 207 { 208 checkClassBinding!(typeof(this))(); 209 return ptrcall!(GodotError)(GDNativeClassBinding.loadEncrypted, _godot_object, path, key); 210 } 211 /** 212 Loads the encrypted config file specified as a parameter, using the provided `password` to decrypt it. The file's contents are parsed and loaded in the $(D ConfigFile) object which the method was called on. 213 Returns one of the $(D error) code constants (`OK` on success). 214 */ 215 GodotError loadEncryptedPass(in String path, in String password) 216 { 217 checkClassBinding!(typeof(this))(); 218 return ptrcall!(GodotError)(GDNativeClassBinding.loadEncryptedPass, _godot_object, path, password); 219 } 220 /** 221 Parses the passed string as the contents of a config file. The string is parsed and loaded in the ConfigFile object which the method was called on. 222 Returns one of the $(D error) code constants (`OK` on success). 223 */ 224 GodotError parse(in String data) 225 { 226 checkClassBinding!(typeof(this))(); 227 return ptrcall!(GodotError)(GDNativeClassBinding.parse, _godot_object, data); 228 } 229 /** 230 Saves the contents of the $(D ConfigFile) object to the file specified as a parameter. The output file uses an INI-style structure. 231 Returns one of the $(D error) code constants (`OK` on success). 232 */ 233 GodotError save(in String path) 234 { 235 checkClassBinding!(typeof(this))(); 236 return ptrcall!(GodotError)(GDNativeClassBinding.save, _godot_object, path); 237 } 238 /** 239 Saves the contents of the $(D ConfigFile) object to the AES-256 encrypted file specified as a parameter, using the provided `key` to encrypt it. The output file uses an INI-style structure. 240 Returns one of the $(D error) code constants (`OK` on success). 241 */ 242 GodotError saveEncrypted(in String path, in PoolByteArray key) 243 { 244 checkClassBinding!(typeof(this))(); 245 return ptrcall!(GodotError)(GDNativeClassBinding.saveEncrypted, _godot_object, path, key); 246 } 247 /** 248 Saves the contents of the $(D ConfigFile) object to the AES-256 encrypted file specified as a parameter, using the provided `password` to encrypt it. The output file uses an INI-style structure. 249 Returns one of the $(D error) code constants (`OK` on success). 250 */ 251 GodotError saveEncryptedPass(in String path, in String password) 252 { 253 checkClassBinding!(typeof(this))(); 254 return ptrcall!(GodotError)(GDNativeClassBinding.saveEncryptedPass, _godot_object, path, password); 255 } 256 /** 257 Assigns a value to the specified key of the specified section. If either the section or the key do not exist, they are created. Passing a `null` value deletes the specified key if it exists, and deletes the section if it ends up empty once the key has been removed. 258 */ 259 void setValue(VariantArg2)(in String section, in String key, in VariantArg2 value) 260 { 261 checkClassBinding!(typeof(this))(); 262 ptrcall!(void)(GDNativeClassBinding.setValue, _godot_object, section, key, value); 263 } 264 }