mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 17:38:10 +08:00
98 lines
3.8 KiB
C#
98 lines
3.8 KiB
C#
// ---------------------------------------------------------------------------------------------
|
|
// Copyright (c) 2021-2023, 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 IngameDebugConsole;
|
|
using Pal3.Command;
|
|
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 InternalCommands;");
|
|
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++)
|
|
{
|
|
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 AvailableInConsoleAttribute);
|
|
}
|
|
}
|
|
} |