Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/io/file_access_memory.cpp
9973 views
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
35
static HashMap<String, Vector<uint8_t>> *files = nullptr;
36
37
void FileAccessMemory::register_file(const String &p_name, const Vector<uint8_t> &p_data) {
38
if (!files) {
39
files = memnew((HashMap<String, Vector<uint8_t>>));
40
}
41
42
String name;
43
if (ProjectSettings::get_singleton()) {
44
name = ProjectSettings::get_singleton()->globalize_path(p_name);
45
} else {
46
name = p_name;
47
}
48
//name = DirAccess::normalize_path(name);
49
50
(*files)[name] = p_data;
51
}
52
53
void FileAccessMemory::cleanup() {
54
if (!files) {
55
return;
56
}
57
58
memdelete(files);
59
}
60
61
Ref<FileAccess> FileAccessMemory::create() {
62
return memnew(FileAccessMemory);
63
}
64
65
bool FileAccessMemory::file_exists(const String &p_name) {
66
String name = fix_path(p_name);
67
//name = DirAccess::normalize_path(name);
68
69
return files && (files->find(name) != nullptr);
70
}
71
72
Error FileAccessMemory::open_custom(const uint8_t *p_data, uint64_t p_len) {
73
data = (uint8_t *)p_data;
74
length = p_len;
75
pos = 0;
76
return OK;
77
}
78
79
Error FileAccessMemory::open_internal(const String &p_path, int p_mode_flags) {
80
ERR_FAIL_NULL_V(files, ERR_FILE_NOT_FOUND);
81
82
String name = fix_path(p_path);
83
//name = DirAccess::normalize_path(name);
84
85
HashMap<String, Vector<uint8_t>>::Iterator E = files->find(name);
86
ERR_FAIL_COND_V_MSG(!E, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s'.", p_path));
87
88
data = E->value.ptrw();
89
length = E->value.size();
90
pos = 0;
91
92
return OK;
93
}
94
95
bool FileAccessMemory::is_open() const {
96
return data != nullptr;
97
}
98
99
void FileAccessMemory::seek(uint64_t p_position) {
100
ERR_FAIL_NULL(data);
101
pos = p_position;
102
}
103
104
void FileAccessMemory::seek_end(int64_t p_position) {
105
ERR_FAIL_NULL(data);
106
pos = length + p_position;
107
}
108
109
uint64_t FileAccessMemory::get_position() const {
110
ERR_FAIL_NULL_V(data, 0);
111
return pos;
112
}
113
114
uint64_t FileAccessMemory::get_length() const {
115
ERR_FAIL_NULL_V(data, 0);
116
return length;
117
}
118
119
bool FileAccessMemory::eof_reached() const {
120
return pos >= length;
121
}
122
123
uint64_t FileAccessMemory::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
124
if (!p_length) {
125
return 0;
126
}
127
128
ERR_FAIL_NULL_V(p_dst, -1);
129
ERR_FAIL_NULL_V(data, -1);
130
131
uint64_t left = length - pos;
132
uint64_t read = MIN(p_length, left);
133
134
if (read < p_length) {
135
WARN_PRINT("Reading less data than requested");
136
}
137
138
memcpy(p_dst, &data[pos], read);
139
pos += read;
140
141
return read;
142
}
143
144
Error FileAccessMemory::get_error() const {
145
return pos >= length ? ERR_FILE_EOF : OK;
146
}
147
148
void FileAccessMemory::flush() {
149
ERR_FAIL_NULL(data);
150
}
151
152
bool FileAccessMemory::store_buffer(const uint8_t *p_src, uint64_t p_length) {
153
if (!p_length) {
154
return true;
155
}
156
157
ERR_FAIL_NULL_V(p_src, false);
158
159
uint64_t left = length - pos;
160
uint64_t write = MIN(p_length, left);
161
162
memcpy(&data[pos], p_src, write);
163
pos += write;
164
165
ERR_FAIL_COND_V_MSG(write < p_length, false, "Writing less data than requested.");
166
167
return true;
168
}
169
170