2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* resource_format_binary.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
2021-01-01 20:13:46 +01:00
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2014-02-10 02:10:30 +01:00
# include "resource_format_binary.h"
2017-08-26 17:46:49 +02:00
2020-11-07 23:33:38 +01:00
# include "core/config/project_settings.h"
2021-06-11 14:51:48 +02:00
# include "core/io/dir_access.h"
2017-08-26 17:46:49 +02:00
# include "core/io/file_access_compressed.h"
2020-11-07 23:33:38 +01:00
# include "core/io/image.h"
2017-08-26 17:46:49 +02:00
# include "core/io/marshalls.h"
# include "core/version.h"
2014-02-10 02:10:30 +01:00
//#define print_bl(m_what) print_line(m_what)
2017-09-02 22:32:31 +02:00
# define print_bl(m_what) (void)(m_what)
2014-02-10 02:10:30 +01:00
enum {
//numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization)
2017-03-05 16:44:50 +01:00
VARIANT_NIL = 1 ,
VARIANT_BOOL = 2 ,
VARIANT_INT = 3 ,
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
VARIANT_FLOAT = 4 ,
2017-03-05 16:44:50 +01:00
VARIANT_STRING = 5 ,
VARIANT_VECTOR2 = 10 ,
VARIANT_RECT2 = 11 ,
VARIANT_VECTOR3 = 12 ,
VARIANT_PLANE = 13 ,
2021-01-20 08:02:02 +01:00
VARIANT_QUATERNION = 14 ,
2017-11-17 03:09:00 +01:00
VARIANT_AABB = 15 ,
2017-03-05 16:44:50 +01:00
VARIANT_MATRIX3 = 16 ,
VARIANT_TRANSFORM = 17 ,
VARIANT_MATRIX32 = 18 ,
VARIANT_COLOR = 20 ,
VARIANT_NODE_PATH = 22 ,
VARIANT_RID = 23 ,
VARIANT_OBJECT = 24 ,
VARIANT_INPUT_EVENT = 25 ,
VARIANT_DICTIONARY = 26 ,
VARIANT_ARRAY = 30 ,
VARIANT_RAW_ARRAY = 31 ,
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
VARIANT_INT32_ARRAY = 32 ,
VARIANT_FLOAT32_ARRAY = 33 ,
2017-03-05 16:44:50 +01:00
VARIANT_STRING_ARRAY = 34 ,
VARIANT_VECTOR3_ARRAY = 35 ,
VARIANT_COLOR_ARRAY = 36 ,
VARIANT_VECTOR2_ARRAY = 37 ,
VARIANT_INT64 = 40 ,
VARIANT_DOUBLE = 41 ,
2020-02-19 20:27:19 +01:00
VARIANT_CALLABLE = 42 ,
VARIANT_SIGNAL = 43 ,
2020-02-20 22:58:05 +01:00
VARIANT_STRING_NAME = 44 ,
2020-02-22 04:26:41 +01:00
VARIANT_VECTOR2I = 45 ,
VARIANT_RECT2I = 46 ,
VARIANT_VECTOR3I = 47 ,
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
VARIANT_INT64_ARRAY = 48 ,
VARIANT_FLOAT64_ARRAY = 49 ,
2017-03-05 16:44:50 +01:00
OBJECT_EMPTY = 0 ,
OBJECT_EXTERNAL_RESOURCE = 1 ,
OBJECT_INTERNAL_RESOURCE = 2 ,
OBJECT_EXTERNAL_RESOURCE_INDEX = 3 ,
2021-07-21 02:36:56 +02:00
// Version 2: added 64 bits support for float and int.
// Version 3: changed nodepath encoding.
// Version 4: new string ID for ext/subresources, breaks forward compat.
FORMAT_VERSION = 4 ,
2017-11-22 12:34:43 +01:00
FORMAT_VERSION_CAN_RENAME_DEPS = 1 ,
FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3 ,
2014-02-10 02:10:30 +01:00
} ;
2020-02-28 12:27:04 +01:00
void ResourceLoaderBinary : : _advance_padding ( uint32_t p_len ) {
2017-03-05 16:44:50 +01:00
uint32_t extra = 4 - ( p_len % 4 ) ;
if ( extra < 4 ) {
2020-05-14 16:41:43 +02:00
for ( uint32_t i = 0 ; i < extra ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > get_8 ( ) ; //pad to 32
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
}
}
2020-02-28 12:27:04 +01:00
StringName ResourceLoaderBinary : : _get_string ( ) {
2017-02-15 12:29:46 +01:00
uint32_t id = f - > get_32 ( ) ;
2017-03-05 16:44:50 +01:00
if ( id & 0x80000000 ) {
2017-12-15 13:04:57 +01:00
uint32_t len = id & 0x7FFFFFFF ;
2019-02-21 20:57:39 +01:00
if ( ( int ) len > str_buf . size ( ) ) {
2017-02-15 12:29:46 +01:00
str_buf . resize ( len ) ;
}
2020-05-14 16:41:43 +02:00
if ( len = = 0 ) {
2017-02-15 12:29:46 +01:00
return StringName ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
f - > get_buffer ( ( uint8_t * ) & str_buf [ 0 ] , len ) ;
2017-02-15 12:29:46 +01:00
String s ;
s . parse_utf8 ( & str_buf [ 0 ] ) ;
return s ;
}
return string_map [ id ] ;
}
2020-02-28 12:27:04 +01:00
Error ResourceLoaderBinary : : parse_variant ( Variant & r_v ) {
2014-02-10 02:10:30 +01:00
uint32_t type = f - > get_32 ( ) ;
2017-03-05 16:44:50 +01:00
print_bl ( " find property of type: " + itos ( type ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
switch ( type ) {
2014-02-10 02:10:30 +01:00
case VARIANT_NIL : {
2017-03-05 16:44:50 +01:00
r_v = Variant ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_BOOL : {
2017-03-05 16:44:50 +01:00
r_v = bool ( f - > get_32 ( ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_INT : {
2017-03-05 16:44:50 +01:00
r_v = int ( f - > get_32 ( ) ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-01-09 00:58:39 +01:00
case VARIANT_INT64 : {
2017-03-05 16:44:50 +01:00
r_v = int64_t ( f - > get_64 ( ) ) ;
2017-01-09 00:58:39 +01:00
} break ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
case VARIANT_FLOAT : {
2017-03-05 16:44:50 +01:00
r_v = f - > get_real ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
2017-01-09 00:58:39 +01:00
case VARIANT_DOUBLE : {
2017-03-05 16:44:50 +01:00
r_v = f - > get_double ( ) ;
2017-01-09 00:58:39 +01:00
} break ;
2014-02-10 02:10:30 +01:00
case VARIANT_STRING : {
2017-03-05 16:44:50 +01:00
r_v = get_unicode_string ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_VECTOR2 : {
Vector2 v ;
2017-03-05 16:44:50 +01:00
v . x = f - > get_real ( ) ;
v . y = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
2020-02-22 04:26:41 +01:00
} break ;
case VARIANT_VECTOR2I : {
Vector2i v ;
v . x = f - > get_32 ( ) ;
v . y = f - > get_32 ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_RECT2 : {
Rect2 v ;
2017-06-04 00:25:13 +02:00
v . position . x = f - > get_real ( ) ;
v . position . y = f - > get_real ( ) ;
2017-03-05 16:44:50 +01:00
v . size . x = f - > get_real ( ) ;
v . size . y = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
2020-02-22 04:26:41 +01:00
} break ;
case VARIANT_RECT2I : {
Rect2i v ;
v . position . x = f - > get_32 ( ) ;
v . position . y = f - > get_32 ( ) ;
v . size . x = f - > get_32 ( ) ;
v . size . y = f - > get_32 ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_VECTOR3 : {
Vector3 v ;
2017-03-05 16:44:50 +01:00
v . x = f - > get_real ( ) ;
v . y = f - > get_real ( ) ;
v . z = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
2020-02-22 04:26:41 +01:00
case VARIANT_VECTOR3I : {
Vector3i v ;
v . x = f - > get_32 ( ) ;
v . y = f - > get_32 ( ) ;
v . z = f - > get_32 ( ) ;
r_v = v ;
} break ;
2014-02-10 02:10:30 +01:00
case VARIANT_PLANE : {
Plane v ;
2017-03-05 16:44:50 +01:00
v . normal . x = f - > get_real ( ) ;
v . normal . y = f - > get_real ( ) ;
v . normal . z = f - > get_real ( ) ;
2020-05-10 16:47:11 +02:00
v . d = f - > get_real ( ) ;
2017-03-05 16:44:50 +01:00
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
2021-01-20 08:02:02 +01:00
case VARIANT_QUATERNION : {
Quaternion v ;
2017-03-05 16:44:50 +01:00
v . x = f - > get_real ( ) ;
v . y = f - > get_real ( ) ;
v . z = f - > get_real ( ) ;
v . w = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
2017-11-17 03:09:00 +01:00
case VARIANT_AABB : {
AABB v ;
2017-06-06 20:33:51 +02:00
v . position . x = f - > get_real ( ) ;
v . position . y = f - > get_real ( ) ;
v . position . z = f - > get_real ( ) ;
2017-03-05 16:44:50 +01:00
v . size . x = f - > get_real ( ) ;
v . size . y = f - > get_real ( ) ;
v . size . z = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_MATRIX32 : {
2017-01-11 04:52:51 +01:00
Transform2D v ;
2017-03-05 16:44:50 +01:00
v . elements [ 0 ] . x = f - > get_real ( ) ;
v . elements [ 0 ] . y = f - > get_real ( ) ;
v . elements [ 1 ] . x = f - > get_real ( ) ;
v . elements [ 1 ] . y = f - > get_real ( ) ;
v . elements [ 2 ] . x = f - > get_real ( ) ;
v . elements [ 2 ] . y = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_MATRIX3 : {
2017-01-11 04:52:51 +01:00
Basis v ;
2017-03-05 16:44:50 +01:00
v . elements [ 0 ] . x = f - > get_real ( ) ;
v . elements [ 0 ] . y = f - > get_real ( ) ;
v . elements [ 0 ] . z = f - > get_real ( ) ;
v . elements [ 1 ] . x = f - > get_real ( ) ;
v . elements [ 1 ] . y = f - > get_real ( ) ;
v . elements [ 1 ] . z = f - > get_real ( ) ;
v . elements [ 2 ] . x = f - > get_real ( ) ;
v . elements [ 2 ] . y = f - > get_real ( ) ;
v . elements [ 2 ] . z = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_TRANSFORM : {
2020-10-17 07:08:21 +02:00
Transform3D v ;
2017-03-05 16:44:50 +01:00
v . basis . elements [ 0 ] . x = f - > get_real ( ) ;
v . basis . elements [ 0 ] . y = f - > get_real ( ) ;
v . basis . elements [ 0 ] . z = f - > get_real ( ) ;
v . basis . elements [ 1 ] . x = f - > get_real ( ) ;
v . basis . elements [ 1 ] . y = f - > get_real ( ) ;
v . basis . elements [ 1 ] . z = f - > get_real ( ) ;
v . basis . elements [ 2 ] . x = f - > get_real ( ) ;
v . basis . elements [ 2 ] . y = f - > get_real ( ) ;
v . basis . elements [ 2 ] . z = f - > get_real ( ) ;
v . origin . x = f - > get_real ( ) ;
v . origin . y = f - > get_real ( ) ;
v . origin . z = f - > get_real ( ) ;
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_COLOR : {
2021-01-25 20:46:35 +01:00
Color v ; // Colors should always be in single-precision.
v . r = f - > get_float ( ) ;
v . g = f - > get_float ( ) ;
v . b = f - > get_float ( ) ;
v . a = f - > get_float ( ) ;
2017-03-05 16:44:50 +01:00
r_v = v ;
2014-02-10 02:10:30 +01:00
} break ;
2020-02-20 22:58:05 +01:00
case VARIANT_STRING_NAME : {
r_v = StringName ( get_unicode_string ( ) ) ;
} break ;
2014-02-10 02:10:30 +01:00
case VARIANT_NODE_PATH : {
Vector < StringName > names ;
Vector < StringName > subnames ;
bool absolute ;
int name_count = f - > get_16 ( ) ;
uint32_t subname_count = f - > get_16 ( ) ;
2017-03-05 16:44:50 +01:00
absolute = subname_count & 0x8000 ;
subname_count & = 0x7FFF ;
2017-11-22 12:34:43 +01:00
if ( ver_format < FORMAT_VERSION_NO_NODEPATH_PROPERTY ) {
subname_count + = 1 ; // has a property field, so we should count it as well
}
2014-02-10 02:10:30 +01:00
2020-05-14 16:41:43 +02:00
for ( int i = 0 ; i < name_count ; i + + ) {
2017-02-15 12:29:46 +01:00
names . push_back ( _get_string ( ) ) ;
2020-05-14 16:41:43 +02:00
}
for ( uint32_t i = 0 ; i < subname_count ; i + + ) {
2017-02-15 12:29:46 +01:00
subnames . push_back ( _get_string ( ) ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2017-05-30 22:20:15 +02:00
NodePath np = NodePath ( names , subnames , absolute ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
r_v = np ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_RID : {
2017-03-05 16:44:50 +01:00
r_v = f - > get_32 ( ) ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_OBJECT : {
2019-02-12 21:10:08 +01:00
uint32_t objtype = f - > get_32 ( ) ;
2014-02-10 02:10:30 +01:00
2019-02-12 21:10:08 +01:00
switch ( objtype ) {
2014-02-10 02:10:30 +01:00
case OBJECT_EMPTY : {
//do none
} break ;
case OBJECT_INTERNAL_RESOURCE : {
2017-03-05 16:44:50 +01:00
uint32_t index = f - > get_32 ( ) ;
2021-07-21 02:36:56 +02:00
String path ;
if ( using_named_scene_ids ) { // New format.
ERR_FAIL_INDEX_V ( ( int ) index , internal_resources . size ( ) , ERR_PARSE_ERROR ) ;
path = internal_resources [ index ] . path ;
} else {
path + = res_path + " :: " + itos ( index ) ;
}
2020-05-01 14:34:23 +02:00
2021-02-11 18:18:45 +01:00
//always use internal cache for loading internal resources
if ( ! internal_index_cache . has ( path ) ) {
WARN_PRINT ( String ( " Couldn't load resource (no cache): " + path ) . utf8 ( ) . get_data ( ) ) ;
r_v = Variant ( ) ;
2020-04-20 04:19:21 +02:00
} else {
2021-02-11 18:18:45 +01:00
r_v = internal_index_cache [ path ] ;
2014-02-10 02:10:30 +01:00
}
} break ;
case OBJECT_EXTERNAL_RESOURCE : {
2015-08-24 01:15:56 +02:00
//old file format, still around for compatibility
2014-02-10 02:10:30 +01:00
2019-02-12 21:10:08 +01:00
String exttype = get_unicode_string ( ) ;
2014-02-10 02:10:30 +01:00
String path = get_unicode_string ( ) ;
2021-08-30 01:43:47 +02:00
if ( path . find ( " :// " ) = = - 1 & & path . is_relative_path ( ) ) {
2014-02-10 02:10:30 +01:00
// path is relative to file being loaded, so convert to a resource path
2017-07-19 22:00:46 +02:00
path = ProjectSettings : : get_singleton ( ) - > localize_path ( res_path . get_base_dir ( ) . plus_file ( path ) ) ;
2014-02-10 02:10:30 +01:00
}
2015-08-24 01:15:56 +02:00
if ( remaps . find ( path ) ) {
2017-03-05 16:44:50 +01:00
path = remaps [ path ] ;
2015-08-24 01:15:56 +02:00
}
2019-02-12 21:10:08 +01:00
RES res = ResourceLoader : : load ( path , exttype ) ;
2014-02-10 02:10:30 +01:00
if ( res . is_null ( ) ) {
2017-03-05 16:44:50 +01:00
WARN_PRINT ( String ( " Couldn't load resource: " + path ) . utf8 ( ) . get_data ( ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
r_v = res ;
2014-02-10 02:10:30 +01:00
2015-08-24 01:15:56 +02:00
} break ;
case OBJECT_EXTERNAL_RESOURCE_INDEX : {
//new file format, just refers to an index in the external list
2017-08-31 23:30:35 +02:00
int erindex = f - > get_32 ( ) ;
2015-08-24 01:15:56 +02:00
2017-08-31 23:30:35 +02:00
if ( erindex < 0 | | erindex > = external_resources . size ( ) ) {
2019-01-26 22:35:31 +01:00
WARN_PRINT ( " Broken external resource! (index out of size) " ) ;
2017-03-05 16:44:50 +01:00
r_v = Variant ( ) ;
2015-08-24 01:15:56 +02:00
} else {
2020-02-28 12:27:04 +01:00
if ( external_resources [ erindex ] . cache . is_null ( ) ) {
//cache not here yet, wait for it?
if ( use_sub_threads ) {
Error err ;
external_resources . write [ erindex ] . cache = ResourceLoader : : load_threaded_get ( external_resources [ erindex ] . path , & err ) ;
2015-08-24 01:15:56 +02:00
2020-02-28 12:27:04 +01:00
if ( err ! = OK | | external_resources [ erindex ] . cache . is_null ( ) ) {
if ( ! ResourceLoader : : get_abort_on_missing_resources ( ) ) {
ResourceLoader : : notify_dependency_error ( local_path , external_resources [ erindex ] . path , external_resources [ erindex ] . type ) ;
} else {
error = ERR_FILE_MISSING_DEPENDENCIES ;
ERR_FAIL_V_MSG ( error , " Can't load dependency: " + external_resources [ erindex ] . path + " . " ) ;
}
}
}
2015-08-24 01:15:56 +02:00
}
2020-02-28 12:27:04 +01:00
r_v = external_resources [ erindex ] . cache ;
2015-08-24 01:15:56 +02:00
}
2014-02-10 02:10:30 +01:00
} break ;
default : {
ERR_FAIL_V ( ERR_FILE_CORRUPT ) ;
} break ;
}
} break ;
2020-02-19 20:27:19 +01:00
case VARIANT_CALLABLE : {
r_v = Callable ( ) ;
} break ;
case VARIANT_SIGNAL : {
r_v = Signal ( ) ;
} break ;
2014-02-10 02:10:30 +01:00
case VARIANT_DICTIONARY : {
2017-03-05 16:44:50 +01:00
uint32_t len = f - > get_32 ( ) ;
2017-01-11 12:53:31 +01:00
Dictionary d ; //last bit means shared
2017-03-05 16:44:50 +01:00
len & = 0x7FFFFFFF ;
for ( uint32_t i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
Variant key ;
Error err = parse_variant ( key ) ;
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( err , ERR_FILE_CORRUPT , " Error when trying to parse Variant. " ) ;
2014-02-10 02:10:30 +01:00
Variant value ;
err = parse_variant ( value ) ;
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( err , ERR_FILE_CORRUPT , " Error when trying to parse Variant. " ) ;
2017-03-05 16:44:50 +01:00
d [ key ] = value ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
r_v = d ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_ARRAY : {
2017-03-05 16:44:50 +01:00
uint32_t len = f - > get_32 ( ) ;
2017-01-11 12:53:31 +01:00
Array a ; //last bit means shared
2017-03-05 16:44:50 +01:00
len & = 0x7FFFFFFF ;
2014-02-10 02:10:30 +01:00
a . resize ( len ) ;
2017-03-05 16:44:50 +01:00
for ( uint32_t i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
Variant val ;
Error err = parse_variant ( val ) ;
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( err , ERR_FILE_CORRUPT , " Error when trying to parse Variant. " ) ;
2017-03-05 16:44:50 +01:00
a [ i ] = val ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
r_v = a ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_RAW_ARRAY : {
uint32_t len = f - > get_32 ( ) ;
2020-02-17 22:06:54 +01:00
Vector < uint8_t > array ;
2014-02-10 02:10:30 +01:00
array . resize ( len ) ;
2020-02-17 22:06:54 +01:00
uint8_t * w = array . ptrw ( ) ;
f - > get_buffer ( w , len ) ;
2014-02-10 02:10:30 +01:00
_advance_padding ( len ) ;
2020-02-17 22:06:54 +01:00
2017-03-05 16:44:50 +01:00
r_v = array ;
2014-02-10 02:10:30 +01:00
} break ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
case VARIANT_INT32_ARRAY : {
2014-02-10 02:10:30 +01:00
uint32_t len = f - > get_32 ( ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
Vector < int32_t > array ;
2014-02-10 02:10:30 +01:00
array . resize ( len ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
int32_t * w = array . ptrw ( ) ;
f - > get_buffer ( ( uint8_t * ) w , len * sizeof ( int32_t ) ) ;
2014-02-10 02:10:30 +01:00
# ifdef BIG_ENDIAN_ENABLED
{
2017-03-05 16:44:50 +01:00
uint32_t * ptr = ( uint32_t * ) w . ptr ( ) ;
for ( int i = 0 ; i < len ; i + + ) {
ptr [ i ] = BSWAP32 ( ptr [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
}
# endif
2020-02-17 22:06:54 +01:00
2017-03-05 16:44:50 +01:00
r_v = array ;
2014-02-10 02:10:30 +01:00
} break ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
case VARIANT_INT64_ARRAY : {
2014-02-10 02:10:30 +01:00
uint32_t len = f - > get_32 ( ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
Vector < int64_t > array ;
2014-02-10 02:10:30 +01:00
array . resize ( len ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
int64_t * w = array . ptrw ( ) ;
f - > get_buffer ( ( uint8_t * ) w , len * sizeof ( int64_t ) ) ;
# ifdef BIG_ENDIAN_ENABLED
{
uint64_t * ptr = ( uint64_t * ) w . ptr ( ) ;
for ( int i = 0 ; i < len ; i + + ) {
ptr [ i ] = BSWAP64 ( ptr [ i ] ) ;
}
}
# endif
r_v = array ;
} break ;
case VARIANT_FLOAT32_ARRAY : {
uint32_t len = f - > get_32 ( ) ;
Vector < float > array ;
array . resize ( len ) ;
float * w = array . ptrw ( ) ;
f - > get_buffer ( ( uint8_t * ) w , len * sizeof ( float ) ) ;
2014-02-10 02:10:30 +01:00
# ifdef BIG_ENDIAN_ENABLED
{
2017-03-05 16:44:50 +01:00
uint32_t * ptr = ( uint32_t * ) w . ptr ( ) ;
for ( int i = 0 ; i < len ; i + + ) {
ptr [ i ] = BSWAP32 ( ptr [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
}
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
# endif
r_v = array ;
} break ;
case VARIANT_FLOAT64_ARRAY : {
uint32_t len = f - > get_32 ( ) ;
Vector < double > array ;
array . resize ( len ) ;
double * w = array . ptrw ( ) ;
f - > get_buffer ( ( uint8_t * ) w , len * sizeof ( double ) ) ;
# ifdef BIG_ENDIAN_ENABLED
{
uint64_t * ptr = ( uint64_t * ) w . ptr ( ) ;
for ( int i = 0 ; i < len ; i + + ) {
ptr [ i ] = BSWAP64 ( ptr [ i ] ) ;
}
}
2014-02-10 02:10:30 +01:00
# endif
2017-03-05 16:44:50 +01:00
r_v = array ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_STRING_ARRAY : {
uint32_t len = f - > get_32 ( ) ;
2020-02-17 22:06:54 +01:00
Vector < String > array ;
2014-02-10 02:10:30 +01:00
array . resize ( len ) ;
2020-02-17 22:06:54 +01:00
String * w = array . ptrw ( ) ;
2020-05-14 16:41:43 +02:00
for ( uint32_t i = 0 ; i < len ; i + + ) {
2017-03-05 16:44:50 +01:00
w [ i ] = get_unicode_string ( ) ;
2020-05-14 16:41:43 +02:00
}
2020-02-17 22:06:54 +01:00
2017-03-05 16:44:50 +01:00
r_v = array ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_VECTOR2_ARRAY : {
uint32_t len = f - > get_32 ( ) ;
2020-02-17 22:06:54 +01:00
Vector < Vector2 > array ;
2014-02-10 02:10:30 +01:00
array . resize ( len ) ;
2020-02-17 22:06:54 +01:00
Vector2 * w = array . ptrw ( ) ;
2017-03-05 16:44:50 +01:00
if ( sizeof ( Vector2 ) = = 8 ) {
2020-02-17 22:06:54 +01:00
f - > get_buffer ( ( uint8_t * ) w , len * sizeof ( real_t ) * 2 ) ;
2014-02-10 02:10:30 +01:00
# ifdef BIG_ENDIAN_ENABLED
2017-03-05 16:44:50 +01:00
{
uint32_t * ptr = ( uint32_t * ) w . ptr ( ) ;
for ( int i = 0 ; i < len * 2 ; i + + ) {
ptr [ i ] = BSWAP32 ( ptr [ i ] ) ;
}
2014-02-10 02:10:30 +01:00
}
# endif
} else {
2019-08-15 04:57:49 +02:00
ERR_FAIL_V_MSG ( ERR_UNAVAILABLE , " Vector2 size is NOT 8! " ) ;
2014-02-10 02:10:30 +01:00
}
2020-02-17 22:06:54 +01:00
2017-03-05 16:44:50 +01:00
r_v = array ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_VECTOR3_ARRAY : {
uint32_t len = f - > get_32 ( ) ;
2020-02-17 22:06:54 +01:00
Vector < Vector3 > array ;
2014-02-10 02:10:30 +01:00
array . resize ( len ) ;
2020-02-17 22:06:54 +01:00
Vector3 * w = array . ptrw ( ) ;
2017-03-05 16:44:50 +01:00
if ( sizeof ( Vector3 ) = = 12 ) {
2020-02-17 22:06:54 +01:00
f - > get_buffer ( ( uint8_t * ) w , len * sizeof ( real_t ) * 3 ) ;
2014-02-10 02:10:30 +01:00
# ifdef BIG_ENDIAN_ENABLED
{
2017-03-05 16:44:50 +01:00
uint32_t * ptr = ( uint32_t * ) w . ptr ( ) ;
for ( int i = 0 ; i < len * 3 ; i + + ) {
ptr [ i ] = BSWAP32 ( ptr [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
}
# endif
} else {
2019-08-15 04:57:49 +02:00
ERR_FAIL_V_MSG ( ERR_UNAVAILABLE , " Vector3 size is NOT 12! " ) ;
2014-02-10 02:10:30 +01:00
}
2020-02-17 22:06:54 +01:00
2017-03-05 16:44:50 +01:00
r_v = array ;
2014-02-10 02:10:30 +01:00
} break ;
case VARIANT_COLOR_ARRAY : {
uint32_t len = f - > get_32 ( ) ;
2020-02-17 22:06:54 +01:00
Vector < Color > array ;
2014-02-10 02:10:30 +01:00
array . resize ( len ) ;
2020-02-17 22:06:54 +01:00
Color * w = array . ptrw ( ) ;
2017-03-05 16:44:50 +01:00
if ( sizeof ( Color ) = = 16 ) {
2020-02-17 22:06:54 +01:00
f - > get_buffer ( ( uint8_t * ) w , len * sizeof ( real_t ) * 4 ) ;
2014-02-10 02:10:30 +01:00
# ifdef BIG_ENDIAN_ENABLED
2017-03-05 16:44:50 +01:00
{
uint32_t * ptr = ( uint32_t * ) w . ptr ( ) ;
for ( int i = 0 ; i < len * 4 ; i + + ) {
ptr [ i ] = BSWAP32 ( ptr [ i ] ) ;
}
2014-02-10 02:10:30 +01:00
}
# endif
} else {
2019-08-15 04:57:49 +02:00
ERR_FAIL_V_MSG ( ERR_UNAVAILABLE , " Color size is NOT 16! " ) ;
2014-02-10 02:10:30 +01:00
}
2020-02-17 22:06:54 +01:00
2017-03-05 16:44:50 +01:00
r_v = array ;
2014-02-10 02:10:30 +01:00
} break ;
default : {
ERR_FAIL_V ( ERR_FILE_CORRUPT ) ;
} break ;
}
return OK ; //never reach anyway
}
2020-02-28 12:27:04 +01:00
void ResourceLoaderBinary : : set_local_path ( const String & p_local_path ) {
2017-03-05 16:44:50 +01:00
res_path = p_local_path ;
2014-02-10 02:10:30 +01:00
}
2020-02-28 12:27:04 +01:00
Ref < Resource > ResourceLoaderBinary : : get_resource ( ) {
2014-02-10 02:10:30 +01:00
return resource ;
}
2020-05-14 14:29:06 +02:00
2020-02-28 12:27:04 +01:00
Error ResourceLoaderBinary : : load ( ) {
2020-05-14 16:41:43 +02:00
if ( error ! = OK ) {
2014-02-10 02:10:30 +01:00
return error ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
int stage = 0 ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
for ( int i = 0 ; i < external_resources . size ( ) ; i + + ) {
String path = external_resources [ i ] . path ;
2017-02-15 12:29:46 +01:00
2015-08-24 01:15:56 +02:00
if ( remaps . has ( path ) ) {
2017-03-05 16:44:50 +01:00
path = remaps [ path ] ;
2015-08-24 01:15:56 +02:00
}
2014-02-10 02:10:30 +01:00
2021-08-30 01:43:47 +02:00
if ( path . find ( " :// " ) = = - 1 & & path . is_relative_path ( ) ) {
2020-02-28 12:27:04 +01:00
// path is relative to file being loaded, so convert to a resource path
path = ProjectSettings : : get_singleton ( ) - > localize_path ( path . get_base_dir ( ) . plus_file ( external_resources [ i ] . path ) ) ;
}
external_resources . write [ i ] . path = path ; //remap happens here, not on load because on load it can actually be used for filesystem dock resource remap
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
if ( ! use_sub_threads ) {
external_resources . write [ i ] . cache = ResourceLoader : : load ( path , external_resources [ i ] . type ) ;
if ( external_resources [ i ] . cache . is_null ( ) ) {
if ( ! ResourceLoader : : get_abort_on_missing_resources ( ) ) {
ResourceLoader : : notify_dependency_error ( local_path , path , external_resources [ i ] . type ) ;
} else {
error = ERR_FILE_MISSING_DEPENDENCIES ;
ERR_FAIL_V_MSG ( error , " Can't load dependency: " + path + " . " ) ;
}
2014-02-10 02:10:30 +01:00
}
} else {
2021-02-11 18:18:45 +01:00
Error err = ResourceLoader : : load_threaded_request ( path , external_resources [ i ] . type , use_sub_threads , ResourceFormatLoader : : CACHE_MODE_REUSE , local_path ) ;
2020-02-28 12:27:04 +01:00
if ( err ! = OK ) {
if ( ! ResourceLoader : : get_abort_on_missing_resources ( ) ) {
ResourceLoader : : notify_dependency_error ( local_path , path , external_resources [ i ] . type ) ;
} else {
error = ERR_FILE_MISSING_DEPENDENCIES ;
ERR_FAIL_V_MSG ( error , " Can't load dependency: " + path + " . " ) ;
}
}
2014-02-10 02:10:30 +01:00
}
stage + + ;
}
2020-02-28 12:27:04 +01:00
for ( int i = 0 ; i < internal_resources . size ( ) ; i + + ) {
bool main = i = = ( internal_resources . size ( ) - 1 ) ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
//maybe it is loaded already
String path ;
2021-07-21 02:36:56 +02:00
String id ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
if ( ! main ) {
2020-05-01 14:34:23 +02:00
path = internal_resources [ i ] . path ;
if ( path . begins_with ( " local:// " ) ) {
path = path . replace_first ( " local:// " , " " ) ;
2021-07-21 02:36:56 +02:00
id = path ;
2020-05-01 14:34:23 +02:00
path = res_path + " :: " + path ;
2021-07-21 02:36:56 +02:00
internal_resources . write [ i ] . path = path ; // Update path.
2020-05-01 14:34:23 +02:00
}
2021-02-11 18:18:45 +01:00
if ( cache_mode = = ResourceFormatLoader : : CACHE_MODE_REUSE ) {
2020-04-20 04:19:21 +02:00
if ( ResourceCache : : has ( path ) ) {
//already loaded, don't do anything
stage + + ;
error = OK ;
continue ;
}
2020-02-28 12:27:04 +01:00
}
} else {
2021-02-11 18:18:45 +01:00
if ( cache_mode ! = ResourceFormatLoader : : CACHE_MODE_IGNORE & & ! ResourceCache : : has ( res_path ) ) {
2020-02-28 12:27:04 +01:00
path = res_path ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
}
2020-02-28 12:27:04 +01:00
uint64_t offset = internal_resources [ i ] . offset ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
f - > seek ( offset ) ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
String t = get_unicode_string ( ) ;
2014-02-10 02:10:30 +01:00
2021-02-11 18:18:45 +01:00
RES res ;
2014-02-10 02:10:30 +01:00
2021-02-11 18:18:45 +01:00
if ( cache_mode = = ResourceFormatLoader : : CACHE_MODE_REPLACE & & ResourceCache : : has ( path ) ) {
//use the existing one
Resource * r = ResourceCache : : get ( path ) ;
if ( r - > get_class ( ) = = t ) {
r - > reset_state ( ) ;
res = Ref < Resource > ( r ) ;
}
2020-02-28 12:27:04 +01:00
}
2014-02-10 02:10:30 +01:00
2021-02-11 18:18:45 +01:00
if ( res . is_null ( ) ) {
//did not replace
2014-02-10 02:10:30 +01:00
2021-06-18 00:03:09 +02:00
Object * obj = ClassDB : : instantiate ( t ) ;
2021-02-11 18:18:45 +01:00
if ( ! obj ) {
error = ERR_FILE_CORRUPT ;
ERR_FAIL_V_MSG ( ERR_FILE_CORRUPT , local_path + " :Resource of unrecognized type in file: " + t + " . " ) ;
}
Resource * r = Object : : cast_to < Resource > ( obj ) ;
if ( ! r ) {
String obj_class = obj - > get_class ( ) ;
error = ERR_FILE_CORRUPT ;
memdelete ( obj ) ; //bye
ERR_FAIL_V_MSG ( ERR_FILE_CORRUPT , local_path + " :Resource type in resource field not a resource, type is: " + obj_class + " . " ) ;
}
res = RES ( r ) ;
if ( path ! = String ( ) & & cache_mode ! = ResourceFormatLoader : : CACHE_MODE_IGNORE ) {
r - > set_path ( path , cache_mode = = ResourceFormatLoader : : CACHE_MODE_REPLACE ) ; //if got here because the resource with same path has different type, replace it
}
2021-07-21 02:36:56 +02:00
r - > set_scene_unique_id ( id ) ;
2020-04-20 04:19:21 +02:00
}
2014-02-10 02:10:30 +01:00
2020-04-20 04:19:21 +02:00
if ( ! main ) {
2020-05-01 14:34:23 +02:00
internal_index_cache [ path ] = res ;
2020-04-20 04:19:21 +02:00
}
2020-02-28 12:27:04 +01:00
int pc = f - > get_32 ( ) ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
//set properties
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
for ( int j = 0 ; j < pc ; j + + ) {
StringName name = _get_string ( ) ;
2017-12-15 12:38:24 +01:00
2020-02-28 12:27:04 +01:00
if ( name = = StringName ( ) ) {
error = ERR_FILE_CORRUPT ;
ERR_FAIL_V ( ERR_FILE_CORRUPT ) ;
}
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
Variant value ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
error = parse_variant ( value ) ;
2020-05-14 16:41:43 +02:00
if ( error ) {
2020-02-28 12:27:04 +01:00
return error ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
res - > set ( name , value ) ;
}
2014-02-10 02:10:30 +01:00
# ifdef TOOLS_ENABLED
2020-02-28 12:27:04 +01:00
res - > set_edited ( false ) ;
2014-02-10 02:10:30 +01:00
# endif
2020-02-28 12:27:04 +01:00
stage + + ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
if ( progress ) {
* progress = ( i + 1 ) / float ( internal_resources . size ( ) ) ;
}
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
resource_cache . push_back ( res ) ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
if ( main ) {
f - > close ( ) ;
resource = res ;
resource - > set_as_translation_remapped ( translation_remapped ) ;
error = OK ;
return OK ;
}
2014-02-10 02:10:30 +01:00
}
2020-02-28 12:27:04 +01:00
return ERR_FILE_EOF ;
2014-02-10 02:10:30 +01:00
}
2020-02-28 12:27:04 +01:00
void ResourceLoaderBinary : : set_translation_remapped ( bool p_remapped ) {
2017-06-28 22:00:18 +02:00
translation_remapped = p_remapped ;
}
2017-03-05 16:44:50 +01:00
static void save_ustring ( FileAccess * f , const String & p_string ) {
2015-08-24 01:15:56 +02:00
CharString utf8 = p_string . utf8 ( ) ;
2017-03-05 16:44:50 +01:00
f - > store_32 ( utf8 . length ( ) + 1 ) ;
f - > store_buffer ( ( const uint8_t * ) utf8 . get_data ( ) , utf8 . length ( ) + 1 ) ;
2015-08-24 01:15:56 +02:00
}
static String get_ustring ( FileAccess * f ) {
int len = f - > get_32 ( ) ;
Vector < char > str_buf ;
str_buf . resize ( len ) ;
2017-03-05 16:44:50 +01:00
f - > get_buffer ( ( uint8_t * ) & str_buf [ 0 ] , len ) ;
2015-08-24 01:15:56 +02:00
String s ;
s . parse_utf8 ( & str_buf [ 0 ] ) ;
return s ;
}
2020-02-28 12:27:04 +01:00
String ResourceLoaderBinary : : get_unicode_string ( ) {
2014-02-10 02:10:30 +01:00
int len = f - > get_32 ( ) ;
2017-03-05 16:44:50 +01:00
if ( len > str_buf . size ( ) ) {
2014-02-10 02:10:30 +01:00
str_buf . resize ( len ) ;
}
2020-05-14 16:41:43 +02:00
if ( len = = 0 ) {
2015-12-09 13:08:41 +01:00
return String ( ) ;
2020-05-14 16:41:43 +02:00
}
2017-03-05 16:44:50 +01:00
f - > get_buffer ( ( uint8_t * ) & str_buf [ 0 ] , len ) ;
2014-02-10 02:10:30 +01:00
String s ;
s . parse_utf8 ( & str_buf [ 0 ] ) ;
return s ;
}
2020-02-28 12:27:04 +01:00
void ResourceLoaderBinary : : get_dependencies ( FileAccess * p_f , List < String > * p_dependencies , bool p_add_types ) {
2021-07-23 21:01:18 +02:00
open ( p_f , false , true ) ;
2020-05-14 16:41:43 +02:00
if ( error ) {
2014-02-10 02:10:30 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < external_resources . size ( ) ; i + + ) {
2021-07-23 21:01:18 +02:00
String dep ;
if ( external_resources [ i ] . uid ! = ResourceUID : : INVALID_ID ) {
dep = ResourceUID : : get_singleton ( ) - > id_to_text ( external_resources [ i ] . uid ) ;
} else {
dep = external_resources [ i ] . path ;
}
2014-03-14 02:57:24 +01:00
2017-03-05 16:44:50 +01:00
if ( p_add_types & & external_resources [ i ] . type ! = String ( ) ) {
dep + = " :: " + external_resources [ i ] . type ;
2015-08-24 01:15:56 +02:00
}
2014-03-14 02:57:24 +01:00
p_dependencies - > push_back ( dep ) ;
2014-02-10 02:10:30 +01:00
}
}
2021-07-23 21:01:18 +02:00
void ResourceLoaderBinary : : open ( FileAccess * p_f , bool p_no_resources , bool p_keep_uuid_paths ) {
2017-03-05 16:44:50 +01:00
error = OK ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
f = p_f ;
2014-02-10 02:10:30 +01:00
uint8_t header [ 4 ] ;
2017-03-05 16:44:50 +01:00
f - > get_buffer ( header , 4 ) ;
if ( header [ 0 ] = = ' R ' & & header [ 1 ] = = ' S ' & & header [ 2 ] = = ' C ' & & header [ 3 ] = = ' C ' ) {
2020-02-11 04:39:20 +01:00
// Compressed.
2017-03-05 16:44:50 +01:00
FileAccessCompressed * fac = memnew ( FileAccessCompressed ) ;
2020-02-11 04:39:20 +01:00
error = fac - > open_after_magic ( f ) ;
if ( error ! = OK ) {
memdelete ( fac ) ;
f - > close ( ) ;
ERR_FAIL_MSG ( " Failed to open binary resource file: " + local_path + " . " ) ;
}
2017-03-05 16:44:50 +01:00
f = fac ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( header [ 0 ] ! = ' R ' | | header [ 1 ] ! = ' S ' | | header [ 2 ] ! = ' R ' | | header [ 3 ] ! = ' C ' ) {
2020-02-11 04:39:20 +01:00
// Not normal.
2017-03-05 16:44:50 +01:00
error = ERR_FILE_UNRECOGNIZED ;
2020-02-11 04:39:20 +01:00
f - > close ( ) ;
2019-08-15 04:57:49 +02:00
ERR_FAIL_MSG ( " Unrecognized binary resource file: " + local_path + " . " ) ;
2014-02-10 02:10:30 +01:00
}
bool big_endian = f - > get_32 ( ) ;
bool use_real64 = f - > get_32 ( ) ;
2021-05-20 14:58:03 +02:00
f - > set_big_endian ( big_endian ! = 0 ) ; //read big endian if saved as big endian
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
uint32_t ver_major = f - > get_32 ( ) ;
uint32_t ver_minor = f - > get_32 ( ) ;
2017-11-22 12:34:43 +01:00
ver_format = f - > get_32 ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
print_bl ( " big endian: " + itos ( big_endian ) ) ;
2017-09-02 22:32:31 +02:00
# ifdef BIG_ENDIAN_ENABLED
print_bl ( " endian swap: " + itos ( ! big_endian ) ) ;
# else
print_bl ( " endian swap: " + itos ( big_endian ) ) ;
# endif
2017-03-05 16:44:50 +01:00
print_bl ( " real64: " + itos ( use_real64 ) ) ;
print_bl ( " major: " + itos ( ver_major ) ) ;
print_bl ( " minor: " + itos ( ver_minor ) ) ;
print_bl ( " format: " + itos ( ver_format ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ver_format > FORMAT_VERSION | | ver_major > VERSION_MAJOR ) {
2014-02-10 02:10:30 +01:00
f - > close ( ) ;
2020-10-05 13:52:34 +02:00
ERR_FAIL_MSG ( vformat ( " File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s). " ,
local_path , ver_format , ver_major , ver_minor , VERSION_BRANCH ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
type = get_unicode_string ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
print_bl ( " type: " + type ) ;
2014-02-10 02:10:30 +01:00
importmd_ofs = f - > get_64 ( ) ;
2021-07-21 02:36:56 +02:00
uint32_t flags = f - > get_32 ( ) ;
if ( flags & ResourceFormatSaverBinaryInstance : : FORMAT_FLAG_NAMED_SCENE_IDS ) {
using_named_scene_ids = true ;
}
2021-07-23 21:01:18 +02:00
if ( flags & ResourceFormatSaverBinaryInstance : : FORMAT_FLAG_UIDS ) {
using_uids = true ;
}
if ( using_uids ) {
uid = f - > get_64 ( ) ;
} else {
2021-07-30 07:25:18 +02:00
f - > get_64 ( ) ; // skip over uid field
2021-07-23 21:01:18 +02:00
uid = ResourceUID : : INVALID_ID ;
}
2021-07-30 07:24:29 +02:00
for ( int i = 0 ; i < ResourceFormatSaverBinaryInstance : : RESERVED_FIELDS ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > get_32 ( ) ; //skip a few reserved fields
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2021-07-23 21:01:18 +02:00
if ( p_no_resources ) {
return ;
}
2017-03-05 16:44:50 +01:00
uint32_t string_table_size = f - > get_32 ( ) ;
2014-02-10 02:10:30 +01:00
string_map . resize ( string_table_size ) ;
2017-03-05 16:44:50 +01:00
for ( uint32_t i = 0 ; i < string_table_size ; i + + ) {
2014-02-10 02:10:30 +01:00
StringName s = get_unicode_string ( ) ;
2018-07-25 03:11:03 +02:00
string_map . write [ i ] = s ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
print_bl ( " strings: " + itos ( string_table_size ) ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
uint32_t ext_resources_size = f - > get_32 ( ) ;
for ( uint32_t i = 0 ; i < ext_resources_size ; i + + ) {
2017-08-08 07:02:49 +02:00
ExtResource er ;
2017-03-05 16:44:50 +01:00
er . type = get_unicode_string ( ) ;
er . path = get_unicode_string ( ) ;
2021-07-23 21:01:18 +02:00
if ( using_uids ) {
er . uid = f - > get_64 ( ) ;
if ( ! p_keep_uuid_paths & & er . uid ! = ResourceUID : : INVALID_ID ) {
if ( ResourceUID : : get_singleton ( ) - > has_id ( er . uid ) ) {
// If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
er . path = ResourceUID : : get_singleton ( ) - > get_id_path ( er . uid ) ;
} else {
Fix various typos
Follow-up typos found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,fave,findn,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint`
2021-07-25 13:18:25 +02:00
WARN_PRINT ( String ( res_path + " : In external resource # " + itos ( i ) + " , invalid UUID: " + ResourceUID : : get_singleton ( ) - > id_to_text ( er . uid ) + " - using text path instead: " + er . path ) . utf8 ( ) . get_data ( ) ) ;
2021-07-23 21:01:18 +02:00
}
}
}
2017-12-15 12:38:24 +01:00
2014-02-10 02:10:30 +01:00
external_resources . push_back ( er ) ;
}
2017-03-05 16:44:50 +01:00
print_bl ( " ext resources: " + itos ( ext_resources_size ) ) ;
uint32_t int_resources_size = f - > get_32 ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( uint32_t i = 0 ; i < int_resources_size ; i + + ) {
2017-08-08 07:02:49 +02:00
IntResource ir ;
2017-03-05 16:44:50 +01:00
ir . path = get_unicode_string ( ) ;
ir . offset = f - > get_64 ( ) ;
2014-02-10 02:10:30 +01:00
internal_resources . push_back ( ir ) ;
}
2017-03-05 16:44:50 +01:00
print_bl ( " int resources: " + itos ( int_resources_size ) ) ;
2014-02-10 02:10:30 +01:00
if ( f - > eof_reached ( ) ) {
2017-03-05 16:44:50 +01:00
error = ERR_FILE_CORRUPT ;
2020-02-11 04:39:20 +01:00
f - > close ( ) ;
2019-08-15 04:57:49 +02:00
ERR_FAIL_MSG ( " Premature end of file (EOF): " + local_path + " . " ) ;
2014-02-10 02:10:30 +01:00
}
}
2020-02-28 12:27:04 +01:00
String ResourceLoaderBinary : : recognize ( FileAccess * p_f ) {
2017-03-05 16:44:50 +01:00
error = OK ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
f = p_f ;
2014-02-10 02:10:30 +01:00
uint8_t header [ 4 ] ;
2017-03-05 16:44:50 +01:00
f - > get_buffer ( header , 4 ) ;
if ( header [ 0 ] = = ' R ' & & header [ 1 ] = = ' S ' & & header [ 2 ] = = ' C ' & & header [ 3 ] = = ' C ' ) {
2020-02-11 04:39:20 +01:00
// Compressed.
2017-03-05 16:44:50 +01:00
FileAccessCompressed * fac = memnew ( FileAccessCompressed ) ;
2020-02-11 04:39:20 +01:00
error = fac - > open_after_magic ( f ) ;
if ( error ! = OK ) {
memdelete ( fac ) ;
f - > close ( ) ;
return " " ;
}
2017-03-05 16:44:50 +01:00
f = fac ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
} else if ( header [ 0 ] ! = ' R ' | | header [ 1 ] ! = ' S ' | | header [ 2 ] ! = ' R ' | | header [ 3 ] ! = ' C ' ) {
2020-02-11 04:39:20 +01:00
// Not normal.
2017-03-05 16:44:50 +01:00
error = ERR_FILE_UNRECOGNIZED ;
2020-02-11 04:39:20 +01:00
f - > close ( ) ;
2014-02-10 02:10:30 +01:00
return " " ;
}
bool big_endian = f - > get_32 ( ) ;
2017-09-02 22:32:31 +02:00
f - > get_32 ( ) ; // use_real64
2014-02-10 02:10:30 +01:00
2021-05-20 14:58:03 +02:00
f - > set_big_endian ( big_endian ! = 0 ) ; //read big endian if saved as big endian
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
uint32_t ver_major = f - > get_32 ( ) ;
2017-09-02 22:32:31 +02:00
f - > get_32 ( ) ; // ver_minor
2017-03-05 16:44:50 +01:00
uint32_t ver_format = f - > get_32 ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ver_format > FORMAT_VERSION | | ver_major > VERSION_MAJOR ) {
2014-02-10 02:10:30 +01:00
f - > close ( ) ;
return " " ;
}
2017-03-05 16:44:50 +01:00
String type = get_unicode_string ( ) ;
2014-02-10 02:10:30 +01:00
return type ;
}
2020-02-28 12:27:04 +01:00
ResourceLoaderBinary : : ~ ResourceLoaderBinary ( ) {
2020-05-14 16:41:43 +02:00
if ( f ) {
2014-02-10 02:10:30 +01:00
memdelete ( f ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
}
2021-02-11 18:18:45 +01:00
RES ResourceFormatLoaderBinary : : load ( const String & p_path , const String & p_original_path , Error * r_error , bool p_use_sub_threads , float * r_progress , CacheMode p_cache_mode ) {
2020-05-14 16:41:43 +02:00
if ( r_error ) {
2017-03-05 16:44:50 +01:00
* r_error = ERR_FILE_CANT_OPEN ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
Error err ;
2017-03-05 16:44:50 +01:00
FileAccess * f = FileAccess : : open ( p_path , FileAccess : : READ , & err ) ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
ERR_FAIL_COND_V_MSG ( err ! = OK , RES ( ) , " Cannot open file ' " + p_path + " '. " ) ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
ResourceLoaderBinary loader ;
2021-02-11 18:18:45 +01:00
loader . cache_mode = p_cache_mode ;
2020-02-28 12:27:04 +01:00
loader . use_sub_threads = p_use_sub_threads ;
loader . progress = r_progress ;
2017-08-31 23:57:03 +02:00
String path = p_original_path ! = " " ? p_original_path : p_path ;
2020-02-28 12:27:04 +01:00
loader . local_path = ProjectSettings : : get_singleton ( ) - > localize_path ( path ) ;
loader . res_path = loader . local_path ;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader . open ( f ) ;
err = loader . load ( ) ;
if ( r_error ) {
* r_error = err ;
}
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
if ( err ) {
return RES ( ) ;
}
return loader . resource ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
void ResourceFormatLoaderBinary : : get_recognized_extensions_for_type ( const String & p_type , List < String > * p_extensions ) const {
if ( p_type = = " " ) {
2014-02-10 02:10:30 +01:00
get_recognized_extensions ( p_extensions ) ;
return ;
}
List < String > extensions ;
2017-03-05 16:44:50 +01:00
ClassDB : : get_extensions_for_type ( p_type , & extensions ) ;
2014-02-10 02:10:30 +01:00
extensions . sort ( ) ;
2021-07-24 15:46:25 +02:00
for ( const String & E : extensions ) {
2021-07-16 05:45:57 +02:00
String ext = E . to_lower ( ) ;
2014-02-10 02:10:30 +01:00
p_extensions - > push_back ( ext ) ;
}
}
2020-05-14 14:29:06 +02:00
2017-03-05 16:44:50 +01:00
void ResourceFormatLoaderBinary : : get_recognized_extensions ( List < String > * p_extensions ) const {
2014-02-10 02:10:30 +01:00
List < String > extensions ;
2017-01-03 03:03:46 +01:00
ClassDB : : get_resource_base_extensions ( & extensions ) ;
2014-02-10 02:10:30 +01:00
extensions . sort ( ) ;
2021-07-24 15:46:25 +02:00
for ( const String & E : extensions ) {
2021-07-16 05:45:57 +02:00
String ext = E . to_lower ( ) ;
2014-02-10 02:10:30 +01:00
p_extensions - > push_back ( ext ) ;
}
}
2017-03-05 16:44:50 +01:00
bool ResourceFormatLoaderBinary : : handles_type ( const String & p_type ) const {
2014-02-10 02:10:30 +01:00
return true ; //handles all
}
2017-03-05 16:44:50 +01:00
void ResourceFormatLoaderBinary : : get_dependencies ( const String & p_path , List < String > * p_dependencies , bool p_add_types ) {
FileAccess * f = FileAccess : : open ( p_path , FileAccess : : READ ) ;
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_MSG ( ! f , " Cannot open file ' " + p_path + " '. " ) ;
2014-02-10 02:10:30 +01:00
2020-02-28 12:27:04 +01:00
ResourceLoaderBinary loader ;
loader . local_path = ProjectSettings : : get_singleton ( ) - > localize_path ( p_path ) ;
loader . res_path = loader . local_path ;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader . get_dependencies ( f , p_dependencies , p_add_types ) ;
2015-08-24 01:15:56 +02:00
}
2017-03-05 16:44:50 +01:00
Error ResourceFormatLoaderBinary : : rename_dependencies ( const String & p_path , const Map < String , String > & p_map ) {
2017-01-14 12:26:56 +01:00
//Error error=OK;
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
FileAccess * f = FileAccess : : open ( p_path , FileAccess : : READ ) ;
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( ! f , ERR_CANT_OPEN , " Cannot open file ' " + p_path + " '. " ) ;
2015-08-24 01:15:56 +02:00
2020-04-02 01:20:12 +02:00
FileAccess * fw = nullptr ; //=FileAccess::open(p_path+".depren");
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
String local_path = p_path . get_base_dir ( ) ;
2015-08-24 01:15:56 +02:00
uint8_t header [ 4 ] ;
2017-03-05 16:44:50 +01:00
f - > get_buffer ( header , 4 ) ;
if ( header [ 0 ] = = ' R ' & & header [ 1 ] = = ' S ' & & header [ 2 ] = = ' C ' & & header [ 3 ] = = ' C ' ) {
2020-02-11 04:39:20 +01:00
// Compressed.
2017-03-05 16:44:50 +01:00
FileAccessCompressed * fac = memnew ( FileAccessCompressed ) ;
2020-02-11 04:39:20 +01:00
Error err = fac - > open_after_magic ( f ) ;
if ( err ! = OK ) {
memdelete ( fac ) ;
memdelete ( f ) ;
ERR_FAIL_V_MSG ( err , " Cannot open file ' " + p_path + " '. " ) ;
}
2017-03-05 16:44:50 +01:00
f = fac ;
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
FileAccessCompressed * facw = memnew ( FileAccessCompressed ) ;
2015-08-24 01:15:56 +02:00
facw - > configure ( " RSCC " ) ;
2020-02-11 04:39:20 +01:00
err = facw - > _open ( p_path + " .depren " , FileAccess : : WRITE ) ;
2015-08-24 01:15:56 +02:00
if ( err ) {
memdelete ( fac ) ;
memdelete ( facw ) ;
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( err , ERR_FILE_CORRUPT , " Cannot create file ' " + p_path + " .depren'. " ) ;
2015-08-24 01:15:56 +02:00
}
2017-03-05 16:44:50 +01:00
fw = facw ;
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
} else if ( header [ 0 ] ! = ' R ' | | header [ 1 ] ! = ' S ' | | header [ 2 ] ! = ' R ' | | header [ 3 ] ! = ' C ' ) {
2020-02-11 04:39:20 +01:00
// Not normal.
2015-08-24 01:15:56 +02:00
memdelete ( f ) ;
2019-09-25 10:28:50 +02:00
ERR_FAIL_V_MSG ( ERR_FILE_UNRECOGNIZED , " Unrecognized binary resource file ' " + local_path + " '. " ) ;
2015-08-24 01:15:56 +02:00
} else {
2017-03-05 16:44:50 +01:00
fw = FileAccess : : open ( p_path + " .depren " , FileAccess : : WRITE ) ;
2015-08-24 01:15:56 +02:00
if ( ! fw ) {
memdelete ( f ) ;
}
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( ! fw , ERR_CANT_CREATE , " Cannot create file ' " + p_path + " .depren'. " ) ;
2017-09-03 05:22:54 +02:00
uint8_t magic [ 4 ] = { ' R ' , ' S ' , ' R ' , ' C ' } ;
fw - > store_buffer ( magic , 4 ) ;
2015-08-24 01:15:56 +02:00
}
bool big_endian = f - > get_32 ( ) ;
bool use_real64 = f - > get_32 ( ) ;
2021-05-20 14:58:03 +02:00
f - > set_big_endian ( big_endian ! = 0 ) ; //read big endian if saved as big endian
2017-09-02 22:32:31 +02:00
# ifdef BIG_ENDIAN_ENABLED
fw - > store_32 ( ! big_endian ) ;
# else
fw - > store_32 ( big_endian ) ;
# endif
2021-05-20 14:58:03 +02:00
fw - > set_big_endian ( big_endian ! = 0 ) ;
2015-08-24 01:15:56 +02:00
fw - > store_32 ( use_real64 ) ; //use real64
2017-03-05 16:44:50 +01:00
uint32_t ver_major = f - > get_32 ( ) ;
uint32_t ver_minor = f - > get_32 ( ) ;
uint32_t ver_format = f - > get_32 ( ) ;
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
if ( ver_format < FORMAT_VERSION_CAN_RENAME_DEPS ) {
2015-08-24 01:15:56 +02:00
memdelete ( f ) ;
memdelete ( fw ) ;
DirAccess * da = DirAccess : : create ( DirAccess : : ACCESS_FILESYSTEM ) ;
2017-03-05 16:44:50 +01:00
da - > remove ( p_path + " .depren " ) ;
2015-08-24 01:15:56 +02:00
memdelete ( da ) ;
2018-02-17 14:00:39 +01:00
//use the old approach
2015-08-24 01:15:56 +02:00
2019-11-07 09:44:15 +01:00
WARN_PRINT ( " This file is old, so it can't refactor dependencies, opening and resaving ' " + p_path + " '. " ) ;
2015-08-24 01:15:56 +02:00
Error err ;
2017-03-05 16:44:50 +01:00
f = FileAccess : : open ( p_path , FileAccess : : READ , & err ) ;
2019-06-20 16:59:48 +02:00
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( err ! = OK , ERR_FILE_CANT_OPEN , " Cannot open file ' " + p_path + " '. " ) ;
2015-08-24 01:15:56 +02:00
2020-02-28 12:27:04 +01:00
ResourceLoaderBinary loader ;
loader . local_path = ProjectSettings : : get_singleton ( ) - > localize_path ( p_path ) ;
loader . res_path = loader . local_path ;
loader . remaps = p_map ;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader . open ( f ) ;
2015-08-24 01:15:56 +02:00
2020-02-28 12:27:04 +01:00
err = loader . load ( ) ;
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( err ! = ERR_FILE_EOF , ERR_FILE_CORRUPT ) ;
2020-02-28 12:27:04 +01:00
RES res = loader . get_resource ( ) ;
2017-03-05 16:44:50 +01:00
ERR_FAIL_COND_V ( ! res . is_valid ( ) , ERR_FILE_CORRUPT ) ;
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
return ResourceFormatSaverBinary : : singleton - > save ( p_path , res ) ;
2015-08-24 01:15:56 +02:00
}
2017-03-05 16:44:50 +01:00
if ( ver_format > FORMAT_VERSION | | ver_major > VERSION_MAJOR ) {
2015-08-24 01:15:56 +02:00
memdelete ( f ) ;
memdelete ( fw ) ;
2020-10-05 13:52:34 +02:00
ERR_FAIL_V_MSG ( ERR_FILE_UNRECOGNIZED ,
vformat ( " File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s). " ,
local_path , ver_format , ver_major , ver_minor , VERSION_BRANCH ) ) ;
2015-08-24 01:15:56 +02:00
}
2018-03-19 16:37:57 +01:00
// Since we're not actually converting the file contents, leave the version
// numbers in the file untouched.
fw - > store_32 ( ver_major ) ;
fw - > store_32 ( ver_minor ) ;
fw - > store_32 ( ver_format ) ;
2015-08-24 01:15:56 +02:00
2017-03-05 16:44:50 +01:00
save_ustring ( fw , get_ustring ( f ) ) ; //type
2015-08-24 01:15:56 +02:00
2019-03-26 18:51:13 +01:00
uint64_t md_ofs = f - > get_position ( ) ;
uint64_t importmd_ofs = f - > get_64 ( ) ;
2015-08-24 01:15:56 +02:00
fw - > store_64 ( 0 ) ; //metadata offset
2021-07-23 21:01:18 +02:00
uint32_t flags = f - > get_32 ( ) ;
bool using_uids = ( flags & ResourceFormatSaverBinaryInstance : : FORMAT_FLAG_UIDS ) ;
uint64_t uid_data = f - > get_64 ( ) ;
fw - > store_32 ( flags ) ;
fw - > store_64 ( uid_data ) ;
2021-07-30 07:24:29 +02:00
for ( int i = 0 ; i < ResourceFormatSaverBinaryInstance : : RESERVED_FIELDS ; i + + ) {
2021-07-30 03:17:49 +02:00
fw - > store_32 ( 0 ) ; // reserved
2015-08-24 01:15:56 +02:00
f - > get_32 ( ) ;
}
//string table
2017-03-05 16:44:50 +01:00
uint32_t string_table_size = f - > get_32 ( ) ;
2015-08-24 01:15:56 +02:00
fw - > store_32 ( string_table_size ) ;
2017-03-05 16:44:50 +01:00
for ( uint32_t i = 0 ; i < string_table_size ; i + + ) {
2015-08-24 01:15:56 +02:00
String s = get_ustring ( f ) ;
2017-03-05 16:44:50 +01:00
save_ustring ( fw , s ) ;
2015-08-24 01:15:56 +02:00
}
//external resources
2017-03-05 16:44:50 +01:00
uint32_t ext_resources_size = f - > get_32 ( ) ;
2015-08-24 01:15:56 +02:00
fw - > store_32 ( ext_resources_size ) ;
2017-03-05 16:44:50 +01:00
for ( uint32_t i = 0 ; i < ext_resources_size ; i + + ) {
2015-08-24 01:15:56 +02:00
String type = get_ustring ( f ) ;
String path = get_ustring ( f ) ;
2021-07-23 21:01:18 +02:00
if ( using_uids ) {
ResourceUID : : ID uid = f - > get_64 ( ) ;
if ( uid ! = ResourceUID : : INVALID_ID ) {
if ( ResourceUID : : get_singleton ( ) - > has_id ( uid ) ) {
// If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
path = ResourceUID : : get_singleton ( ) - > get_id_path ( uid ) ;
}
}
}
2017-03-05 16:44:50 +01:00
bool relative = false ;
2015-08-24 01:15:56 +02:00
if ( ! path . begins_with ( " res:// " ) ) {
2017-03-05 16:44:50 +01:00
path = local_path . plus_file ( path ) . simplify_path ( ) ;
relative = true ;
2015-08-24 01:15:56 +02:00
}
if ( p_map . has ( path ) ) {
2017-03-05 16:44:50 +01:00
String np = p_map [ path ] ;
path = np ;
2015-08-24 01:15:56 +02:00
}
2021-07-23 21:01:18 +02:00
String full_path = path ;
2015-08-24 01:15:56 +02:00
if ( relative ) {
//restore relative
2017-03-05 16:44:50 +01:00
path = local_path . path_to_file ( path ) ;
2015-08-24 01:15:56 +02:00
}
2017-03-05 16:44:50 +01:00
save_ustring ( fw , type ) ;
save_ustring ( fw , path ) ;
2021-07-23 21:01:18 +02:00
if ( using_uids ) {
ResourceUID : : ID uid = ResourceSaver : : get_resource_id_for_path ( full_path ) ;
2021-07-30 03:17:49 +02:00
fw - > store_64 ( uid ) ;
2021-07-23 21:01:18 +02:00
}
2015-08-24 01:15:56 +02:00
}
2017-09-10 15:37:49 +02:00
int64_t size_diff = ( int64_t ) fw - > get_position ( ) - ( int64_t ) f - > get_position ( ) ;
2015-08-24 01:15:56 +02:00
//internal resources
2017-03-05 16:44:50 +01:00
uint32_t int_resources_size = f - > get_32 ( ) ;
2015-08-24 01:15:56 +02:00
fw - > store_32 ( int_resources_size ) ;
2017-03-05 16:44:50 +01:00
for ( uint32_t i = 0 ; i < int_resources_size ; i + + ) {
String path = get_ustring ( f ) ;
uint64_t offset = f - > get_64 ( ) ;
save_ustring ( fw , path ) ;
fw - > store_64 ( offset + size_diff ) ;
2015-08-24 01:15:56 +02:00
}
//rest of file
uint8_t b = f - > get_8 ( ) ;
2017-03-05 16:44:50 +01:00
while ( ! f - > eof_reached ( ) ) {
2015-08-24 01:15:56 +02:00
fw - > store_8 ( b ) ;
b = f - > get_8 ( ) ;
}
2017-03-05 16:44:50 +01:00
bool all_ok = fw - > get_error ( ) = = OK ;
2015-08-24 01:15:56 +02:00
fw - > seek ( md_ofs ) ;
2017-03-05 16:44:50 +01:00
fw - > store_64 ( importmd_ofs + size_diff ) ;
2015-08-24 01:15:56 +02:00
memdelete ( f ) ;
memdelete ( fw ) ;
if ( ! all_ok ) {
return ERR_CANT_CREATE ;
}
DirAccess * da = DirAccess : : create ( DirAccess : : ACCESS_RESOURCES ) ;
da - > remove ( p_path ) ;
2017-03-05 16:44:50 +01:00
da - > rename ( p_path + " .depren " , p_path ) ;
2015-08-24 01:15:56 +02:00
memdelete ( da ) ;
return OK ;
2014-02-10 02:10:30 +01:00
}
String ResourceFormatLoaderBinary : : get_resource_type ( const String & p_path ) const {
2017-03-05 16:44:50 +01:00
FileAccess * f = FileAccess : : open ( p_path , FileAccess : : READ ) ;
2014-02-10 02:10:30 +01:00
if ( ! f ) {
2021-03-12 14:35:16 +01:00
return " " ; //could not read
2014-02-10 02:10:30 +01:00
}
2020-02-28 12:27:04 +01:00
ResourceLoaderBinary loader ;
loader . local_path = ProjectSettings : : get_singleton ( ) - > localize_path ( p_path ) ;
loader . res_path = loader . local_path ;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
String r = loader . recognize ( f ) ;
2019-10-03 22:39:08 +02:00
return ClassDB : : get_compatibility_remapped_class ( r ) ;
2014-02-10 02:10:30 +01:00
}
2021-07-23 21:01:18 +02:00
ResourceUID : : ID ResourceFormatLoaderBinary : : get_resource_uid ( const String & p_path ) const {
String ext = p_path . get_extension ( ) . to_lower ( ) ;
if ( ! ClassDB : : is_resource_extension ( ext ) ) {
return ResourceUID : : INVALID_ID ;
}
FileAccess * f = FileAccess : : open ( p_path , FileAccess : : READ ) ;
if ( ! f ) {
return ResourceUID : : INVALID_ID ; //could not read
}
ResourceLoaderBinary loader ;
loader . local_path = ProjectSettings : : get_singleton ( ) - > localize_path ( p_path ) ;
loader . res_path = loader . local_path ;
//loader.set_local_path( Globals::get_singleton()->localize_path(p_path) );
loader . open ( f , true ) ;
if ( loader . error ! = OK ) {
return ResourceUID : : INVALID_ID ; //could not read
}
return loader . uid ;
}
2014-02-10 02:10:30 +01:00
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
2017-12-15 12:38:24 +01:00
void ResourceFormatSaverBinaryInstance : : _pad_buffer ( FileAccess * f , int p_bytes ) {
2017-03-05 16:44:50 +01:00
int extra = 4 - ( p_bytes % 4 ) ;
if ( extra < 4 ) {
2020-05-14 16:41:43 +02:00
for ( int i = 0 ; i < extra ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > store_8 ( 0 ) ; //pad to 32
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
}
}
2021-07-21 02:36:56 +02:00
void ResourceFormatSaverBinaryInstance : : write_variant ( FileAccess * f , const Variant & p_property , Map < RES , int > & resource_map , Map < RES , int > & external_resources , Map < StringName , int > & string_map , const PropertyInfo & p_hint ) {
2017-03-05 16:44:50 +01:00
switch ( p_property . get_type ( ) ) {
2014-02-10 02:10:30 +01:00
case Variant : : NIL : {
f - > store_32 ( VARIANT_NIL ) ;
// don't store anything
} break ;
case Variant : : BOOL : {
f - > store_32 ( VARIANT_BOOL ) ;
2017-03-05 16:44:50 +01:00
bool val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( val ) ;
} break ;
case Variant : : INT : {
2017-01-09 00:58:39 +01:00
int64_t val = p_property ;
2018-10-04 15:38:52 +02:00
if ( val > 0x7FFFFFFF | | val < - ( int64_t ) 0x80000000 ) {
2017-01-09 00:58:39 +01:00
f - > store_32 ( VARIANT_INT64 ) ;
f - > store_64 ( val ) ;
} else {
f - > store_32 ( VARIANT_INT ) ;
2019-02-12 21:10:08 +01:00
f - > store_32 ( int32_t ( p_property ) ) ;
2017-01-09 00:58:39 +01:00
}
2014-02-10 02:10:30 +01:00
} break ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
case Variant : : FLOAT : {
2017-01-09 00:58:39 +01:00
double d = p_property ;
float fl = d ;
2017-03-05 16:44:50 +01:00
if ( double ( fl ) ! = d ) {
2017-01-09 00:58:39 +01:00
f - > store_32 ( VARIANT_DOUBLE ) ;
f - > store_double ( d ) ;
} else {
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
f - > store_32 ( VARIANT_FLOAT ) ;
2017-01-09 00:58:39 +01:00
f - > store_real ( fl ) ;
}
2014-02-10 02:10:30 +01:00
} break ;
case Variant : : STRING : {
f - > store_32 ( VARIANT_STRING ) ;
2017-03-05 16:44:50 +01:00
String val = p_property ;
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , val ) ;
2014-02-10 02:10:30 +01:00
} break ;
case Variant : : VECTOR2 : {
f - > store_32 ( VARIANT_VECTOR2 ) ;
2017-03-05 16:44:50 +01:00
Vector2 val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . x ) ;
f - > store_real ( val . y ) ;
2020-02-22 04:26:41 +01:00
} break ;
case Variant : : VECTOR2I : {
f - > store_32 ( VARIANT_VECTOR2I ) ;
Vector2i val = p_property ;
f - > store_32 ( val . x ) ;
f - > store_32 ( val . y ) ;
2014-02-10 02:10:30 +01:00
} break ;
case Variant : : RECT2 : {
f - > store_32 ( VARIANT_RECT2 ) ;
2017-03-05 16:44:50 +01:00
Rect2 val = p_property ;
2017-06-04 00:25:13 +02:00
f - > store_real ( val . position . x ) ;
f - > store_real ( val . position . y ) ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . size . x ) ;
f - > store_real ( val . size . y ) ;
2020-02-22 04:26:41 +01:00
} break ;
case Variant : : RECT2I : {
f - > store_32 ( VARIANT_RECT2I ) ;
Rect2i val = p_property ;
f - > store_32 ( val . position . x ) ;
f - > store_32 ( val . position . y ) ;
f - > store_32 ( val . size . x ) ;
f - > store_32 ( val . size . y ) ;
2014-02-10 02:10:30 +01:00
} break ;
case Variant : : VECTOR3 : {
f - > store_32 ( VARIANT_VECTOR3 ) ;
2017-03-05 16:44:50 +01:00
Vector3 val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . x ) ;
f - > store_real ( val . y ) ;
f - > store_real ( val . z ) ;
2020-02-22 04:26:41 +01:00
} break ;
case Variant : : VECTOR3I : {
f - > store_32 ( VARIANT_VECTOR3I ) ;
Vector3i val = p_property ;
f - > store_32 ( val . x ) ;
f - > store_32 ( val . y ) ;
f - > store_32 ( val . z ) ;
2014-02-10 02:10:30 +01:00
} break ;
case Variant : : PLANE : {
f - > store_32 ( VARIANT_PLANE ) ;
2017-03-05 16:44:50 +01:00
Plane val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . normal . x ) ;
f - > store_real ( val . normal . y ) ;
f - > store_real ( val . normal . z ) ;
2020-05-10 16:47:11 +02:00
f - > store_real ( val . d ) ;
2014-02-10 02:10:30 +01:00
} break ;
2021-01-20 08:02:02 +01:00
case Variant : : QUATERNION : {
f - > store_32 ( VARIANT_QUATERNION ) ;
Quaternion val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . x ) ;
f - > store_real ( val . y ) ;
f - > store_real ( val . z ) ;
f - > store_real ( val . w ) ;
} break ;
2017-11-17 03:09:00 +01:00
case Variant : : AABB : {
f - > store_32 ( VARIANT_AABB ) ;
AABB val = p_property ;
2017-06-06 20:33:51 +02:00
f - > store_real ( val . position . x ) ;
f - > store_real ( val . position . y ) ;
f - > store_real ( val . position . z ) ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . size . x ) ;
f - > store_real ( val . size . y ) ;
f - > store_real ( val . size . z ) ;
} break ;
2017-01-11 04:52:51 +01:00
case Variant : : TRANSFORM2D : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_MATRIX32 ) ;
2017-03-05 16:44:50 +01:00
Transform2D val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . elements [ 0 ] . x ) ;
f - > store_real ( val . elements [ 0 ] . y ) ;
f - > store_real ( val . elements [ 1 ] . x ) ;
f - > store_real ( val . elements [ 1 ] . y ) ;
f - > store_real ( val . elements [ 2 ] . x ) ;
f - > store_real ( val . elements [ 2 ] . y ) ;
} break ;
2017-01-11 04:52:51 +01:00
case Variant : : BASIS : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_MATRIX3 ) ;
2017-03-05 16:44:50 +01:00
Basis val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . elements [ 0 ] . x ) ;
f - > store_real ( val . elements [ 0 ] . y ) ;
f - > store_real ( val . elements [ 0 ] . z ) ;
f - > store_real ( val . elements [ 1 ] . x ) ;
f - > store_real ( val . elements [ 1 ] . y ) ;
f - > store_real ( val . elements [ 1 ] . z ) ;
f - > store_real ( val . elements [ 2 ] . x ) ;
f - > store_real ( val . elements [ 2 ] . y ) ;
f - > store_real ( val . elements [ 2 ] . z ) ;
} break ;
2021-04-28 09:36:08 +02:00
case Variant : : TRANSFORM3D : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_TRANSFORM ) ;
2020-10-17 07:08:21 +02:00
Transform3D val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . basis . elements [ 0 ] . x ) ;
f - > store_real ( val . basis . elements [ 0 ] . y ) ;
f - > store_real ( val . basis . elements [ 0 ] . z ) ;
f - > store_real ( val . basis . elements [ 1 ] . x ) ;
f - > store_real ( val . basis . elements [ 1 ] . y ) ;
f - > store_real ( val . basis . elements [ 1 ] . z ) ;
f - > store_real ( val . basis . elements [ 2 ] . x ) ;
f - > store_real ( val . basis . elements [ 2 ] . y ) ;
f - > store_real ( val . basis . elements [ 2 ] . z ) ;
f - > store_real ( val . origin . x ) ;
f - > store_real ( val . origin . y ) ;
f - > store_real ( val . origin . z ) ;
} break ;
case Variant : : COLOR : {
f - > store_32 ( VARIANT_COLOR ) ;
2017-03-05 16:44:50 +01:00
Color val = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_real ( val . r ) ;
f - > store_real ( val . g ) ;
f - > store_real ( val . b ) ;
f - > store_real ( val . a ) ;
2020-02-20 22:58:05 +01:00
} break ;
case Variant : : STRING_NAME : {
f - > store_32 ( VARIANT_STRING_NAME ) ;
String val = p_property ;
save_unicode_string ( f , val ) ;
2014-02-10 02:10:30 +01:00
} break ;
case Variant : : NODE_PATH : {
f - > store_32 ( VARIANT_NODE_PATH ) ;
2017-03-05 16:44:50 +01:00
NodePath np = p_property ;
2014-02-10 02:10:30 +01:00
f - > store_16 ( np . get_name_count ( ) ) ;
uint16_t snc = np . get_subname_count ( ) ;
2020-05-14 16:41:43 +02:00
if ( np . is_absolute ( ) ) {
2017-03-05 16:44:50 +01:00
snc | = 0x8000 ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
f - > store_16 ( snc ) ;
2017-12-15 12:38:24 +01:00
for ( int i = 0 ; i < np . get_name_count ( ) ; i + + ) {
if ( string_map . has ( np . get_name ( i ) ) ) {
f - > store_32 ( string_map [ np . get_name ( i ) ] ) ;
} else {
save_unicode_string ( f , np . get_name ( i ) , true ) ;
}
}
for ( int i = 0 ; i < np . get_subname_count ( ) ; i + + ) {
if ( string_map . has ( np . get_subname ( i ) ) ) {
f - > store_32 ( string_map [ np . get_subname ( i ) ] ) ;
} else {
save_unicode_string ( f , np . get_subname ( i ) , true ) ;
}
}
2014-02-10 02:10:30 +01:00
} break ;
2020-11-09 14:53:05 +01:00
case Variant : : RID : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_RID ) ;
2019-08-15 04:57:49 +02:00
WARN_PRINT ( " Can't save RIDs. " ) ;
2014-02-10 02:10:30 +01:00
RID val = p_property ;
f - > store_32 ( val . get_id ( ) ) ;
} break ;
case Variant : : OBJECT : {
f - > store_32 ( VARIANT_OBJECT ) ;
RES res = p_property ;
if ( res . is_null ( ) ) {
f - > store_32 ( OBJECT_EMPTY ) ;
return ; // don't save it
}
2017-03-05 16:44:50 +01:00
if ( res - > get_path ( ) . length ( ) & & res - > get_path ( ) . find ( " :: " ) = = - 1 ) {
2015-08-24 01:15:56 +02:00
f - > store_32 ( OBJECT_EXTERNAL_RESOURCE_INDEX ) ;
f - > store_32 ( external_resources [ res ] ) ;
2014-02-10 02:10:30 +01:00
} else {
2021-07-21 02:36:56 +02:00
if ( ! resource_map . has ( res ) ) {
2014-02-10 02:10:30 +01:00
f - > store_32 ( OBJECT_EMPTY ) ;
2019-08-15 04:57:49 +02:00
ERR_FAIL_MSG ( " Resource was not pre cached for the resource section, most likely due to circular reference. " ) ;
2014-02-10 02:10:30 +01:00
}
f - > store_32 ( OBJECT_INTERNAL_RESOURCE ) ;
2021-07-21 02:36:56 +02:00
f - > store_32 ( resource_map [ res ] ) ;
2014-02-10 02:10:30 +01:00
//internal resource
}
} break ;
2020-02-19 20:27:19 +01:00
case Variant : : CALLABLE : {
f - > store_32 ( VARIANT_CALLABLE ) ;
WARN_PRINT ( " Can't save Callables. " ) ;
} break ;
case Variant : : SIGNAL : {
f - > store_32 ( VARIANT_SIGNAL ) ;
WARN_PRINT ( " Can't save Signals. " ) ;
} break ;
2014-02-10 02:10:30 +01:00
case Variant : : DICTIONARY : {
f - > store_32 ( VARIANT_DICTIONARY ) ;
Dictionary d = p_property ;
2017-01-11 12:53:31 +01:00
f - > store_32 ( uint32_t ( d . size ( ) ) ) ;
2014-02-10 02:10:30 +01:00
List < Variant > keys ;
d . get_key_list ( & keys ) ;
2021-07-24 15:46:25 +02:00
for ( const Variant & E : keys ) {
2017-01-14 12:26:56 +01:00
/*
2021-07-16 05:45:57 +02:00
if ( ! _check_type ( dict [ E ] ) )
2017-01-14 12:26:56 +01:00
continue ;
*/
2014-02-10 02:10:30 +01:00
2021-07-16 05:45:57 +02:00
write_variant ( f , E , resource_map , external_resources , string_map ) ;
write_variant ( f , d [ E ] , resource_map , external_resources , string_map ) ;
2014-02-10 02:10:30 +01:00
}
} break ;
case Variant : : ARRAY : {
f - > store_32 ( VARIANT_ARRAY ) ;
2017-03-05 16:44:50 +01:00
Array a = p_property ;
2017-01-11 12:53:31 +01:00
f - > store_32 ( uint32_t ( a . size ( ) ) ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < a . size ( ) ; i + + ) {
2021-07-21 02:36:56 +02:00
write_variant ( f , a [ i ] , resource_map , external_resources , string_map ) ;
2014-02-10 02:10:30 +01:00
}
} break ;
2020-02-17 22:06:54 +01:00
case Variant : : PACKED_BYTE_ARRAY : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_RAW_ARRAY ) ;
2020-02-17 22:06:54 +01:00
Vector < uint8_t > arr = p_property ;
2017-03-05 16:44:50 +01:00
int len = arr . size ( ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( len ) ;
2020-02-17 22:06:54 +01:00
const uint8_t * r = arr . ptr ( ) ;
f - > store_buffer ( r , len ) ;
2017-12-15 12:38:24 +01:00
_pad_buffer ( f , len ) ;
2014-02-10 02:10:30 +01:00
} break ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
case Variant : : PACKED_INT32_ARRAY : {
f - > store_32 ( VARIANT_INT32_ARRAY ) ;
Vector < int32_t > arr = p_property ;
2017-03-05 16:44:50 +01:00
int len = arr . size ( ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( len ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
const int32_t * r = arr . ptr ( ) ;
2020-05-14 16:41:43 +02:00
for ( int i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > store_32 ( r [ i ] ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
} break ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
case Variant : : PACKED_INT64_ARRAY : {
f - > store_32 ( VARIANT_INT64_ARRAY ) ;
Vector < int64_t > arr = p_property ;
2017-03-05 16:44:50 +01:00
int len = arr . size ( ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( len ) ;
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
const int64_t * r = arr . ptr ( ) ;
2020-05-14 16:41:43 +02:00
for ( int i = 0 ; i < len ; i + + ) {
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
f - > store_64 ( r [ i ] ) ;
2020-05-14 16:41:43 +02:00
}
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
} break ;
case Variant : : PACKED_FLOAT32_ARRAY : {
f - > store_32 ( VARIANT_FLOAT32_ARRAY ) ;
Vector < float > arr = p_property ;
int len = arr . size ( ) ;
f - > store_32 ( len ) ;
const float * r = arr . ptr ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > store_real ( r [ i ] ) ;
}
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
} break ;
case Variant : : PACKED_FLOAT64_ARRAY : {
f - > store_32 ( VARIANT_FLOAT64_ARRAY ) ;
Vector < double > arr = p_property ;
int len = arr . size ( ) ;
f - > store_32 ( len ) ;
const double * r = arr . ptr ( ) ;
for ( int i = 0 ; i < len ; i + + ) {
f - > store_double ( r [ i ] ) ;
}
2014-02-10 02:10:30 +01:00
} break ;
2020-02-17 22:06:54 +01:00
case Variant : : PACKED_STRING_ARRAY : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_STRING_ARRAY ) ;
2020-02-17 22:06:54 +01:00
Vector < String > arr = p_property ;
2017-03-05 16:44:50 +01:00
int len = arr . size ( ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( len ) ;
2020-02-17 22:06:54 +01:00
const String * r = arr . ptr ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , r [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
} break ;
2020-02-17 22:06:54 +01:00
case Variant : : PACKED_VECTOR3_ARRAY : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_VECTOR3_ARRAY ) ;
2020-02-17 22:06:54 +01:00
Vector < Vector3 > arr = p_property ;
2017-03-05 16:44:50 +01:00
int len = arr . size ( ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( len ) ;
2020-02-17 22:06:54 +01:00
const Vector3 * r = arr . ptr ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > store_real ( r [ i ] . x ) ;
f - > store_real ( r [ i ] . y ) ;
f - > store_real ( r [ i ] . z ) ;
}
} break ;
2020-02-17 22:06:54 +01:00
case Variant : : PACKED_VECTOR2_ARRAY : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_VECTOR2_ARRAY ) ;
2020-02-17 22:06:54 +01:00
Vector < Vector2 > arr = p_property ;
2017-03-05 16:44:50 +01:00
int len = arr . size ( ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( len ) ;
2020-02-17 22:06:54 +01:00
const Vector2 * r = arr . ptr ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > store_real ( r [ i ] . x ) ;
f - > store_real ( r [ i ] . y ) ;
}
} break ;
2020-02-17 22:06:54 +01:00
case Variant : : PACKED_COLOR_ARRAY : {
2014-02-10 02:10:30 +01:00
f - > store_32 ( VARIANT_COLOR_ARRAY ) ;
2020-02-17 22:06:54 +01:00
Vector < Color > arr = p_property ;
2017-03-05 16:44:50 +01:00
int len = arr . size ( ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( len ) ;
2020-02-17 22:06:54 +01:00
const Color * r = arr . ptr ( ) ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < len ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > store_real ( r [ i ] . r ) ;
f - > store_real ( r [ i ] . g ) ;
f - > store_real ( r [ i ] . b ) ;
f - > store_real ( r [ i ] . a ) ;
}
} break ;
default : {
2019-08-15 04:57:49 +02:00
ERR_FAIL_MSG ( " Invalid variant. " ) ;
2014-02-10 02:10:30 +01:00
}
}
}
2017-03-05 16:44:50 +01:00
void ResourceFormatSaverBinaryInstance : : _find_resources ( const Variant & p_variant , bool p_main ) {
switch ( p_variant . get_type ( ) ) {
2014-02-10 02:10:30 +01:00
case Variant : : OBJECT : {
2020-02-13 20:03:10 +01:00
RES res = p_variant ;
2014-02-10 02:10:30 +01:00
2020-05-14 16:41:43 +02:00
if ( res . is_null ( ) | | external_resources . has ( res ) ) {
2014-02-10 02:10:30 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! p_main & & ( ! bundle_resources ) & & res - > get_path ( ) . length ( ) & & res - > get_path ( ) . find ( " :: " ) = = - 1 ) {
2019-02-24 14:45:08 +01:00
if ( res - > get_path ( ) = = path ) {
2019-11-06 17:03:04 +01:00
ERR_PRINT ( " Circular reference to resource being saved found: ' " + local_path + " ' will be null next time it's loaded. " ) ;
2019-02-24 14:45:08 +01:00
return ;
}
2015-08-24 01:15:56 +02:00
int idx = external_resources . size ( ) ;
2017-03-05 16:44:50 +01:00
external_resources [ res ] = idx ;
2014-02-10 02:10:30 +01:00
return ;
}
2020-05-14 16:41:43 +02:00
if ( resource_set . has ( res ) ) {
2014-02-10 02:10:30 +01:00
return ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
List < PropertyInfo > property_list ;
res - > get_property_list ( & property_list ) ;
2021-07-24 15:46:25 +02:00
for ( const PropertyInfo & E : property_list ) {
2021-07-16 05:45:57 +02:00
if ( E . usage & PROPERTY_USAGE_STORAGE ) {
Variant value = res - > get ( E . name ) ;
if ( E . usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT ) {
2019-02-22 00:49:42 +01:00
RES sres = value ;
if ( sres . is_valid ( ) ) {
NonPersistentKey npk ;
npk . base = res ;
2021-07-16 05:45:57 +02:00
npk . property = E . name ;
2019-02-22 00:49:42 +01:00
non_persistent_map [ npk ] = sres ;
resource_set . insert ( sres ) ;
saved_resources . push_back ( sres ) ;
}
} else {
_find_resources ( value ) ;
}
2014-02-10 02:10:30 +01:00
}
}
2015-06-22 05:03:19 +02:00
resource_set . insert ( res ) ;
2014-02-10 02:10:30 +01:00
saved_resources . push_back ( res ) ;
} break ;
case Variant : : ARRAY : {
2017-03-05 16:44:50 +01:00
Array varray = p_variant ;
int len = varray . size ( ) ;
for ( int i = 0 ; i < len ; i + + ) {
2019-06-26 15:08:25 +02:00
const Variant & v = varray . get ( i ) ;
2014-02-10 02:10:30 +01:00
_find_resources ( v ) ;
}
} break ;
case Variant : : DICTIONARY : {
2017-03-05 16:44:50 +01:00
Dictionary d = p_variant ;
2014-02-10 02:10:30 +01:00
List < Variant > keys ;
d . get_key_list ( & keys ) ;
2021-07-24 15:46:25 +02:00
for ( const Variant & E : keys ) {
2021-07-16 05:45:57 +02:00
_find_resources ( E ) ;
Variant v = d [ E ] ;
2014-02-10 02:10:30 +01:00
_find_resources ( v ) ;
}
} break ;
case Variant : : NODE_PATH : {
//take the chance and save node path strings
NodePath np = p_variant ;
2020-05-14 16:41:43 +02:00
for ( int i = 0 ; i < np . get_name_count ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
get_string_index ( np . get_name ( i ) ) ;
2020-05-14 16:41:43 +02:00
}
for ( int i = 0 ; i < np . get_subname_count ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
get_string_index ( np . get_subname ( i ) ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
} break ;
2019-04-09 17:08:36 +02:00
default : {
}
2014-02-10 02:10:30 +01:00
}
}
2017-12-15 12:38:24 +01:00
void ResourceFormatSaverBinaryInstance : : save_unicode_string ( FileAccess * f , const String & p_string , bool p_bit_on_len ) {
2014-02-10 02:10:30 +01:00
CharString utf8 = p_string . utf8 ( ) ;
2017-12-15 12:38:24 +01:00
if ( p_bit_on_len ) {
2018-09-26 16:35:32 +02:00
f - > store_32 ( ( utf8 . length ( ) + 1 ) | 0x80000000 ) ;
2017-12-15 12:38:24 +01:00
} else {
f - > store_32 ( utf8 . length ( ) + 1 ) ;
}
2017-03-05 16:44:50 +01:00
f - > store_buffer ( ( const uint8_t * ) utf8 . get_data ( ) , utf8 . length ( ) + 1 ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
int ResourceFormatSaverBinaryInstance : : get_string_index ( const String & p_string ) {
StringName s = p_string ;
2020-05-14 16:41:43 +02:00
if ( string_map . has ( s ) ) {
2014-02-10 02:10:30 +01:00
return string_map [ s ] ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
string_map [ s ] = strings . size ( ) ;
2014-02-10 02:10:30 +01:00
strings . push_back ( s ) ;
2017-03-05 16:44:50 +01:00
return strings . size ( ) - 1 ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
Error ResourceFormatSaverBinaryInstance : : save ( const String & p_path , const RES & p_resource , uint32_t p_flags ) {
2014-02-10 02:10:30 +01:00
Error err ;
2017-03-05 16:44:50 +01:00
if ( p_flags & ResourceSaver : : FLAG_COMPRESS ) {
FileAccessCompressed * fac = memnew ( FileAccessCompressed ) ;
2014-02-10 02:10:30 +01:00
fac - > configure ( " RSCC " ) ;
2017-03-05 16:44:50 +01:00
f = fac ;
err = fac - > _open ( p_path , FileAccess : : WRITE ) ;
2020-05-14 16:41:43 +02:00
if ( err ) {
2014-02-10 02:10:30 +01:00
memdelete ( f ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
} else {
2017-03-05 16:44:50 +01:00
f = FileAccess : : open ( p_path , FileAccess : : WRITE , & err ) ;
2014-02-10 02:10:30 +01:00
}
2019-09-25 10:28:50 +02:00
ERR_FAIL_COND_V_MSG ( err ! = OK , err , " Cannot create file ' " + p_path + " '. " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
relative_paths = p_flags & ResourceSaver : : FLAG_RELATIVE_PATHS ;
skip_editor = p_flags & ResourceSaver : : FLAG_OMIT_EDITOR_PROPERTIES ;
bundle_resources = p_flags & ResourceSaver : : FLAG_BUNDLE_RESOURCES ;
big_endian = p_flags & ResourceSaver : : FLAG_SAVE_BIG_ENDIAN ;
takeover_paths = p_flags & ResourceSaver : : FLAG_REPLACE_SUBRESOURCE_PATHS ;
2014-03-14 02:57:24 +01:00
2020-05-14 16:41:43 +02:00
if ( ! p_path . begins_with ( " res:// " ) ) {
2017-03-05 16:44:50 +01:00
takeover_paths = false ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
local_path = p_path . get_base_dir ( ) ;
2019-02-24 14:45:08 +01:00
path = ProjectSettings : : get_singleton ( ) - > localize_path ( p_path ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
_find_resources ( p_resource , true ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( ! ( p_flags & ResourceSaver : : FLAG_COMPRESS ) ) {
2014-02-10 02:10:30 +01:00
//save header compressed
2017-03-05 16:44:50 +01:00
static const uint8_t header [ 4 ] = { ' R ' , ' S ' , ' R ' , ' C ' } ;
f - > store_buffer ( header , 4 ) ;
2014-02-10 02:10:30 +01:00
}
if ( big_endian ) {
f - > store_32 ( 1 ) ;
2021-05-20 14:58:03 +02:00
f - > set_big_endian ( true ) ;
2020-05-14 16:41:43 +02:00
} else {
2014-02-10 02:10:30 +01:00
f - > store_32 ( 0 ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
f - > store_32 ( 0 ) ; //64 bits file, false for now
f - > store_32 ( VERSION_MAJOR ) ;
f - > store_32 ( VERSION_MINOR ) ;
f - > store_32 ( FORMAT_VERSION ) ;
2017-03-05 16:44:50 +01:00
if ( f - > get_error ( ) ! = OK & & f - > get_error ( ) ! = ERR_FILE_EOF ) {
2015-03-02 04:54:10 +01:00
f - > close ( ) ;
2019-08-30 13:46:05 +02:00
memdelete ( f ) ;
2015-03-02 04:54:10 +01:00
return ERR_CANT_CREATE ;
}
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , p_resource - > get_class ( ) ) ;
2017-09-02 22:32:31 +02:00
f - > store_64 ( 0 ) ; //offset to import metadata
2021-07-23 21:01:18 +02:00
f - > store_32 ( FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS ) ;
ResourceUID : : ID uid = ResourceSaver : : get_resource_id_for_path ( p_path , true ) ;
f - > store_64 ( uid ) ;
2021-07-30 07:24:29 +02:00
for ( int i = 0 ; i < ResourceFormatSaverBinaryInstance : : RESERVED_FIELDS ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > store_32 ( 0 ) ; // reserved
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
List < ResourceData > resources ;
{
2021-07-24 15:46:25 +02:00
for ( const RES & E : saved_resources ) {
2014-02-10 02:10:30 +01:00
ResourceData & rd = resources . push_back ( ResourceData ( ) ) - > get ( ) ;
2021-07-16 05:45:57 +02:00
rd . type = E - > get_class ( ) ;
2014-02-10 02:10:30 +01:00
List < PropertyInfo > property_list ;
2021-07-16 05:45:57 +02:00
E - > get_property_list ( & property_list ) ;
2014-02-10 02:10:30 +01:00
2021-07-24 15:46:25 +02:00
for ( const PropertyInfo & F : property_list ) {
2021-07-16 05:45:57 +02:00
if ( skip_editor & & F . name . begins_with ( " __editor " ) ) {
2014-02-10 02:10:30 +01:00
continue ;
2020-05-14 16:41:43 +02:00
}
2021-07-16 05:45:57 +02:00
if ( ( F . usage & PROPERTY_USAGE_STORAGE ) ) {
2014-02-10 02:10:30 +01:00
Property p ;
2021-07-16 05:45:57 +02:00
p . name_idx = get_string_index ( F . name ) ;
2019-02-22 00:49:42 +01:00
2021-07-16 05:45:57 +02:00
if ( F . usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT ) {
2019-02-22 00:49:42 +01:00
NonPersistentKey npk ;
2021-07-16 05:45:57 +02:00
npk . base = E ;
npk . property = F . name ;
2019-02-22 00:49:42 +01:00
if ( non_persistent_map . has ( npk ) ) {
p . value = non_persistent_map [ npk ] ;
}
} else {
2021-07-16 05:45:57 +02:00
p . value = E - > get ( F . name ) ;
2019-02-22 00:49:42 +01:00
}
2018-11-08 15:30:02 +01:00
2021-07-16 05:45:57 +02:00
Variant default_value = ClassDB : : class_get_default_property_value ( E - > get_class ( ) , F . name ) ;
2018-11-08 15:30:02 +01:00
if ( default_value . get_type ( ) ! = Variant : : NIL & & bool ( Variant : : evaluate ( Variant : : OP_EQUAL , p . value , default_value ) ) ) {
2014-02-10 02:10:30 +01:00
continue ;
2018-11-08 15:30:02 +01:00
}
2021-07-16 05:45:57 +02:00
p . pi = F ;
2014-02-10 02:10:30 +01:00
rd . properties . push_back ( p ) ;
}
}
}
}
f - > store_32 ( strings . size ( ) ) ; //string table size
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < strings . size ( ) ; i + + ) {
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , strings [ i ] ) ;
2014-02-10 02:10:30 +01:00
}
// save external resource table
f - > store_32 ( external_resources . size ( ) ) ; //amount of external resources
2015-08-24 01:15:56 +02:00
Vector < RES > save_order ;
save_order . resize ( external_resources . size ( ) ) ;
2017-03-05 16:44:50 +01:00
for ( Map < RES , int > : : Element * E = external_resources . front ( ) ; E ; E = E - > next ( ) ) {
2018-07-25 03:11:03 +02:00
save_order . write [ E - > get ( ) ] = E - > key ( ) ;
2015-08-24 01:15:56 +02:00
}
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < save_order . size ( ) ; i + + ) {
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , save_order [ i ] - > get_save_class ( ) ) ;
2015-08-24 01:15:56 +02:00
String path = save_order [ i ] - > get_path ( ) ;
2017-03-05 16:44:50 +01:00
path = relative_paths ? local_path . path_to_file ( path ) : path ;
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , path ) ;
2021-07-23 21:01:18 +02:00
ResourceUID : : ID ruid = ResourceSaver : : get_resource_id_for_path ( save_order [ i ] - > get_path ( ) , false ) ;
f - > store_64 ( ruid ) ;
2014-02-10 02:10:30 +01:00
}
// save internal resource table
f - > store_32 ( saved_resources . size ( ) ) ; //amount of internal resources
Vector < uint64_t > ofs_pos ;
2021-07-21 02:36:56 +02:00
Set < String > used_unique_ids ;
2015-06-22 05:03:19 +02:00
2021-07-16 05:45:57 +02:00
for ( RES & r : saved_resources ) {
2017-03-05 16:44:50 +01:00
if ( r - > get_path ( ) = = " " | | r - > get_path ( ) . find ( " :: " ) ! = - 1 ) {
2021-07-21 02:36:56 +02:00
if ( r - > get_scene_unique_id ( ) ! = " " ) {
if ( used_unique_ids . has ( r - > get_scene_unique_id ( ) ) ) {
r - > set_scene_unique_id ( " " ) ;
2015-06-22 05:03:19 +02:00
} else {
2021-07-21 02:36:56 +02:00
used_unique_ids . insert ( r - > get_scene_unique_id ( ) ) ;
2015-06-22 05:03:19 +02:00
}
}
}
}
2021-07-21 02:36:56 +02:00
Map < RES , int > resource_map ;
int res_index = 0 ;
2021-07-16 05:45:57 +02:00
for ( RES & r : saved_resources ) {
2017-03-05 16:44:50 +01:00
if ( r - > get_path ( ) = = " " | | r - > get_path ( ) . find ( " :: " ) ! = - 1 ) {
2021-07-21 02:36:56 +02:00
if ( r - > get_scene_unique_id ( ) = = " " ) {
String new_id ;
while ( true ) {
new_id = r - > get_class ( ) + " _ " + Resource : : generate_scene_unique_id ( ) ;
if ( ! used_unique_ids . has ( new_id ) ) {
break ;
}
2015-06-22 05:03:19 +02:00
}
2021-07-21 02:36:56 +02:00
r - > set_scene_unique_id ( new_id ) ;
used_unique_ids . insert ( new_id ) ;
2015-06-22 05:03:19 +02:00
}
2021-07-21 02:36:56 +02:00
save_unicode_string ( f , " local:// " + r - > get_scene_unique_id ( ) ) ;
2014-06-28 04:21:45 +02:00
if ( takeover_paths ) {
2021-07-21 02:36:56 +02:00
r - > set_path ( p_path + " :: " + r - > get_scene_unique_id ( ) , true ) ;
2014-06-28 04:21:45 +02:00
}
2016-08-31 22:58:51 +02:00
# ifdef TOOLS_ENABLED
r - > set_edited ( false ) ;
# endif
2015-10-22 04:57:43 +02:00
} else {
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , r - > get_path ( ) ) ; //actual external
2015-10-22 04:57:43 +02:00
}
2017-09-10 15:37:49 +02:00
ofs_pos . push_back ( f - > get_position ( ) ) ;
2014-02-10 02:10:30 +01:00
f - > store_64 ( 0 ) ; //offset in 64 bits
2021-07-21 02:36:56 +02:00
resource_map [ r ] = res_index + + ;
2014-02-10 02:10:30 +01:00
}
Vector < uint64_t > ofs_table ;
2017-08-26 17:46:49 +02:00
//now actually save the resources
2021-07-24 15:46:25 +02:00
for ( const ResourceData & rd : resources ) {
2017-09-10 15:37:49 +02:00
ofs_table . push_back ( f - > get_position ( ) ) ;
2017-12-15 12:38:24 +01:00
save_unicode_string ( f , rd . type ) ;
2014-02-10 02:10:30 +01:00
f - > store_32 ( rd . properties . size ( ) ) ;
2021-07-24 15:46:25 +02:00
for ( const Property & p : rd . properties ) {
2014-02-10 02:10:30 +01:00
f - > store_32 ( p . name_idx ) ;
2021-07-24 15:46:25 +02:00
write_variant ( f , p . value , resource_map , external_resources , string_map , p . pi ) ;
2014-02-10 02:10:30 +01:00
}
}
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < ofs_table . size ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
f - > seek ( ofs_pos [ i ] ) ;
f - > store_64 ( ofs_table [ i ] ) ;
}
f - > seek_end ( ) ;
2017-03-05 16:44:50 +01:00
f - > store_buffer ( ( const uint8_t * ) " RSRC " , 4 ) ; //magic at end
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( f - > get_error ( ) ! = OK & & f - > get_error ( ) ! = ERR_FILE_EOF ) {
2015-03-02 04:54:10 +01:00
f - > close ( ) ;
2019-08-30 13:46:05 +02:00
memdelete ( f ) ;
2015-03-02 04:54:10 +01:00
return ERR_CANT_CREATE ;
}
2014-02-10 02:10:30 +01:00
f - > close ( ) ;
2019-08-30 13:46:05 +02:00
memdelete ( f ) ;
2014-02-10 02:10:30 +01:00
return OK ;
}
2017-03-05 16:44:50 +01:00
Error ResourceFormatSaverBinary : : save ( const String & p_path , const RES & p_resource , uint32_t p_flags ) {
2017-07-19 22:00:46 +02:00
String local_path = ProjectSettings : : get_singleton ( ) - > localize_path ( p_path ) ;
2014-02-10 02:10:30 +01:00
ResourceFormatSaverBinaryInstance saver ;
2017-03-05 16:44:50 +01:00
return saver . save ( local_path , p_resource , p_flags ) ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
bool ResourceFormatSaverBinary : : recognize ( const RES & p_resource ) const {
2014-02-10 02:10:30 +01:00
return true ; //all recognized
}
2017-03-05 16:44:50 +01:00
void ResourceFormatSaverBinary : : get_recognized_extensions ( const RES & p_resource , List < String > * p_extensions ) const {
2014-02-10 02:10:30 +01:00
String base = p_resource - > get_base_extension ( ) . to_lower ( ) ;
2016-06-28 18:58:40 +02:00
p_extensions - > push_back ( base ) ;
2020-05-14 16:41:43 +02:00
if ( base ! = " res " ) {
2017-02-15 12:29:46 +01:00
p_extensions - > push_back ( " res " ) ;
2020-05-14 16:41:43 +02:00
}
2014-02-10 02:10:30 +01:00
}
2020-04-02 01:20:12 +02:00
ResourceFormatSaverBinary * ResourceFormatSaverBinary : : singleton = nullptr ;
2015-08-24 01:15:56 +02:00
ResourceFormatSaverBinary : : ResourceFormatSaverBinary ( ) {
2017-03-05 16:44:50 +01:00
singleton = this ;
2015-08-24 01:15:56 +02:00
}