Added generalisation of csproj file location

This commit is contained in:
Kah Wei 2022-09-22 12:31:26 +08:00
parent 5ff375113f
commit 5bc24b09d4
3 changed files with 77 additions and 50 deletions

View File

@ -22,14 +22,16 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
/*--------------------------------------------------------------------------------*/
/* Static Definitions */
/*--------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Static Definitions */
/*----------------------------------------------------------------------------------*/
const std::string SHScriptEngine::DEFAULT_CSHARP_NAMESPACE = std::string("SHADE");
const std::string SHScriptEngine::CSPROJ_DIR = "..\\..\\TempScriptsFolder";
const std::string SHScriptEngine::CSPROJ_PATH = std::string(CSPROJ_DIR) + "\\SHADE_Scripting.csproj";
/*---------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::Init()
{
// Do not allow initialization if already initialised
@ -99,9 +101,9 @@ namespace SHADE
dotNet.Exit();
}
/*---------------------------------------------------------------------------------*/
/* Script Manipulation Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Script Manipulation Functions */
/*-----------------------------------------------------------------------------------*/
bool SHScriptEngine::AddScript(const SHEntity& entity, const std::string_view& scriptName)
{
return csScriptsAdd(entity.GetEID(), scriptName.data());
@ -140,41 +142,40 @@ namespace SHADE
return result;
}
/*---------------------------------------------------------------------------------*/
/* Script Serialisation Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Script Serialisation Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::DeserialiseScript(const SHEntity& entity, const std::string& yaml) const
{
csScriptDeserialise(entity.GetEID(), yaml.c_str());
}
/*---------------------------------------------------------------------------------*/
/* Script Editor Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Script Editor Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity) const
{
csEditorRenderScripts(entity.GetEID());
}
/*---------------------------------------------------------------------------------*/
/* Static Utility Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Static Utility Functions */
/*-----------------------------------------------------------------------------------*/
bool SHScriptEngine::BuildScriptAssembly(bool debug) const
{
constexpr std::string_view BUILD_LOG_PATH = "../Build.log";
static const std::string BUILD_LOG_PATH = "../Build.log";
// Generate csproj file if it doesn't exist
static const std::filesystem::path CSPROJ_PATH = "../SHADE_Scripting.csproj";
if (!std::filesystem::exists(CSPROJ_PATH))
{
GenerateScriptsCsProjFile(CSPROJ_PATH);
}
// Prepare directory (delete useless files)
deleteFolder("net5.0");
deleteFolder("ref");
deleteFolder("../SHADE_Scripting");
deleteFolder("../obj");
deleteFolder(CSPROJ_DIR + "\\net5.0");
deleteFolder(CSPROJ_DIR + "\\ref");
deleteFolder(CSPROJ_DIR + "\\obj");
deleteFolder(CSPROJ_DIR + "\\bin");
// Attempt to build the assembly
std::ostringstream oss;
@ -184,7 +185,7 @@ namespace SHADE
const bool BUILD_SUCCESS = execProcess
(
L"C:\\Windows\\system32\\cmd.exe",
L"/K \"dotnet build \"../SHADE_Scripting.csproj\" -c Debug -o \"./tmp/\" -fl -flp:LogFile=build.log;Verbosity=quiet & exit\""
L"/K \"" + generateBuildCommand(debug) + L" & exit\""
) == 0;
if (BUILD_SUCCESS)
{
@ -202,6 +203,7 @@ namespace SHADE
// Clean up built files
deleteFolder("./tmp");
deleteFolder(CSPROJ_DIR + "\\obj");
// Read the build log and output to the console
dumpBuildLog(BUILD_LOG_PATH);
@ -209,7 +211,7 @@ namespace SHADE
deleteFile(BUILD_LOG_PATH);
return BUILD_SUCCESS;
}
}
void SHScriptEngine::GenerateScriptsCsProjFile(const std::filesystem::path& path) const
{
@ -222,11 +224,11 @@ namespace SHADE
<Configurations>Release;Debug</Configurations>\n\
</PropertyGroup>\n\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n\
<OutputPath>.\\bin_Release-x64</OutputPath>\n\
<OutputPath>.\\bin\\Release</OutputPath>\n\
<PlatformTarget>x64</PlatformTarget>\n\
</PropertyGroup>\n\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\"> \n\
<OutputPath>.\\bin_Debug-x64</OutputPath>\n\
<OutputPath>.\\bin\\Debug</OutputPath>\n\
<PlatformTarget>x64</PlatformTarget>\n\
<DefineConstants>DEBUG;TRACE</DefineConstants>\n\
<Optimize>false</Optimize>\n\
@ -245,7 +247,8 @@ namespace SHADE
</ItemGroup>\n\
<ItemGroup>\n\
<Reference Include=\"SHADE_Managed\">\n\
<HintPath>.\\bin\\SHADE_Managed.dll</HintPath>\n\
<HintPath Condition=\"Exists('..\\bin\\Debug\\SHADE_Managed.dll')\">..\\bin\\Debug\\SHADE_Managed.dll</HintPath>\
<HintPath Condition=\"Exists('..\\bin\\Release\\SHADE_Managed.dll')\">..\\bin\\Release\\SHADE_Managed.dll</HintPath>\
</Reference>\n\
</ItemGroup>\n\
</Project>";
@ -262,9 +265,9 @@ namespace SHADE
file.close();
}
/*---------------------------------------------------------------------------------*/
/* Helper Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::loadFunctions()
{
std::ostringstream oss;
@ -404,25 +407,25 @@ namespace SHADE
}
}
}
void SHScriptEngine::deleteFile(const std::string_view& filePath)
void SHScriptEngine::deleteFile(const std::string& filePath)
{
try
{
std::filesystem::remove(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
}
void SHScriptEngine::deleteFolder(const std::string_view& filePath)
try
{
try
{
std::filesystem::remove_all(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
std::filesystem::remove(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
}
void SHScriptEngine::deleteFolder(const std::string& filePath)
{
try
{
std::filesystem::remove_all(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
}
bool SHScriptEngine::fileExists(const std::string_view& filePath)
bool SHScriptEngine::fileExists(const std::filesystem::path& filePath)
{
std::error_code error;
if (std::filesystem::exists(filePath, error))
@ -483,4 +486,14 @@ namespace SHADE
}
}
}
std::wstring SHScriptEngine::generateBuildCommand(bool debug)
{
std::wostringstream oss;
oss << "dotnet build \"" << SHStringUtils::StrToWstr(CSPROJ_PATH) << "\" -c ";
oss << debug ? "Debug" : "Release";
oss << " -o \"./tmp/\" -fl -flp:LogFile=build.log;Verbosity=quiet";
return oss.str();
}
}

View File

@ -208,6 +208,8 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
static constexpr std::string_view DEFAULT_CSHARP_LIB_NAME = "SHADE_Managed";
static constexpr std::string_view MANAGED_SCRIPT_LIB_NAME = "SHADE_Scripting";
static const std::string CSPROJ_DIR;
static const std::string CSPROJ_PATH;
static const std::string DEFAULT_CSHARP_NAMESPACE;
/*-----------------------------------------------------------------------------*/
@ -260,18 +262,19 @@ namespace SHADE
/// Deletes the file as specified by the file path.
/// </summary>
/// <param name="filePath">File path to the file to delete.</param>
static void deleteFile(const std::string_view& filePath);
static void deleteFile(const std::string& filePath);
/// <summary>
/// Deletes the folder and all files in it as specified by the file path.
/// </summary>
/// <param name="filePath">File path to the file to delete.</param>
static void deleteFolder(const std::string_view& filePath);
static void deleteFolder(const std::string& filePath);
/// <summary>
/// Checks if a specified file exists.
/// </summary>
/// <param name="filePath">File path to the file to check.</param>
/// <returns> True if the file exists </returns>
static bool fileExists(const std::string_view& filePath);
static bool fileExists(const std::filesystem::path& filePath);
static DWORD execProcess(const std::wstring& path, const std::wstring& args);
static std::wstring generateBuildCommand(bool debug);
};
}

View File

@ -0,0 +1,11 @@
using SHADE;
public class TestScript : Script
{
public TestScript(GameObject gameObj) : base(gameObj) {}
protected override void update()
{
Debug.Log("TestScript.Update()");
}
}