// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Delegate.h"
#include "Engine/Core/Types/String.h"
#include "Engine/Scripting/ScriptingType.h"
///
/// Game scrips building service. Compiles user C# scripts into binary assemblies. Exposes many events used to track scripts compilation and reloading.
///
API_CLASS(Static, Namespace="FlaxEditor") class ScriptsBuilder
{
DECLARE_SCRIPTING_TYPE_NO_SPAWN(ScriptsBuilder);
public:
typedef Delegate CompileMsgDelegate;
public:
///
/// Action called on compilation end, bool param is true if success, otherwise false
///
static Delegate OnCompilationEnd;
///
/// Action called when compilation success
///
static Action OnCompilationSuccess;
///
/// Action called when compilation fails
///
static Action OnCompilationFailed;
public:
///
/// Gets amount of source code compile actions since Editor startup.
///
API_PROPERTY() static int32 GetCompilationsCount();
///
/// Checks if last scripting building failed due to errors.
///
/// True if last compilation result was a fail, otherwise false.
API_PROPERTY() static bool LastCompilationFailed();
///
/// Returns true if source code has been edited.
///
/// True if source code is dirty, otherwise false.
API_PROPERTY() static bool IsSourceDirty();
///
/// Returns true if source code workspace has been edited (source file moved or deleted).
///
/// True if source code workspace is dirty, otherwise false.
API_PROPERTY() static bool IsSourceWorkspaceDirty();
///
/// Returns true if source code has been edited and is dirty for given amount of time
///
/// Time to use for checking.
/// True if source code is dirty, otherwise false.
static bool IsSourceDirtyFor(const TimeSpan& timeout);
///
/// Returns true if scripts are being now compiled/reloaded.
///
/// True if scripts are being compiled/reloaded now.
API_PROPERTY() static bool IsCompiling();
///
/// Returns true if source code has been compiled and assemblies are ready to load.
///
/// True if assemblies are ready to load.
API_PROPERTY() static bool IsReady();
///
/// Indicates that scripting directory has been modified so scripts need to be rebuild.
///
API_FUNCTION() static void MarkWorkspaceDirty();
///
/// Checks if need to compile source code. If so calls compilation.
///
API_FUNCTION() static void CheckForCompile();
///
/// Requests project source code compilation.
///
API_FUNCTION() static void Compile();
///
/// Invokes the Flax.Build tool in the current project workspace and waits for the process end (blocking). Prints the build tool output to the log. Can be invoked from any thread.
///
/// The Flax.Build tool invocation arguments.
/// The custom working directory. Use empty or null to execute build tool in the project folder.
/// True if failed, otherwise false.
API_FUNCTION() static bool RunBuildTool(const StringView& args, const StringView& workingDir = StringView::Empty);
///
/// Generates the project files.
///
/// The additional Flax.Build tool invocation arguments.
/// True if failed, otherwise false.
API_FUNCTION() static bool GenerateProject(const StringView& customArgs = StringView::Empty);
///
/// Tries to find a script type with the given name.
///
/// The script full name.
/// Found script type or null if missing or invalid name.
API_FUNCTION() static MClass* FindScript(const StringView& scriptName);
// Gets the list of existing in-build code editors.
API_FUNCTION() static void GetExistingEditors(int32* result, int32 count);
// Gets the options for the game scripts to use for the Editor.
API_FUNCTION() static void GetBinariesConfiguration(API_PARAM(Out) StringView& target, API_PARAM(Out) StringView& platform, API_PARAM(Out) StringView& architecture, API_PARAM(Out) StringView& configuration);
static void GetBinariesConfiguration(const Char*& target, const Char*& platform, const Char*& architecture, const Char*& configuration);
public:
///
/// Filters the project namespace text value to be valid (remove whitespace characters and other invalid ones).
///
/// The in/out value.
static void FilterNamespaceText(String& value);
};