// 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
{
///
/// Proxy object for C# files
///
/// ///
public abstract class CSharpProxy : ScriptProxy
{
///
/// The script files extension filter.
///
public static readonly string ExtensionFilter = "*.cs";
///
public override bool IsProxyFor(ContentItem item)
{
return item is CSharpScriptItem;
}
///
/// Gets the path for the C# template.
///
/// The path to the template
protected abstract void GetTemplatePath(out string path);
///
public override ContentItem ConstructItem(string path)
{
return new CSharpScriptItem(path);
}
///
public override void Create(string outputPath, object arg)
{
// Load template
GetTemplatePath(out var templatePath);
var scriptTemplate = File.ReadAllText(templatePath);
// Find the module that this script is being added (based on the path)
var scriptNamespace = Editor.Instance.GameProject.Name;
var project = TryGetProjectAtFolder(outputPath, out var moduleName);
if (project != null)
{
scriptNamespace = moduleName.Length != 0 ? moduleName : project.Name;
}
scriptNamespace = scriptNamespace.Replace(" ", "");
// Format
var gameSettings = GameSettings.Load();
var scriptName = ScriptItem.CreateScriptName(outputPath);
var copyrightComment = string.IsNullOrEmpty(gameSettings.CopyrightNotice) ? string.Empty : string.Format("// {0}{1}{1}", gameSettings.CopyrightNotice, Environment.NewLine);
scriptTemplate = scriptTemplate.Replace("%copyright%", copyrightComment);
scriptTemplate = scriptTemplate.Replace("%class%", scriptName);
scriptTemplate = scriptTemplate.Replace("%namespace%", scriptNamespace);
// Save
File.WriteAllText(outputPath, scriptTemplate, Encoding.UTF8);
}
///
public override string FileExtension => "cs";
///
public override Color AccentColor => Color.FromRGB(0x1c9c2b);
}
///
/// Context proxy object for C# Script files.
///
///
[ContentContextMenu("New/C#/C# Script")]
public class CSharpScriptProxy : CSharpProxy
{
///
public override string Name => "C# Script";
///
protected override void GetTemplatePath(out string path)
{
path = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/ScriptTemplate.cs");
}
}
///
/// Context proxy object for C# Actor files.
///
///
[ContentContextMenu("New/C#/C# Actor")]
public class CSharpActorProxy : CSharpProxy
{
///
public override string Name => "C# Actor";
///
protected override void GetTemplatePath(out string path)
{
path = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/ActorTemplate.cs");
}
}
///
/// Context proxy object for C# GamePlugin files.
///
///
[ContentContextMenu("New/C#/C# GamePlugin")]
public class CSharpGamePluginProxy : CSharpProxy
{
///
public override string Name => "C# GamePlugin";
///
protected override void GetTemplatePath(out string path)
{
path = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/GamePluginTemplate.cs");
}
}
///
/// Context proxy object for empty C# files.
///
///
[ContentContextMenu("New/C#/C# Empty File")]
public class CSharpEmptyProxy : CSharpProxy
{
///
public override string Name => "C# Empty File";
///
protected override void GetTemplatePath(out string path)
{
path = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/CSharpEmptyTemplate.cs");
}
}
///
/// Context proxy object for empty C# class files.
///
///
[ContentContextMenu("New/C#/C# Class")]
public class CSharpEmptyClassProxy : CSharpProxy
{
///
public override string Name => "C# Class";
///
protected override void GetTemplatePath(out string path)
{
path = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/EmptyClassTemplate.cs");
}
}
///
/// Context proxy object for empty C# struct files.
///
///
[ContentContextMenu("New/C#/C# Struct")]
public class CSharpEmptyStructProxy : CSharpProxy
{
///
public override string Name => "C# Struct";
///
protected override void GetTemplatePath(out string path)
{
path = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/EmptyStructTemplate.cs");
}
}
///
/// Context proxy object for empty C# interface files.
///
///
[ContentContextMenu("New/C#/C# Interface")]
public class CSharpEmptyInterfaceProxy : CSharpProxy
{
///
public override string Name => "C# Interface";
///
protected override void GetTemplatePath(out string path)
{
path = StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Scripting/EmptyInterfaceTemplate.cs");
}
}
}