Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace AssetClassGenerator {
- class Program {
- public static AssetGroup RootGroup;
- public static string ClassNamespace;
- static void Main(string[] args) {
- var rootAssetFolder = "Assets";
- var classNamespace = "Files";
- var outputDir = "";
- var version = System.Reflection.Assembly.GetExecutingAssembly()
- .GetName()
- .Version
- .ToString();
- Console.WriteLine("= Asset Class Generator v{0} =", version);
- if (args.Length > 0) {
- if (HelpRequired(args[0])) {
- Console.WriteLine("Help");
- Console.WriteLine("\n");
- Console.WriteLine("\tArgs: RootAssetFolder ClassNamespace CopyToPath");
- Console.WriteLine("\n");
- Console.WriteLine("\tRootAssetFolder is the folder containing all assets. If not set defaults to Assets.");
- Console.WriteLine("\n");
- Console.WriteLine("\tClassNamespace is the namespace that the class will be in. Usually the same as your project. If not set defaults to Files.");
- Console.WriteLine("\n");
- Console.WriteLine("\tCopyToPath is the destination to copy all the assets to. If not set then it wont copy any of the files.");
- Console.WriteLine("\n");
- Console.WriteLine("Example");
- Console.WriteLine("\n");
- Console.WriteLine("\tAssetClassGenerator.exe GameProject Assets bin\\Release\\");
- Console.WriteLine("\n");
- Console.WriteLine("Visual Studio Command Example");
- Console.WriteLine("\n");
- Console.WriteLine("\tcd \"$(ProjectDir)\"");
- Console.WriteLine("\tcall \"$(ProjectDir)AssetClassGenerator.exe\" \"$(ProjectName)\" Assets \"$(TargetDir)\\\"");
- Console.WriteLine("\n");
- return;
- }
- classNamespace = args[0];
- Console.WriteLine("== Arg: {0}", args[0]);
- }
- if (args.Length > 1) {
- rootAssetFolder = args[1];
- Console.WriteLine("== Arg: {0}", args[1]);
- }
- if (args.Length > 2) {
- outputDir = args[2];
- Console.WriteLine("== Arg: {0}", args[2]);
- }
- if (!Directory.Exists(rootAssetFolder)) {
- Console.WriteLine("*** Error: Directory {0} not found.", rootAssetFolder);
- return;
- }
- ClassNamespace = classNamespace;
- RootGroup = new AssetGroup(rootAssetFolder, rootAssetFolder);
- if (!Directory.Exists(rootAssetFolder)) {
- Directory.CreateDirectory(rootAssetFolder);
- }
- var files = Directory.GetFiles(rootAssetFolder, "*.*", SearchOption.AllDirectories);
- foreach (var file in files) {
- var directories = file.Split(Path.DirectorySeparatorChar);
- var mainGroup = directories[directories.Length - 2];
- var assetName = Path.GetFileNameWithoutExtension(file);
- var parentGroup = RootGroup;
- for (var i = 0; i < directories.Length - 1; i++) {
- var groupName = directories[i];
- if (groupName != parentGroup.Name) {
- if (!parentGroup.Groups.ContainsKey(groupName)) {
- parentGroup.Groups.Add(groupName, new AssetGroup(groupName, rootAssetFolder));
- }
- parentGroup = parentGroup.Groups[groupName];
- }
- }
- assetName = UppercaseWords(assetName);
- assetName = assetName.Replace(" ", "");
- parentGroup.Add(TrimRootDirectory(file, rootAssetFolder), assetName);
- }
- if (outputDir != "") {
- try {
- CopyDirectory(rootAssetFolder, outputDir + rootAssetFolder + "\\");
- }
- catch(Exception e) {
- Console.WriteLine("*** Copy Error {0}", e.Message);
- }
- }
- var output = "using System;\n";
- output += "using System.Text;\n";
- output += "\n";
- output += string.Format("namespace {0} {{\n", classNamespace);
- output += RootGroup.CreateClassString();
- output += "}";
- #if DEBUG
- var outputPath = "../../Assets.cs";
- #else
- var outputPath = "Assets.cs";
- #endif
- File.WriteAllText(outputPath, output);
- }
- static string TrimDirectories(string path, int count = 1) {
- for (var i = 0; i < count; i++) {
- path = path.Replace("\\", "/");
- var firstSlashIndex = path.IndexOf('/');
- if (firstSlashIndex >= 0) {
- path = path.Substring(firstSlashIndex + 1);
- }
- }
- return path;
- }
- static string UppercaseWords(string value) {
- char[] array = value.ToCharArray();
- // Handle the first letter in the string.
- if (array.Length >= 1) {
- if (char.IsLower(array[0])) {
- array[0] = char.ToUpper(array[0]);
- }
- }
- // Scan through the letters, checking for spaces.
- // ... Uppercase the lowercase letters following spaces.
- for (int i = 1; i < array.Length; i++) {
- if (array[i - 1] == ' ') {
- if (char.IsLower(array[i])) {
- array[i] = char.ToUpper(array[i]);
- }
- }
- }
- return new string(array);
- }
- static string TrimRootDirectory(string path, string rootPath) {
- if (path.IndexOf(rootPath) == 0) {
- return TrimDirectories(path);
- }
- return path;
- }
- private static void CopyDirectory(string sourcePath, string destPath) {
- if (!Directory.Exists(destPath)) {
- Directory.CreateDirectory(destPath);
- }
- foreach (string file in Directory.GetFiles(sourcePath)) {
- string dest = Path.Combine(destPath, Path.GetFileName(file));
- File.Copy(file, dest, true);
- }
- foreach (string folder in Directory.GetDirectories(sourcePath)) {
- string dest = Path.Combine(destPath, Path.GetFileName(folder));
- CopyDirectory(folder, dest);
- }
- }
- private static bool HelpRequired(string param) {
- return param == "-h" || param == "--help" || param == "/?";
- }
- }
- struct Asset {
- public string Filepath;
- public string Name;
- public Asset(string filepath, string name) {
- Filepath = filepath.Replace("\\", "/");
- Name = name;
- }
- public override string ToString() {
- return string.Format("{0} : {1}", Name, Filepath);
- }
- }
- class AssetGroup {
- public List<Asset> Assets = new List<Asset>();
- public Dictionary<string, AssetGroup> Groups = new Dictionary<string, AssetGroup>();
- public string Name;
- public string RootPath;
- public AssetGroup(string name = "File", string rootPath = "Assets") {
- Name = name;
- RootPath = rootPath;
- }
- public void Add(Asset asset) {
- Assets.Add(asset);
- }
- public void Add(string filepath, string name) {
- Assets.Add(new Asset(filepath, name));
- }
- public bool ContainsDuplicateAssetName(string name) {
- // check reserved words
- if (name == "Find") return true;
- if (name == Program.ClassNamespace) return true;
- // check for duplicate
- var count = 0;
- foreach (var a in Assets) {
- if (a.Name == name) {
- count++;
- if (count == 2) {
- return true;
- }
- }
- }
- return false;
- }
- public string CreateClassString(int depth = 1) {
- var indent = "";
- for (int i = 0; i < depth; i++) {
- indent += "\t";
- }
- var output = "";
- output += string.Format(indent + "public class {0} {{\n", Name);
- output += "\n";
- if (depth == 1) {
- output += indent + "\tstatic string Find(string str) {\n";
- output += indent + "\t\tstring assets;\n";
- output += "#if DEBUG\n";
- output += string.Format(indent + "\t\tassets = \"../../{0}/\";\n", RootPath);
- output += "#else\n";
- output += string.Format(indent + "\t\tassets = \"{0}/\";\n", RootPath);
- output += "#endif\n";
- output += indent + "\t\treturn assets + str;\n";
- output += indent + "\t}\n\n";
- }
- var formatString = "\tpublic static string {0} = Find(\"{1}\");\n";
- foreach (var a in Assets) {
- var assetName = a.Name;
- var addedExtension = false;
- if (assetName == Name) {
- var ext = Path.GetExtension(a.Filepath).Substring(1).ToCharArray();
- ext[0] = char.ToUpper(ext[0]);
- assetName += new string(ext);
- addedExtension = true;
- }
- var addedNumber = false;
- var dupeCount = 0;
- while (ContainsDuplicateAssetName(assetName)) {
- if (!addedExtension) {
- var ext = Path.GetExtension(a.Filepath).Substring(1).ToCharArray();
- ext[0] = char.ToUpper(ext[0]);
- assetName += new string(ext);
- addedExtension = true;
- }
- else {
- // I dunno if any of this can even happen.
- if (addedNumber) {
- assetName = assetName.Substring(0, assetName.Length - 2);
- }
- addedNumber = true;
- if (dupeCount > 99) dupeCount = 99;
- assetName += string.Format("{0:00}", dupeCount);
- }
- }
- output += string.Format(indent + formatString, assetName, a.Filepath);
- }
- foreach (var group in Groups) {
- output += indent + "\n";
- output += group.Value.CreateClassString(depth+1);
- }
- output += indent + "\n";
- output += indent + "}\n";
- return output.Replace("\t", " "); // Spaces not tabs
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement