Advertisement
Guest User

Untitled

a guest
Nov 18th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace AssetClassGenerator {
  9.     class Program {
  10.  
  11.         public static AssetGroup RootGroup;
  12.         public static string ClassNamespace;
  13.  
  14.         static void Main(string[] args) {
  15.  
  16.             var rootAssetFolder = "Assets";
  17.             var classNamespace = "Files";
  18.             var outputDir = "";
  19.  
  20.             var version = System.Reflection.Assembly.GetExecutingAssembly()
  21.                                            .GetName()
  22.                                            .Version
  23.                                            .ToString();
  24.  
  25.             Console.WriteLine("= Asset Class Generator v{0} =", version);
  26.  
  27.             if (args.Length > 0) {
  28.                 if (HelpRequired(args[0])) {
  29.                     Console.WriteLine("Help");
  30.                     Console.WriteLine("\n");
  31.                     Console.WriteLine("\tArgs: RootAssetFolder ClassNamespace CopyToPath");
  32.                     Console.WriteLine("\n");
  33.                     Console.WriteLine("\tRootAssetFolder is the folder containing all assets.  If not set defaults to Assets.");
  34.                     Console.WriteLine("\n");
  35.                     Console.WriteLine("\tClassNamespace is the namespace that the class will be in.  Usually the same as your project.  If not set defaults to Files.");
  36.                     Console.WriteLine("\n");
  37.                     Console.WriteLine("\tCopyToPath is the destination to copy all the assets to.  If not set then it wont copy any of the files.");
  38.                     Console.WriteLine("\n");
  39.                     Console.WriteLine("Example");
  40.                     Console.WriteLine("\n");
  41.                     Console.WriteLine("\tAssetClassGenerator.exe GameProject Assets bin\\Release\\");
  42.                     Console.WriteLine("\n");
  43.                     Console.WriteLine("Visual Studio Command Example");
  44.                     Console.WriteLine("\n");
  45.                     Console.WriteLine("\tcd \"$(ProjectDir)\"");
  46.                     Console.WriteLine("\tcall \"$(ProjectDir)AssetClassGenerator.exe\" \"$(ProjectName)\" Assets \"$(TargetDir)\\\"");
  47.                     Console.WriteLine("\n");
  48.                     return;
  49.                 }
  50.  
  51.                 classNamespace = args[0];
  52.                 Console.WriteLine("== Arg: {0}", args[0]);
  53.             }
  54.             if (args.Length > 1) {
  55.                 rootAssetFolder = args[1];
  56.                 Console.WriteLine("== Arg: {0}", args[1]);
  57.             }
  58.             if (args.Length > 2) {
  59.                 outputDir = args[2];
  60.                 Console.WriteLine("== Arg: {0}", args[2]);
  61.             }
  62.  
  63.             if (!Directory.Exists(rootAssetFolder)) {
  64.                 Console.WriteLine("*** Error: Directory {0} not found.", rootAssetFolder);
  65.                 return;
  66.             }
  67.  
  68.             ClassNamespace = classNamespace;
  69.             RootGroup = new AssetGroup(rootAssetFolder, rootAssetFolder);
  70.            
  71.             if (!Directory.Exists(rootAssetFolder)) {
  72.                 Directory.CreateDirectory(rootAssetFolder);
  73.             }
  74.  
  75.             var files = Directory.GetFiles(rootAssetFolder, "*.*", SearchOption.AllDirectories);
  76.             foreach (var file in files) {
  77.                 var directories = file.Split(Path.DirectorySeparatorChar);
  78.                 var mainGroup = directories[directories.Length - 2];
  79.                 var assetName = Path.GetFileNameWithoutExtension(file);
  80.  
  81.                 var parentGroup = RootGroup;
  82.  
  83.                 for (var i = 0; i < directories.Length - 1; i++) {
  84.                     var groupName = directories[i];
  85.  
  86.                     if (groupName != parentGroup.Name) {
  87.                         if (!parentGroup.Groups.ContainsKey(groupName)) {
  88.                             parentGroup.Groups.Add(groupName, new AssetGroup(groupName, rootAssetFolder));
  89.                         }
  90.  
  91.                         parentGroup = parentGroup.Groups[groupName];
  92.                     }
  93.                 }
  94.  
  95.                 assetName = UppercaseWords(assetName);
  96.                 assetName = assetName.Replace(" ", "");
  97.  
  98.                 parentGroup.Add(TrimRootDirectory(file, rootAssetFolder), assetName);
  99.             }
  100.  
  101.             if (outputDir != "") {
  102.                 try {
  103.                     CopyDirectory(rootAssetFolder, outputDir + rootAssetFolder + "\\");
  104.                 }
  105.                 catch(Exception e) {
  106.                     Console.WriteLine("*** Copy Error {0}", e.Message);
  107.                 }
  108.             }
  109.            
  110.  
  111.             var output = "using System;\n";
  112.             output += "using System.Text;\n";
  113.             output += "\n";
  114.             output += string.Format("namespace {0} {{\n", classNamespace);
  115.  
  116.             output += RootGroup.CreateClassString();
  117.  
  118.             output += "}";
  119.  
  120. #if DEBUG
  121.             var outputPath = "../../Assets.cs";
  122. #else
  123.             var outputPath = "Assets.cs";
  124. #endif
  125.  
  126.             File.WriteAllText(outputPath, output);
  127.         }
  128.  
  129.         static string TrimDirectories(string path, int count = 1) {
  130.             for (var i = 0; i < count; i++) {
  131.                 path = path.Replace("\\", "/");
  132.                 var firstSlashIndex = path.IndexOf('/');
  133.                 if (firstSlashIndex >= 0) {
  134.                     path = path.Substring(firstSlashIndex + 1);
  135.                 }
  136.  
  137.             }
  138.             return path;
  139.         }
  140.  
  141.         static string UppercaseWords(string value) {
  142.             char[] array = value.ToCharArray();
  143.             // Handle the first letter in the string.
  144.             if (array.Length >= 1) {
  145.                 if (char.IsLower(array[0])) {
  146.                     array[0] = char.ToUpper(array[0]);
  147.                 }
  148.             }
  149.             // Scan through the letters, checking for spaces.
  150.             // ... Uppercase the lowercase letters following spaces.
  151.             for (int i = 1; i < array.Length; i++) {
  152.                 if (array[i - 1] == ' ') {
  153.                     if (char.IsLower(array[i])) {
  154.                         array[i] = char.ToUpper(array[i]);
  155.                     }
  156.                 }
  157.             }
  158.             return new string(array);
  159.         }
  160.  
  161.         static string TrimRootDirectory(string path, string rootPath) {
  162.             if (path.IndexOf(rootPath) == 0) {
  163.                 return TrimDirectories(path);
  164.             }
  165.             return path;
  166.         }
  167.  
  168.         private static void CopyDirectory(string sourcePath, string destPath) {
  169.             if (!Directory.Exists(destPath)) {
  170.                 Directory.CreateDirectory(destPath);
  171.             }
  172.  
  173.             foreach (string file in Directory.GetFiles(sourcePath)) {
  174.                 string dest = Path.Combine(destPath, Path.GetFileName(file));
  175.                 File.Copy(file, dest, true);
  176.             }
  177.  
  178.             foreach (string folder in Directory.GetDirectories(sourcePath)) {
  179.                 string dest = Path.Combine(destPath, Path.GetFileName(folder));
  180.                 CopyDirectory(folder, dest);
  181.             }
  182.         }
  183.  
  184.         private static bool HelpRequired(string param) {
  185.             return param == "-h" || param == "--help" || param == "/?";
  186.         }
  187.     }
  188.  
  189.     struct Asset {
  190.  
  191.         public string Filepath;
  192.         public string Name;
  193.  
  194.         public Asset(string filepath, string name) {
  195.             Filepath = filepath.Replace("\\", "/");
  196.             Name = name;
  197.         }
  198.  
  199.         public override string ToString() {
  200.             return string.Format("{0} : {1}", Name, Filepath);
  201.         }
  202.     }
  203.  
  204.     class AssetGroup {
  205.         public List<Asset> Assets = new List<Asset>();
  206.         public Dictionary<string, AssetGroup> Groups = new Dictionary<string, AssetGroup>();
  207.  
  208.         public string Name;
  209.         public string RootPath;
  210.  
  211.         public AssetGroup(string name = "File", string rootPath = "Assets") {
  212.             Name = name;
  213.             RootPath = rootPath;
  214.         }
  215.  
  216.         public void Add(Asset asset) {
  217.             Assets.Add(asset);
  218.         }
  219.  
  220.         public void Add(string filepath, string name) {
  221.             Assets.Add(new Asset(filepath, name));
  222.         }
  223.  
  224.         public bool ContainsDuplicateAssetName(string name) {
  225.             // check reserved words
  226.             if (name == "Find") return true;
  227.             if (name == Program.ClassNamespace) return true;
  228.  
  229.             // check for duplicate
  230.             var count = 0;
  231.             foreach (var a in Assets) {
  232.                 if (a.Name == name) {
  233.                     count++;
  234.                     if (count == 2) {
  235.                         return true;
  236.                     }
  237.                 }
  238.             }
  239.             return false;
  240.         }
  241.  
  242.         public string CreateClassString(int depth = 1) {
  243.             var indent = "";
  244.             for (int i = 0; i < depth; i++) {
  245.                 indent += "\t";
  246.             }
  247.             var output = "";
  248.             output += string.Format(indent + "public class {0} {{\n", Name);
  249.             output += "\n";
  250.             if (depth == 1) {
  251.                 output += indent + "\tstatic string Find(string str) {\n";
  252.                 output += indent + "\t\tstring assets;\n";
  253.                 output += "#if DEBUG\n";
  254.                 output += string.Format(indent + "\t\tassets = \"../../{0}/\";\n", RootPath);
  255.                 output += "#else\n";
  256.                 output += string.Format(indent + "\t\tassets = \"{0}/\";\n", RootPath);
  257.                 output += "#endif\n";
  258.                 output += indent + "\t\treturn assets + str;\n";
  259.                 output += indent + "\t}\n\n";
  260.             }
  261.  
  262.             var formatString = "\tpublic static string {0} = Find(\"{1}\");\n";
  263.  
  264.             foreach (var a in Assets) {
  265.                 var assetName = a.Name;
  266.                 var addedExtension = false;
  267.  
  268.                 if (assetName == Name) {
  269.                     var ext = Path.GetExtension(a.Filepath).Substring(1).ToCharArray();
  270.                     ext[0] = char.ToUpper(ext[0]);
  271.                     assetName += new string(ext);
  272.  
  273.                     addedExtension = true;
  274.                 }
  275.  
  276.                 var addedNumber = false;
  277.                 var dupeCount = 0;
  278.  
  279.                 while (ContainsDuplicateAssetName(assetName)) {
  280.                     if (!addedExtension) {
  281.                         var ext = Path.GetExtension(a.Filepath).Substring(1).ToCharArray();
  282.                         ext[0] = char.ToUpper(ext[0]);
  283.                         assetName += new string(ext);
  284.                        
  285.                         addedExtension = true;
  286.                     }
  287.                     else {
  288.                         // I dunno if any of this can even happen.
  289.                         if (addedNumber) {
  290.                             assetName = assetName.Substring(0, assetName.Length - 2);
  291.                         }
  292.                         addedNumber = true;
  293.                         if (dupeCount > 99) dupeCount = 99;
  294.                         assetName += string.Format("{0:00}", dupeCount);
  295.                     }
  296.                 }
  297.  
  298.                 output += string.Format(indent + formatString, assetName, a.Filepath);
  299.             }
  300.  
  301.             foreach (var group in Groups) {
  302.                 output += indent + "\n";
  303.                 output += group.Value.CreateClassString(depth+1);
  304.             }
  305.  
  306.             output += indent + "\n";
  307.             output += indent + "}\n";
  308.  
  309.             return output.Replace("\t", "    "); // Spaces not tabs
  310.         }
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement