public static bool DoesSheetContainImages(CastleDBParser.RootNode rootNode, CastleDBParser.SheetNode sheetNode)
        {
            Debug.Log(sheetNode.Name);

            for (int i = 0; i < sheetNode.Columns.Count; i++)
            {
                CastleDBParser.ColumnNode column = sheetNode.Columns[i];
                string typeNum = GetTypeNumFromCastleDBTypeString(column.TypeStr);

                if (typeNum == "7")
                {
                    return(true);
                }
                else if (typeNum == "6")
                {
                    string refType  = GetTypeFromCastleDBColumn(column);
                    var    refSheet = rootNode.GetSheetWithName(refType);
                    Debug.Log(refType);
                    return(DoesSheetContainImages(rootNode, refSheet));
                }
                else if (typeNum == "8")
                {
                    var refSheet = rootNode.GetSheetWithName(column.Name);
                    Debug.Log(column.Name);
                    return(DoesSheetContainImages(rootNode, refSheet));
                }
            }

            return(false);
        }
        public static string GetTypeFromCastleDBColumn(CastleDBParser.ColumnNode column)
        {
            string typeString = GetTypeNumFromCastleDBTypeString(column.TypeStr);

            switch (typeString)
            {
            case "1":
                return("string");

            case "2":
                return("bool");

            case "3":
                return("int");

            case "4":
                return("float");

            case "5":     //enum
                return("Enum");

            case "10":     //enum flag
                return("Enum");

            case "6":     //ref type
                return(GetRefTypeFromTypeString(column.TypeStr));

            case "7":     //image type
                return("Texture");

            case "8":     //nested list type
                return(column.Name);

            case "11":     //color
                //TODO: fix color encoding  https://docs.unity3d.com/ScriptReference/ColorUtility.TryParseHtmlString.html
                return("string");

            // return typeof(Color);
            default:
                return("string");
            }
        }
Ejemplo n.º 3
0
        public static string GetTypeFromCastleDBColumn(CastleDBParser.ColumnNode column)
        {
            string typeString = GetTypeNumFromCastleDBTypeString(column.TypeStr);

            switch (typeString)
            {
            case "1":
                return("string");

            case "2":
                return("bool");

            case "3":
                return("int");

            case "4":
                return("float");

            case "5":     //enum
                return("Enum");

            case "10":     //enum flag
                return("Enum");

            case "6":     //ref type
                return(GetRefTypeFromTypeString(column.TypeStr));

            case "7":     // Image
                return("Texture");

            case "8":     //nested list type
                return(column.Name);

            case "11":     //color
                return("Color");

            default:
                return("string");
            }
        }
Ejemplo n.º 4
0
        public static void GenerateTypes(CastleDBParser.RootNode root, CastleDBConfig configFile, string dbname)
        {
            // Create scripts
            List <string>  scripts     = new List <string>();
            CastleDBConfig config      = configFile;
            String         FieldIndent = "        ";

            InitTypePath(config);
            InitPath(configFile.GeneratedTypesLocation + $"/{dbname}/");
            foreach (CastleDBParser.SheetNode sheet in root.Sheets)
            {
                string scriptPath = $"Assets/{config.GeneratedTypesLocation}/{dbname}/{sheet.Name}.cs";
                scripts.Add(scriptPath);

                //generate fields
                string fieldText = "";
                for (int i = 0; i < sheet.Columns.Count; i++)
                {
                    fieldText += FieldIndent;
                    CastleDBParser.ColumnNode column = sheet.Columns[i];
                    string fieldType = CastleDBUtils.GetTypeFromCastleDBColumn(column);
                    if (fieldType != "Enum") //non-enum, normal field
                    {
                        if (CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr) == "8")
                        {
                            fieldText += ($"public List<{fieldType}> {column.Name}List = new List<{fieldType}>();");
                        }
                        else
                        {
                            fieldText += ($"public {fieldType} {column.Name};");
                        }
                    }
                    else //enum type
                    {
                        string[] enumValueNames = CastleDBUtils.GetEnumValuesFromTypeString(column.TypeStr);
                        string   enumEntries    = "";
                        if (CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr) == "10") //flag
                        {
                            fieldText += ($"public {column.Name}Flag {column.Name};");
                            for (int val = 0; val < enumValueNames.Length; val++)
                            {
                                enumEntries += (enumValueNames[val] + " = " + (int)Math.Pow(2, val));
                                if (val + 1 < enumValueNames.Length)
                                {
                                    enumEntries += ",";
                                }
                                ;
                            }
                            fieldText += ($"[FlagsAttribute] public enum {column.Name}Flag {{ {enumEntries} }}");
                        }
                        else
                        {
                            fieldText += ($"public {column.Name}Enum {column.Name};");
                            for (int val = 0; val < enumValueNames.Length; val++)
                            {
                                enumEntries += (enumValueNames[val] + " = " + val);
                                if (val + 1 < enumValueNames.Length)
                                {
                                    enumEntries += ",";
                                }
                                ;
                            }
                            fieldText += ($"public enum {column.Name}Enum {{  {enumEntries} }}");
                        }
                    }

                    fieldText += "\r\n";
                }

                #region ("Row Values Field")
                string possibleValuesText = "";
                if (!sheet.NestedType)
                {
                    possibleValuesText += $"public enum RowValues {{";

                    for (int i = 0; i < sheet.Rows.Count; i++)
                    {
                        string rowName = sheet.Rows[i][config.GUIDColumnName];
                        possibleValuesText += rowName;
                        if (i + 1 < sheet.Rows.Count)
                        {
                            possibleValuesText += " , ";
                        }
                    }
                    possibleValuesText += " }\r\n";
                }
                #endregion

                //generate the constructor that sets the fields based on the passed in value
                string constructorText   = "";
                string constructorIndent = "            ";
                if (!sheet.NestedType)
                {
                    constructorText += $"{constructorIndent}SimpleJSON.JSONNode node = root.GetSheetWithName(\"{sheet.Name}\").Rows[(int)line];\r\n";
                }
                for (int i = 0; i < sheet.Columns.Count; i++)
                {
                    constructorText += constructorIndent;
                    CastleDBParser.ColumnNode column = sheet.Columns[i];
                    string castText = CastleDBUtils.GetCastStringFromCastleDBTypeStr(column.TypeStr);
                    string enumCast = "";
                    string typeNum  = CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr);
                    if (typeNum == "8")
                    {
                        //list type
                        constructorText += $"foreach(var item in node[\"{column.Name}\"]) {{ {column.Name}List.Add(new {column.Name}(root, item));}}";
                    }
                    else if (typeNum == "6")
                    {
                        //working area:
                        //ref type
                        string refType = CastleDBUtils.GetTypeFromCastleDBColumn(column);
                        //look up the line based on the passed in row
                        constructorText += $"{column.Name} = new {refType}(root,{refType}.GetRowValue(node[\"{column.Name}\"]));";
                    }
                    else if (typeNum == "7") // Image
                    {
                        constructorText += $"{column.Name} = Resources.Load<Texture>(node[\"{column.Name}\"]) as Texture;";
                    }
                    else if (typeNum == "11") // Color
                    {
                        constructorText += $"{column.Name} = CastleDBUtils.GetColorFromString( node[\"{column.Name}\"]);";
                    }
                    else
                    {
                        if (typeNum == "10")
                        {
                            enumCast = $"({column.Name}Flag)";
                        }
                        else if (typeNum == "5")
                        {
                            enumCast = $"({column.Name}Enum)";
                        }
                        constructorText += $"{column.Name} = {enumCast}node[\"{column.Name}\"]{castText};";
                    }

                    constructorText += "\r\n";
                }



                string getMethodText = "";
                if (!sheet.NestedType)
                {
                    getMethodText += $@"
        public static {sheet.Name}.RowValues GetRowValue(string name)
        {{
            var values = (RowValues[])Enum.GetValues(typeof(RowValues));
            for (int i = 0; i < values.Length; i++)
            {{
                if(values[i].ToString() == name)
                {{
                    return values[i];
                }}
            }}
            return values[0];
        }}";
                }

                string ctor = "";
                if (!sheet.NestedType)
                {
                    ctor = $"public {sheet.Name} (CastleDBParser.RootNode root, RowValues line)";
                }
                else
                {
                    ctor = $"public {sheet.Name} (CastleDBParser.RootNode root, SimpleJSON.JSONNode node)";
                }
                // string usings = "using UnityEngine;\r\n using System;\r\n using System.Collections.Generic;\r\n using SimpleJSON;\r\n using CastleDBImporter;\r\n";
                string fullClassText = $@"
using UnityEngine;
using System;
using System.Collections.Generic;
using SimpleJSON;
using CastleDBImporter;
using {config.GeneratedTypesNamespace}.{dbname};

namespace {config.GeneratedTypesNamespace}.{dbname}
{{ 
    public class {sheet.Name}
    {{
{fieldText}
        {possibleValuesText} 
        {ctor} 
        {{
{constructorText}
        }}  
        {getMethodText}
    }}
}}";
                if (!config.SuppressBuildInfo)
                {
                    Debug.Log("Generating CDB Class: " + sheet.Name);
                }

                File.WriteAllText(scriptPath, fullClassText);
            }

            //build the CastleDB file
            string cdbscriptPath = $"Assets/{config.GeneratedTypesLocation}/{dbname}/{dbname}.cs";
            scripts.Add(cdbscriptPath);
            //fields
            string cdbfields          = "";
            string cdbconstructorBody = "";
            string classTexts         = "";
            foreach (CastleDBParser.SheetNode sheet in root.Sheets)
            {
                if (sheet.NestedType)
                {
                    continue;
                }                               //only write main types to CastleDB
                cdbfields          += $"        public {sheet.Name}Type {sheet.Name};\r\n";
                cdbconstructorBody += $"            {sheet.Name} = new {sheet.Name}Type();\r\n";

                //get a list of all the row names
                classTexts += $"        public class {sheet.Name}Type \r\n        {{\r\n";
                for (int i = 0; i < sheet.Rows.Count; i++)
                {
                    string rowName = sheet.Rows[i][config.GUIDColumnName];
                    classTexts += $"           public {sheet.Name} {rowName} {{ get {{ return Get({configFile.GeneratedTypesNamespace}.{dbname}.{sheet.Name}.RowValues.{rowName}); }} }} \r\n";
                }

                classTexts += $"           private {sheet.Name} Get({sheet.Name}.RowValues line) {{ return new {sheet.Name}(parsedDB.Root, line); }}\r\n";
                classTexts += $@"
           public {sheet.Name}[] GetAll() 
           {{
               var values = ({sheet.Name}.RowValues[])Enum.GetValues(typeof({sheet.Name}.RowValues));
               {sheet.Name}[] returnList = new {sheet.Name}[values.Length];
               for (int i = 0; i < values.Length; i++)
               {{
                   returnList[i] = Get(values[i]);
               }}
               return returnList;
           }}";
                classTexts += $"\r\n        }} //END OF {sheet.Name} \r\n\r\n";
            }

            string fullCastle = $@"
using UnityEngine;
using CastleDBImporter;
using System.Collections.Generic;
using System;
using {config.GeneratedTypesNamespace}.{dbname};

namespace {config.GeneratedTypesNamespace}.{dbname}
{{
    public class {dbname}
    {{
        static CastleDBParser parsedDB;
{cdbfields}
        
{classTexts}

        public {dbname}(TextAsset castleDBAsset)
        {{
            parsedDB = new CastleDBParser(castleDBAsset);
{cdbconstructorBody}
        }}
    }}
}}";
            if (!config.SuppressBuildInfo)
            {
                Debug.Log("Generating CastleDB class");

                if (CastleDBConfig.NamesHaveSpaces)
                {
                    Debug.Log("Your CastleDB file contains spaces in names! These spaces will be automatically converted to '_'.");
                }

                Debug.Log("To suppress log messages from import, check the CastleDBConfig flag 'Suppress Import Messages'");
            }

            File.WriteAllText(cdbscriptPath, fullCastle);
            AssetDatabase.Refresh();
        }
        public static void GenerateTypes(CastleDBParser.RootNode root, CastleDBConfig configFile)
        {
            // Create scripts
            List <string>  scripts = new List <string>();
            CastleDBConfig config  = configFile;

            InitTypePath(config);

            foreach (CastleDBParser.SheetNode sheet in root.Sheets)
            {
                Debug.Log("Doinog: " + sheet.Name);
                string scriptPath = $"Assets/{config.GeneratedTypesLocation}/{sheet.Name}.cs";
                scripts.Add(scriptPath);

                //generate fields
                string fieldText = "";
                for (int i = 0; i < sheet.Columns.Count; i++)
                {
                    CastleDBParser.ColumnNode column = sheet.Columns[i];
                    string fieldType = CastleDBUtils.GetTypeFromCastleDBColumn(column);
                    if (fieldType != "Enum") //non-enum, normal field
                    {
                        if (CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr) == "8")
                        {
                            fieldText += ($"public List<{fieldType}> {column.Name}List = new List<{fieldType}>();\n\t\t");
                        }
                        else
                        {
                            fieldText += ($"public {fieldType} {column.Name};\n\t\t");
                        }
                    }
                    else //enum type
                    {
                        string[] enumValueNames = CastleDBUtils.GetEnumValuesFromTypeString(column.TypeStr);
                        string   enumEntries    = "";
                        if (CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr) == "10") //flag
                        {
                            fieldText += ($"public {column.Name}Flag {column.Name};\n");
                            for (int val = 0; val < enumValueNames.Length; val++)
                            {
                                enumEntries += (enumValueNames[val] + " = " + (int)Math.Pow(2, val));
                                if (val + 1 < enumValueNames.Length)
                                {
                                    enumEntries += ",";
                                }
                                ;
                            }
                            fieldText += ($"[FlagsAttribute] public enum {column.Name}Flag {{ {enumEntries} }}");
                        }
                        else
                        {
                            fieldText += ($"public {column.Name}Enum {column.Name};\n");
                            for (int val = 0; val < enumValueNames.Length; val++)
                            {
                                enumEntries += (enumValueNames[val] + " = " + val);
                                if (val + 1 < enumValueNames.Length)
                                {
                                    enumEntries += ",";
                                }
                                ;
                            }
                            fieldText += ($"public enum {column.Name}Enum {{  {enumEntries} }}");
                        }
                    }
                }

                //generate the constructor that sets the fields based on the passed in value
                StringBuilder constructorText = new StringBuilder();


                for (int i = 0; i < sheet.Columns.Count; i++)
                {
                    CastleDBParser.ColumnNode column = sheet.Columns[i];
                    string castText = CastleDBUtils.GetCastStringFromCastleDBTypeStr(column.TypeStr);
                    string enumCast = "";
                    string typeNum  = CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr);
                    if (typeNum == "8")
                    {
                        //list type
                        if (CastleDBUtils.DoesSheetContainImages(root, root.GetSheetWithName(column.Name)))
                        {
                            constructorText.Append($"\t\t\tforeach(var item in node[\"{column.Name}\"]) {{ {column.Name}List.Add(new {column.Name}(root, item, DatabaseImages));}}\n");
                        }
                        else
                        {
                            constructorText.Append($"\t\t\tforeach(var item in node[\"{column.Name}\"]) {{ {column.Name}List.Add(new {column.Name}(root, item));}}\n");
                        }
                    }
                    else if (typeNum == "6")
                    {
                        //working area:
                        //ref type
                        string refType = CastleDBUtils.GetTypeFromCastleDBColumn(column);
                        //look up the line based on the passed in row
                        if (CastleDBUtils.DoesSheetContainImages(root, sheet))
                        {
                            constructorText.Append(
                                $"{column.Name} = new {config.GeneratedTypesNamespace}.{refType}(root, root.GetSheetWithName(\"{refType}\")." +
                                $"Rows.Find( pred => pred[\"{config.GUIDColumnName}\"] == node[\"{column.Name}\"]), DatabaseImages);\n"
                                );
                        }
                        else
                        {
                            constructorText.Append(
                                $"{column.Name} = new {config.GeneratedTypesNamespace}.{refType}(root, root.GetSheetWithName(\"{refType}\")." +
                                $"Rows.Find( pred => pred[\"{config.GUIDColumnName}\"] == node[\"{column.Name}\"]));\n"
                                );
                        }
                    }
                    else if (typeNum == "7") // Image type
                    {
                        constructorText.Append($"\t\t\t{column.Name} = DatabaseImages[node[\"{column.Name}\"]];\n");
                    }
                    else
                    {
                        if (typeNum == "10")
                        {
                            enumCast = $"({column.Name}Flag)";
                        }
                        else if (typeNum == "5")
                        {
                            enumCast = $"({column.Name}Enum)";
                        }
                        constructorText.Append($"\t\t\t{column.Name} = {enumCast}node[\"{column.Name}\"]{castText};\n");
                    }
                }

                string ctor = "";
                if (CastleDBUtils.DoesSheetContainImages(root, sheet))
                {
                    ctor = $"public {sheet.Name} (CastleDBParser.RootNode root, SimpleJSON.JSONNode node, Dictionary<string, Texture> DatabaseImages)";
                }
                else
                {
                    ctor = $"public {sheet.Name} (CastleDBParser.RootNode root, SimpleJSON.JSONNode node)";
                }
                string fullClassText = $@"
using UnityEngine;
using System;
using System.Collections.Generic;
using SimpleJSON;
using CastleDBImporter;
namespace {config.GeneratedTypesNamespace}
{{ 
    public class {sheet.Name}
    {{
        {fieldText}
        {ctor} 
        {{
            {constructorText}
        }} 
    }}
}}";
                Debug.Log("Generating CDB Class: " + sheet.Name);
                File.WriteAllText(scriptPath, fullClassText);
            }

            //build the CastleDB file
            string cdbscriptPath = $"Assets/{config.GeneratedTypesLocation}/CastleDB.cs";

            scripts.Add(cdbscriptPath);
            //fields
            string cdbfields          = "";
            string cdbconstructorBody = "";

            foreach (CastleDBParser.SheetNode sheet in root.Sheets)
            {
                if (sheet.NestedType)
                {
                    continue;
                }                                //only write main types to CastleDB
                cdbfields          += $"public Dictionary<string, {sheet.Name}> {sheet.Name};\n";
                cdbconstructorBody += $"{sheet.Name} = new Dictionary<string, {sheet.Name}>();\n";

                //get a list of all the row names
                cdbconstructorBody += $"\t\t\tforeach( var row in root.GetSheetWithName(\"{sheet.Name}\").Rows ) {{\n";

                if (CastleDBUtils.DoesSheetContainImages(root, sheet))
                {
                    cdbconstructorBody += $"\t\t\t\t{sheet.Name}[row[\"id\"]] = new {sheet.Name}(root, row, parsedDB.DatabaseImages);\n\t\t\t}}";
                }
                else
                {
                    cdbconstructorBody += $"\t\t\t\t{sheet.Name}[row[\"id\"]] = new {sheet.Name}(root, row);\n\t\t\t}}";
                }
            }

            string fullCastle = $@"
using UnityEngine;
using CastleDBImporter;
using System.Collections.Generic;
using System;

namespace {config.GeneratedTypesNamespace}
{{
    public class CastleDB
    {{
        static CastleDBParser parsedDB;
        {cdbfields}
        public CastleDB(TextAsset castleDBAsset, TextAsset castleDBImagesAsset = null)
        {{
            parsedDB = new CastleDBParser(castleDBAsset, castleDBImagesAsset);
            CastleDBParser.RootNode root = parsedDB.Root;
            { cdbconstructorBody }
        }}
    }}
}}";

            Debug.Log("Generating CastleDB class");
            File.WriteAllText(cdbscriptPath, fullCastle);
            AssetDatabase.Refresh();
        }
Ejemplo n.º 6
0
        public static void GenerateTypes(CastleDBParser.RootNode root)
        {
            // Create scripts
            List <string> scripts = new List <string>();

            InitTypePath();

            foreach (CastleDBParser.SheetNode sheet in root.Sheets)
            {
                string scriptPath = $"Assets/{CastleDBParser.Config.GeneratedTypesLocation}/{sheet.Name}.cs";
                scripts.Add(scriptPath);

                //generate fields
                string fieldText = "";
                for (int i = 0; i < sheet.Columns.Count; i++)
                {
                    CastleDBParser.ColumnNode column = sheet.Columns[i];
                    string fieldType = CastleDBUtils.GetTypeFromCastleDBColumn(column);
                    if (fieldType != "Enum") //non-enum, normal field
                    {
                        if (CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr) == "8")
                        {
                            fieldText += ($"public List<{fieldType}> {column.Name}List = new List<{fieldType}>();\n");
                        }
                        else
                        {
                            fieldText += ($"public {fieldType} {column.Name};\n");
                        }
                    }
                    else //enum type
                    {
                        string[] enumValueNames = CastleDBUtils.GetEnumValuesFromTypeString(column.TypeStr);
                        string   enumEntries    = "";
                        if (CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr) == "10") //flag
                        {
                            fieldText += ($"public {column.Name}Flag {column.Name};\n");
                            for (int val = 0; val < enumValueNames.Length; val++)
                            {
                                enumEntries += (enumValueNames[val] + " = " + (int)Math.Pow(2, val));
                                if (val + 1 < enumValueNames.Length)
                                {
                                    enumEntries += ",";
                                }
                                ;
                            }
                            fieldText += ($"[FlagsAttribute] public enum {column.Name}Flag {{ {enumEntries} }}");
                        }
                        else
                        {
                            fieldText += ($"public {column.Name}Enum {column.Name};\n");
                            for (int val = 0; val < enumValueNames.Length; val++)
                            {
                                enumEntries += (enumValueNames[val] + " = " + val);
                                if (val + 1 < enumValueNames.Length)
                                {
                                    enumEntries += ",";
                                }
                                ;
                            }
                            fieldText += ($"public enum {column.Name}Enum {{  {enumEntries} }}");
                        }
                    }
                }

                //generate the constructor that sets the fields based on the passed in value
                string constructorText = "";
                if (!sheet.NestedType)
                {
                    constructorText += $"SimpleJSON.JSONNode node = root.GetSheetWithName(\"{sheet.Name}\").Rows[(int)line];\n";
                }
                for (int i = 0; i < sheet.Columns.Count; i++)
                {
                    CastleDBParser.ColumnNode column = sheet.Columns[i];
                    string castText = CastleDBUtils.GetCastStringFromCastleDBTypeStr(column.TypeStr);
                    string enumCast = "";
                    string typeNum  = CastleDBUtils.GetTypeNumFromCastleDBTypeString(column.TypeStr);
                    if (typeNum == "8")
                    {
                        //list type
                        constructorText += $"foreach(var item in node[\"{column.Name}\"]) {{ {column.Name}List.Add(new {column.Name}(root, item));}}\n";
                    }
                    else if (typeNum == "6")
                    {
                        //working area:
                        //ref type
                        string refType = CastleDBUtils.GetTypeFromCastleDBColumn(column);
                        //look up the line based on the passed in row
                        constructorText += $"{column.Name} = new {CastleDBParser.Config.GeneratedTypesNamespace}.{refType}(root,{CastleDBParser.Config.GeneratedTypesNamespace}.{refType}.GetRowValue(node[\"{column.Name}\"]));\n";
                    }
                    else
                    {
                        if (typeNum == "10")
                        {
                            enumCast = $"({column.Name}Flag)";
                        }
                        else if (typeNum == "5")
                        {
                            enumCast = $"({column.Name}Enum)";
                        }
                        constructorText += $"{column.Name} = {enumCast}node[\"{column.Name}\"]{castText};\n";
                    }
                }

                //need to construct an enum of possible types
                string possibleValuesText = "";
                if (!sheet.NestedType)
                {
                    possibleValuesText += $"public enum RowValues {{ \n";
                    foreach (var name in sheet.RowNames)
                    {
                        possibleValuesText += name;
                        if (sheet.RowNames.IndexOf(name) + 1 < sheet.RowNames.Count)
                        {
                            possibleValuesText += ", \n";
                        }
                    }
                    possibleValuesText += "\n }";
                }

                string getMethodText = "";
                if (!sheet.NestedType)
                {
                    getMethodText += $@"
public static {sheet.Name}.RowValues GetRowValue(string name)
{{
    var values = (RowValues[])Enum.GetValues(typeof(RowValues));
    for (int i = 0; i < values.Length; i++)
    {{
        if(values[i].ToString() == name)
        {{
            return values[i];
        }}
    }}
    return values[0];
}}";
                }

                string ctor = "";
                if (!sheet.NestedType)
                {
                    ctor = $"public {sheet.Name} (CastleDBParser.RootNode root, RowValues line)";
                }
                else
                {
                    ctor = $"public {sheet.Name} (CastleDBParser.RootNode root, SimpleJSON.JSONNode node)";
                }
                // string usings = "using UnityEngine;\n using System;\n using System.Collections.Generic;\n using SimpleJSON;\n using CastleDBImporter;\n";
                string fullClassText = $@"
using UnityEngine;
using System;
using System.Collections.Generic;
using SimpleJSON;
using CastleDBImporter;
namespace {CastleDBParser.Config.GeneratedTypesNamespace}
{{ 
    public class {sheet.Name}
    {{
        {fieldText}
        {possibleValuesText} 
        {ctor} 
        {{
            {constructorText}
        }}  
        {getMethodText}
    }}
}}";
                Debug.Log("Generating CDB Class: " + sheet.Name);
                File.WriteAllText(scriptPath, fullClassText);
            }

            //build the CastleDB file
            string cdbscriptPath = $"Assets/{CastleDBParser.Config.GeneratedTypesLocation}/CastleDB.cs";

            scripts.Add(cdbscriptPath);
            //fields
            string cdbfields          = "";
            string cdbconstructorBody = "";
            string classTexts         = "";

            foreach (CastleDBParser.SheetNode sheet in root.Sheets)
            {
                if (sheet.NestedType)
                {
                    continue;
                }                               //only write main types to CastleDB
                cdbfields          += $"public {sheet.Name}Type {sheet.Name};\n";
                cdbconstructorBody += $"{sheet.Name} = new {sheet.Name}Type();";

                //get a list of all the row names
                classTexts += $"public class {sheet.Name}Type \n {{";
                foreach (var name in sheet.RowNames)
                {
                    classTexts += $"public {sheet.Name} {name} {{ get {{ return Get({CastleDBParser.Config.GeneratedTypesNamespace}.{sheet.Name}.RowValues.{name}); }} }} \n";
                }

                classTexts += $"private {sheet.Name} Get({CastleDBParser.Config.GeneratedTypesNamespace}.{sheet.Name}.RowValues line) {{ return new {sheet.Name}(parsedDB.Root, line); }}\n";
                classTexts += $@"
                public {sheet.Name}[] GetAll() 
                {{
                    var values = ({CastleDBParser.Config.GeneratedTypesNamespace}.{sheet.Name}.RowValues[])Enum.GetValues(typeof({CastleDBParser.Config.GeneratedTypesNamespace}.{sheet.Name}.RowValues));
                    {sheet.Name}[] returnList = new {sheet.Name}[values.Length];
                    for (int i = 0; i < values.Length; i++)
                    {{
                        returnList[i] = Get(values[i]);
                    }}
                    return returnList;
                }}";
                classTexts += $"\n }} //END OF {sheet.Name} \n";
            }

            string fullCastle = $@"
using UnityEngine;
using CastleDBImporter;
using System.Collections.Generic;
using System;

namespace {CastleDBParser.Config.GeneratedTypesNamespace}
{{
    public class CastleDB
    {{
        static CastleDBParser parsedDB;
        {cdbfields}
        public CastleDB(TextAsset castleDBAsset)
        {{
            parsedDB = new CastleDBParser(castleDBAsset);
            {cdbconstructorBody}
        }}
        {classTexts}
    }}
}}";

            Debug.Log("Generating CastleDB class");
            File.WriteAllText(cdbscriptPath, fullCastle);
            AssetDatabase.Refresh();
        }