Exemplo n.º 1
0
        /// <summary>
        /// Converts multi-line tab delimited string data into a list of type T.  Used primarily to
        /// initialize struct data to be persisted using the Persist method that came from a spreadsheet.  Note, this method
        /// does not attempt to validate the data, if it cannot converted, T.column returns the default value.
        /// </summary>
        /// <typeparam name="T">Type of data that will be persisted.</typeparam>
        /// <param name="aText">The text you want to convert.  If it's in the clipboard, use EditorGUIUtility.systemCopyBuffer.</param>
        /// <param name="aConverters">Is a dictionary of methods that converts string data to the corresponding type.</param>
        /// <returns>List of T.</returns>
        public static List <T> ConvertText <T>(string aText, Dictionary <Type, Func <String, object> > aConverters) where T : new()
        {
            // Get all the properties of the data we want to convert into
            PropertyInfo[] INFOS = typeof(T).GetProperties();

            // Create a new list
            List <T> DATA = new List <T>();

            using (StringReader READER = new StringReader(aText)) {
                string LINE;

                // Read a new row of data
                while ((LINE = READER.ReadLine()) != null)
                {
                    T             ROW     = new T();
                    List <string> COLUMNS = LINE.Split("\t".ToCharArray()).ToList();

                    // Go through the columns, only convert what is available (extra text columns are ignored, extra T attributes are uninitialized
                    for (int INDEX = 0; INDEX < Math.Min(COLUMNS.Count, INFOS.Length); INDEX++)
                    {
                        PropertyInfo INFO = INFOS[INDEX];

                        // Do we have a converter for the T.column's type?
                        if (aConverters.ContainsKey(INFO.PropertyType))
                        {
                            try {
                                // Convert the string data
                                object RESULT = aConverters[INFO.PropertyType](COLUMNS[INDEX]);

                                // Save the converted data into the row
                                INFO.SetValue(ROW, RESULT, null);
                            } catch {
                                // ignore - could not be converted
                            }
                        } // else ignore - could not be converted
                    }

                    DATA.Add(ROW);
                }
            }

            return(DATA);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            if (!File.Exists("C:/dc_mmo/src/SrcXML/Config.txt"))
            {
                ERROR("Config.txt can't be found");
            }
            Console.WriteLine("Scanning Config.txt");
            List <string> Folders    = new List <string>();
            List <string> Projects   = new List <string>();
            string        FOLDERPATH = "";

            string[] LINES      = File.ReadAllLines("C:/dc_mmo/src/SrcXML/Config.txt");
            string   FOLDERSREX = "";

            foreach (string LINE in LINES)
            {
                if (LINE.Split('=').Length > 1)
                {
                    string KEY   = LINE.Split('=')[0];
                    string VALUE = LINE.Split('=')[1];
                    foreach (string sVALUE in VALUE.Split(';'))
                    {
                        switch (KEY.Trim().ToLower())
                        {
                        case "folderpath":
                        {
                            FOLDERPATH = sVALUE.Trim();
                            Console.WriteLine("Folder path: " + sVALUE.Trim());
                        }
                        break;

                        case "folders":
                        {
                            FOLDERSREX = sVALUE.Trim().Replace(';', '|');
                            Folders.Add(sVALUE.Trim());
                            Console.WriteLine("Folder added: " + sVALUE.Trim());
                        }
                        break;

                        case "projects":
                        {
                            Projects.Add(sVALUE.Trim());
                            Console.WriteLine("Project added: " + sVALUE.Trim());
                        }
                        break;
                        }
                    }
                }
            }
            List <string> CFiles = new List <string>();
            List <string> HFiles = new List <string>();
            List <string> NFiles = new List <string>();

            foreach (string FOLDER in Folders)
            {
                Console.WriteLine("START Scanning folder: " + FOLDERPATH + FOLDER);
                string[] FILES = GetAllFiles(FOLDERPATH + FOLDER);
                foreach (string FILE in FILES)
                {
                    string EXTENSION = Path.GetExtension(FILE).ToLower();
                    string FILERES   = FILE.Replace(FOLDERPATH, "");
                    switch (EXTENSION)
                    {
                    case ".c":
                    case ".cpp":
                    {
                        CFiles.Add(FILERES);
                    }
                    break;

                    case ".hpp":
                    {
                        HFiles.Add(FILERES);
                    }
                    break;

                    default:
                    {
                        NFiles.Add(FILERES);
                    }
                    break;
                    }
                }
                Console.WriteLine("END Scanning folder: " + FOLDERPATH + FOLDER);
            }
            Console.WriteLine("Found '" + CFiles.Count + "' C/C++ files");
            Console.WriteLine("Found '" + HFiles.Count + "' Header files");
            Console.WriteLine("Found '" + NFiles.Count + "' Unknown files");

            StringBuilder CPPFiles = new StringBuilder();

            {
                CPPFiles.AppendLine("\t<ItemGroup>");
                foreach (string FILE in CFiles)
                {
                    CPPFiles.AppendLine("\t\t<ClInclude Include=\"..\\" + FILE + "\" />");
                }
                CPPFiles.AppendLine("\t</ItemGroup>");
            }
            Console.WriteLine("Built XML for C/C++ files");

            StringBuilder HEADFiles = new StringBuilder();

            {
                HEADFiles.AppendLine("\t<ItemGroup>");
                foreach (string FILE in HFiles)
                {
                    HEADFiles.AppendLine("\t\t<ClInclude Include=\"..\\" + FILE + "\" />");
                }
                HEADFiles.AppendLine("\t</ItemGroup>");
            }
            Console.WriteLine("Built XML for Header files");

            StringBuilder UFiles = new StringBuilder();

            {
                UFiles.AppendLine("\t<ItemGroup>");
                foreach (string FILE in NFiles)
                {
                    UFiles.AppendLine("\t\t<ClInclude Include=\"..\\" + FILE + "\" />");
                }
                UFiles.AppendLine("\t</ItemGroup>");
            }
            Console.WriteLine("Built XML for Unknown files");

            Console.WriteLine("Building filter stuff");
            StringBuilder FilterFile = new StringBuilder();

            FilterFile.AppendLine("<ItemGroup>");
            Dictionary <string, bool> FILTERDIRS = new Dictionary <string, bool>();
            List <string>             AllFiles   = new List <string>();

            foreach (string FILE in CFiles)
            {
                AllFiles.Add(FILE);
            }
            foreach (string FILE in HFiles)
            {
                AllFiles.Add(FILE);
            }
            foreach (string FILE in NFiles)
            {
                AllFiles.Add(FILE);
            }
            foreach (string FILE in AllFiles)
            {
                string FILEPATH = FILE.Replace("\\" + Path.GetFileName(FILE), "");
                if (!FILTERDIRS.ContainsKey(FILEPATH))
                {
                    FILTERDIRS.Add(FILEPATH, true);
                }
                string[] SPLITPATH = FILEPATH.Split('\\');
                string   SDFULL    = "";
                foreach (string SD in SPLITPATH)
                {
                    SDFULL += "\\" + SD;
                    while (SDFULL[0] == '\\')
                    {
                        SDFULL = SDFULL.Substring(1);
                    }
                    if (!FILTERDIRS.ContainsKey(SDFULL))
                    {
                        FILTERDIRS.Add(SDFULL, true);
                    }
                }
                FilterFile.AppendLine("\t<ClInclude Include=\"..\\" + FILE + "\">");
                FilterFile.AppendLine("\t\t<Filter>" + FILEPATH + "</Filter>");
                FilterFile.AppendLine("\t</ClInclude>");
            }
            FilterFile.AppendLine("</ItemGroup>");

            StringBuilder FilterFileHeader = new StringBuilder();

            FilterFileHeader.AppendLine("\t<ItemGroup>");
            foreach (KeyValuePair <string, bool> FILTERX in FILTERDIRS)
            {
                FilterFileHeader.AppendLine("\t\t<Filter Include=\"" + FILTERX.Key + "\">");
                FilterFileHeader.AppendLine("\t\t</Filter>");
            }
            FilterFileHeader.AppendLine("\t</ItemGroup>");
            Console.WriteLine("Finished building filter stuff");

            foreach (string SD in Projects)
            {
                string SDFilters = SD + ".filters";
                // Project
                {
                    Console.WriteLine("Replacing project stuff from: " + SD);
                    string FinalXML = CPPFiles.ToString() + "\n" + HEADFiles.ToString() + "\n" + UFiles.ToString() + "\n";
                    foreach (string FSD in Folders)
                    {
                        string ProjectXMLFile = File.ReadAllText(SD);
                        string ResultString   = null;
                        try
                        {
                            ResultString = Regex.Replace(
                                ProjectXMLFile,
                                "<ItemGroup>(?!.*stdafx).*?(" + FOLDERSREX + ").*?</ItemGroup>",
                                new MatchEvaluator(ComputeReplacement),
                                RegexOptions.Singleline | RegexOptions.IgnoreCase
                                );
                            ProjectXMLFile = ResultString;
                        }
                        catch { }
                        ProjectXMLFile = ProjectXMLFile.Replace(@"<Import Project=""$(VCTargetsPath)\Microsoft.Cpp.targets"" />", FinalXML + "\n" + @"<Import Project=""$(VCTargetsPath)\Microsoft.Cpp.targets"" />");
                        File.WriteAllText(SD, ProjectXMLFile);
                    }
                }
                // Filter
                {
                    Console.WriteLine("Replacing filter file: " + SDFilters);
                    string FinalFilterXML = FilterFileHeader.ToString() + "\n" + FilterFile.ToString() + "\n";

                    string ProjectXMLFile = File.ReadAllText(SDFilters);
                    string ResultString   = null;
                    try
                    {
                        ResultString = Regex.Replace(
                            ProjectXMLFile,
                            "<ItemGroup>(?!.*stdafx).*?(" + FOLDERSREX + ").*?</ItemGroup>",
                            new MatchEvaluator(ComputeReplacement),
                            RegexOptions.Singleline | RegexOptions.IgnoreCase
                            );
                        ProjectXMLFile = ResultString;
                    }
                    catch { }
                    ProjectXMLFile = ProjectXMLFile.Replace("</Project>", FinalFilterXML + "\n</Project>");
                    File.WriteAllText(SDFilters, ProjectXMLFile);
                }
            }
            Console.WriteLine("All done... Press any key to continue....");
            Console.ReadKey();
        }