| 2075 | } |
| 2076 | |
| 2077 | Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_string_func, void *p_store_string_ud, EncodeResourceFunc p_encode_res_func, void *p_encode_res_ud, int p_recursion_count, bool p_compat, bool p_full_objects) { |
| 2078 | static bool modified_security_assistance_disabled = GLOBAL_GET("application/run/disable_modified_security_assistance"); |
| 2079 | p_full_objects = p_full_objects || modified_security_assistance_disabled; |
| 2080 | |
| 2081 | switch (p_variant.get_type()) { |
| 2082 | case Variant::NIL: { |
| 2083 | p_store_string_func(p_store_string_ud, "null"); |
| 2084 | } break; |
| 2085 | case Variant::BOOL: { |
| 2086 | p_store_string_func(p_store_string_ud, p_variant.operator bool() ? "true" : "false"); |
| 2087 | } break; |
| 2088 | case Variant::INT: { |
| 2089 | p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t())); |
| 2090 | } break; |
| 2091 | case Variant::FLOAT: { |
| 2092 | const double value = p_variant.operator double(); |
| 2093 | String s; |
| 2094 | /// @todo Hack to avoid garbage digits when the underlying float is 32-bit. |
| 2095 | if ((double)(float)value == value) { |
| 2096 | s = rtos_fix((float)value, p_compat); |
| 2097 | } else { |
| 2098 | s = rtos_fix(value, p_compat); |
| 2099 | } |
| 2100 | // Append ".0" to floats to ensure they are float literals. |
| 2101 | if (s != "inf" && s != "-inf" && s != "nan" && !s.contains_char('.') && !s.contains_char('e') && !s.contains_char('E')) { |
| 2102 | s += ".0"; |
| 2103 | } |
| 2104 | p_store_string_func(p_store_string_ud, s); |
| 2105 | } break; |
| 2106 | case Variant::STRING: { |
| 2107 | String str = p_variant; |
| 2108 | str = "\"" + str.c_escape_multiline() + "\""; |
| 2109 | p_store_string_func(p_store_string_ud, str); |
| 2110 | } break; |
| 2111 | |
| 2112 | // Math types. |
| 2113 | case Variant::VECTOR2: { |
| 2114 | Vector2 v = p_variant; |
| 2115 | p_store_string_func(p_store_string_ud, "Vector2(" + rtos_fix(v.x, p_compat) + ", " + rtos_fix(v.y, p_compat) + ")"); |
| 2116 | } break; |
| 2117 | case Variant::VECTOR2I: { |
| 2118 | Vector2i v = p_variant; |
| 2119 | p_store_string_func(p_store_string_ud, "Vector2i(" + itos(v.x) + ", " + itos(v.y) + ")"); |
| 2120 | } break; |
| 2121 | case Variant::RECT2: { |
| 2122 | Rect2 aabb = p_variant; |
| 2123 | p_store_string_func(p_store_string_ud, "Rect2(" + rtos_fix(aabb.position.x, p_compat) + ", " + rtos_fix(aabb.position.y, p_compat) + ", " + rtos_fix(aabb.size.x, p_compat) + ", " + rtos_fix(aabb.size.y, p_compat) + ")"); |
| 2124 | } break; |
| 2125 | case Variant::RECT2I: { |
| 2126 | Rect2i aabb = p_variant; |
| 2127 | p_store_string_func(p_store_string_ud, "Rect2i(" + itos(aabb.position.x) + ", " + itos(aabb.position.y) + ", " + itos(aabb.size.x) + ", " + itos(aabb.size.y) + ")"); |
| 2128 | } break; |
| 2129 | case Variant::VECTOR3: { |
| 2130 | Vector3 v = p_variant; |
| 2131 | p_store_string_func(p_store_string_ud, "Vector3(" + rtos_fix(v.x, p_compat) + ", " + rtos_fix(v.y, p_compat) + ", " + rtos_fix(v.z, p_compat) + ")"); |
| 2132 | } break; |
| 2133 | case Variant::VECTOR3I: { |
| 2134 | Vector3i v = p_variant; |
nothing calls this directly
no test coverage detected