コード例 #1
0
        public override void Generate(GeneratorExecutionContext context, StringBuilder sb)
        {
            sb.AppendLine($"{Tab(1)}internal static partial class ContentReferences");
            sb.Append(Tab(1));
            sb.AppendLine("{");
            StringBuilder allTexturesBuilder = new($"{Tab(2)}public static readonly string[] _AllTextures = new string[]\n{Tab(2)}{{");
            StringBuilder allSoundsBuilder   = new($"{Tab(2)}public static readonly string[] _AllSounds = new string[]\n{Tab(2)}{{");
            int           textureCount       = 0;
            int           soundCount         = 0;

            foreach (var fullname in Directory.GetFiles(context.GetProjectLocaltion() + "/content", "*.*", SearchOption.TopDirectoryOnly).OrderBy(x => Path.GetFileName(x)))
            {
                string filename = Path.GetFileName(fullname);
                string fieldName;
                switch (Path.GetExtension(fullname).ToLower())
                {
                case ".png":
                {
                    switch (filename)
                    {
                    case "preview.png": continue;

                    case "screenshot.png": continue;

                    default:
                        break;
                    }
                    fieldName = GetFieldName(prefix_Texture, filename);
                    allTexturesBuilder.Append($"\n{Tab(3)}{fieldName},");
                    textureCount++;
                    break;
                }

                case ".wav":
                {
                    fieldName = GetFieldName(prefix_Sound, filename);
                    allSoundsBuilder.Append($"\n{Tab(3)}{fieldName},");
                    soundCount++;
                    break;
                }

                default: continue;
                }
                ;
                sb.AppendLine($"{Tab(2)}public const string {fieldName} = \"{filename}\";");
            }
            allTexturesBuilder.AppendLine($"\n{Tab(2)}}};");
            allSoundsBuilder.AppendLine($"\n{Tab(2)}}};");
            sb.Append(allTexturesBuilder);
            sb.Append(allSoundsBuilder);
            sb.AppendLine($"{Tab(2)}public const int ModTextureCount = {textureCount.ToString()};");
            sb.AppendLine($"{Tab(2)}public const int ModSoundCount = {soundCount.ToString()};");
            sb.Append(Tab(1));
            sb.AppendLine("}");
        }