classref: Directory and ConfigFile

This commit is contained in:
Rémi Verschelde 2016-05-12 08:41:43 +02:00
parent 2fbf4f563b
commit 22d1385caf
3 changed files with 74 additions and 32 deletions

View file

@ -1893,15 +1893,15 @@ void _Directory::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_drive","idx"),&_Directory::get_drive); ObjectTypeDB::bind_method(_MD("get_drive","idx"),&_Directory::get_drive);
ObjectTypeDB::bind_method(_MD("change_dir:Error","todir"),&_Directory::change_dir); ObjectTypeDB::bind_method(_MD("change_dir:Error","todir"),&_Directory::change_dir);
ObjectTypeDB::bind_method(_MD("get_current_dir"),&_Directory::get_current_dir); ObjectTypeDB::bind_method(_MD("get_current_dir"),&_Directory::get_current_dir);
ObjectTypeDB::bind_method(_MD("make_dir:Error","name"),&_Directory::make_dir); ObjectTypeDB::bind_method(_MD("make_dir:Error","path"),&_Directory::make_dir);
ObjectTypeDB::bind_method(_MD("make_dir_recursive:Error","name"),&_Directory::make_dir_recursive); ObjectTypeDB::bind_method(_MD("make_dir_recursive:Error","path"),&_Directory::make_dir_recursive);
ObjectTypeDB::bind_method(_MD("file_exists","name"),&_Directory::file_exists); ObjectTypeDB::bind_method(_MD("file_exists","path"),&_Directory::file_exists);
ObjectTypeDB::bind_method(_MD("dir_exists","name"),&_Directory::dir_exists); ObjectTypeDB::bind_method(_MD("dir_exists","path"),&_Directory::dir_exists);
// ObjectTypeDB::bind_method(_MD("get_modified_time","file"),&_Directory::get_modified_time); // ObjectTypeDB::bind_method(_MD("get_modified_time","file"),&_Directory::get_modified_time);
ObjectTypeDB::bind_method(_MD("get_space_left"),&_Directory::get_space_left); ObjectTypeDB::bind_method(_MD("get_space_left"),&_Directory::get_space_left);
ObjectTypeDB::bind_method(_MD("copy:Error","from","to"),&_Directory::copy); ObjectTypeDB::bind_method(_MD("copy:Error","from","to"),&_Directory::copy);
ObjectTypeDB::bind_method(_MD("rename:Error","from","to"),&_Directory::rename); ObjectTypeDB::bind_method(_MD("rename:Error","from","to"),&_Directory::rename);
ObjectTypeDB::bind_method(_MD("remove:Error","file"),&_Directory::remove); ObjectTypeDB::bind_method(_MD("remove:Error","path"),&_Directory::remove);
} }

View file

@ -207,7 +207,7 @@ Error ConfigFile::load(const String& p_path) {
void ConfigFile::_bind_methods(){ void ConfigFile::_bind_methods(){
ObjectTypeDB::bind_method(_MD("set_value","section","key","value"),&ConfigFile::set_value); ObjectTypeDB::bind_method(_MD("set_value","section","key","value"),&ConfigFile::set_value);
ObjectTypeDB::bind_method(_MD("get_value","section","key","default"),&ConfigFile::get_value,DEFVAL(Variant())); ObjectTypeDB::bind_method(_MD("get_value:Variant","section","key","default"),&ConfigFile::get_value,DEFVAL(Variant()));
ObjectTypeDB::bind_method(_MD("has_section","section"),&ConfigFile::has_section); ObjectTypeDB::bind_method(_MD("has_section","section"),&ConfigFile::has_section);
ObjectTypeDB::bind_method(_MD("has_section_key","section","key"),&ConfigFile::has_section_key); ObjectTypeDB::bind_method(_MD("has_section_key","section","key"),&ConfigFile::has_section_key);

View file

@ -8519,8 +8519,23 @@
</class> </class>
<class name="ConfigFile" inherits="Reference" category="Core"> <class name="ConfigFile" inherits="Reference" category="Core">
<brief_description> <brief_description>
Helper class to handle INI-style files.
</brief_description> </brief_description>
<description> <description>
This helper class can be used to store [Variant] values on the filesystem using an INI-style formatting. The stored values as referenced by a section and a key. The stored data can be saved to or parsed from a file, though ConfigFile objects can also be used directly with accessing the filesystem.
The following example shows how to parse a INI-style file from the system, read its contents and store new values in it:
[codeblock]
var config = ConfigFile.new()
var err = config.load("user://settings.cfg")
if err == OK: # if not, something went wrong with the file loading
# Look for the display/width pair, and default to 1024 if missing
var screen_width = get_value("display", "width", 1024)
# Store a variable if and only it hasn't been defined yet
if not config.has_section_key("audio", "mute"):
config.set_value("audio", "mute", false)
# Save the changes by overwriting the previous file
config.save("user://settings.cfg")
[/codeblock]
</description> </description>
<methods> <methods>
<method name="set_value"> <method name="set_value">
@ -8531,9 +8546,12 @@
<argument index="2" name="value" type="Variant"> <argument index="2" name="value" type="Variant">
</argument> </argument>
<description> <description>
Assign a value to the specified key of the the specified section. If the section and/or the key do not exist, they are created. Passing a [code]NULL[/code] value deletes the specified key if it exists (and deletes the section if it ends up empty once the key has been removed).
</description> </description>
</method> </method>
<method name="get_value" qualifiers="const"> <method name="get_value" qualifiers="const">
<return type="Variant">
</return>
<argument index="0" name="section" type="String"> <argument index="0" name="section" type="String">
</argument> </argument>
<argument index="1" name="key" type="String"> <argument index="1" name="key" type="String">
@ -8541,6 +8559,7 @@
<argument index="2" name="default" type="Variant" default="NULL"> <argument index="2" name="default" type="Variant" default="NULL">
</argument> </argument>
<description> <description>
Return 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 [i]default[/i] argument (and thus [code]NULL[/code] if not specified).
</description> </description>
</method> </method>
<method name="has_section" qualifiers="const"> <method name="has_section" qualifiers="const">
@ -8549,6 +8568,7 @@
<argument index="0" name="section" type="String"> <argument index="0" name="section" type="String">
</argument> </argument>
<description> <description>
Check if the specified section exists.
</description> </description>
</method> </method>
<method name="has_section_key" qualifiers="const"> <method name="has_section_key" qualifiers="const">
@ -8559,12 +8579,14 @@
<argument index="1" name="key" type="String"> <argument index="1" name="key" type="String">
</argument> </argument>
<description> <description>
Check if the specified section-key pair exists.
</description> </description>
</method> </method>
<method name="get_sections" qualifiers="const"> <method name="get_sections" qualifiers="const">
<return type="StringArray"> <return type="StringArray">
</return> </return>
<description> <description>
Return an array of all defined section identifiers.
</description> </description>
</method> </method>
<method name="get_section_keys" qualifiers="const"> <method name="get_section_keys" qualifiers="const">
@ -8573,6 +8595,7 @@
<argument index="0" name="section" type="String"> <argument index="0" name="section" type="String">
</argument> </argument>
<description> <description>
Return an array of all defined key identifiers in the specified section.
</description> </description>
</method> </method>
<method name="load"> <method name="load">
@ -8581,6 +8604,7 @@
<argument index="0" name="path" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Load the config file specified as a parameter. The file's contents are parsed and loaded in the ConfigFile object from which the method was called. The return value is one of the OK, FAILED or ERR_* constants listed in [@Global Scope] (if the load was successful, it returns OK).
</description> </description>
</method> </method>
<method name="save"> <method name="save">
@ -8589,6 +8613,8 @@
<argument index="0" name="path" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Save the contents of the ConfigFile object to the file specified as a parameter. The output file uses an INI-style structure.
The return value is one of the OK, FAILED or ERR_* constants listed in [@Global Scope] (if the save was successful, it returns OK).
</description> </description>
</method> </method>
</methods> </methods>
@ -10111,22 +10137,22 @@ This approximation makes straight segments between each point, then subdivides t
Type used to handle the filesystem. Type used to handle the filesystem.
</brief_description> </brief_description>
<description> <description>
Directory type. Is used to manage directories and their content (not restricted to the project folder). Directory type. It is used to manage directories and their content (not restricted to the project folder).
Example for how to iterate through the files of a directory: Here is an example on how to iterate through the files of a directory:
[codeblock] [codeblock]
func dir(path): func dir_contents(path):
var d = Directory.new() var dir = Directory.new()
if d.open( path )==0: if dir.open(path) == OK:
d.list_dir_begin() dir.list_dir_begin()
var file_name = d.get_next() var file_name = dir.get_next()
while (file_name != ""): while (file_name != ""):
if d.current_is_dir(): if dir.current_is_dir():
print("Found directory: " + file_name) print("Found directory: " + file_name)
else: else:
print("Found file: " + file_name) print("Found file: " + file_name)
file_name = d.get_next() file_name = dir.get_next()
else: else:
print("Some open Error, maybe directory not found?") print("An error occurred when trying to access the path.")
[/codeblock] [/codeblock]
</description> </description>
<methods> <methods>
@ -10136,40 +10162,42 @@ This approximation makes straight segments between each point, then subdivides t
<argument index="0" name="path" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Opens a directory to work with. Needs a path, example "res://folder" Open an existing directory of the filesystem. The [i]path[/i] argument can be within the project tree ([code]res://folder[/code]), the user directory ([code]user://folder[/code]) or an absolute path of the user filesystem (e.g. [code]/tmp/folder[/code] or [code]C:\tmp\folder[/code]).
The method returns one of the error code constants defined in [@Global Scope] (OK or ERR_*).
</description> </description>
</method> </method>
<method name="list_dir_begin"> <method name="list_dir_begin">
<return type="bool"> <return type="bool">
</return> </return>
<description> <description>
Loads all file names of the current directory (prepares the get_next() function). Initialise the stream used to list all files and directories using the [method get_next] function, closing the current opened stream if needed. Once the stream has been processed, it should typically be closed with [method list_dir_end].
Return false if the stream could not be initialised.
</description> </description>
</method> </method>
<method name="get_next"> <method name="get_next">
<return type="String"> <return type="String">
</return> </return>
<description> <description>
Is used to iterate through the files of the current directory. Returns the name(no path) of the current file/directory, it also contains "." and ".." . Return the next element (file or directory) in the current directory (including [code].[/code] and [code]..[/code]). The name of the file or directory is returned (and not its full path). Once the stream has been fully processed, the method returns an empty String and closes the stream automatically (i.e. [method list_dir_end] would not be mandatory in such a case).
Returns an empty String "" at the end of the list.
</description> </description>
</method> </method>
<method name="current_is_dir" qualifiers="const"> <method name="current_is_dir" qualifiers="const">
<return type="bool"> <return type="bool">
</return> </return>
<description> <description>
Returns true if the current file you are looking at with get_next() is a directory or "." or ".." otherwise false. Return whether the current item processed with the last [method get_next] call is a directory ([code].[/code] and [code]..[/code] are considered directories).
</description> </description>
</method> </method>
<method name="list_dir_end"> <method name="list_dir_end">
<description> <description>
Run this to empty the list of remaining files in get_next(). You can use it to end the iteration, as soon as your goal is reached. Close the current stream opened with [method list_dir_begin] (whether it has been fully processed with [method get_next] or not does not matter).
</description> </description>
</method> </method>
<method name="get_drive_count"> <method name="get_drive_count">
<return type="int"> <return type="int">
</return> </return>
<description> <description>
On Windows, return the number of drives (partitions) mounted on the current filesystem. On other platforms, the method returns 0.
</description> </description>
</method> </method>
<method name="get_drive"> <method name="get_drive">
@ -10178,6 +10206,7 @@ Returns an empty String "" at the end of the list.
<argument index="0" name="idx" type="int"> <argument index="0" name="idx" type="int">
</argument> </argument>
<description> <description>
On Windows, return the name of the drive (partition) passed as an argument (e.g. [code]C:[/code]). On other platforms, or if the requested drive does not existed, the method returns an empty String.
</description> </description>
</method> </method>
<method name="change_dir"> <method name="change_dir">
@ -10186,53 +10215,60 @@ Returns an empty String "" at the end of the list.
<argument index="0" name="todir" type="String"> <argument index="0" name="todir" type="String">
</argument> </argument>
<description> <description>
Needs a path or name to the next directory. When the target directory is in the current directory you can use "newfolder" otherwise you need the full path "res://currentfolder/newfolder" Change the currently opened directory to the one passed as an argument. The argument can be relative to the current directory (e.g. [code]newdir[/code] or [code]../newdir[/code]), or an absolute path (e.g. [code]/tmp/newdir[/code] or [code]res://somedir/newdir[/code]).
The method returns one of the error code constants defined in [@Global Scope] (OK or ERR_*).
</description> </description>
</method> </method>
<method name="get_current_dir"> <method name="get_current_dir">
<return type="String"> <return type="String">
</return> </return>
<description> <description>
Returns a path to the current directory, example: "res://folder" Return the absolute path to the currently opened directory (e.g. [code]res://folder[/code] or [code]C:\tmp\folder[/code]).
</description> </description>
</method> </method>
<method name="make_dir"> <method name="make_dir">
<return type="Error"> <return type="Error">
</return> </return>
<argument index="0" name="name" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Create a directory. The argument can be relative to the current directory, or an absolute path. The target directory should be placed in an already existing directory (to create the full path recursively, see [method make_dir_recursive]).
The method returns one of the error code constants defined in [@Global Scope] (OK, FAILED or ERR_*).
</description> </description>
</method> </method>
<method name="make_dir_recursive"> <method name="make_dir_recursive">
<return type="Error"> <return type="Error">
</return> </return>
<argument index="0" name="name" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Create a target directory and all necessary intermediate directories in its path, by calling [method make_dir] recursively. The argument can be relative to the current directory, or an absolute path.
Returns one of the error code constants defined in [@Global Scope] (OK, FAILED or ERR_*).
</description> </description>
</method> </method>
<method name="file_exists"> <method name="file_exists">
<return type="bool"> <return type="bool">
</return> </return>
<argument index="0" name="name" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Return whether the target file exists. The argument can be relative to the current directory, or an absolute path.
</description> </description>
</method> </method>
<method name="dir_exists"> <method name="dir_exists">
<return type="bool"> <return type="bool">
</return> </return>
<argument index="0" name="name" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Returns true if directory exists otherwise false. Needs a path, example: "res://folder" Return whether the target directory exists. The argument can be relative to the current directory, or an absolute path.
</description> </description>
</method> </method>
<method name="get_space_left"> <method name="get_space_left">
<return type="int"> <return type="int">
</return> </return>
<description> <description>
On Unix desktop systems, return the available space on the current directory's disk. On other platforms, this information is not available and the method returns 0 or -1.
</description> </description>
</method> </method>
<method name="copy"> <method name="copy">
@ -10243,6 +10279,8 @@ Returns an empty String "" at the end of the list.
<argument index="1" name="to" type="String"> <argument index="1" name="to" type="String">
</argument> </argument>
<description> <description>
Copy the [i]from[/i] file to the [i]to[/i] destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten.
Returns one of the error code constants defined in [@Global Scope] (OK, FAILED or ERR_*).
</description> </description>
</method> </method>
<method name="rename"> <method name="rename">
@ -10253,14 +10291,18 @@ Returns an empty String "" at the end of the list.
<argument index="1" name="to" type="String"> <argument index="1" name="to" type="String">
</argument> </argument>
<description> <description>
Rename (move) the [i]from[/i] file to the [i]to[/i] destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten.
Returns one of the error code constants defined in [@Global Scope] (OK or FAILED).
</description> </description>
</method> </method>
<method name="remove"> <method name="remove">
<return type="Error"> <return type="Error">
</return> </return>
<argument index="0" name="file" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Delete the target file or an empty directory. The argument can be relative to the current directory, or an absolute path. If the target directory is not empty, the operation will fail.
Returns one of the error code constants defined in [@Global Scope] (OK or FAILED).
</description> </description>
</method> </method>
</methods> </methods>