Files
OpenRA/OpenRA.Mods.Common/UtilityCommands/Documentation/ExtractSpriteSequenceDocsCommand.cs
let5sne.win10 9cf6ebb986
Some checks failed
Continuous Integration / Linux (.NET 8.0) (push) Has been cancelled
Continuous Integration / Windows (.NET 8.0) (push) Has been cancelled
Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 21:46:54 +08:00

104 lines
3.6 KiB
C#

#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Mods.Common.UtilityCommands.Documentation.Objects;
using OpenRA.Primitives;
namespace OpenRA.Mods.Common.UtilityCommands.Documentation
{
sealed class ExtractSpriteSequenceDocsCommand : IUtilityCommand
{
string IUtilityCommand.Name => "--sprite-sequence-docs";
bool IUtilityCommand.ValidateArguments(string[] args) => true;
[Desc("[VERSION]", "Generate sprite sequence documentation in JSON format.")]
void IUtilityCommand.Run(Utility utility, string[] args)
{
// HACK: The engine code assumes that Game.modData is set.
Game.ModData = utility.ModData;
var version = utility.ModData.Manifest.Metadata.Version;
if (args.Length > 1)
version = args[1];
var objectCreator = utility.ModData.ObjectCreator;
var spriteSequenceTypes = objectCreator.GetTypesImplementing<ISpriteSequence>().OrderBy(t => t.Namespace).ThenBy(t => t.Name);
var json = GenerateJson(version, spriteSequenceTypes);
Console.WriteLine(json);
}
static string GenerateJson(string version, IEnumerable<Type> sequenceTypes)
{
var relatedEnumTypes = new HashSet<Type>();
var pdbTypesCache = DocumentationHelpers.CreatePdbTypesCache();
var sequenceTypesInfo = sequenceTypes
.Where(x => !x.ContainsGenericParameters && !x.IsAbstract)
.Select(type => new ExtractedClassInfo
{
Namespace = type.Namespace,
Name = type.Name,
Filename = DocumentationHelpers.GetSourceFilenameForType(type, pdbTypesCache),
Description = string.Join(" ", type.GetCustomAttributes<DescAttribute>(false).SelectMany(d => d.Lines)),
InheritedTypes = type.BaseTypes()
.Select(y => y.Name)
.Where(y => y != type.Name && y != "Object"),
Properties = type.GetFields(BindingFlags.NonPublic | BindingFlags.Static)
.Where(fi => fi.FieldType.IsGenericType && fi.FieldType.GetGenericTypeDefinition() == typeof(SpriteSequenceField<>))
.Select(fi =>
{
var description = string.Join(" ", fi.GetCustomAttributes<DescAttribute>(false)
.SelectMany(d => d.Lines));
var valueType = fi.FieldType.GetGenericArguments()[0];
var key = (string)fi.FieldType
.GetProperty(nameof(SpriteSequenceField<bool>.Key))?
.GetValue(fi.GetValue(null));
var defaultValueProperty = fi.FieldType.GetProperty(nameof(SpriteSequenceField<bool>.DefaultValue));
var defaultValue = defaultValueProperty?.GetValue(fi.GetValue(null));
if (defaultValueProperty != null && defaultValueProperty.PropertyType.IsEnum)
relatedEnumTypes.Add(defaultValueProperty.PropertyType);
return new ExtractedClassFieldInfo
{
PropertyName = key,
DefaultValue = defaultValue?.ToString(),
InternalType = Util.InternalTypeName(valueType),
UserFriendlyType = Util.FriendlyTypeName(valueType),
Description = description
};
})
});
var result = new
{
Version = version,
SpriteSequenceTypes = sequenceTypesInfo,
RelatedEnums = DocumentationHelpers.GetRelatedEnumInfos(relatedEnumTypes, pdbTypesCache)
};
return JsonSerializer.Serialize(result);
}
}
}