| 1 | /**************************************************************************/ |
| 2 | /* file_access_memory.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "file_access_memory.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/dir_access.h" |
| 35 | #include "core/templates/rb_map.h" |
| 36 | |
| 37 | static HashMap<String, Vector<uint8_t>> *files = nullptr; |
| 38 | |
| 39 | void FileAccessMemory::register_file(String p_name, Vector<uint8_t> p_data) { |
| 40 | if (!files) { |
| 41 | files = memnew((HashMap<String, Vector<uint8_t>>)); |
| 42 | } |
| 43 | |
| 44 | String name; |
| 45 | if (ProjectSettings::get_singleton()) { |
| 46 | name = ProjectSettings::get_singleton()->globalize_path(p_name); |
| 47 | } else { |
| 48 | name = p_name; |
| 49 | } |
| 50 | //name = DirAccess::normalize_path(name); |
| 51 | |
| 52 | (*files)[name] = p_data; |
| 53 | } |
| 54 | |
| 55 | void FileAccessMemory::cleanup() { |
| 56 | if (!files) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | memdelete(files); |
| 61 | } |
| 62 | |
| 63 | Ref<FileAccess> FileAccessMemory::create() { |
| 64 | return memnew(FileAccessMemory); |
| 65 | } |
| 66 | |
| 67 | bool FileAccessMemory::file_exists(const String &p_name) { |
| 68 | String name = fix_path(p_name); |
| 69 | //name = DirAccess::normalize_path(name); |
| 70 | |
| 71 | return files && (files->find(name) != nullptr); |
| 72 | } |
| 73 | |
| 74 | Error FileAccessMemory::open_custom(const uint8_t *p_data, uint64_t p_len) { |
| 75 | data = (uint8_t *)p_data; |
| 76 | length = p_len; |
| 77 | pos = 0; |
| 78 | return OK; |
| 79 | } |
| 80 | |
| 81 | Error FileAccessMemory::open_internal(const String &p_path, int p_mode_flags) { |
| 82 | ERR_FAIL_NULL_V(files, ERR_FILE_NOT_FOUND); |
| 83 | |
| 84 | String name = fix_path(p_path); |
| 85 | //name = DirAccess::normalize_path(name); |
| 86 | |
| 87 | HashMap<String, Vector<uint8_t>>::Iterator E = files->find(name); |
| 88 | ERR_FAIL_COND_V_MSG(!E, ERR_FILE_NOT_FOUND, "Can't find file '" + p_path + "'." ); |
| 89 | |
| 90 | data = E->value.ptrw(); |
| 91 | length = E->value.size(); |
| 92 | pos = 0; |
| 93 | |
| 94 | return OK; |
| 95 | } |
| 96 | |
| 97 | bool FileAccessMemory::is_open() const { |
| 98 | return data != nullptr; |
| 99 | } |
| 100 | |
| 101 | void FileAccessMemory::seek(uint64_t p_position) { |
| 102 | ERR_FAIL_NULL(data); |
| 103 | pos = p_position; |
| 104 | } |
| 105 | |
| 106 | void FileAccessMemory::seek_end(int64_t p_position) { |
| 107 | ERR_FAIL_NULL(data); |
| 108 | pos = length + p_position; |
| 109 | } |
| 110 | |
| 111 | uint64_t FileAccessMemory::get_position() const { |
| 112 | ERR_FAIL_NULL_V(data, 0); |
| 113 | return pos; |
| 114 | } |
| 115 | |
| 116 | uint64_t FileAccessMemory::get_length() const { |
| 117 | ERR_FAIL_NULL_V(data, 0); |
| 118 | return length; |
| 119 | } |
| 120 | |
| 121 | bool FileAccessMemory::eof_reached() const { |
| 122 | return pos > length; |
| 123 | } |
| 124 | |
| 125 | uint8_t FileAccessMemory::get_8() const { |
| 126 | uint8_t ret = 0; |
| 127 | if (pos < length) { |
| 128 | ret = data[pos]; |
| 129 | } |
| 130 | ++pos; |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | uint64_t FileAccessMemory::get_buffer(uint8_t *p_dst, uint64_t p_length) const { |
| 136 | ERR_FAIL_COND_V(!p_dst && p_length > 0, -1); |
| 137 | ERR_FAIL_NULL_V(data, -1); |
| 138 | |
| 139 | uint64_t left = length - pos; |
| 140 | uint64_t read = MIN(p_length, left); |
| 141 | |
| 142 | if (read < p_length) { |
| 143 | WARN_PRINT("Reading less data than requested" ); |
| 144 | } |
| 145 | |
| 146 | memcpy(p_dst, &data[pos], read); |
| 147 | pos += read; |
| 148 | |
| 149 | return read; |
| 150 | } |
| 151 | |
| 152 | Error FileAccessMemory::get_error() const { |
| 153 | return pos >= length ? ERR_FILE_EOF : OK; |
| 154 | } |
| 155 | |
| 156 | void FileAccessMemory::flush() { |
| 157 | ERR_FAIL_NULL(data); |
| 158 | } |
| 159 | |
| 160 | void FileAccessMemory::store_8(uint8_t p_byte) { |
| 161 | ERR_FAIL_NULL(data); |
| 162 | ERR_FAIL_COND(pos >= length); |
| 163 | data[pos++] = p_byte; |
| 164 | } |
| 165 | |
| 166 | void FileAccessMemory::store_buffer(const uint8_t *p_src, uint64_t p_length) { |
| 167 | ERR_FAIL_COND(!p_src && p_length > 0); |
| 168 | uint64_t left = length - pos; |
| 169 | uint64_t write = MIN(p_length, left); |
| 170 | if (write < p_length) { |
| 171 | WARN_PRINT("Writing less data than requested" ); |
| 172 | } |
| 173 | |
| 174 | memcpy(&data[pos], p_src, write); |
| 175 | pos += write; |
| 176 | } |
| 177 | |