Skip to content

Commit

Permalink
Use TOOLS_ENABLED check for editor functionality
Browse files Browse the repository at this point in the history
- Error when tests are enabled on `template_release`
  • Loading branch information
voidedWarranties committed Jul 29, 2023
1 parent df1c3b8 commit c906639
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ env_main.Append(CPPPATH=["src/"])

# Catch2
if env["tests"]:
if env["target"] != "editor":
raise ValueError("Tests can only be enabled on the editor target")

env_main.Append(CPPDEFINES="TESTS_ENABLED", CPPPATH=["extern/Catch2/extras/"])
sources.append(File("extern/Catch2/extras/catch_amalgamated.cpp"))

Expand Down
16 changes: 16 additions & 0 deletions src/luau_script.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class ScriptLanguage;

using namespace godot;

#ifdef TOOLS_ENABLED
class PlaceHolderScriptInstance;
#endif // TOOLS_ENABLED

typedef HashMap<uint64_t, List<Pair<StringName, Variant>>> ScriptInstanceState;

Expand All @@ -57,7 +59,9 @@ class LuauScript : public ScriptExtension {
friend class LuauLanguage;
friend class LuauCache;
friend class LuauScriptInstance;
#ifdef TOOLS_ENABLED
friend class PlaceHolderScriptInstance;
#endif // TOOLS_ENABLED

public:
enum LoadStage {
Expand All @@ -80,7 +84,9 @@ class LuauScript : public ScriptExtension {
bool source_changed_cache;

HashMap<uint64_t, LuauScriptInstance *> instances;
#ifdef TOOLS_ENABLED
HashMap<uint64_t, PlaceHolderScriptInstance *> placeholders;
#endif // TOOLS_ENABLED

bool _is_loading = false;
bool valid = false;
Expand All @@ -98,8 +104,10 @@ class LuauScript : public ScriptExtension {
Error finish_load();
Error try_load(lua_State *L, String *r_err = nullptr);

#ifdef TOOLS_ENABLED
void update_exports_values(List<GDProperty> &r_properties, HashMap<StringName, Variant> &r_values);
bool update_exports_internal(PlaceHolderScriptInstance *p_instance_to_update);
#endif // TOOLS_ENABLED

Error reload_tables();

Expand Down Expand Up @@ -165,8 +173,10 @@ class LuauScript : public ScriptExtension {
void _update_exports() override;
void _placeholder_erased(void *p_placeholder) override;

#ifdef TOOLS_ENABLED
bool placeholder_has(Object *p_object) const;
PlaceHolderScriptInstance *placeholder_get(Object *p_object);
#endif // TOOLS_ENABLED

/* TO IMPLEMENT */
int32_t _get_member_line(const StringName &p_member) const override { return -1; }
Expand All @@ -191,7 +201,9 @@ class LuauScript : public ScriptExtension {
bool add_dependency(const Ref<LuauScript> &p_script);

void load_module(lua_State *L);
#if TOOLS_ENABLED
void unload_module();
#endif // TOOLS_ENABLED

void error(const char *p_method, String p_msg, int p_line = 0) const;

Expand Down Expand Up @@ -306,6 +318,7 @@ class LuauScriptInstance : public ScriptInstance {
// ! sync with core/object/script_language
// need to reimplement here because Godot does not expose placeholders to GDExtension.
// doing this is okay because all Godot functions which request a placeholder instance assign it to a ScriptInstance *
#ifdef TOOLS_ENABLED
class PlaceHolderScriptInstance final : public ScriptInstance {
Object *owner = nullptr;
Ref<LuauScript> script;
Expand Down Expand Up @@ -339,6 +352,7 @@ class PlaceHolderScriptInstance final : public ScriptInstance {
PlaceHolderScriptInstance(const Ref<LuauScript> &p_script, Object *p_owner);
~PlaceHolderScriptInstance();
};
#endif // TOOLS_ENABLED

#define THREAD_EXECUTION_TIMEOUT 10

Expand Down Expand Up @@ -392,7 +406,9 @@ class LuauLanguage : public ScriptLanguageExtension {

void discover_core_scripts(const String &p_path = "res://");

#ifdef TOOLS_ENABLED
List<Ref<LuauScript>> get_scripts() const;
#endif // TOOLS_ENABLED

protected:
static void _bind_methods() {}
Expand Down
36 changes: 36 additions & 0 deletions src/luau_script_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ using namespace godot;
// See COPYRIGHT.txt for license information.

void *LuauScript::_placeholder_instance_create(Object *p_for_object) const {
#ifdef TOOLS_ENABLED
PlaceHolderScriptInstance *internal = memnew(PlaceHolderScriptInstance(Ref<LuauScript>(this), p_for_object));
return internal::gdextension_interface_script_instance_create(&PlaceHolderScriptInstance::INSTANCE_INFO, internal);
#else
return nullptr;
#endif // TOOLS_ENABLED
}

void LuauScript::_update_exports() {
#ifdef TOOLS_ENABLED
if (_is_module)
return;

Expand All @@ -48,8 +53,10 @@ void LuauScript::_update_exports() {
if (scr->has_dependency(this) && !this->has_dependency(scr))
scr->_update_exports();
}
#endif // TOOLS_ENABLED
}

#ifdef TOOLS_ENABLED
void LuauScript::update_exports_values(List<GDProperty> &r_properties, HashMap<StringName, Variant> &r_values) {
if (base.is_valid() && base->_is_valid()) {
base->update_exports_values(r_properties, r_values);
Expand Down Expand Up @@ -122,28 +129,38 @@ bool LuauScript::update_exports_internal(PlaceHolderScriptInstance *p_instance_t

return changed;
}
#endif // TOOLS_ENABLED

void LuauScript::_placeholder_erased(void *p_placeholder) {
#ifdef TOOLS_ENABLED
placeholders.erase(((PlaceHolderScriptInstance *)p_placeholder)->get_owner()->get_instance_id());
#endif // TOOLS_ENABLED
}

#ifdef TOOLS_ENABLED
bool LuauScript::placeholder_has(Object *p_object) const {
return placeholders.has(p_object->get_instance_id());
}

PlaceHolderScriptInstance *LuauScript::placeholder_get(Object *p_object) {
return placeholders.get(p_object->get_instance_id());
}
#endif // TOOLS_ENABLED

Ref<Script> LuauLanguage::_make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
#ifdef TOOLS_ENABLED
Ref<LuauScript> script;
script.instantiate();

// TODO: actual template stuff

return script;
#else
return Ref<Script>();
#endif // TOOLS_ENABLED
}

#ifdef TOOLS_ENABLED
// Sort such that base scripts and modules come first.
struct LuauScriptDepSort {
bool operator()(const Ref<LuauScript> &p_a, const Ref<LuauScript> &p_b) const {
Expand All @@ -163,7 +180,9 @@ struct LuauScriptDepSort {
return false;
}
};
#endif // TOOLS_ENABLED

#ifdef TOOLS_ENABLED
void LuauScript::unload_module() {
luau_data.bytecode.clear(); // Forces recompile on next load.

Expand All @@ -180,7 +199,9 @@ void LuauScript::unload_module() {
lua_pop(L, 1); // MODULE_TABLE
}
}
#endif // TOOLS_ENABLED

#ifdef TOOLS_ENABLED
List<Ref<LuauScript>> LuauLanguage::get_scripts() const {
List<Ref<LuauScript>> scripts;

Expand All @@ -205,8 +226,10 @@ List<Ref<LuauScript>> LuauLanguage::get_scripts() const {

return scripts;
}
#endif // TOOLS_ENABLED

void LuauLanguage::_reload_all_scripts() {
#ifdef TOOLS_ENABLED
List<Ref<LuauScript>> scripts = get_scripts();

for (Ref<LuauScript> &script : scripts) {
Expand All @@ -217,9 +240,11 @@ void LuauLanguage::_reload_all_scripts() {
script->load_source_code(script->get_path());
script->_reload(true);
}
#endif // TOOLS_ENABLED
}

void LuauLanguage::_reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
#ifdef TOOLS_ENABLED
// Step 1: List all scripts
List<Ref<LuauScript>> scripts = get_scripts();

Expand Down Expand Up @@ -331,13 +356,19 @@ void LuauLanguage::_reload_tool_script(const Ref<Script> &p_script, bool p_soft_
}
}
}
#endif // TOOLS_ENABLED
}

bool LuauLanguage::_handles_global_class_type(const String &p_type) const {
#ifdef TOOLS_ENABLED
return p_type == _get_type();
#else
return false;
#endif // TOOLS_ENABLED
}

Dictionary LuauLanguage::_get_global_class_name(const String &p_path) const {
#ifdef TOOLS_ENABLED
Error err = OK;
Ref<LuauScript> script = LuauCache::get_singleton()->get_script(p_path, err);
if (err != OK)
Expand Down Expand Up @@ -375,6 +406,9 @@ Dictionary LuauLanguage::_get_global_class_name(const String &p_path) const {
ret["icon_path"] = def.icon_path;

return ret;
#else
return Dictionary();
#endif // TOOLS_ENABLED
}

//////////////////////////
Expand All @@ -384,6 +418,7 @@ Dictionary LuauLanguage::_get_global_class_name(const String &p_path) const {
// Implementation mostly mirrors Godot's implementation.
// See license information in COPYRIGHT.txt.

#ifdef TOOLS_ENABLED
#define PLACEHOLDER_SELF ((PlaceHolderScriptInstance *)p_self)

static GDExtensionScriptInstanceInfo init_placeholder_instance_info() {
Expand Down Expand Up @@ -678,3 +713,4 @@ PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
script->_placeholder_erased(this);
}
}
#endif // TOOLS_ENABLED

0 comments on commit c906639

Please sign in to comment.