Program.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Program.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.IO;
  12. using System.Linq;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Content.Pipeline;
  15. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  16. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  17. #endregion
  18. namespace CompileEffect
  19. {
  20. /// <summary>
  21. /// Commandline utility compiles .fx effect sources file into binary files that can be consumed by XNA Game Studio.
  22. /// </summary>
  23. class Program
  24. {
  25. static int Main(string[] args)
  26. {
  27. // Make sure we have the right number of commandline arguments.
  28. if (args.Length != 4)
  29. {
  30. Console.Error.WriteLine("Usage: CompileEffect <targetPlatform> <targetProfile> <input.fx> <output.bin>");
  31. return 1;
  32. }
  33. // Parse the commandline arguments.
  34. TargetPlatform targetPlatform;
  35. if (!Enum.TryParse(args[0], true, out targetPlatform))
  36. {
  37. Console.Error.WriteLine("Invalid target platform {0}. Valid options are {1}.", args[0], GetEnumValues<TargetPlatform>());
  38. return 1;
  39. }
  40. GraphicsProfile targetProfile;
  41. if (!Enum.TryParse(args[1], true, out targetProfile))
  42. {
  43. Console.Error.WriteLine("Invalid target profile {0}. Valid options are {1}.", args[1], GetEnumValues<GraphicsProfile>());
  44. return 1;
  45. }
  46. string inputFilename = args[2];
  47. string outputFilename = args[3];
  48. try
  49. {
  50. Console.WriteLine("Compiling {0} -> {1} for {2}, {3}", Path.GetFileName(inputFilename), outputFilename, targetPlatform, targetProfile);
  51. ContentBuildLogger logger = new CustomLogger();
  52. // Import the effect source code.
  53. EffectImporter importer = new EffectImporter();
  54. ContentImporterContext importerContext = new CustomImporterContext(logger);
  55. EffectContent sourceEffect = importer.Import(inputFilename, importerContext);
  56. // Compile the effect.
  57. EffectProcessor processor = new EffectProcessor();
  58. ContentProcessorContext processorContext = new CustomProcessorContext(targetPlatform, targetProfile, logger);
  59. CompiledEffectContent compiledEffect = processor.Process(sourceEffect, processorContext);
  60. // Write out the compiled effect code.
  61. File.WriteAllBytes(outputFilename, compiledEffect.GetEffectCode());
  62. }
  63. catch (Exception e)
  64. {
  65. Console.Error.WriteLine("Error: {0}", e.Message);
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. /// <summary>
  71. /// Helper returns a comma separated list of all the possible values of an enum.
  72. /// </summary>
  73. static string GetEnumValues<T>()
  74. {
  75. T[] values = (T[])Enum.GetValues(typeof(T));
  76. return string.Join(", ", from t in values select t.ToString());
  77. }
  78. }
  79. }