public void TestComplexTypes()
        {
            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();

            rcr.Headers    = new CsvHeader[1];
            rcr.Headers[0] = new CsvHeader("Contained (NonGraphicalTests.IO.Csv.TypeToDeserialize)");

            rcr.Records = new List <string[]>();
            rcr.Records.Add(new string[1]);
            rcr.Records[0][0] = "X = 4";

            List <Container> listToPopulate = new List <Container>();

            rcr.CreateObjectList(typeof(Container), listToPopulate);

            if (listToPopulate[0].Contained.X != 4)
            {
                throw new Exception("Complex types are not being deserialized properly");
            }


            rcr.Records[0][0] = "AvailableEnemies = (\"Dragon\")";
            listToPopulate    = new List <Container>();

            rcr.CreateObjectList(typeof(Container), listToPopulate);

            if (listToPopulate[0].Contained.AvailableEnemies[0] != "Dragon")
            {
                throw new Exception("Inline lists in complex objects are not deserializing properly");
            }
        }
        static RuntimeCsvRepresentation LoadCsv(ReferencedFileSave r)
        {
            RuntimeCsvRepresentation rcr = FlatRedBallServices.Load <RuntimeCsvRepresentation>(ElementRuntime.ContentDirectory + r.Name,
                                                                                               GluxManager.ContentManagerName);

            return(rcr);
        }
        internal static List <string> GetMemberNamesFrom(ReferencedFileSave rfs)
        {
            List <string> toReturn = new List <string>();


            string fileName = rfs.Name;

            fileName = ProjectManager.MakeAbsolute(fileName);

            RuntimeCsvRepresentation rcr = CsvFileManager.CsvDeserializeToRuntime(
                fileName);


            for (int i = 0; i < rcr.Headers.Length; i++)
            {
                string memberName = rcr.Headers[i].Name;

                if (memberName.Trim().StartsWith("//"))
                {
                    continue;
                }

                memberName = StringFunctions.RemoveWhitespace(memberName);

                if (memberName.Contains("("))
                {
                    memberName = memberName.Substring(0, memberName.IndexOfAny(new char[] { '(' }));
                }

                toReturn.Add(memberName);
            }


            return(toReturn);
        }
        private string GetCsvContents(EntitySave entity, PlatformerEntityViewModel viewModel)
        {
            List <PlatformerValues> values = new List <PlatformerValues>();

            foreach (var valuesViewModel in viewModel.PlatformerValues)
            {
                values.Add(valuesViewModel.ToValues());
            }

            RuntimeCsvRepresentation rcr = RuntimeCsvRepresentation.FromList(values);

            var nameHeader = rcr.Headers[0];

            nameHeader.IsRequired = true;
            // Setting it to IsRequired is not sufficient, need to
            // modify the "Original Text" prop
            // chop off the closing quote, and add ", required)"
            nameHeader.OriginalText = nameHeader.OriginalText.Substring(0, nameHeader.OriginalText.Length - 1) + ", required)";

            rcr.Headers[0] = nameHeader;

            var toReturn = rcr.GenerateCsvString();

            return(toReturn);
        }
        private static void GetStartingAndCount(RuntimeCsvRepresentation rcr, int requiredIndex, string requiredName, out int startingRow, out int count)
        {
            startingRow = -1;
            count       = 0;

            for (int i = 0; i < rcr.Records.Count; i++)
            {
                if (startingRow == -1)
                {
                    if (rcr.Records[i][requiredIndex] == requiredName)
                    {
                        startingRow = i;
                        count       = 1;
                    }
                }
                else if (startingRow != -1)
                {
                    if (string.IsNullOrEmpty(rcr.Records[i][requiredIndex]))
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Beispiel #6
0
        private void ReadFromStream(BinaryReader reader, string assetName, out RuntimeCsvRepresentation csvOutput)
        {
            csvOutput = new FlatRedBall.IO.Csv.RuntimeCsvRepresentation();

            int numberOfRows = reader.ReadInt32();

            int numberOfColumns = reader.ReadInt32();

            csvOutput.Name = assetName;

            csvOutput.Headers = new CsvHeader[numberOfColumns];

            for (int i = 0; i < numberOfColumns; i++)
            {
                csvOutput.Headers[i] = new CsvHeader(reader.ReadString());
            }

            csvOutput.Records = new List <string[]>();

            for (int row = 0; row < numberOfRows; row++)
            {
                string[] record = new string[numberOfColumns];
                csvOutput.Records.Add(record);
                for (int column = 0; column < numberOfColumns; column++)
                {
                    record[column] = reader.ReadString();
                }
            }
        }
        private static string GetRequiredKeyType(RuntimeCsvRepresentation rcr, List <TypedMemberBase> members, Dictionary <string, string> untypedMembers, int requiredHeader)
        {
            // At this point the headers have their proper names (like XOffset) and don't include their type, so we
            // can use the simple .Name property
            string requiredMember = rcr.Headers[requiredHeader].Name;

            string type = null;

            foreach (TypedMemberBase tmb in members)
            {
                if (tmb.MemberName == requiredMember)
                {
                    type = tmb.MemberType.FullName;
                    break;
                }
            }

            if (string.IsNullOrEmpty(type))
            {
                foreach (KeyValuePair <string, string> kvp in untypedMembers)
                {
                    if (kvp.Key == requiredMember)
                    {
                        type = kvp.Value;
                        break;
                    }
                }
            }
            return(type);
        }
 private void SetLocalizationDb(string filePath)
 {
     GlueCommands.Self.TryMultipleTimes(() =>
     {
         LocalizationDb = CsvFileManager.CsvDeserializeToRuntime(filePath);
     }, 5);
 }
Beispiel #9
0
        private string GetCsvContents(EntitySave entity, RacingEntityViewModel viewModel)
        {
            List <RacingEntityValues> values = new List <RacingEntityValues>();

            var defaultValues = new RacingEntityValues();

            defaultValues.Name = "DefaultValues";
            // leave the defaults from the class

            values.Add(defaultValues);

            RuntimeCsvRepresentation rcr = RuntimeCsvRepresentation.FromList(values);

            var nameHeader = rcr.Headers[0];

            // Setting it to IsRequired is not sufficient, need to
            // modify the "Original Text" prop
            // chop off the closing quote, and add ", required)"
            nameHeader.OriginalText = nameHeader.OriginalText.Substring(0, nameHeader.OriginalText.Length - 1) + ", required)";

            rcr.Headers[0] = nameHeader;

            // if we want more defaults here...
            rcr.Records.Add(new string[0]);

            var toReturn = rcr.GenerateCsvString();

            return(toReturn);
        }
        private static void AddAvailableValuesFromFileToList(string absoluteFile, List <string> stringsToReturn, bool shouldAppendFileName)
        {
            if (System.IO.File.Exists(absoluteFile))
            {
                try
                {
                    string toAppend = "";
                    if (shouldAppendFileName)
                    {
                        // Eventually we want to make this relative to the container, not just the folename
                        toAppend = " in " + FileManager.RemovePath(absoluteFile);
                    }

                    RuntimeCsvRepresentation rcr =
                        CsvFileManager.CsvDeserializeToRuntime(absoluteFile);

                    rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();

                    int requiredIndex = -1;

                    for (int i = 0; i < rcr.Headers.Length; i++)
                    {
                        if (rcr.Headers[i].IsRequired)
                        {
                            requiredIndex = i;
                            break;
                        }
                    }

                    if (requiredIndex != -1)
                    {
                        foreach (string[] record in rcr.Records)
                        {
                            string possibleValue = record[requiredIndex];
                            if (!string.IsNullOrEmpty(possibleValue))
                            {
                                if (shouldAppendFileName)
                                {
                                    stringsToReturn.Add(possibleValue + toAppend);
                                }
                                else
                                {
                                    stringsToReturn.Add(possibleValue);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    // do nothing for now...
                }
            }
        }
        public static void GetDictionaryTypes(ReferencedFileSave referencedFileSave, out string keyType, out string valueType)
        {
            valueType = referencedFileSave.GetTypeForCsvFile();

            // To know the value type, we gotta pop this bad boy open and find the first requied type
            keyType = null;

            char oldDelimiter = CsvFileManager.Delimiter;

            switch (referencedFileSave.CsvDelimiter)
            {
            case AvailableDelimiters.Comma:
                CsvFileManager.Delimiter = ',';
                break;

            case AvailableDelimiters.Tab:
                CsvFileManager.Delimiter = '\t';
                break;

            case AvailableDelimiters.Pipe:
                CsvFileManager.Delimiter = '|';
                break;
            }

            string absoluteFileName = ProjectManager.MakeAbsolute(referencedFileSave.Name);

            // If the file doesn't exist this will generate bad code.  But this isn't
            // considered a silent failure because Glue will raise flags about missing
            // files earlier (like when it first starts up).  We don't want to crash the
            // entire application in this case.
            if (System.IO.File.Exists(absoluteFileName))
            {
                RuntimeCsvRepresentation rcr = CsvFileManager.CsvDeserializeToRuntime(absoluteFileName);

                // See if any of the headers are required
                foreach (CsvHeader header in rcr.Headers)
                {
                    int indexOfOpeningParen = header.Name.IndexOf("(");

                    if (indexOfOpeningParen != -1)
                    {
                        if (header.Name.IndexOf("required", indexOfOpeningParen) != -1)
                        {
                            keyType = CsvHeader.GetClassNameFromHeader(header.Name);
                            break;
                        }
                    }
                }
            }

            CsvFileManager.Delimiter = oldDelimiter;
        }
        public void TestSerialization()
        {
            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();

            rcr.Headers = new CsvHeader[4];
            rcr.Headers[0] = new CsvHeader("Header1");
            rcr.Headers[1] = new CsvHeader("Header2");
            rcr.Headers[2] = new CsvHeader("Newline Test1");
            rcr.Headers[3] = new CsvHeader("Something");
            rcr.Headers[3].IsRequired = true;

            rcr.Records = new List<string[]>();
            rcr.Records.Add(new string[4]);
            rcr.Records[0][0] = "NoQuotes \"Quotes\" NoQuotes";
            rcr.Records[0][1] = "Value = \"Something\"";
            rcr.Records[0][2] = string.Concat("Line 1", Environment.NewLine, "Line 2");
            rcr.Records[0][3] = "";

            string result = rcr.GenerateCsvString();
            if (!result.Contains("NoQuotes \"\"Quotes\"\" NoQuotes"))
            {
                throw new Exception("rcr.GenerateCsvString is not properly adding double quotes for quotes");
            }

            if (!result.Contains("\"Value = \"\"Something\"\"\""))
            {
                throw new Exception("rcr.GenerateCsvString is not properly wrapping text in quotes when it should");
            }
            
            if (!result.Contains(string.Concat("\"", rcr.Records[0][2], "\"")))
            {
                throw new Exception("rcr.GenerateCsvString is not properly wrapping newlined text in quotes");
            }

            FileManager.SaveText(result, "SavedCsv.csv");

            RuntimeCsvRepresentation loaded = CsvFileManager.CsvDeserializeToRuntime("SavedCsv.csv");

            for (int i = 0; i < loaded.Records.Count; i++)
            {
                for (int j = 0; j < loaded.Records[i].Length; j++)
                {
                    var loadedAtIandJ = loaded.Records[i][j];
                    var rcrAtIandJ = rcr.Records[i][j];
                    if (loadedAtIandJ != rcrAtIandJ)
                    {
                        throw new Exception("Loaded RCR doesn't equal what was saved");
                    }
                }
            }

        }
        public void TestSerialization()
        {
            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();

            rcr.Headers               = new CsvHeader[4];
            rcr.Headers[0]            = new CsvHeader("Header1");
            rcr.Headers[1]            = new CsvHeader("Header2");
            rcr.Headers[2]            = new CsvHeader("Newline Test1");
            rcr.Headers[3]            = new CsvHeader("Something");
            rcr.Headers[3].IsRequired = true;

            rcr.Records = new List <string[]>();
            rcr.Records.Add(new string[4]);
            rcr.Records[0][0] = "NoQuotes \"Quotes\" NoQuotes";
            rcr.Records[0][1] = "Value = \"Something\"";
            rcr.Records[0][2] = string.Concat("Line 1", Environment.NewLine, "Line 2");
            rcr.Records[0][3] = "";

            string result = rcr.GenerateCsvString();

            if (!result.Contains("NoQuotes \"\"Quotes\"\" NoQuotes"))
            {
                throw new Exception("rcr.GenerateCsvString is not properly adding double quotes for quotes");
            }

            if (!result.Contains("\"Value = \"\"Something\"\"\""))
            {
                throw new Exception("rcr.GenerateCsvString is not properly wrapping text in quotes when it should");
            }

            if (!result.Contains(string.Concat("\"", rcr.Records[0][2], "\"")))
            {
                throw new Exception("rcr.GenerateCsvString is not properly wrapping newlined text in quotes");
            }

            FileManager.SaveText(result, "SavedCsv.csv");

            RuntimeCsvRepresentation loaded = CsvFileManager.CsvDeserializeToRuntime("SavedCsv.csv");

            for (int i = 0; i < loaded.Records.Count; i++)
            {
                for (int j = 0; j < loaded.Records[i].Length; j++)
                {
                    var loadedAtIandJ = loaded.Records[i][j];
                    var rcrAtIandJ    = rcr.Records[i][j];
                    if (loadedAtIandJ != rcrAtIandJ)
                    {
                        throw new Exception("Loaded RCR doesn't equal what was saved");
                    }
                }
            }
        }
        static IEnumerable <string> RequiredColumnValues(RuntimeCsvRepresentation rcr)
        {
            int requiredHeader = rcr.GetRequiredIndex();

            foreach (string[] record in rcr.Records)
            {
                string value = record[requiredHeader];

                if (!string.IsNullOrEmpty(value) && !value.StartsWith("//"))
                {
                    yield return(value);
                }
            }
        }
        private static string GetClassInfo(string fileName, RuntimeCsvRepresentation rcr, CustomClassSave customClass, out List <TypedMemberBase> members, out Dictionary <string, string> untypedMembers)
        {
            bool usesCustomClass = customClass != null;
            List <RuntimeCsvRepresentation> rcrsForClass = new List <RuntimeCsvRepresentation>();

            if (usesCustomClass)
            {
                foreach (string name in customClass.CsvFilesUsingThis)
                {
                    ReferencedFileSave foundRfs = ObjectFinder.Self.GetReferencedFileSaveFromFile(name);

                    if (foundRfs == null)
                    {
                        int m = 3;
                    }
                    else
                    {
                        fileName = foundRfs.Name;
                        fileName = ProjectManager.MakeAbsolute(fileName);

                        RuntimeCsvRepresentation runtimeToAdd = null;
                        try
                        {
                            runtimeToAdd = CsvFileManager.CsvDeserializeToRuntime(fileName);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Error trying to parse CSV:\n" + e.ToString());
                        }

                        if (runtimeToAdd != null)
                        {
                            rcrsForClass.Add(runtimeToAdd);
                        }
                    }
                }
            }
            else if (rcr != null)
            {
                rcrsForClass.Add(rcr);
            }



            GetClassInfoFromCsv(rcrsForClass, customClass, out members, out untypedMembers);
            return(fileName);
        }
Beispiel #16
0
        public void TestCsvGeneration()
        {
            string nameInCsv = "Name With Spaces and Invalids*~";

            string constName = CsvCodeGenerator.GetConstNameForValue(nameInCsv);

            if (constName.Contains(' '))
            {
                throw new Exception("Const name for CSV is invalid!");
            }

            if (constName.IndexOfAny(NameVerifier.InvalidCharacters) != -1)
            {
                throw new Exception("Const name contains invalid characters!");
            }

            Type type = TypeManager.GetTypeFromString("bool");

            if (type != typeof(bool))
            {
                throw new Exception("TypeManager is not properly returning type from \"bool\"");
            }

            type = TypeManager.GetTypeFromString("System.Boolean");
            if (type != typeof(bool))
            {
                throw new Exception("TypeManager is not properly returning type from \"bool\"");
            }

            type = TypeManager.GetTypeFromString("Boolean");
            if (type != typeof(bool))
            {
                throw new Exception("TypeManager is not properly returning type from \"bool\"");
            }

            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();

            rcr.Headers = new CsvHeader[2];
            rcr.Headers[0].OriginalText = "Variable";
            rcr.Headers[1].OriginalText = "Variable (float)";
            string why = CsvCodeGenerator.GetWhyCsvIsWrong(rcr, false, null);

            if (string.IsNullOrEmpty(why))
            {
                throw new Exception("Glue should be reporting errors on CSVs that have duplicate header names (but of different types)");
            }
        }
        public static string GetWhyCsvIsWrong(RuntimeCsvRepresentation rcr, bool createsDictionary, string fileName)
        {
            string duplicateHeader        = null;
            string duplicateRequiredField = null;

            duplicateHeader = rcr.GetFirstDuplicateHeader;

            // Check for duplicates ignoring the type

            if (createsDictionary)
            {
                duplicateRequiredField = rcr.FirstDuplicateRequiredField;
            }

            if (!string.IsNullOrEmpty(duplicateHeader))
            {
                return("The CSV file " + fileName + "\n\nhas the following duplicate header.\n\n" +
                       duplicateHeader);
            }
            else if (createsDictionary && !string.IsNullOrEmpty(duplicateRequiredField))
            {
                return("The CSV file " + fileName + "\n\nhas a duplicate required field:" + duplicateRequiredField);
            }

            // WE also have to check for duplicates with different types but the same name
            List <string> namesFound = new List <string>();

            foreach (var header in rcr.Headers)
            {
                string name = CsvHeader.GetNameWithoutParentheses(header.OriginalText);
                if (!string.IsNullOrEmpty(name))
                {
                    if (namesFound.Contains(name))
                    {
                        return("The CSV file " + fileName + " has a duplicate header: " + name);
                    }
                    else
                    {
                        namesFound.Add(name);
                    }
                }
            }


            return(null);
        }
Beispiel #18
0
        internal void CreateAdditionalCsvFile(string name, GlobalOrProjectSpecific globalOrProjectSpecific)
        {
            // We're going to load the full
            // AvailableAssetTypes just to get
            // the headers.  Then we'll
            // create a new RCR, copy over the headers
            // then save off the RCR using the argument
            // fileName.  The new one will be empty except
            // for the headers.
            RuntimeCsvRepresentation rcr = CsvFileManager.CsvDeserializeToRuntime(
                mCoreTypesFileLocation);

            rcr.Records = new List <string[]>();


            string desiredFullPath = null;

            if (globalOrProjectSpecific == GlobalOrProjectSpecific.Global)
            {
                desiredFullPath = GlobalCustomContentTypesFolder + name + ".csv";
            }
            else // project-specific
            {
                desiredFullPath = ProjectSpecificContentTypesFolder + name + ".csv";
            }
            if (File.Exists(desiredFullPath))
            {
                MessageBox.Show("The CSV " + desiredFullPath + " already exists.");
            }
            else
            {
                try
                {
                    CsvFileManager.Serialize(rcr, desiredFullPath);
                }
                catch (UnauthorizedAccessException)
                {
                    MessageBox.Show("Unable to save the CSV file.  You need to run Glue as an administrator");
                }
                catch (Exception e)
                {
                    MessageBox.Show("Unknown error attempting to create the CSV:\n\n" + e.ToString());
                }
            }
        }
        /// <summary>
        /// Verifies that the CSV is properly set up - this code doesn't generate anything because uniform type CSVs don't actually make
        /// a new class.  They'll instead deserialize to something like string[]
        /// </summary>
        /// <param name="rfs">The file reference to the CSV.</param>
        /// <param name="fileName">The filename of the CSV</param>
        /// <param name="oldDelimiter">The old CSV delimiter - what to set the CSV delimiter back to after deserializing this CSV.</param>
        private static void CheckUniformTypeValidity(ReferencedFileSave rfs, string fileName, char oldDelimiter)
        {
            string duplicateHeader;

            RuntimeCsvRepresentation rcr = CsvFileManager.CsvDeserializeToRuntime(
                fileName);

            CsvFileManager.Delimiter = oldDelimiter;
            duplicateHeader          = rcr.GetFirstDuplicateHeader;

            if (rfs.CreatesDictionary)
            {
                string duplicateRequiredField = rcr.FirstDuplicateRequiredField;

                if (!string.IsNullOrEmpty(duplicateRequiredField))
                {
                    System.Windows.Forms.MessageBox.Show("The CSV file " + fileName + "\n\nhas a duplicate required field:" + duplicateRequiredField);
                }
            }
        }
        private object EvaluateIndexerExpression(ICSharpCode.NRefactory.CSharp.IndexerExpression indexerExpression, CodeContext codeContext)
        {
            object evaluatedTarget = EvaluateExpression(indexerExpression.Target, codeContext);

            if (evaluatedTarget is RuntimeCsvRepresentation)
            {
                List <object> evaluatedArguments = new List <object>();
                foreach (var argument in indexerExpression.Arguments)
                {
                    evaluatedArguments.Add(EvaluateExpression(argument, codeContext));
                }
                string requiredKey = evaluatedArguments[0] as string;



                RuntimeCsvRepresentation rcr = evaluatedTarget as RuntimeCsvRepresentation;

                return(GetCsvEntryByRequiredKey(requiredKey, rcr));
            }
            return(null);
        }
        private static object GetCsvEntryByRequiredKey(string requiredKey, RuntimeCsvRepresentation rcr)
        {
            rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();

            int requiredIndex = rcr.GetRequiredIndex();

            int startingRow;
            int count;

            GetStartingAndCount(rcr, requiredIndex, requiredKey, out startingRow, out count);
            CsvEntry csvEntry = null;

            if (startingRow != -1)
            {
                csvEntry = new CsvEntry();

                csvEntry.RuntimeCsvRepresentation = rcr;
                csvEntry.Count      = count;
                csvEntry.StartIndex = startingRow;
            }

            return(csvEntry);
        }
Beispiel #22
0
        private string GetCsvContents(EntitySave entity, TopDownEntityViewModel viewModel)
        {
            List <TopDownValues> values = new List <TopDownValues>();

            // create a default entry:
            var defaultValue = new TopDownValues();

            defaultValue.Name                        = "DefaultValues";
            defaultValue.MaxSpeed                    = 250;
            defaultValue.AccelerationTime            = 1;
            defaultValue.DecelerationTime            = .5f;
            defaultValue.UpdateDirectionFromVelocity = true;

            values.Add(defaultValue);

            RuntimeCsvRepresentation rcr = RuntimeCsvRepresentation.FromList(values);

            var nameHeader = rcr.Headers[0];

            nameHeader.IsRequired = true;
            // Setting it to IsRequired is not sufficient, need to
            // modify the "Original Text" prop
            // chop off the closing quote, and add ", required)"
            nameHeader.OriginalText = nameHeader.OriginalText.Substring(0, nameHeader.OriginalText.Length - 1) + ", required)";

            rcr.Headers[0] = nameHeader;

            var movementDefaults = new string[]
            {
            };

            rcr.Records.Add(movementDefaults);

            var toReturn = rcr.GenerateCsvString();

            return(toReturn);
        }
        public void TestCreateObjectList()
        {
            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();

            rcr.Headers    = new CsvHeader[3];
            rcr.Headers[0] = new CsvHeader("X (float)");
            rcr.Headers[1] = new CsvHeader("Visible (bool)");
            rcr.Headers[2] = new CsvHeader("AvailableEnemies (List<string>)");

            rcr.Records = new List <string[]>();
            rcr.Records.Add(new string[3]);
            rcr.Records[0][0] = "3";
            rcr.Records[0][1] = "true";
            rcr.Records[0][2] = "Dragon";

            List <TypeToDeserialize> listToPopulate = new List <TypeToDeserialize>();

            rcr.CreateObjectList(typeof(TypeToDeserialize), listToPopulate);

            if (listToPopulate[0].AvailableEnemies.Count != 1 || listToPopulate[0].AvailableEnemies[0] != "Dragon")
            {
                throw new Exception("Lists are not being deserialized properly");
            }
        }
        private static void FillCodeBlockWithKeys(ICodeBlock codeBlock, string keyType, RuntimeCsvRepresentation rcr)
        {
            foreach (string value in RequiredColumnValues(rcr))
            {
                string rightSideOfEquals = value;

                if (keyType == "System.String")
                {
                    rightSideOfEquals = "\"" + rightSideOfEquals + "\"";
                }
                string leftSideOfEquals = GetConstNameForValue(value);

                codeBlock.Line("public const " + keyType + " " + leftSideOfEquals + " = " + rightSideOfEquals + ";");
            }
        }
        private static void DeserializeToRcr(AvailableDelimiters delimiter, string fileName, out RuntimeCsvRepresentation rcr, out bool succeeded)
        {
            rcr = null;

            succeeded = true;

            try
            {
                // Let's load this bad boy and get info about the class we need to make
                rcr = CsvFileManager.CsvDeserializeToRuntime(fileName);
            }
            catch (Exception e)
            {
                GlueGui.ShowMessageBox("Error parsing CSV\n\n" + fileName + "\nAttempted to use " + delimiter + " as the delimiter.  If this is not the delimiter of the file, try changing the delimiter in Glue after the file is added");

                FlatRedBall.Glue.Plugins.PluginManager.ReceiveError(e.ToString());

                succeeded = false;
            }
        }
        private static void FillOrderedListWithKeys(ICodeBlock codeBlock, string type, RuntimeCsvRepresentation rcr)
        {
            int numberAdded = 0;
            foreach (string value in RequiredColumnValues(rcr))
            {
                if (numberAdded == 0)
                {
                    codeBlock.Line("public static System.Collections.Generic.List<" + type + "> OrderedList = new System.Collections.Generic.List<" + type + ">");
                    codeBlock.Line("{");
                    codeBlock.TabCount++;

                }
                string name = GetConstNameForValue(value);
                
                if (numberAdded != 0)
                {
                    // This is a little more efficient than doing a count first, then adding
                    codeBlock.Line("," + name);
                }
                else
                {
                    codeBlock.Line(name);
                }
                numberAdded++;
            }

            if (numberAdded != 0)
            {
                codeBlock.TabCount--;
                codeBlock.Line("};");
            }
        }
        private static string GetClassInfoFromCsvs(ReferencedFileSave rfs, string fileName, RuntimeCsvRepresentation rcr, out string className, out List <TypedMemberBase> members, out Dictionary <string, string> untypedMembers)
        {
            className = rfs.GetUnqualifiedTypeForCsv();

            CustomClassSave customClass = GetCustomClassForCsv(rfs.Name);

            fileName = GetClassInfo(fileName, rcr, customClass, out members, out untypedMembers);
            return(fileName);
        }
        public static string GetWhyCsvIsWrong(RuntimeCsvRepresentation rcr, bool createsDictionary, string fileName)
        {
            string duplicateHeader = null;
            string duplicateRequiredField = null;
            duplicateHeader = rcr.GetFirstDuplicateHeader;

            // Check for duplicates ignoring the type

            if (createsDictionary)
            {
                duplicateRequiredField = rcr.FirstDuplicateRequiredField;
            }

            if (!string.IsNullOrEmpty(duplicateHeader))
            {
                return "The CSV file " + fileName + "\n\nhas the following duplicate header.\n\n" +
                    duplicateHeader;
            }
            else if (createsDictionary && !string.IsNullOrEmpty(duplicateRequiredField))
            {
                return "The CSV file " + fileName + "\n\nhas a duplicate required field:" + duplicateRequiredField;
            }

            // WE also have to check for duplicates with different types but the same name
            List<string> namesFound = new List<string>();
            foreach (var header in rcr.Headers)
            {
                string name = CsvHeader.GetNameWithoutParentheses(header.OriginalText);
                if (!string.IsNullOrEmpty(name))
                {
                    if (namesFound.Contains(name))
                    {
                        return "The CSV file " + fileName + " has a duplicate header: " + name;
                    }
                    else
                    {
                        namesFound.Add(name);
                    }
                }
            }


            return null;
        }
        private static void DeserializeToRcr(AvailableDelimiters delimiter, string fileName, out RuntimeCsvRepresentation rcr, out bool succeeded)
        {
            rcr = null;

            succeeded = true;

            try
            {
                // Let's load this bad boy and get info about the class we need to make
                rcr = CsvFileManager.CsvDeserializeToRuntime(fileName);
            }
            catch (Exception e)
            {
                GlueGui.ShowMessageBox("Error parsing CSV\n\n" + fileName + "\nAttempted to use " + delimiter + " as the delimiter.  If this is not the delimiter of the file, try changing the delimiter in Glue after the file is added");

                FlatRedBall.Glue.Plugins.PluginManager.ReceiveError(e.ToString());

                succeeded = false;
            }
        }
        private static void GetMembersForRcr(List <TypedMemberBase> members,
                                             Dictionary <string, string> untypedMembers, List <string> membersAlreadyAdded, RuntimeCsvRepresentation rcr)
        {
            for (int i = 0; i < rcr.Headers.Length; i++)
            {
                CsvHeader header = rcr.Headers[i];

                string memberName = header.Name;
                if (string.IsNullOrWhiteSpace(memberName) == false && memberName.Trim().StartsWith("//") == false)
                {
                    Type   type;
                    string classType;

                    GetTypeFromHeader(ref header, ref memberName, out type, out classType);
                    TryAddMember(members, untypedMembers, membersAlreadyAdded, memberName, type, classType);
                }
            }
        }
Beispiel #31
0
        public void SaveCsv(string fileName)
        {
            RuntimeCsvRepresentation rcr = RuntimeCsvRepresentation.FromList <BuildToolAssociation>(this.BuildToolList);

            CsvFileManager.Serialize(rcr, fileName);
        }
        private static void FillCodeBlockWithKeys(ICodeBlock codeBlock, string keyType, RuntimeCsvRepresentation rcr)
        {

            foreach(string value in RequiredColumnValues(rcr))
            {
                string rightSideOfEquals = value;

                if (keyType == "System.String")
                {
                    rightSideOfEquals = "\"" + rightSideOfEquals + "\"";
                }
                string leftSideOfEquals = GetConstNameForValue(value);

                codeBlock.Line("public const " + keyType + " " + leftSideOfEquals + " = " + rightSideOfEquals + ";");
            }
        }
        static IEnumerable<string> RequiredColumnValues(RuntimeCsvRepresentation rcr)
        {
            int requiredHeader = rcr.GetRequiredIndex();
            foreach (string[] record in rcr.Records)
            {
                string value = record[requiredHeader];

                if (!string.IsNullOrEmpty(value) && !value.StartsWith("//"))
                {
                    yield return value;
                }
            }
        }
        private static string GetRequiredKeyType(RuntimeCsvRepresentation rcr, List<TypedMemberBase> members, Dictionary<string, string> untypedMembers, int requiredHeader)
        {
            // At this point the headers have their proper names (like XOffset) and don't include their type, so we
            // can use the simple .Name property
            string requiredMember = rcr.Headers[requiredHeader].Name;

            string type = null;

            foreach (TypedMemberBase tmb in members)
            {
                if (tmb.MemberName == requiredMember)
                {
                    type = tmb.MemberType.FullName;
                    break;
                }
            }

            if (string.IsNullOrEmpty(type))
            {
                foreach (KeyValuePair<string, string> kvp in untypedMembers)
                {
                    if (kvp.Key == requiredMember)
                    {
                        type = kvp.Value;
                        break;
                    }
                }
            }
            return type;
        }
        private static void GetMembersForRcr(List<TypedMemberBase> members, 
            Dictionary<string, string> untypedMembers, List<string> membersAlreadyAdded, RuntimeCsvRepresentation rcr)
        {
            for (int i = 0; i < rcr.Headers.Length; i++)
            {
                CsvHeader header = rcr.Headers[i];

                string memberName = header.Name;
                if (string.IsNullOrWhiteSpace(memberName) == false && memberName.Trim().StartsWith("//") == false)
                {



                    Type type;
                    string classType;
                    
                    GetTypeFromHeader(ref header, ref memberName, out type, out classType);
                    TryAddMember(members, untypedMembers, membersAlreadyAdded, memberName, type, classType);
                }

            }
        }
Beispiel #36
0
        public static object GetValueFromCsv(string rowValue, string variableName, string csvType, IElement element, ElementRuntime elementRuntime)
        {
            // We should check the csvType (which might be a class already).
            // If we don't get anything back then we should try stripping off
            // the extension and path
            object foundValue = null;


            ReferencedFileSave rfs = ObjectFinder.Self.GetFirstCsvUsingClass(csvType, element);

            if (rfs == null && FileManager.GetExtension(csvType) == "csv")
            {
                string strippedType = FileManager.RemovePath(FileManager.RemoveExtension(csvType));

                rfs = ObjectFinder.Self.GetFirstCsvUsingClass(strippedType, element);
            }

            if (rfs != null)
            {
                RuntimeCsvRepresentation rcr = null;

                // This could be global content or it could be a RFS in the element
                if (element.ContainsRecursively(rfs))
                {
                    var loadedFile = elementRuntime.LoadReferencedFileSave(rfs, true, element);
                    rcr = loadedFile.RuntimeObject as RuntimeCsvRepresentation;
                }
                else
                {
                    var loadedFile =
                        GluxManager.GlobalContentFilesRuntime.LoadReferencedFileSave(rfs, true, element);
                    // Load this thing from global content
                    rcr = loadedFile.RuntimeObject as
                          RuntimeCsvRepresentation;
                }
                if (rcr.GetRequiredIndex() == -1)
                {
                    rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();
                }
                int indexOfColumn = 0;
                for (int i = 0; i < rcr.Headers.Length; i++)
                {
                    if (rcr.Headers[i].Name == variableName)
                    {
                        indexOfColumn = i;
                        break;
                    }
                }

                if (rcr != null)
                {
                    // Right now I'm writing this to only support dictionaries.
                    int      indexOfRequired = rcr.GetRequiredIndex();
                    string[] matchingRecord  = null;

                    foreach (string[] record in rcr.Records)
                    {
                        try
                        {
                            if (record.Length > indexOfRequired &&
                                record[indexOfRequired] == rowValue)
                            {
                                if (indexOfColumn >= record.Length)
                                {
                                    int m = 3;
                                }
                                foundValue = record[indexOfColumn];

                                foundValue = ConvertValueToType(foundValue, rcr.Headers[indexOfColumn].OriginalText);


                                break;
                            }
                        }
                        catch
                        {
                            int m = 3;
                        }
                    }
                }
            }

            // We need to find a RFS either in this IElement or in GlobalContentFiles that match this name
            return(foundValue);
        }
        private static string GetClassInfo(string fileName, RuntimeCsvRepresentation rcr, CustomClassSave customClass, out List<TypedMemberBase> members, out Dictionary<string, string> untypedMembers)
        {
            bool usesCustomClass = customClass != null;
            List<RuntimeCsvRepresentation> rcrsForClass = new List<RuntimeCsvRepresentation>();

            if (usesCustomClass)
            {
                foreach (string name in customClass.CsvFilesUsingThis)
                {
                    ReferencedFileSave foundRfs = ObjectFinder.Self.GetReferencedFileSaveFromFile(name);

                    if (foundRfs == null)
                    {
                        int m = 3;
                    }
                    else
                    {
                        fileName = foundRfs.Name;
                        fileName = ProjectManager.MakeAbsolute(fileName);

                        RuntimeCsvRepresentation runtimeToAdd = null;
                        try
                        {
                            runtimeToAdd = CsvFileManager.CsvDeserializeToRuntime(fileName);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Error trying to parse CSV:\n" + e.ToString());
                        }

                        if (runtimeToAdd != null)
                        {
                            rcrsForClass.Add(runtimeToAdd);
                        }
                    }
                }
            }
            else if(rcr != null)
            {
                rcrsForClass.Add(rcr);
            }




            GetClassInfoFromCsv(rcrsForClass, customClass, out members, out untypedMembers);
            return fileName;
        }
        public void TestCreateObjectList()
        {
            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();

            rcr.Headers = new CsvHeader[3];
            rcr.Headers[0] = new CsvHeader("X (float)");
            rcr.Headers[1] = new CsvHeader("Visible (bool)");
            rcr.Headers[2] = new CsvHeader("AvailableEnemies (List<string>)");

            rcr.Records = new List<string[]>();
            rcr.Records.Add(new string[3]);
            rcr.Records[0][0] = "3";
            rcr.Records[0][1] = "true";
            rcr.Records[0][2] = "Dragon";

            List<TypeToDeserialize> listToPopulate = new List<TypeToDeserialize>();

            rcr.CreateObjectList(typeof(TypeToDeserialize), listToPopulate);

            if (listToPopulate[0].AvailableEnemies.Count != 1 || listToPopulate[0].AvailableEnemies[0] != "Dragon")
            {
                throw new Exception("Lists are not being deserialized properly");
            }

        }
        private static string GetClassInfoFromCsvs(ReferencedFileSave rfs, string fileName, RuntimeCsvRepresentation rcr, out string className, out List<TypedMemberBase> members, out Dictionary<string, string> untypedMembers)
        {
            className = rfs.GetUnqualifiedTypeForCsv();

            CustomClassSave customClass = GetCustomClassForCsv(rfs.Name);

            fileName = GetClassInfo(fileName, rcr, customClass, out members, out untypedMembers);
            return fileName;
        }
        public void TestComplexTypes()
        {
            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();

            rcr.Headers = new CsvHeader[1];
            rcr.Headers[0] = new CsvHeader("Contained (NonGraphicalTests.IO.Csv.TypeToDeserialize)");

            rcr.Records = new List<string[]>();
            rcr.Records.Add(new string[1]);
            rcr.Records[0][0] = "X = 4";

            List<Container> listToPopulate = new List<Container>();

            rcr.CreateObjectList(typeof(Container), listToPopulate);

            if (listToPopulate[0].Contained.X != 4)
            {
                throw new Exception("Complex types are not being deserialized properly");
            }


            rcr.Records[0][0] = "AvailableEnemies = (\"Dragon\")";
            listToPopulate = new List<Container>();

            rcr.CreateObjectList(typeof(Container), listToPopulate);

            if (listToPopulate[0].Contained.AvailableEnemies[0] != "Dragon")
            {
                throw new Exception("Inline lists in complex objects are not deserializing properly");
            }
        }
        private static void FillOrderedListWithKeys(ICodeBlock codeBlock, string type, RuntimeCsvRepresentation rcr)
        {
            int numberAdded = 0;

            foreach (string value in RequiredColumnValues(rcr))
            {
                if (numberAdded == 0)
                {
                    codeBlock.Line("public static System.Collections.Generic.List<" + type + "> OrderedList = new System.Collections.Generic.List<" + type + ">");
                    codeBlock.Line("{");
                    codeBlock.TabCount++;
                }
                string name = GetConstNameForValue(value);

                if (numberAdded != 0)
                {
                    // This is a little more efficient than doing a count first, then adding
                    codeBlock.Line("," + name);
                }
                else
                {
                    codeBlock.Line(name);
                }
                numberAdded++;
            }

            if (numberAdded != 0)
            {
                codeBlock.TabCount--;
                codeBlock.Line("};");
            }
        }
        private static void GetStartingAndCount(RuntimeCsvRepresentation rcr, int requiredIndex, string requiredName, out int startingRow, out int count)
        {
            startingRow = -1;
            count = 0;

            for (int i = 0; i < rcr.Records.Count; i++)
            {
                if (startingRow == -1)
                {
                    if (rcr.Records[i][requiredIndex] == requiredName)
                    {
                        startingRow = i;
                        count = 1;
                    }
                }
                else if (startingRow != -1)
                {
                    if (string.IsNullOrEmpty(rcr.Records[i][requiredIndex]))
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        private static object GetCsvEntryByRequiredKey(string requiredKey, RuntimeCsvRepresentation rcr)
        {
            rcr.RemoveHeaderWhitespaceAndDetermineIfRequired();

            int requiredIndex = rcr.GetRequiredIndex();

            int startingRow;
            int count;
            GetStartingAndCount(rcr, requiredIndex, requiredKey, out startingRow, out count);
            CsvEntry csvEntry = null;

            if (startingRow != -1)
            {
                csvEntry = new CsvEntry();

                csvEntry.RuntimeCsvRepresentation = rcr;
                csvEntry.Count = count;
                csvEntry.StartIndex = startingRow;
            }

            return csvEntry;
        }
        public void TestCsvGeneration()
        {

            string nameInCsv = "Name With Spaces and Invalids*~";

            string constName = CsvCodeGenerator.GetConstNameForValue(nameInCsv);

            if (constName.Contains(' '))
            {
                throw new Exception("Const name for CSV is invalid!");
            }

            if (constName.IndexOfAny(NameVerifier.InvalidCharacters) != -1)
            {
                throw new Exception("Const name contains invalid characters!");
            }

            Type type = TypeManager.GetTypeFromString("bool");
            if (type != typeof(bool))
            {
                throw new Exception("TypeManager is not properly returning type from \"bool\"");
            }

            type = TypeManager.GetTypeFromString("System.Boolean");
            if (type != typeof(bool))
            {
                throw new Exception("TypeManager is not properly returning type from \"bool\"");
            }

            type = TypeManager.GetTypeFromString("Boolean");
            if (type != typeof(bool))
            {
                throw new Exception("TypeManager is not properly returning type from \"bool\"");
            }

            RuntimeCsvRepresentation rcr = new RuntimeCsvRepresentation();
            rcr.Headers = new CsvHeader[2];
            rcr.Headers[0].OriginalText = "Variable";
            rcr.Headers[1].OriginalText = "Variable (float)";
            string why = CsvCodeGenerator.GetWhyCsvIsWrong(rcr, false, null);
            if (string.IsNullOrEmpty(why))
            {
                throw new Exception("Glue should be reporting errors on CSVs that have duplicate header names (but of different types)");
            }
        }
Beispiel #45
0
        public static RuntimeCsvRepresentation GetRuntimeCsvRepresentationFromStream(Stream stream, HeaderPresence headerPresence, bool trimEmptyLines, char delimiter)
        {
            System.IO.StreamReader streamReader = new StreamReader(stream);

            RuntimeCsvRepresentation runtimeCsvRepresentation = null;

            bool hasHeaders = headerPresence == HeaderPresence.HasHeaders;

            using (CsvReader csv = new CsvReader(streamReader, hasHeaders, delimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, CsvReader.DefaultComment, true, CsvReader.DefaultBufferSize))
            {
                csv.SkipsComments = false;

                runtimeCsvRepresentation = new RuntimeCsvRepresentation();

                string[] fileHeaders = csv.GetFieldHeaders();
                runtimeCsvRepresentation.Headers = new CsvHeader[fileHeaders.Length];

                for (int i = 0; i < fileHeaders.Length; i++)
                {
                    runtimeCsvRepresentation.Headers[i] = new CsvHeader(fileHeaders[i]);
                }

                // use field count instead of header count because there may not be headers
                int numberOfHeaders = csv.FieldCount;

                runtimeCsvRepresentation.Records = new List<string[]>();

                while (csv.ReadNextRecord())
                {
                    string[] newRecord = new string[numberOfHeaders];

                    bool shouldAddRow = !trimEmptyLines;

                    for (int i = 0; i < numberOfHeaders; i++)
                    {
                        string record = csv[i];

                        newRecord[i] = record;
                        if (!string.IsNullOrEmpty(record))
                        {
                            shouldAddRow = true;
                        }
                    }

                    if (shouldAddRow)
                    {
                        runtimeCsvRepresentation.Records.Add(newRecord);
                    }

                }
            }

            // Vic says - not sure how this got here, but it causes a crash!
            //streamReader.DiscardBufferedData();
            streamReader.Close();
            streamReader.Dispose();
            return runtimeCsvRepresentation;
        }