2016-05-07 19:03:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2016-05-07 19:31:15 +00:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2016-05-07 19:03:44 +00:00
|
|
|
|
|
|
|
|
|
namespace cscz
|
|
|
|
|
{
|
2016-06-27 20:09:45 +00:00
|
|
|
|
public enum GenerationMode
|
|
|
|
|
{
|
|
|
|
|
PrivateVariablesWithProperties,
|
|
|
|
|
PublicVariables
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-07 19:03:44 +00:00
|
|
|
|
public class ClassGenerator
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A mapping of namespaces to using shortcuts.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Dictionary<string, string> UsingShortcuts = new Dictionary<string, string>()
|
|
|
|
|
{
|
|
|
|
|
{ "c", "System.Collections.Generic" },
|
|
|
|
|
{ "cc", "System.Collections.Concurrent" },
|
2016-05-09 08:55:54 +00:00
|
|
|
|
{ "r", "System.Text.RegularExpressions" },
|
|
|
|
|
{ "n", "System.Net" },
|
2016-06-27 20:22:27 +00:00
|
|
|
|
{ "s", "System.Net.Sockets" },
|
|
|
|
|
{ "x", "Microsoft.Xna.Framework" },
|
|
|
|
|
{ "xg", "Microsoft.Xna.Framework.Graphics" },
|
|
|
|
|
{ "xs", "Microsoft.Xna.Framework.Storage" },
|
|
|
|
|
{ "xi", "Microsoft.Xna.Framework.Input" },
|
|
|
|
|
{ "oo", "OpenTK.Graphics.OpenGL" },
|
2016-05-07 19:03:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The using statements to add to the generated class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<string> UsingStatements = new List<string>()
|
|
|
|
|
{
|
2016-06-27 19:39:14 +00:00
|
|
|
|
"System"
|
2016-05-07 19:03:44 +00:00
|
|
|
|
};
|
|
|
|
|
public List<string> Signatures = new List<string>();
|
|
|
|
|
|
|
|
|
|
public string ClassName = "Carrot";
|
|
|
|
|
|
2016-06-27 20:09:45 +00:00
|
|
|
|
public GenerationMode GenerationMode = GenerationMode.PublicVariables;
|
|
|
|
|
|
2016-05-07 19:03:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether to make data members private and create public properties for them instead of making the
|
|
|
|
|
/// data members public.
|
2016-06-27 20:09:45 +00:00
|
|
|
|
/// This is read only. Please refer to GenerationMode in order to change this field.
|
2016-05-07 19:03:44 +00:00
|
|
|
|
/// </summary>
|
2016-06-27 20:09:45 +00:00
|
|
|
|
public bool CreateProperties
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (GenerationMode == GenerationMode.PrivateVariablesWithProperties)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-07 19:03:44 +00:00
|
|
|
|
|
|
|
|
|
public ClassGenerator ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-07 19:31:15 +00:00
|
|
|
|
public void ParseString(string source)
|
|
|
|
|
{
|
|
|
|
|
StringReader lines = new StringReader(source);
|
|
|
|
|
|
|
|
|
|
string nextLine = string.Empty;
|
|
|
|
|
do {
|
|
|
|
|
nextLine = lines.ReadLine();
|
|
|
|
|
|
|
|
|
|
if(nextLine == null) break;
|
2016-05-08 10:39:56 +00:00
|
|
|
|
if(nextLine.Length == 0) continue;
|
2016-05-07 19:31:15 +00:00
|
|
|
|
|
|
|
|
|
// It's a using statement
|
|
|
|
|
if(nextLine.StartsWith("u "))
|
|
|
|
|
{
|
|
|
|
|
string[] parts = Regex.Split(nextLine.Trim(), @"\s+");
|
|
|
|
|
UsingStatements.Add(parts[1]);
|
|
|
|
|
}
|
2016-05-08 10:39:56 +00:00
|
|
|
|
else if(nextLine.StartsWith("#"))
|
|
|
|
|
{
|
|
|
|
|
// It's the class name
|
|
|
|
|
ClassName = UppercaseFirstLetter(nextLine.TrimStart(new char[] { '#' })).Trim();
|
|
|
|
|
}
|
2016-05-07 19:31:15 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Signatures.Add(nextLine.Trim());
|
|
|
|
|
}
|
|
|
|
|
} while(nextLine != null);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-07 19:03:44 +00:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
StringBuilder result = new StringBuilder();
|
2016-07-25 18:22:22 +00:00
|
|
|
|
|
|
|
|
|
// Using statements
|
2016-05-07 19:03:44 +00:00
|
|
|
|
foreach(string usingStatement in UsingStatements)
|
|
|
|
|
result.AppendLine(string.Format("using {0};", expandUsingShortcut(usingStatement)));
|
2016-07-25 18:22:22 +00:00
|
|
|
|
|
2016-05-07 19:03:44 +00:00
|
|
|
|
result.AppendLine();
|
2016-07-25 18:22:22 +00:00
|
|
|
|
|
|
|
|
|
// Class name
|
2016-05-07 19:03:44 +00:00
|
|
|
|
result.AppendLine(string.Format("class {0} ", ClassName));
|
|
|
|
|
result.AppendLine("{");
|
|
|
|
|
|
2016-07-25 18:22:22 +00:00
|
|
|
|
// Class body parts declaration
|
2016-05-07 19:03:44 +00:00
|
|
|
|
StringBuilder properties = new StringBuilder();
|
|
|
|
|
StringBuilder constructorSignature = new StringBuilder(string.Format("\tpublic {0}(", ClassName));
|
|
|
|
|
StringBuilder constructorBody = new StringBuilder();
|
|
|
|
|
|
2016-07-25 18:22:22 +00:00
|
|
|
|
// Data member signature handling
|
2016-05-07 19:03:44 +00:00
|
|
|
|
foreach (string signature in Signatures)
|
|
|
|
|
{
|
2016-07-25 18:22:22 +00:00
|
|
|
|
string[] signatureParts = Regex.Split(signature, @"\s+");
|
|
|
|
|
string datatypeName = signatureParts[0];
|
|
|
|
|
string privateDataMemberName = LowercaseFirstLetter(signatureParts[1]);
|
|
|
|
|
string publicDataMemberName = UppercaseFirstLetter(signatureParts[1]);
|
2016-05-07 19:03:44 +00:00
|
|
|
|
|
|
|
|
|
constructorSignature.AppendFormat("{0} in{1}, ", datatypeName, publicDataMemberName);
|
|
|
|
|
|
|
|
|
|
if (CreateProperties)
|
|
|
|
|
{
|
2016-07-25 18:22:22 +00:00
|
|
|
|
// Private data member
|
2016-05-07 19:03:44 +00:00
|
|
|
|
result.AppendLine(string.Format("\tprivate {0} {1};", datatypeName, privateDataMemberName));
|
|
|
|
|
|
2016-07-25 18:22:22 +00:00
|
|
|
|
// Public property associated with above private data member
|
2016-05-07 19:03:44 +00:00
|
|
|
|
properties.AppendLine(string.Format("\tpublic {0} {1}", datatypeName, publicDataMemberName));
|
|
|
|
|
properties.AppendLine(string.Format("\t{{"));
|
|
|
|
|
properties.AppendLine(string.Format("\t\tget {{ return {0}; }}", privateDataMemberName));
|
|
|
|
|
properties.AppendLine(string.Format("\t\tset {{ {0} = value; }}", privateDataMemberName));
|
|
|
|
|
properties.AppendLine(string.Format("\t}}"));
|
|
|
|
|
|
2016-07-25 18:22:22 +00:00
|
|
|
|
// Constructor body
|
2016-05-07 19:03:44 +00:00
|
|
|
|
constructorBody.AppendLine(string.Format("\t\t{0} = in{0};", publicDataMemberName));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-07-25 18:22:22 +00:00
|
|
|
|
// Constuctor body
|
2016-06-27 20:09:45 +00:00
|
|
|
|
result.AppendLine(string.Format("\tpublic {0} {1};", datatypeName, publicDataMemberName));
|
2016-07-25 18:22:22 +00:00
|
|
|
|
constructorBody.AppendLine(string.Format("\t\t{0} = in{0};", publicDataMemberName));
|
2016-05-07 19:03:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the properties to the output
|
|
|
|
|
result.AppendLine(properties.ToString());
|
|
|
|
|
|
|
|
|
|
// Add the constructor to the output
|
|
|
|
|
constructorSignature.Remove(constructorSignature.Length - 2, 2); // Remove the last ", "
|
|
|
|
|
constructorSignature.Append(")");
|
|
|
|
|
result.AppendLine(constructorSignature.ToString());
|
|
|
|
|
result.AppendLine("\t{");
|
|
|
|
|
result.Append(constructorBody.ToString());
|
|
|
|
|
result.AppendLine("\t}");
|
|
|
|
|
|
|
|
|
|
// Close the class off and end the file
|
|
|
|
|
result.AppendLine("}");
|
|
|
|
|
|
|
|
|
|
return result.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string expandUsingShortcut(string shortcut)
|
|
|
|
|
{
|
|
|
|
|
if (UsingShortcuts.ContainsKey(shortcut))
|
|
|
|
|
return UsingShortcuts[shortcut];
|
|
|
|
|
else
|
|
|
|
|
return shortcut;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string UppercaseFirstLetter(string str)
|
|
|
|
|
{
|
|
|
|
|
if (str == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (str.Length > 1)
|
|
|
|
|
return char.ToUpper(str[0]) + str.Substring(1);
|
|
|
|
|
return str.ToUpper();
|
|
|
|
|
}
|
|
|
|
|
private string LowercaseFirstLetter(string str)
|
|
|
|
|
{
|
|
|
|
|
if (str == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (str.Length > 1)
|
|
|
|
|
return char.ToLower(str[0]) + str.Substring(1);
|
|
|
|
|
return str.ToLower();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|