// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
using System.IO;
using System.Text;
using FlaxEditor.Content.Settings;
using FlaxEngine;
namespace FlaxEditor.Content
{
///
/// Context proxy object for C++ files.
///
///
public abstract class CppProxy : ScriptProxy
{
///
/// Gets the paths for header and source files to format.
///
/// The header template path.
/// The source template path.
protected abstract void GetTemplatePaths(out string headerTemplate, out string sourceTemplate);
///
public override bool IsProxyFor(ContentItem item)
{
return false;
}
///
public override void Create(string outputPath, object arg)
{
// Find the module that this script is being added (based on the path)
var module = string.Empty;
var project = TryGetProjectAtFolder(outputPath, out var moduleName);
if (project != null)
{
module = moduleName.ToUpperInvariant() + "_API ";
}
var gameSettings = GameSettings.Load();
var scriptName = ScriptItem.CreateScriptName(outputPath);
var filename = Path.GetFileNameWithoutExtension(outputPath);
var copyrightComment = string.IsNullOrEmpty(gameSettings.CopyrightNotice) ? string.Empty : string.Format("// {0}{1}{1}", gameSettings.CopyrightNotice, Environment.NewLine);
GetTemplatePaths(out var headerTemplatePath, out var sourceTemplatePath);
if (headerTemplatePath != null)
{
var headerTemplate = File.ReadAllText(headerTemplatePath);
headerTemplate = headerTemplate.Replace("%copyright%", copyrightComment);
headerTemplate = headerTemplate.Replace("%class%", scriptName);
headerTemplate = headerTemplate.Replace("%module%", module);
headerTemplate = headerTemplate.Replace("%filename%", filename);
File.WriteAllText(Path.ChangeExtension(outputPath, ".h"), headerTemplate, Encoding.UTF8);
}
if (sourceTemplatePath != null)
{
var sourceTemplate = File.ReadAllText(sourceTemplatePath);
sourceTemplate = sourceTemplate.Replace("%copyright%", copyrightComment);
sourceTemplate = sourceTemplate.Replace("%class%", scriptName);
sourceTemplate = sourceTemplate.Replace("%module%", module);
sourceTemplate = sourceTemplate.Replace("%filename%", filename);
File.WriteAllText(outputPath, sourceTemplate, Encoding.UTF8);
}
}
///
public override string FileExtension => "cpp";
///
public override Color AccentColor => Color.FromRGB(0x9c1c9c);
}
///
/// Context proxy object for C++ script files.
///
///
[ContentContextMenu("New/C++/C++ Script")]
public class CppScriptProxy : CppProxy
{
///
public override string Name => "C++ Script";
///
public override bool IsProxyFor(ContentItem item)
{
return item is CppScriptItem;
}
///
protected override void GetTemplatePaths(out string headerTemplate, out string sourceTemplate)
{
headerTemplate = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/ScriptTemplate.h");
sourceTemplate = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/ScriptTemplate.cpp");
}
}
///
/// Context proxy object for C++ Json Asset files.
///
///
[ContentContextMenu("New/C++/C++ Function Library")]
public class CppStaticClassProxy : CppProxy
{
///
public override string Name => "C++ Function Library";
///
protected override void GetTemplatePaths(out string headerTemplate, out string sourceTemplate)
{
headerTemplate = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/CppStaticClassTemplate.h");
sourceTemplate = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/CppStaticClassTemplate.cpp");
}
}
///
/// Context proxy object for C++ Json Asset files.
///
///
[ContentContextMenu("New/C++/C++ Json Asset")]
public class CppAssetProxy : CppProxy
{
///
public override string Name => "C++ Json Asset";
///
protected override void GetTemplatePaths(out string headerTemplate, out string sourceTemplate)
{
headerTemplate = null;
sourceTemplate = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/CppAssetTemplate.h");
//sourceTemplate = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/CppAssetTemplate.cpp");
}
///
public override string FileExtension => "h";
}
}