Mono: support custom script templates.

Also fixes a bug that prevented methods like `duplicate()` from copying the source code. (Copied from GDScript implementation)
This commit is contained in:
Andreas Haas 2017-10-30 22:17:20 +01:00
parent 157fa55e34
commit f3218c24c7
No known key found for this signature in database
GPG Key ID: B5FFAE1B65FBD2E1
2 changed files with 71 additions and 2 deletions

View File

@ -297,6 +297,20 @@ Ref<Script> CSharpLanguage::get_template(const String &p_class_name, const Strin
return script;
}
bool CSharpLanguage::is_using_templates() {
return true;
}
void CSharpLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
String src = p_script->get_source_code();
src = src.replace("%BASE%", p_base_class_name)
.replace("%CLASS%", p_class_name)
.replace("%TS%", _get_indentation());
p_script->set_source_code(src);
}
Script *CSharpLanguage::create_script() const {
return memnew(CSharpScript);
@ -396,6 +410,25 @@ String CSharpLanguage::make_function(const String &p_class, const String &p_name
#endif
}
String CSharpLanguage::_get_indentation() const {
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
bool use_space_indentation = EDITOR_DEF("text_editor/indent/type", 0);
if (use_space_indentation) {
int indent_size = EDITOR_DEF("text_editor/indent/size", 4);
String space_indent = "";
for (int i = 0; i < indent_size; i++) {
space_indent += " ";
}
return space_indent;
}
}
#endif
return "\t";
}
void CSharpLanguage::frame() {
const Ref<MonoGCHandle> &task_scheduler_handle = GDMonoUtils::mono_cache.task_scheduler_handle;
@ -1411,6 +1444,34 @@ void CSharpScript::_resource_path_changed() {
}
}
bool CSharpScript::_get(const StringName &p_name, Variant &r_ret) const {
if (p_name == CSharpLanguage::singleton->string_names._script_source) {
r_ret = get_source_code();
return true;
}
return false;
}
bool CSharpScript::_set(const StringName &p_name, const Variant &p_value) {
if (p_name == CSharpLanguage::singleton->string_names._script_source) {
set_source_code(p_value);
reload();
return true;
}
return false;
}
void CSharpScript::_get_property_list(List<PropertyInfo> *p_properties) const {
p_properties->push_back(PropertyInfo(Variant::STRING, CSharpLanguage::singleton->string_names._script_source, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
}
void CSharpScript::_bind_methods() {
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "new", &CSharpScript::_new, MethodInfo(Variant::OBJECT, "new"));
@ -1984,5 +2045,6 @@ CSharpLanguage::StringNameCache::StringNameCache() {
_set = StaticCString::create("_set");
_get = StaticCString::create("_get");
_notification = StaticCString::create("_notification");
_script_source = StaticCString::create("script/source");
dotctor = StaticCString::create(".ctor");
}

View File

@ -116,6 +116,9 @@ protected:
Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error);
virtual void _resource_path_changed();
bool _get(const StringName &p_name, Variant &r_ret) const;
bool _set(const StringName &p_name, const Variant &p_value);
void _get_property_list(List<PropertyInfo> *p_properties) const;
public:
virtual bool can_instance() const;
@ -228,16 +231,17 @@ class CSharpLanguage : public ScriptLanguage {
StringName _set;
StringName _get;
StringName _notification;
StringName _script_source;
StringName dotctor; // .ctor
StringNameCache();
};
StringNameCache string_names;
int lang_idx;
public:
StringNameCache string_names;
_FORCE_INLINE_ int get_language_index() { return lang_idx; }
void set_language_index(int p_idx);
@ -266,6 +270,8 @@ public:
virtual void get_comment_delimiters(List<String> *p_delimiters) const;
virtual void get_string_delimiters(List<String> *p_delimiters) const;
virtual Ref<Script> get_template(const String &p_class_name, const String &p_base_class_name) const;
virtual bool is_using_templates();
virtual void make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script);
/* TODO */ virtual bool validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions) const { return true; }
virtual Script *create_script() const;
virtual bool has_named_classes() const;
@ -273,6 +279,7 @@ public:
/* TODO? */ virtual int find_function(const String &p_function, const String &p_code) const { return -1; }
virtual String make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const;
/* TODO? */ Error complete_code(const String &p_code, const String &p_base_path, Object *p_owner, List<String> *r_options, String &r_call_hint) { return ERR_UNAVAILABLE; }
virtual String _get_indentation() const;
/* TODO? */ virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {}
/* TODO */ virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) {}