summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
Diffstat (limited to 'core/io')
-rw-r--r--core/io/compression.cpp87
-rw-r--r--core/io/compression.h3
-rw-r--r--core/io/file_access_encrypted.cpp73
-rw-r--r--core/io/file_access_encrypted.h9
-rw-r--r--core/io/file_access_pack.cpp82
-rw-r--r--core/io/file_access_pack.h20
-rw-r--r--core/io/file_access_zip.cpp11
-rw-r--r--core/io/file_access_zip.h12
-rw-r--r--core/io/ip_address.cpp4
-rw-r--r--core/io/json.cpp20
-rw-r--r--core/io/json.h8
-rw-r--r--core/io/pck_packer.cpp191
-rw-r--r--core/io/pck_packer.h14
-rw-r--r--core/io/resource_importer.cpp3
-rw-r--r--core/io/resource_loader.cpp6
-rw-r--r--core/io/translation_loader_po.cpp110
-rw-r--r--core/io/xml_parser.cpp4
17 files changed, 524 insertions, 133 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp
index 99ca8107e4..7480262835 100644
--- a/core/io/compression.cpp
+++ b/core/io/compression.cpp
@@ -180,8 +180,95 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p
ERR_FAIL_V(-1);
}
+/**
+ This will handle both Gzip and Deflat streams. It will automatically allocate the output buffer into the provided p_dst_vect Vector.
+ This is required for compressed data who's final uncompressed size is unknown, as is the case for HTTP response bodies.
+ This is much slower however than using Compression::decompress because it may result in multiple full copies of the output buffer.
+*/
+int Compression::decompress_dynamic(Vector<uint8_t> *p_dst_vect, int p_max_dst_size, const uint8_t *p_src, int p_src_size, Mode p_mode) {
+ int ret;
+ uint8_t *dst = nullptr;
+ int out_mark = 0;
+ z_stream strm;
+
+ ERR_FAIL_COND_V(p_src_size <= 0, Z_DATA_ERROR);
+
+ // This function only supports GZip and Deflate
+ int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16;
+ ERR_FAIL_COND_V(p_mode != MODE_DEFLATE && p_mode != MODE_GZIP, Z_ERRNO);
+
+ // Initialize the stream
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = 0;
+ strm.next_in = Z_NULL;
+
+ int err = inflateInit2(&strm, window_bits);
+ ERR_FAIL_COND_V(err != Z_OK, -1);
+
+ // Setup the stream inputs
+ strm.next_in = (Bytef *)p_src;
+ strm.avail_in = p_src_size;
+
+ // Ensure the destination buffer is empty
+ p_dst_vect->resize(0);
+
+ // decompress until deflate stream ends or end of file
+ do {
+ // Add another chunk size to the output buffer
+ // This forces a copy of the whole buffer
+ p_dst_vect->resize(p_dst_vect->size() + gzip_chunk);
+ // Get pointer to the actual output buffer
+ dst = p_dst_vect->ptrw();
+
+ // Set the stream to the new output stream
+ // Since it was copied, we need to reset the stream to the new buffer
+ strm.next_out = &(dst[out_mark]);
+ strm.avail_out = gzip_chunk;
+
+ // run inflate() on input until output buffer is full and needs to be resized
+ // or input runs out
+ do {
+ ret = inflate(&strm, Z_SYNC_FLUSH);
+
+ switch (ret) {
+ case Z_NEED_DICT:
+ ret = Z_DATA_ERROR;
+ [[fallthrough]];
+ case Z_DATA_ERROR:
+ case Z_MEM_ERROR:
+ case Z_STREAM_ERROR:
+ WARN_PRINT(strm.msg);
+ (void)inflateEnd(&strm);
+ p_dst_vect->resize(0);
+ return ret;
+ }
+ } while (strm.avail_out > 0 && strm.avail_in > 0);
+
+ out_mark += gzip_chunk;
+
+ // Encorce max output size
+ if (p_max_dst_size > -1 && strm.total_out > (uint64_t)p_max_dst_size) {
+ (void)inflateEnd(&strm);
+ p_dst_vect->resize(0);
+ return Z_BUF_ERROR;
+ }
+ } while (ret != Z_STREAM_END);
+
+ // If all done successfully, resize the output if it's larger than the actual output
+ if (ret == Z_STREAM_END && (unsigned long)p_dst_vect->size() > strm.total_out) {
+ p_dst_vect->resize(strm.total_out);
+ }
+
+ // clean up and return
+ (void)inflateEnd(&strm);
+ return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
+}
+
int Compression::zlib_level = Z_DEFAULT_COMPRESSION;
int Compression::gzip_level = Z_DEFAULT_COMPRESSION;
int Compression::zstd_level = 3;
bool Compression::zstd_long_distance_matching = false;
int Compression::zstd_window_log_size = 27; // ZSTD_WINDOWLOG_LIMIT_DEFAULT
+int Compression::gzip_chunk = 16384;
diff --git a/core/io/compression.h b/core/io/compression.h
index f195f96ba5..c103fa8eae 100644
--- a/core/io/compression.h
+++ b/core/io/compression.h
@@ -32,6 +32,7 @@
#define COMPRESSION_H
#include "core/typedefs.h"
+#include "core/vector.h"
class Compression {
public:
@@ -40,6 +41,7 @@ public:
static int zstd_level;
static bool zstd_long_distance_matching;
static int zstd_window_log_size;
+ static int gzip_chunk;
enum Mode {
MODE_FASTLZ,
@@ -51,6 +53,7 @@ public:
static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD);
static int get_max_compressed_buffer_size(int p_src_size, Mode p_mode = MODE_ZSTD);
static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD);
+ static int decompress_dynamic(Vector<uint8_t> *p_dst_vect, int p_max_dst_size, const uint8_t *p_src, int p_src_size, Mode p_mode);
Compression() {}
};
diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp
index 5938914cb0..eb684f457e 100644
--- a/core/io/file_access_encrypted.cpp
+++ b/core/io/file_access_encrypted.cpp
@@ -37,52 +37,54 @@
#include <stdio.h>
-#define COMP_MAGIC 0x43454447
-
-Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode) {
+Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic) {
ERR_FAIL_COND_V_MSG(file != nullptr, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open.");
ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER);
pos = 0;
eofed = false;
+ use_magic = p_with_magic;
if (p_mode == MODE_WRITE_AES256) {
data.clear();
writing = true;
file = p_base;
- mode = p_mode;
key = p_key;
} else if (p_mode == MODE_READ) {
writing = false;
key = p_key;
- uint32_t magic = p_base->get_32();
- ERR_FAIL_COND_V(magic != COMP_MAGIC, ERR_FILE_UNRECOGNIZED);
- mode = Mode(p_base->get_32());
- ERR_FAIL_INDEX_V(mode, MODE_MAX, ERR_FILE_CORRUPT);
- ERR_FAIL_COND_V(mode == 0, ERR_FILE_CORRUPT);
+ if (use_magic) {
+ uint32_t magic = p_base->get_32();
+ ERR_FAIL_COND_V(magic != ENCRYPTED_HEADER_MAGIC, ERR_FILE_UNRECOGNIZED);
+ }
unsigned char md5d[16];
p_base->get_buffer(md5d, 16);
length = p_base->get_64();
+
+ unsigned char iv[16];
+ for (int i = 0; i < 16; i++) {
+ iv[i] = p_base->get_8();
+ }
+
base = p_base->get_position();
ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT);
uint32_t ds = length;
if (ds % 16) {
ds += 16 - (ds % 16);
}
-
data.resize(ds);
uint32_t blen = p_base->get_buffer(data.ptrw(), ds);
ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT);
- CryptoCore::AESContext ctx;
- ctx.set_decode_key(key.ptrw(), 256);
+ {
+ CryptoCore::AESContext ctx;
- for (size_t i = 0; i < ds; i += 16) {
- ctx.decrypt_ecb(&data.write[i], &data.write[i]);
+ ctx.set_encode_key(key.ptrw(), 256); // Due to the nature of CFB, same key schedule is used for both encryption and decryption!
+ ctx.decrypt_cfb(ds, iv, data.ptrw(), data.ptrw());
}
data.resize(length);
@@ -119,6 +121,25 @@ void FileAccessEncrypted::close() {
return;
}
+ _release();
+
+ file->close();
+ memdelete(file);
+
+ file = nullptr;
+}
+
+void FileAccessEncrypted::release() {
+ if (!file) {
+ return;
+ }
+
+ _release();
+
+ file = nullptr;
+}
+
+void FileAccessEncrypted::_release() {
if (writing) {
Vector<uint8_t> compressed;
size_t len = data.size();
@@ -138,27 +159,23 @@ void FileAccessEncrypted::close() {
CryptoCore::AESContext ctx;
ctx.set_encode_key(key.ptrw(), 256);
- for (size_t i = 0; i < len; i += 16) {
- ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]);
+ if (use_magic) {
+ file->store_32(ENCRYPTED_HEADER_MAGIC);
}
- file->store_32(COMP_MAGIC);
- file->store_32(mode);
-
file->store_buffer(hash, 16);
file->store_64(data.size());
- file->store_buffer(compressed.ptr(), compressed.size());
- file->close();
- memdelete(file);
- file = nullptr;
- data.clear();
+ unsigned char iv[16];
+ for (int i = 0; i < 16; i++) {
+ iv[i] = Math::rand() % 256;
+ file->store_8(iv[i]);
+ }
- } else {
- file->close();
- memdelete(file);
+ ctx.encrypt_cfb(len, iv, compressed.ptrw(), compressed.ptrw());
+
+ file->store_buffer(compressed.ptr(), compressed.size());
data.clear();
- file = nullptr;
}
}
diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h
index e269c1e30c..fddc6842f3 100644
--- a/core/io/file_access_encrypted.h
+++ b/core/io/file_access_encrypted.h
@@ -33,6 +33,8 @@
#include "core/os/file_access.h"
+#define ENCRYPTED_HEADER_MAGIC 0x43454447
+
class FileAccessEncrypted : public FileAccess {
public:
enum Mode {
@@ -42,7 +44,6 @@ public:
};
private:
- Mode mode = MODE_MAX;
Vector<uint8_t> key;
bool writing = false;
FileAccess *file = nullptr;
@@ -51,13 +52,17 @@ private:
Vector<uint8_t> data;
mutable int pos = 0;
mutable bool eofed = false;
+ bool use_magic = true;
+
+ void _release();
public:
- Error open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode);
+ Error open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic = true);
Error open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode);
virtual Error _open(const String &p_path, int p_mode_flags); ///< open a file
virtual void close(); ///< close a file
+ virtual void release(); ///< finish and keep base file open
virtual bool is_open() const; ///< true when file is open
virtual String get_path() const; /// returns the path for the current open file
diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp
index 37240f234a..8fdbb650d4 100644
--- a/core/io/file_access_pack.cpp
+++ b/core/io/file_access_pack.cpp
@@ -30,13 +30,15 @@
#include "file_access_pack.h"
+#include "core/io/file_access_encrypted.h"
+#include "core/script_language.h"
#include "core/version.h"
#include <stdio.h>
-Error PackedData::add_pack(const String &p_path, bool p_replace_files) {
+Error PackedData::add_pack(const String &p_path, bool p_replace_files, size_t p_offset) {
for (int i = 0; i < sources.size(); i++) {
- if (sources[i]->try_open_pack(p_path, p_replace_files)) {
+ if (sources[i]->try_open_pack(p_path, p_replace_files, p_offset)) {
return OK;
}
}
@@ -44,13 +46,14 @@ Error PackedData::add_pack(const String &p_path, bool p_replace_files) {
return ERR_FILE_UNRECOGNIZED;
}
-void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files) {
+void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted) {
PathMD5 pmd5(path.md5_buffer());
- //printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b);
+ //printf("adding path %s, %lli, %lli\n", path.utf8().get_data(), pmd5.a, pmd5.b);
bool exists = files.has(pmd5);
PackedFile pf;
+ pf.encrypted = p_encrypted;
pf.pack = pkg_path;
pf.offset = ofs;
pf.size = size;
@@ -123,15 +126,24 @@ PackedData::~PackedData() {
//////////////////////////////////////////////////////////////////
-bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) {
+bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset) {
FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
if (!f) {
return false;
}
+ f->seek(p_offset);
+
uint32_t magic = f->get_32();
if (magic != PACK_HEADER_MAGIC) {
+ // loading with offset feature not supported for self contained exe files
+ if (p_offset != 0) {
+ f->close();
+ memdelete(f);
+ ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported.");
+ }
+
//maybe at the end.... self contained exe
f->seek_end();
f->seek(f->get_position() - 4);
@@ -170,6 +182,11 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files)
ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + ".");
}
+ uint32_t pack_flags = f->get_32();
+ uint64_t file_base = f->get_64();
+
+ bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED);
+
for (int i = 0; i < 16; i++) {
//reserved
f->get_32();
@@ -177,6 +194,30 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files)
int file_count = f->get_32();
+ if (enc_directory) {
+ FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
+ if (!fae) {
+ f->close();
+ memdelete(f);
+ ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
+ }
+
+ Vector<uint8_t> key;
+ key.resize(32);
+ for (int i = 0; i < key.size(); i++) {
+ key.write[i] = script_encryption_key[i];
+ }
+
+ Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
+ if (err) {
+ f->close();
+ memdelete(f);
+ memdelete(fae);
+ ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
+ }
+ f = fae;
+ }
+
for (int i = 0; i < file_count; i++) {
uint32_t sl = f->get_32();
CharString cs;
@@ -187,11 +228,13 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files)
String path;
path.parse_utf8(cs.ptr());
- uint64_t ofs = f->get_64();
+ uint64_t ofs = file_base + f->get_64();
uint64_t size = f->get_64();
uint8_t md5[16];
f->get_buffer(md5, 16);
- PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this, p_replace_files);
+ uint32_t flags = f->get_32();
+
+ PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED));
}
f->close();
@@ -225,7 +268,7 @@ void FileAccessPack::seek(size_t p_position) {
eof = false;
}
- f->seek(pf.offset + p_position);
+ f->seek(off + p_position);
pos = p_position;
}
@@ -310,12 +353,35 @@ FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFil
ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
f->seek(pf.offset);
+ off = pf.offset;
+
+ if (pf.encrypted) {
+ FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
+ if (!fae) {
+ ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
+ }
+
+ Vector<uint8_t> key;
+ key.resize(32);
+ for (int i = 0; i < key.size(); i++) {
+ key.write[i] = script_encryption_key[i];
+ }
+
+ Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
+ if (err) {
+ memdelete(fae);
+ ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
+ }
+ f = fae;
+ off = 0;
+ }
pos = 0;
eof = false;
}
FileAccessPack::~FileAccessPack() {
if (f) {
+ f->close();
memdelete(f);
}
}
diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h
index 348bc0c450..d934b0deb5 100644
--- a/core/io/file_access_pack.h
+++ b/core/io/file_access_pack.h
@@ -40,7 +40,15 @@
// Godot's packed file magic header ("GDPC" in ASCII).
#define PACK_HEADER_MAGIC 0x43504447
// The current packed file format version number.
-#define PACK_FORMAT_VERSION 1
+#define PACK_FORMAT_VERSION 2
+
+enum PackFlags {
+ PACK_DIR_ENCRYPTED = 1 << 0
+};
+
+enum PackFileFlags {
+ PACK_FILE_ENCRYPTED = 1 << 0
+};
class PackSource;
@@ -56,6 +64,7 @@ public:
uint64_t size;
uint8_t md5[16];
PackSource *src;
+ bool encrypted;
};
private:
@@ -102,13 +111,13 @@ private:
public:
void add_pack_source(PackSource *p_source);
- void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files); // for PackSource
+ void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted = false); // for PackSource
void set_disabled(bool p_disabled) { disabled = p_disabled; }
_FORCE_INLINE_ bool is_disabled() const { return disabled; }
static PackedData *get_singleton() { return singleton; }
- Error add_pack(const String &p_path, bool p_replace_files);
+ Error add_pack(const String &p_path, bool p_replace_files, size_t p_offset);
_FORCE_INLINE_ FileAccess *try_open_path(const String &p_path);
_FORCE_INLINE_ bool has_path(const String &p_path);
@@ -119,14 +128,14 @@ public:
class PackSource {
public:
- virtual bool try_open_pack(const String &p_path, bool p_replace_files) = 0;
+ virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset) = 0;
virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file) = 0;
virtual ~PackSource() {}
};
class PackedSourcePCK : public PackSource {
public:
- virtual bool try_open_pack(const String &p_path, bool p_replace_files);
+ virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset);
virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file);
};
@@ -135,6 +144,7 @@ class FileAccessPack : public FileAccess {
mutable size_t pos;
mutable bool eof;
+ uint64_t off;
FileAccess *f;
virtual Error _open(const String &p_path, int p_mode_flags);
diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp
index c3a62706c7..ce402fe8ed 100644
--- a/core/io/file_access_zip.cpp
+++ b/core/io/file_access_zip.cpp
@@ -147,8 +147,11 @@ unzFile ZipArchive::get_file_handle(String p_file) const {
return pkg;
}
-bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) {
- //printf("opening zip pack %ls, %i, %i\n", p_name.c_str(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz"));
+bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset = 0) {
+ //printf("opening zip pack %s, %i, %i\n", p_name.utf8().get_data(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz"));
+ // load with offset feature only supported for PCK files
+ ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with ZIP archives.");
+
if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) {
return false;
}
@@ -197,8 +200,8 @@ bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) {
files[fname] = f;
uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files);
- //printf("packed data add path %ls, %ls\n", p_name.c_str(), fname.c_str());
+ PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files, false);
+ //printf("packed data add path %s, %s\n", p_name.utf8().get_data(), fname.utf8().get_data());
if ((i + 1) < gi.number_entry) {
unzGoToNextFile(zfile);
diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h
index 776e830f36..c251b3c424 100644
--- a/core/io/file_access_zip.h
+++ b/core/io/file_access_zip.h
@@ -28,11 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
-#ifdef MINIZIP_ENABLED
-
#ifndef FILE_ACCESS_ZIP_H
#define FILE_ACCESS_ZIP_H
+#ifdef MINIZIP_ENABLED
+
#include "core/io/file_access_pack.h"
#include "core/map.h"
@@ -69,7 +69,7 @@ public:
bool file_exists(String p_name) const;
- virtual bool try_open_pack(const String &p_path, bool p_replace_files);
+ virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset);
FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file);
static ZipArchive *get_singleton();
@@ -79,7 +79,7 @@ public:
};
class FileAccessZip : public FileAccess {
- unzFile zfile;
+ unzFile zfile = nullptr;
unz_file_info64 file_info;
mutable bool at_eof;
@@ -113,6 +113,6 @@ public:
~FileAccessZip();
};
-#endif // FILE_ACCESS_ZIP_H
-
#endif // MINIZIP_ENABLED
+
+#endif // FILE_ACCESS_ZIP_H
diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp
index c7a0ae5605..d0fb63b958 100644
--- a/core/io/ip_address.cpp
+++ b/core/io/ip_address.cpp
@@ -71,7 +71,7 @@ static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) {
}
int n = 0;
- CharType c = p_string[i];
+ char32_t c = p_string[i];
if (c >= '0' && c <= '9') {
n = c - '0';
} else if (c >= 'a' && c <= 'f') {
@@ -101,7 +101,7 @@ void IP_Address::_parse_ipv6(const String &p_string) {
int parts_idx = 0;
for (int i = 0; i < p_string.length(); i++) {
- CharType c = p_string[i];
+ char32_t c = p_string[i];
if (c == ':') {
if (i == 0) {
continue; // next must be a ":"
diff --git a/core/io/json.cpp b/core/io/json.cpp
index 8bdd6385cb..1b89d966fd 100644
--- a/core/io/json.cpp
+++ b/core/io/json.cpp
@@ -125,7 +125,7 @@ String JSON::print(const Variant &p_var, const String &p_indent, bool p_sort_key
return _print_var(p_var, p_indent, 0, p_sort_keys);
}
-Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) {
+Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) {
while (p_len > 0) {
switch (p_str[index]) {
case '\n': {
@@ -180,12 +180,12 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to
} else if (p_str[index] == '\\') {
//escaped characters...
index++;
- CharType next = p_str[index];
+ char32_t next = p_str[index];
if (next == 0) {
r_err_str = "Unterminated String";
return ERR_PARSE_ERROR;
}
- CharType res = 0;
+ char32_t res = 0;
switch (next) {
case 'b':
@@ -206,7 +206,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to
case 'u': {
// hex number
for (int j = 0; j < 4; j++) {
- CharType c = p_str[index + j + 1];
+ char32_t c = p_str[index + j + 1];
if (c == 0) {
r_err_str = "Unterminated String";
return ERR_PARSE_ERROR;
@@ -215,7 +215,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to
r_err_str = "Malformed hex constant in string";
return ERR_PARSE_ERROR;
}
- CharType v;
+ char32_t v;
if (c >= '0' && c <= '9') {
v = c - '0';
} else if (c >= 'a' && c <= 'f') {
@@ -264,7 +264,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to
if (p_str[index] == '-' || (p_str[index] >= '0' && p_str[index] <= '9')) {
//a number
- const CharType *rptr;
+ const char32_t *rptr;
double number = String::to_float(&p_str[index], &rptr);
index += (rptr - &p_str[index]);
r_token.type = TK_NUMBER;
@@ -293,7 +293,7 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to
return ERR_PARSE_ERROR;
}
-Error JSON::_parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) {
+Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
if (token.type == TK_CURLY_BRACKET_OPEN) {
Dictionary d;
Error err = _parse_object(d, p_str, index, p_len, line, r_err_str);
@@ -337,7 +337,7 @@ Error JSON::_parse_value(Variant &value, Token &token, const CharType *p_str, in
}
}
-Error JSON::_parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) {
+Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
Token token;
bool need_comma = false;
@@ -375,7 +375,7 @@ Error JSON::_parse_array(Array &array, const CharType *p_str, int &index, int p_
return ERR_PARSE_ERROR;
}
-Error JSON::_parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) {
+Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
bool at_key = true;
String key;
Token token;
@@ -439,7 +439,7 @@ Error JSON::_parse_object(Dictionary &object, const CharType *p_str, int &index,
}
Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line) {
- const CharType *str = p_json.ptr();
+ const char32_t *str = p_json.ptr();
int idx = 0;
int len = p_json.length();
Token token;
diff --git a/core/io/json.h b/core/io/json.h
index 4fc5630a93..9122228163 100644
--- a/core/io/json.h
+++ b/core/io/json.h
@@ -65,10 +65,10 @@ class JSON {
static String _print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys);
- static Error _get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
- static Error _parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str);
- static Error _parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str);
- static Error _parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str);
+ static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
+ static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str);
+ static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str);
+ static Error _parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str);
public:
static String print(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true);
diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp
index 374b2a5e07..5480d3c535 100644
--- a/core/io/pck_packer.cpp
+++ b/core/io/pck_packer.cpp
@@ -30,36 +30,58 @@
#include "pck_packer.h"
+#include "core/crypto/crypto_core.h"
+#include "core/io/file_access_encrypted.h"
#include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
#include "core/os/file_access.h"
#include "core/version.h"
-static uint64_t _align(uint64_t p_n, int p_alignment) {
- if (p_alignment == 0) {
- return p_n;
+static int _get_pad(int p_alignment, int p_n) {
+ int rest = p_n % p_alignment;
+ int pad = 0;
+ if (rest > 0) {
+ pad = p_alignment - rest;
}
- uint64_t rest = p_n % p_alignment;
- if (rest == 0) {
- return p_n;
- } else {
- return p_n + (p_alignment - rest);
- }
-}
-
-static void _pad(FileAccess *p_file, int p_bytes) {
- for (int i = 0; i < p_bytes; i++) {
- p_file->store_8(0);
- }
+ return pad;
}
void PCKPacker::_bind_methods() {
- ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start, DEFVAL(0));
- ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path"), &PCKPacker::add_file);
+ ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment", "key", "encrypt_directory"), &PCKPacker::pck_start, DEFVAL(0), DEFVAL(String()), DEFVAL(false));
+ ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path", "encrypt"), &PCKPacker::add_file, DEFVAL(false));
ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false));
}
-Error PCKPacker::pck_start(const String &p_file, int p_alignment) {
+Error PCKPacker::pck_start(const String &p_file, int p_alignment, const String &p_key, bool p_encrypt_directory) {
+ ERR_FAIL_COND_V_MSG((p_key.empty() || !p_key.is_valid_hex_number(false) || p_key.length() != 64), ERR_CANT_CREATE, "Invalid Encryption Key (must be 64 characters long).");
+
+ String _key = p_key.to_lower();
+ key.resize(32);
+ for (int i = 0; i < 32; i++) {
+ int v = 0;
+ if (i * 2 < _key.length()) {
+ char32_t ct = _key[i * 2];
+ if (ct >= '0' && ct <= '9') {
+ ct = ct - '0';
+ } else if (ct >= 'a' && ct <= 'f') {
+ ct = 10 + ct - 'a';
+ }
+ v |= ct << 4;
+ }
+
+ if (i * 2 + 1 < _key.length()) {
+ char32_t ct = _key[i * 2 + 1];
+ if (ct >= '0' && ct <= '9') {
+ ct = ct - '0';
+ } else if (ct >= 'a' && ct <= 'f') {
+ ct = 10 + ct - 'a';
+ }
+ v |= ct;
+ }
+ key.write[i] = v;
+ }
+ enc_dir = p_encrypt_directory;
+
if (file != nullptr) {
memdelete(file);
}
@@ -76,16 +98,19 @@ Error PCKPacker::pck_start(const String &p_file, int p_alignment) {
file->store_32(VERSION_MINOR);
file->store_32(VERSION_PATCH);
- for (int i = 0; i < 16; i++) {
- file->store_32(0); // reserved
+ uint32_t pack_flags = 0;
+ if (enc_dir) {
+ pack_flags |= PACK_DIR_ENCRYPTED;
}
+ file->store_32(pack_flags); // flags
files.clear();
+ ofs = 0;
return OK;
}
-Error PCKPacker::add_file(const String &p_file, const String &p_src) {
+Error PCKPacker::add_file(const String &p_file, const String &p_src, bool p_encrypt) {
FileAccess *f = FileAccess::open(p_src, FileAccess::READ);
if (!f) {
return ERR_FILE_CANT_OPEN;
@@ -94,8 +119,32 @@ Error PCKPacker::add_file(const String &p_file, const String &p_src) {
File pf;
pf.path = p_file;
pf.src_path = p_src;
+ pf.ofs = ofs;
pf.size = f->get_len();
- pf.offset_offset = 0;
+
+ Vector<uint8_t> data = FileAccess::get_file_as_array(p_src);
+ {
+ unsigned char hash[16];
+ CryptoCore::md5(data.ptr(), data.size(), hash);
+ pf.md5.resize(16);
+ for (int i = 0; i < 16; i++) {
+ pf.md5.write[i] = hash[i];
+ }
+ }
+ pf.encrypted = p_encrypt;
+
+ uint64_t _size = pf.size;
+ if (p_encrypt) { // Add encryption overhead.
+ if (_size % 16) { // Pad to encryption block size.
+ _size += 16 - (_size % 16);
+ }
+ _size += 16; // hash
+ _size += 8; // data size
+ _size += 16; // iv
+ }
+
+ int pad = _get_pad(alignment, ofs + _size);
+ ofs = ofs + _size + pad;
files.push_back(pf);
@@ -108,27 +157,64 @@ Error PCKPacker::add_file(const String &p_file, const String &p_src) {
Error PCKPacker::flush(bool p_verbose) {
ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use.");
- // write the index
+ int64_t file_base_ofs = file->get_position();
+ file->store_64(0); // files base
+ for (int i = 0; i < 16; i++) {
+ file->store_32(0); // reserved
+ }
+
+ // write the index
file->store_32(files.size());
+ FileAccessEncrypted *fae = nullptr;
+ FileAccess *fhead = file;
+
+ if (enc_dir) {
+ fae = memnew(FileAccessEncrypted);
+ ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE);
+
+ Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
+ ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
+
+ fhead = fae;
+ }
+
for (int i = 0; i < files.size(); i++) {
- file->store_pascal_string(files[i].path);
- files.write[i].offset_offset = file->get_position();
- file->store_64(0); // offset
- file->store_64(files[i].size); // size
-
- // # empty md5
- file->store_32(0);
- file->store_32(0);
- file->store_32(0);
- file->store_32(0);
+ int string_len = files[i].path.utf8().length();
+ int pad = _get_pad(4, string_len);
+
+ fhead->store_32(string_len + pad);
+ fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len);
+ for (int j = 0; j < pad; j++) {
+ fhead->store_8(0);
+ }
+
+ fhead->store_64(files[i].ofs);
+ fhead->store_64(files[i].size); // pay attention here, this is where file is
+ fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file
+
+ uint32_t flags = 0;
+ if (files[i].encrypted) {
+ flags |= PACK_FILE_ENCRYPTED;
+ }
+ fhead->store_32(flags);
+ }
+
+ if (fae) {
+ fae->release();
+ memdelete(fae);
}
- uint64_t ofs = file->get_position();
- ofs = _align(ofs, alignment);
+ int header_padding = _get_pad(alignment, file->get_position());
+ for (int i = 0; i < header_padding; i++) {
+ file->store_8(Math::rand() % 256);
+ }
- _pad(file, ofs - file->get_position());
+ int64_t file_base = file->get_position();
+ file->seek(file_base_ofs);
+ file->store_64(file_base); // update files base
+ file->seek(file_base);
const uint32_t buf_max = 65536;
uint8_t *buf = memnew_arr(uint8_t, buf_max);
@@ -137,26 +223,41 @@ Error PCKPacker::flush(bool p_verbose) {
for (int i = 0; i < files.size(); i++) {
FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ);
uint64_t to_write = files[i].size;
+
+ fae = nullptr;
+ FileAccess *ftmp = file;
+ if (files[i].encrypted) {
+ fae = memnew(FileAccessEncrypted);
+ ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE);
+
+ Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
+ ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
+ ftmp = fae;
+ }
+
while (to_write > 0) {
int read = src->get_buffer(buf, MIN(to_write, buf_max));
- file->store_buffer(buf, read);
+ ftmp->store_buffer(buf, read);
to_write -= read;
}
- uint64_t pos = file->get_position();
- file->seek(files[i].offset_offset); // go back to store the file's offset
- file->store_64(ofs);
- file->seek(pos);
+ if (fae) {
+ fae->release();
+ memdelete(fae);
+ }
- ofs = _align(ofs + files[i].size, alignment);
- _pad(file, ofs - pos);
+ int pad = _get_pad(alignment, file->get_position());
+ for (int j = 0; j < pad; j++) {
+ file->store_8(Math::rand() % 256);
+ }
src->close();
memdelete(src);
count += 1;
- if (p_verbose && files.size() > 0) {
+ const int file_num = files.size();
+ if (p_verbose && (file_num > 0)) {
if (count % 100 == 0) {
- printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100);
+ printf("%i/%i (%.2f)\r", count, file_num, float(count) / file_num * 100);
fflush(stdout);
}
}
diff --git a/core/io/pck_packer.h b/core/io/pck_packer.h
index 2929967a68..a6054dff2c 100644
--- a/core/io/pck_packer.h
+++ b/core/io/pck_packer.h
@@ -40,20 +40,26 @@ class PCKPacker : public Reference {
FileAccess *file = nullptr;
int alignment;
+ uint64_t ofs = 0;
+
+ Vector<uint8_t> key;
+ bool enc_dir = false;
static void _bind_methods();
struct File {
String path;
String src_path;
- int size;
- uint64_t offset_offset;
+ uint64_t ofs;
+ uint64_t size;
+ bool encrypted;
+ Vector<uint8_t> md5;
};
Vector<File> files;
public:
- Error pck_start(const String &p_file, int p_alignment = 0);
- Error add_file(const String &p_file, const String &p_src);
+ Error pck_start(const String &p_file, int p_alignment = 0, const String &p_key = String(), bool p_encrypt_directory = false);
+ Error add_file(const String &p_file, const String &p_src, bool p_encrypt = false);
Error flush(bool p_verbose = false);
PCKPacker() {}
diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp
index 9ed159bd20..4d980bcf1a 100644
--- a/core/io/resource_importer.cpp
+++ b/core/io/resource_importer.cpp
@@ -31,6 +31,7 @@
#include "resource_importer.h"
#include "core/os/os.h"
+#include "core/project_settings.h"
#include "core/variant_parser.h"
bool ResourceFormatImporter::SortImporterByName::operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const {
@@ -374,7 +375,7 @@ Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const St
}
String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const {
- return "res://.import/" + p_for_file.get_file() + "-" + p_for_file.md5_text();
+ return ProjectSettings::IMPORTED_FILES_PATH.plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text());
}
bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const {
diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp
index 534f3e44de..b5c598e860 100644
--- a/core/io/resource_loader.cpp
+++ b/core/io/resource_loader.cpp
@@ -195,7 +195,8 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c
return res;
}
- ERR_FAIL_COND_V_MSG(found, RES(), "Failed loading resource: " + p_path + ".");
+ ERR_FAIL_COND_V_MSG(found, RES(),
+ vformat("Failed loading resource: %s. Make sure resources have been imported by opening the project in the editor at least once.", p_path));
#ifdef TOOLS_ENABLED
FileAccessRef file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
@@ -999,6 +1000,9 @@ void ResourceLoader::load_translation_remaps() {
void ResourceLoader::clear_translation_remaps() {
translation_remaps.clear();
+ while (remapped_list.first() != nullptr) {
+ remapped_list.remove(remapped_list.first());
+ }
}
void ResourceLoader::load_path_remaps() {
diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp
index 11aeddee09..d8ddb213c3 100644
--- a/core/io/translation_loader_po.cpp
+++ b/core/io/translation_loader_po.cpp
@@ -32,26 +32,34 @@
#include "core/os/file_access.h"
#include "core/translation.h"
+#include "core/translation_po.h"
RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
enum Status {
STATUS_NONE,
STATUS_READING_ID,
STATUS_READING_STRING,
+ STATUS_READING_CONTEXT,
+ STATUS_READING_PLURAL,
};
Status status = STATUS_NONE;
String msg_id;
String msg_str;
+ String msg_context;
+ Vector<String> msgs_plural;
String config;
if (r_error) {
*r_error = ERR_FILE_CORRUPT;
}
- Ref<Translation> translation = Ref<Translation>(memnew(Translation));
+ Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO));
int line = 1;
+ int plural_forms = 0;
+ int plural_index = -1;
+ bool entered_context = false;
bool skip_this = false;
bool skip_next = false;
bool is_eof = false;
@@ -63,40 +71,107 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
// If we reached last line and it's not a content line, break, otherwise let processing that last loop
if (is_eof && l.empty()) {
- if (status == STATUS_READING_ID) {
+ if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || (status == STATUS_READING_PLURAL && plural_index != plural_forms - 1)) {
memdelete(f);
- ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading 'msgid' at: " + path + ":" + itos(line));
+ ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line));
} else {
break;
}
}
- if (l.begins_with("msgid")) {
+ if (l.begins_with("msgctxt")) {
+ if (status != STATUS_READING_STRING && status != STATUS_READING_PLURAL) {
+ memdelete(f);
+ ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line));
+ }
+
+ // In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read
+ // and set "entered_context" to true to prevent adding twice.
+ if (!skip_this && msg_id != "") {
+ if (status == STATUS_READING_STRING) {
+ translation->add_message(msg_id, msg_str, msg_context);
+ } else if (status == STATUS_READING_PLURAL) {
+ if (plural_index != plural_forms - 1) {
+ memdelete(f);
+ ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
+ }
+ translation->add_plural_message(msg_id, msgs_plural, msg_context);
+ }
+ }
+ msg_context = "";
+ l = l.substr(7, l.length()).strip_edges();
+ status = STATUS_READING_CONTEXT;
+ entered_context = true;
+ }
+
+ if (l.begins_with("msgid_plural")) {
+ if (plural_forms == 0) {
+ memdelete(f);
+ ERR_FAIL_V_MSG(RES(), "PO file uses 'msgid_plural' but 'Plural-Forms' is invalid or missing in header: " + path + ":" + itos(line));
+ } else if (status != STATUS_READING_ID) {
+ memdelete(f);
+ ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line));
+ }
+ // We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already.
+ // We just have to reset variables related to plurals for "msgstr[]" later on.
+ l = l.substr(12, l.length()).strip_edges();
+ plural_index = -1;
+ msgs_plural.clear();
+ msgs_plural.resize(plural_forms);
+ status = STATUS_READING_PLURAL;
+ } else if (l.begins_with("msgid")) {
if (status == STATUS_READING_ID) {
memdelete(f);
ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line));
}
if (msg_id != "") {
- if (!skip_this) {
- translation->add_message(msg_id, msg_str);
+ if (!skip_this && !entered_context) {
+ if (status == STATUS_READING_STRING) {
+ translation->add_message(msg_id, msg_str, msg_context);
+ } else if (status == STATUS_READING_PLURAL) {
+ if (plural_index != plural_forms - 1) {
+ memdelete(f);
+ ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
+ }
+ translation->add_plural_message(msg_id, msgs_plural, msg_context);
+ }
}
} else if (config == "") {
config = msg_str;
+ // Record plural rule.
+ int p_start = config.find("Plural-Forms");
+ if (p_start != -1) {
+ int p_end = config.find("\n", p_start);
+ translation->set_plural_rule(config.substr(p_start, p_end - p_start));
+ plural_forms = translation->get_plural_forms();
+ }
}
l = l.substr(5, l.length()).strip_edges();
status = STATUS_READING_ID;
+ // If we did not encounter msgctxt, we reset context to empty to reset it.
+ if (!entered_context) {
+ msg_context = "";
+ }
msg_id = "";
msg_str = "";
skip_this = skip_next;
skip_next = false;
+ entered_context = false;
}
- if (l.begins_with("msgstr")) {
+ if (l.begins_with("msgstr[")) {
+ if (status != STATUS_READING_PLURAL) {
+ memdelete(f);
+ ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line));
+ }
+ plural_index++; // Increment to add to the next slot in vector msgs_plural.
+ l = l.substr(9, l.length()).strip_edges();
+ } else if (l.begins_with("msgstr")) {
if (status != STATUS_READING_ID) {
memdelete(f);
- ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' while parsing: " + path + ":" + itos(line));
+ ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line));
}
l = l.substr(6, l.length()).strip_edges();
@@ -108,7 +183,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
skip_next = true;
}
line++;
- continue; //nothing to read or comment
+ continue; // Nothing to read or comment.
}
if (!l.begins_with("\"") || status == STATUS_NONE) {
@@ -146,8 +221,12 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
if (status == STATUS_READING_ID) {
msg_id += l;
- } else {
+ } else if (status == STATUS_READING_STRING) {
msg_str += l;
+ } else if (status == STATUS_READING_CONTEXT) {
+ msg_context += l;
+ } else if (status == STATUS_READING_PLURAL && plural_index >= 0) {
+ msgs_plural.write[plural_index] = msgs_plural[plural_index] + l;
}
line++;
@@ -155,14 +234,23 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) {
memdelete(f);
+ // Add the last set of data from last iteration.
if (status == STATUS_READING_STRING) {
if (msg_id != "") {
if (!skip_this) {
- translation->add_message(msg_id, msg_str);
+ translation->add_message(msg_id, msg_str, msg_context);
}
} else if (config == "") {
config = msg_str;
}
+ } else if (status == STATUS_READING_PLURAL) {
+ if (!skip_this && msg_id != "") {
+ if (plural_index != plural_forms - 1) {
+ memdelete(f);
+ ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line));
+ }
+ translation->add_plural_message(msg_id, msgs_plural, msg_context);
+ }
}
ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + ".");
diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp
index b11267b60f..fc75ac7d1e 100644
--- a/core/io/xml_parser.cpp
+++ b/core/io/xml_parser.cpp
@@ -36,7 +36,7 @@
VARIANT_ENUM_CAST(XMLParser::NodeType);
-static bool _equalsn(const CharType *str1, const CharType *str2, int len) {
+static bool _equalsn(const char32_t *str1, const char32_t *str2, int len) {
int i;
for (i = 0; i < len && str1[i] && str2[i]; ++i) {
if (str1[i] != str2[i]) {
@@ -64,7 +64,7 @@ String XMLParser::_replace_special_characters(const String &origstr) {
int specialChar = -1;
for (int i = 0; i < (int)special_characters.size(); ++i) {
- const CharType *p = &origstr[pos] + 1;
+ const char32_t *p = &origstr[pos] + 1;
if (_equalsn(&special_characters[i][1], p, special_characters[i].length() - 1)) {
specialChar = i;