Path: blob/master/modules/mono/csharp_script_resource_format.cpp
45997 views
/**************************************************************************/1/* csharp_script_resource_format.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "csharp_script_resource_format.h"3132#include "mono_gd/gd_mono_cache.h"3334#include "core/io/file_access.h"3536#ifdef TOOLS_ENABLED37static bool _create_project_solution_if_needed() {38CRASH_COND(CSharpLanguage::get_singleton()->get_godotsharp_editor() == nullptr);39return CSharpLanguage::get_singleton()->get_godotsharp_editor()->call("CreateProjectSolutionIfNeeded");40}41#endif4243Ref<Resource> ResourceFormatLoaderCSharpScript::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) {44if (r_error) {45*r_error = ERR_FILE_CANT_OPEN;46}4748// TODO ignore anything inside bin/ and obj/ in tools builds?4950String real_path = p_path;51if (p_path.begins_with("csharp://")) {52// This is a virtual path used by generic types, extract the real path.53real_path = "res://" + p_path.trim_prefix("csharp://");54real_path = real_path.substr(0, real_path.rfind_char(':'));55}5657Ref<CSharpScript> scr;5859if (GDMonoCache::godot_api_cache_updated) {60GDMonoCache::managed_callbacks.ScriptManagerBridge_GetOrCreateScriptBridgeForPath(&p_path, &scr);61ERR_FAIL_COND_V_MSG(scr.is_null(), Ref<Resource>(), "Could not create C# script '" + real_path + "'.");62} else {63scr.instantiate();64}6566#ifdef DEBUG_ENABLED67Error err = scr->load_source_code(real_path);68ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot load C# script file '" + real_path + "'.");69#endif // DEBUG_ENABLED7071// Only one instance of a C# script is allowed to exist.72ERR_FAIL_COND_V_MSG(!scr->get_path().is_empty() && scr->get_path() != p_original_path, Ref<Resource>(),73"The C# script path is different from the path it was registered in the C# dictionary.");7475Ref<Resource> existing = ResourceCache::get_ref(p_path);76switch (p_cache_mode) {77case ResourceFormatLoader::CACHE_MODE_IGNORE:78case ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP:79break;80case ResourceFormatLoader::CACHE_MODE_REUSE:81if (existing.is_null()) {82scr->set_path(p_original_path);83} else {84scr = existing;85}86break;87case ResourceFormatLoader::CACHE_MODE_REPLACE:88case ResourceFormatLoader::CACHE_MODE_REPLACE_DEEP:89scr->set_path(p_original_path, true);90break;91}9293scr->reload();9495if (r_error) {96*r_error = OK;97}9899return scr;100}101102void ResourceFormatLoaderCSharpScript::get_recognized_extensions(List<String> *p_extensions) const {103p_extensions->push_back("cs");104}105106bool ResourceFormatLoaderCSharpScript::handles_type(const String &p_type) const {107return p_type == "Script" || p_type == CSharpLanguage::get_singleton()->get_type();108}109110String ResourceFormatLoaderCSharpScript::get_resource_type(const String &p_path) const {111return p_path.has_extension("cs") ? CSharpLanguage::get_singleton()->get_type() : "";112}113114Error ResourceFormatSaverCSharpScript::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {115Ref<CSharpScript> sqscr = p_resource;116ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);117118String source = sqscr->get_source_code();119120#ifdef TOOLS_ENABLED121if (!FileAccess::exists(p_path)) {122// The file does not yet exist, let's assume the user just created this script. In such123// cases we need to check whether the solution and csproj were already created or not.124if (!_create_project_solution_if_needed()) {125ERR_PRINT("C# project could not be created; cannot add file: '" + p_path + "'.");126}127}128#endif129130{131Error err;132Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);133ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save C# script file '" + p_path + "'.");134135file->store_string(source);136137if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {138return ERR_CANT_CREATE;139}140}141142#ifdef TOOLS_ENABLED143if (ScriptServer::is_reload_scripts_on_save_enabled()) {144CSharpLanguage::get_singleton()->reload_tool_script(p_resource, false);145}146#endif147148return OK;149}150151void ResourceFormatSaverCSharpScript::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {152if (Object::cast_to<CSharpScript>(p_resource.ptr())) {153p_extensions->push_back("cs");154}155}156157bool ResourceFormatSaverCSharpScript::recognize(const Ref<Resource> &p_resource) const {158return Object::cast_to<CSharpScript>(p_resource.ptr()) != nullptr;159}160161162