Files
Pal3.Unity/Assets/Scripts/Editor/SourceGenerator/DebugCommandsAutoGen.cs
T
Jiaqi Liu afbe502500 Update license header and fix Encamp command
Update license header and fix Encamp command
2022-02-28 18:28:12 -08:00

100 lines
3.9 KiB
C#

// ---------------------------------------------------------------------------------------------
// Copyright (c) 2021-2022, Jiaqi Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ---------------------------------------------------------------------------------------------
namespace Editor.SourceGenerator
{
using System;
using System.Linq;
using System.Reflection;
using Base;
using Pal3.Command.SceCommands;
using IngameDebugConsole;
using UnityEngine;
/// <summary>
/// DebugConsoleCommands.cs source generator
/// </summary>
/// <typeparam name="TCommand">Command type</typeparam>
public class DebugCommandsAutoGen<TCommand> : ISourceGenerator
{
public void GenerateSourceClass(CodeWriter writer, string fileName, string nameSpace)
{
// Header.
writer.WriteLine("// ---------------------------------------------------------------------------------------------");
writer.WriteLine("// <auto-generated>");
writer.WriteLine($"// This code was auto-generated by {GetType().Name}");
writer.WriteLine("// </auto-generated>");
writer.WriteLine("// ---------------------------------------------------------------------------------------------");
writer.WriteLine();
// Begin namespace.
writer.WriteLine($"namespace {nameSpace}");
writer.BeginBlock();
// Begin using.
writer.WriteLine("using IngameDebugConsole;");
writer.WriteLine("using SceCommands;");
writer.WriteLine();
// Begin class.
writer.WriteLine($"// <auto-generated/>");
writer.WriteLine($"public static class {fileName}");
writer.BeginBlock();
Type commandInterfaceType = typeof(TCommand);
var commands = Utility.GetTypesOfInterface(commandInterfaceType)
.Where(IsCommandAvailableInConsole).ToArray();
Debug.Log($"Found {commands.Length} available console commands");
for (var i = 0; i < commands.Length; i++)
{
if (!IsCommandAvailableInConsole(commands[i])) continue;
var commandName = commands[i].Name.Replace("Command", string.Empty);
var properties = commands[i].GetProperties();
writer.WriteLine($"// <auto-generated/>");
var attributeName = nameof(ConsoleMethodAttribute);
writer.WriteLine($"[{attributeName}(\"{commandName}\", \"{GetCommandDescription(commands[i])}\")]");
writer.WriteLine($"public static void {commandName}" +
$"({Utility.GetMethodArgumentDefinitionListAsString(properties)})");
writer.BeginBlock();
writer.WriteLine($"CommandDispatcher<{typeof(TCommand).Name}>.Instance.Dispatch(");
writer.WriteIndent(); writer.WriteLine($"new {commands[i].Name}" +
$"({Utility.GetMethodArgumentListAsString(properties)}));");
writer.EndBlock();
if (i < commands.Length - 1)
{
writer.WriteLine();
}
}
// End class.
writer.EndBlock();
// End namespace.
writer.EndBlock();
}
private string GetCommandDescription(Type command)
{
if (command.GetCustomAttribute<SceCommandAttribute>() is { } attribute)
{
return attribute.Description;
}
return $"Execute {command.Name}.";
}
private static bool IsCommandAvailableInConsole(Type command)
{
return command.GetCustomAttributes()
.Any(attribute => attribute is SceCommandAttribute {IsAvailableInConsole: true});
}
}
}