Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/mono/csharp_script_resource_format.cpp
45997 views
1
/**************************************************************************/
2
/* csharp_script_resource_format.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 "csharp_script_resource_format.h"
32
33
#include "mono_gd/gd_mono_cache.h"
34
35
#include "core/io/file_access.h"
36
37
#ifdef TOOLS_ENABLED
38
static bool _create_project_solution_if_needed() {
39
CRASH_COND(CSharpLanguage::get_singleton()->get_godotsharp_editor() == nullptr);
40
return CSharpLanguage::get_singleton()->get_godotsharp_editor()->call("CreateProjectSolutionIfNeeded");
41
}
42
#endif
43
44
Ref<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) {
45
if (r_error) {
46
*r_error = ERR_FILE_CANT_OPEN;
47
}
48
49
// TODO ignore anything inside bin/ and obj/ in tools builds?
50
51
String real_path = p_path;
52
if (p_path.begins_with("csharp://")) {
53
// This is a virtual path used by generic types, extract the real path.
54
real_path = "res://" + p_path.trim_prefix("csharp://");
55
real_path = real_path.substr(0, real_path.rfind_char(':'));
56
}
57
58
Ref<CSharpScript> scr;
59
60
if (GDMonoCache::godot_api_cache_updated) {
61
GDMonoCache::managed_callbacks.ScriptManagerBridge_GetOrCreateScriptBridgeForPath(&p_path, &scr);
62
ERR_FAIL_COND_V_MSG(scr.is_null(), Ref<Resource>(), "Could not create C# script '" + real_path + "'.");
63
} else {
64
scr.instantiate();
65
}
66
67
#ifdef DEBUG_ENABLED
68
Error err = scr->load_source_code(real_path);
69
ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot load C# script file '" + real_path + "'.");
70
#endif // DEBUG_ENABLED
71
72
// Only one instance of a C# script is allowed to exist.
73
ERR_FAIL_COND_V_MSG(!scr->get_path().is_empty() && scr->get_path() != p_original_path, Ref<Resource>(),
74
"The C# script path is different from the path it was registered in the C# dictionary.");
75
76
Ref<Resource> existing = ResourceCache::get_ref(p_path);
77
switch (p_cache_mode) {
78
case ResourceFormatLoader::CACHE_MODE_IGNORE:
79
case ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP:
80
break;
81
case ResourceFormatLoader::CACHE_MODE_REUSE:
82
if (existing.is_null()) {
83
scr->set_path(p_original_path);
84
} else {
85
scr = existing;
86
}
87
break;
88
case ResourceFormatLoader::CACHE_MODE_REPLACE:
89
case ResourceFormatLoader::CACHE_MODE_REPLACE_DEEP:
90
scr->set_path(p_original_path, true);
91
break;
92
}
93
94
scr->reload();
95
96
if (r_error) {
97
*r_error = OK;
98
}
99
100
return scr;
101
}
102
103
void ResourceFormatLoaderCSharpScript::get_recognized_extensions(List<String> *p_extensions) const {
104
p_extensions->push_back("cs");
105
}
106
107
bool ResourceFormatLoaderCSharpScript::handles_type(const String &p_type) const {
108
return p_type == "Script" || p_type == CSharpLanguage::get_singleton()->get_type();
109
}
110
111
String ResourceFormatLoaderCSharpScript::get_resource_type(const String &p_path) const {
112
return p_path.has_extension("cs") ? CSharpLanguage::get_singleton()->get_type() : "";
113
}
114
115
Error ResourceFormatSaverCSharpScript::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
116
Ref<CSharpScript> sqscr = p_resource;
117
ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
118
119
String source = sqscr->get_source_code();
120
121
#ifdef TOOLS_ENABLED
122
if (!FileAccess::exists(p_path)) {
123
// The file does not yet exist, let's assume the user just created this script. In such
124
// cases we need to check whether the solution and csproj were already created or not.
125
if (!_create_project_solution_if_needed()) {
126
ERR_PRINT("C# project could not be created; cannot add file: '" + p_path + "'.");
127
}
128
}
129
#endif
130
131
{
132
Error err;
133
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
134
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save C# script file '" + p_path + "'.");
135
136
file->store_string(source);
137
138
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
139
return ERR_CANT_CREATE;
140
}
141
}
142
143
#ifdef TOOLS_ENABLED
144
if (ScriptServer::is_reload_scripts_on_save_enabled()) {
145
CSharpLanguage::get_singleton()->reload_tool_script(p_resource, false);
146
}
147
#endif
148
149
return OK;
150
}
151
152
void ResourceFormatSaverCSharpScript::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
153
if (Object::cast_to<CSharpScript>(p_resource.ptr())) {
154
p_extensions->push_back("cs");
155
}
156
}
157
158
bool ResourceFormatSaverCSharpScript::recognize(const Ref<Resource> &p_resource) const {
159
return Object::cast_to<CSharpScript>(p_resource.ptr()) != nullptr;
160
}
161
162