// --------------------------------------------------------------------------------------------- // 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 Pal3.Command.InternalCommands; using UnityEngine; /// /// DebugConsoleCommands.cs source generator /// /// Command type public class DebugCommandsAutoGen : ISourceGenerator { public void GenerateSourceClass(CodeWriter writer, string fileName, string nameSpace) { // Header. writer.WriteLine("// ---------------------------------------------------------------------------------------------"); writer.WriteLine("// "); writer.WriteLine($"// This code was auto-generated by {GetType().Name}"); writer.WriteLine("// "); 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($"// "); 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($"// "); 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() is { } attribute) { return attribute.Description; } return $"Execute {command.Name}."; } private static bool IsCommandAvailableInConsole(Type command) { if (command == typeof(ResetGameStateCommand)) return true; return command.GetCustomAttributes() .Any(attribute => attribute is SceCommandAttribute {IsAvailableInConsole: true}); } } }