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.meta; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.object; 22 import godot.classdb; 23 import godot.reference; 24 /** 25 Helper class to handle INI-style files. 26 27 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: 28 29 30 $(D section) 31 some_key=42 32 string_example="Hello World!" 33 a_vector=Vector3( 1, 0, 2 ) 34 35 36 The stored data can be saved to or parsed from a file, though ConfigFile objects can also be used directly without accessing the filesystem. 37 The following example shows how to parse an INI-style file from the system, read its contents and store new values in it: 38 39 40 var config = ConfigFile.new() 41 var err = config.load("user://settings.cfg") 42 if err == OK: # if not, something went wrong with the file loading 43 # Look for the display/width pair, and default to 1024 if missing 44 var screen_width = config.get_value("display", "width", 1024) 45 # Store a variable if and only if it hasn't been defined yet 46 if not config.has_section_key("audio", "mute"): 47 config.set_value("audio", "mute", false) 48 # Save the changes by overwriting the previous file 49 config.save("user://settings.cfg") 50 51 52 */ 53 @GodotBaseClass struct ConfigFile 54 { 55 enum string _GODOT_internal_name = "ConfigFile"; 56 public: 57 @nogc nothrow: 58 union { godot_object _godot_object; Reference _GODOT_base; } 59 alias _GODOT_base this; 60 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 61 package(godot) __gshared bool _classBindingInitialized = false; 62 package(godot) static struct _classBinding 63 { 64 __gshared: 65 @GodotName("set_value") GodotMethod!(void, String, String, Variant) setValue; 66 @GodotName("get_value") GodotMethod!(Variant, String, String, Variant) getValue; 67 @GodotName("has_section") GodotMethod!(bool, String) hasSection; 68 @GodotName("has_section_key") GodotMethod!(bool, String, String) hasSectionKey; 69 @GodotName("get_sections") GodotMethod!(PoolStringArray) getSections; 70 @GodotName("get_section_keys") GodotMethod!(PoolStringArray, String) getSectionKeys; 71 @GodotName("erase_section") GodotMethod!(void, String) eraseSection; 72 @GodotName("load") GodotMethod!(GodotError, String) load; 73 @GodotName("save") GodotMethod!(GodotError, String) save; 74 } 75 bool opEquals(in ConfigFile other) const { return _godot_object.ptr is other._godot_object.ptr; } 76 ConfigFile opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 77 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 78 mixin baseCasts; 79 static ConfigFile _new() 80 { 81 static godot_class_constructor constructor; 82 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("ConfigFile"); 83 if(constructor is null) return typeof(this).init; 84 return cast(ConfigFile)(constructor()); 85 } 86 @disable new(size_t s); 87 /** 88 Assigns a value to the specified key of the specified section. If the section and/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. 89 */ 90 void setValue(StringArg0, StringArg1, VariantArg2)(in StringArg0 section, in StringArg1 key, in VariantArg2 value) 91 { 92 checkClassBinding!(typeof(this))(); 93 ptrcall!(void)(_classBinding.setValue, _godot_object, section, key, value); 94 } 95 /** 96 Returns the current value for the specified section and key. If the section and/or the key do not exist, the method returns the value of the optional `default` argument, or `null` if it is omitted. 97 */ 98 Variant getValue(StringArg0, StringArg1, VariantArg2)(in StringArg0 section, in StringArg1 key, in VariantArg2 _default = Variant.nil) const 99 { 100 checkClassBinding!(typeof(this))(); 101 return ptrcall!(Variant)(_classBinding.getValue, _godot_object, section, key, _default); 102 } 103 /** 104 Returns `true` if the specified section exists. 105 */ 106 bool hasSection(StringArg0)(in StringArg0 section) const 107 { 108 checkClassBinding!(typeof(this))(); 109 return ptrcall!(bool)(_classBinding.hasSection, _godot_object, section); 110 } 111 /** 112 Returns `true` if the specified section-key pair exists. 113 */ 114 bool hasSectionKey(StringArg0, StringArg1)(in StringArg0 section, in StringArg1 key) const 115 { 116 checkClassBinding!(typeof(this))(); 117 return ptrcall!(bool)(_classBinding.hasSectionKey, _godot_object, section, key); 118 } 119 /** 120 Returns an array of all defined section identifiers. 121 */ 122 PoolStringArray getSections() const 123 { 124 checkClassBinding!(typeof(this))(); 125 return ptrcall!(PoolStringArray)(_classBinding.getSections, _godot_object); 126 } 127 /** 128 Returns an array of all defined key identifiers in the specified section. 129 */ 130 PoolStringArray getSectionKeys(StringArg0)(in StringArg0 section) const 131 { 132 checkClassBinding!(typeof(this))(); 133 return ptrcall!(PoolStringArray)(_classBinding.getSectionKeys, _godot_object, section); 134 } 135 /** 136 Deletes the specified section along with all the key-value pairs inside. 137 */ 138 void eraseSection(StringArg0)(in StringArg0 section) 139 { 140 checkClassBinding!(typeof(this))(); 141 ptrcall!(void)(_classBinding.eraseSection, _godot_object, section); 142 } 143 /** 144 Loads the config file specified as a parameter. The file's contents are parsed and loaded in the ConfigFile object which the method was called on. Returns one of the `OK`, `FAILED` or `ERR_*` constants listed in $(D @GlobalScope). If the load was successful, the return value is `OK`. 145 */ 146 GodotError load(StringArg0)(in StringArg0 path) 147 { 148 checkClassBinding!(typeof(this))(); 149 return ptrcall!(GodotError)(_classBinding.load, _godot_object, path); 150 } 151 /** 152 Saves the contents of the ConfigFile object to the file specified as a parameter. The output file uses an INI-style structure. Returns one of the `OK`, `FAILED` or `ERR_*` constants listed in $(D @GlobalScope). If the load was successful, the return value is `OK`. 153 */ 154 GodotError save(StringArg0)(in StringArg0 path) 155 { 156 checkClassBinding!(typeof(this))(); 157 return ptrcall!(GodotError)(_classBinding.save, _godot_object, path); 158 } 159 }