| 1111 | } |
| 1112 | |
| 1113 | Error GDScript::load_source_code(const String &p_path) { |
| 1114 | if (p_path.is_empty() || p_path.begins_with("gdscript://") || ResourceLoader::get_resource_type(p_path.get_slice("::", 0)) == "PackedScene") { |
| 1115 | return OK; |
| 1116 | } |
| 1117 | |
| 1118 | Vector<uint8_t> sourcef; |
| 1119 | Error err; |
| 1120 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err); |
| 1121 | if (err) { |
| 1122 | const char *err_name; |
| 1123 | if (err < 0 || err >= ERR_MAX) { |
| 1124 | err_name = "(invalid error code)"; |
| 1125 | } else { |
| 1126 | err_name = error_names[err]; |
| 1127 | } |
| 1128 | ERR_FAIL_COND_V_MSG(err, err, "Attempt to open script '" + p_path + "' resulted in error '" + err_name + "'."); |
| 1129 | } |
| 1130 | |
| 1131 | uint64_t len = f->get_length(); |
| 1132 | sourcef.resize(len + 1); |
| 1133 | uint8_t *w = sourcef.ptrw(); |
| 1134 | uint64_t r = f->get_buffer(w, len); |
| 1135 | ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN); |
| 1136 | w[len] = 0; |
| 1137 | |
| 1138 | String s; |
| 1139 | if (s.append_utf8((const char *)w, len) != OK) { |
| 1140 | ERR_FAIL_V_MSG(ERR_INVALID_DATA, "Script '" + p_path + "' contains invalid unicode (UTF-8), so it was not loaded. Please ensure that scripts are saved in valid UTF-8 unicode."); |
| 1141 | } |
| 1142 | |
| 1143 | source = s; |
| 1144 | path = p_path; |
| 1145 | path_valid = true; |
| 1146 | #ifdef TOOLS_ENABLED |
| 1147 | source_changed_cache = true; |
| 1148 | set_edited(false); |
| 1149 | set_last_modified_time(FileAccess::get_modified_time(path)); |
| 1150 | #endif // TOOLS_ENABLED |
| 1151 | return OK; |
| 1152 | } |
| 1153 | |
| 1154 | void GDScript::set_binary_tokens_source(const Vector<uint8_t> &p_binary_tokens) { |
| 1155 | binary_tokens = p_binary_tokens; |