Merge pull request #30121 from DrMoriarty/encrypted_configfile
Add encrypted files support to ConfigFile
This commit is contained in:
commit
a5bd40aa48
2 changed files with 101 additions and 2 deletions
|
@ -30,7 +30,7 @@
|
|||
|
||||
#include "config_file.h"
|
||||
|
||||
#include "core/os/file_access.h"
|
||||
#include "core/io/file_access_encrypted.h"
|
||||
#include "core/os/keyboard.h"
|
||||
#include "core/variant_parser.h"
|
||||
|
||||
|
@ -137,6 +137,48 @@ Error ConfigFile::save(const String &p_path) {
|
|||
return err;
|
||||
}
|
||||
|
||||
return _internal_save(file);
|
||||
}
|
||||
|
||||
Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
|
||||
|
||||
Error err;
|
||||
FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
|
||||
err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256);
|
||||
if (err) {
|
||||
memdelete(fae);
|
||||
memdelete(f);
|
||||
return err;
|
||||
}
|
||||
return _internal_save(fae);
|
||||
}
|
||||
|
||||
Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) {
|
||||
|
||||
Error err;
|
||||
FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err);
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
|
||||
err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256);
|
||||
if (err) {
|
||||
memdelete(fae);
|
||||
memdelete(f);
|
||||
return err;
|
||||
}
|
||||
|
||||
return _internal_save(fae);
|
||||
}
|
||||
|
||||
Error ConfigFile::_internal_save(FileAccess *file) {
|
||||
|
||||
for (OrderedHashMap<String, OrderedHashMap<String, Variant> >::Element E = values.front(); E; E = E.next()) {
|
||||
|
||||
if (E != values.front())
|
||||
|
@ -164,6 +206,48 @@ Error ConfigFile::load(const String &p_path) {
|
|||
if (!f)
|
||||
return ERR_CANT_OPEN;
|
||||
|
||||
return _internal_load(p_path, f);
|
||||
}
|
||||
|
||||
Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_key) {
|
||||
|
||||
Error err;
|
||||
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
|
||||
err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ);
|
||||
if (err) {
|
||||
memdelete(fae);
|
||||
memdelete(f);
|
||||
return err;
|
||||
}
|
||||
return _internal_load(p_path, fae);
|
||||
}
|
||||
|
||||
Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) {
|
||||
|
||||
Error err;
|
||||
FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
|
||||
err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ);
|
||||
if (err) {
|
||||
memdelete(fae);
|
||||
memdelete(f);
|
||||
return err;
|
||||
}
|
||||
|
||||
return _internal_load(p_path, fae);
|
||||
}
|
||||
|
||||
Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) {
|
||||
|
||||
VariantParser::StreamFile stream;
|
||||
stream.f = f;
|
||||
|
||||
|
@ -182,7 +266,7 @@ Error ConfigFile::load(const String &p_path) {
|
|||
next_tag.fields.clear();
|
||||
next_tag.name = String();
|
||||
|
||||
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
|
||||
Error err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
|
||||
if (err == ERR_FILE_EOF) {
|
||||
memdelete(f);
|
||||
return OK;
|
||||
|
@ -215,6 +299,12 @@ void ConfigFile::_bind_methods() {
|
|||
|
||||
ClassDB::bind_method(D_METHOD("load", "path"), &ConfigFile::load);
|
||||
ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted);
|
||||
ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "pass"), &ConfigFile::load_encrypted_pass);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted);
|
||||
ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "pass"), &ConfigFile::save_encrypted_pass);
|
||||
}
|
||||
|
||||
ConfigFile::ConfigFile() {
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
#define CONFIG_FILE_H
|
||||
|
||||
#include "core/ordered_hash_map.h"
|
||||
#include "core/os/file_access.h"
|
||||
#include "core/reference.h"
|
||||
|
||||
class ConfigFile : public Reference {
|
||||
|
@ -42,6 +43,8 @@ class ConfigFile : public Reference {
|
|||
|
||||
PoolStringArray _get_sections() const;
|
||||
PoolStringArray _get_section_keys(const String &p_section) const;
|
||||
Error _internal_load(const String &p_path, FileAccess *f);
|
||||
Error _internal_save(FileAccess *file);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
@ -61,6 +64,12 @@ public:
|
|||
Error save(const String &p_path);
|
||||
Error load(const String &p_path);
|
||||
|
||||
Error load_encrypted(const String &p_path, const Vector<uint8_t> &p_key);
|
||||
Error load_encrypted_pass(const String &p_path, const String &p_pass);
|
||||
|
||||
Error save_encrypted(const String &p_path, const Vector<uint8_t> &p_key);
|
||||
Error save_encrypted_pass(const String &p_path, const String &p_pass);
|
||||
|
||||
ConfigFile();
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue