示例#1
0
        public void Read(Content content, string configPath)
        {
            var config  = JObject.Parse(File.ReadAllText(configPath, Encoding.UTF8));
            var rootDir = Path.GetDirectoryName(configPath) + "/";

            var contentJson = (JObject)config["content"];

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console2.WriteLine("Reading NPL config.");
            Console.ForegroundColor = ConsoleColor.Gray;


            Console2.WriteLine();
            ParseReferences(config, content);
            Console2.WriteLine();


            foreach (var item in contentJson)
            {
                // Read section.
                string sectionName = item.Key;
                var    section     = (JObject)item.Value;

                // Read item.
                string path;
                try
                {
                    path = section["path"].ToString();
                }
                catch
                {
                    Console2.WriteLine($"Key 'path' doesn't exist in  {sectionName}!");
                    throw new Exception($"Key 'path' doesn't exist in  {sectionName}!");
                }

                string linkTo = null;
                linkTo = section["linkTo"]?.ToString();


                Console.ForegroundColor = ConsoleColor.Magenta;
                Console2.WriteLine("Reading content for: " + path);
                Console.ForegroundColor = ConsoleColor.Gray;

                var fileName = Path.GetFileName(path);
                var filePath = Path.GetDirectoryName(path);
                var files    = new string[] { };

                try
                {
                    var searchOpt = SearchOption.TopDirectoryOnly;
                    if (section.ContainsKey("recursive"))
                    {
                        searchOpt = (section["recursive"].ToString().ToLower() == "true") ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
                    }

                    string searchPath;
                    if (linkTo != null && Path.IsPathRooted(filePath))
                    {
                        searchPath = filePath;
                    }
                    else
                    {
                        searchPath = Path.Combine(rootDir, filePath);
                    }

                    files = Directory.GetFiles(searchPath, fileName, searchOpt);
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console2.WriteLine($"    Error reading files from {rootDir}{filePath}: ");
                    Console2.WriteLine("    " + e.Message);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                foreach (var file in files)
                {
                    string name;
                    Item   newItem;

                    if (linkTo != null && Path.IsPathRooted(filePath))
                    {
                        name = file;

                        string relativePathToFileInSearch = file.Remove(0, filePath.Length + 1).Replace('\\', '/');

                        string relativePath = Path.Combine(linkTo, relativePathToFileInSearch).Replace('\\', '/');

                        newItem = new Item(name + ";" + relativePath);
                    }
                    else
                    {
                        name    = file.Remove(0, rootDir.Length).Replace('\\', '/');
                        newItem = new Item(name);
                    }



                    Console2.WriteLine("    Reading " + name);

                    foreach (var sect in section)
                    {
                        if (sect.Key == "path")
                        {                         // path - already get - ignore
                            continue;
                        }
                        else if (sect.Key == "linkTo")
                        {
                            // skip linkTo: it's not a MGCB command and the file concatenation has been
                            // added manually
                            continue;
                        }
                        if (sect.Key == "processorParam")
                        {                         // read processor's parameters
                            JObject processorParam = section["processorParam"] as JObject;
                            foreach (var pp in processorParam)
                            {
                                newItem.Add("processorParam", $"{pp.Key}={pp.Value}");
                            }
                        }
                        else
                        {
                            newItem.Add(sect.Key, sect.Value);
                        }
                    }

                    content.AddContentItem(newItem);
                }
            }

            Console2.WriteLine();
            Console2.WriteLine("Finished reading NPL config!");
            Console2.WriteLine();
        }
示例#2
0
        /// <summary>
        /// Checks if content files exist and checks watched files.
        /// </summary>
        public void CheckIntegrity(string rootPath)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console2.WriteLine("Checking integrity of the final config. Hold on tight!");
            Console2.WriteLine();
            Console.ForegroundColor = ConsoleColor.Gray;

            var checkedItems = new Dictionary <string, Item>();

            foreach (Item item in _contentItems.Values)
            {
                string fullFileName;

                // process rooted files
                if (Path.IsPathRooted(item.Path))
                {
                    fullFileName = item.Path;
                }
                else
                {
                    fullFileName = Path.Combine(rootPath, item.Path);
                }

                // remove link part if it's a linked file
                int separatorIndex = fullFileName.IndexOf(';');
                if (separatorIndex != -1)
                {
                    fullFileName = fullFileName.Substring(0, separatorIndex);
                }

                Console2.WriteLine("Checking " + fullFileName);

                // Don't include if the file doesn't exist.
                if (File.Exists(fullFileName))
                {
                    DateTime itemLastModified = File.GetLastWriteTime(fullFileName);

                    var relativeItemPath = Path.GetDirectoryName(fullFileName);

                    // Watched files are files which aren't tracked by the content pipeline.
                    // But they are tracked by us! We look which files were recently modified
                    // and, if their modification date is more recent than the date of tracked file,
                    // we "modify" the tracked file by changing its Last Modified date. This way
                    // Pipeline thinks the file has been updated and rebuilds it.
                    foreach (var checkWildcard in item.Watch)
                    {
                        Console2.WriteLine("Checking watch for " + checkWildcard);

                        var fileName = Path.GetFileName(checkWildcard);
                        var filePath = Path.GetDirectoryName(checkWildcard);

                        string[] files;

                        Console2.WriteLine("Checking wildcars for: " + Path.Combine(relativeItemPath, filePath));
                        try
                        {
                            files = Directory.GetFiles(Path.Combine(relativeItemPath, filePath), fileName, SearchOption.AllDirectories);
                        }
                        catch
                        {
                            Console2.WriteLine(checkWildcard + " wasn't found. Skipping.");
                            continue;
                        }

                        foreach (var file in files)
                        {
                            Console2.WriteLine("Checking " + file);
                            DateTime fileLastModified = File.GetLastWriteTime(file);
                            DateTime fileCreationTime = File.GetCreationTime(file);

                            if (itemLastModified < fileLastModified || itemLastModified < fileCreationTime)
                            {
                                Console2.WriteLine("Modifying: " + file);
                                File.SetLastWriteTime(Path.Combine(rootPath, item.Path), DateTime.Now);
                                break;
                            }
                        }
                    }

                    checkedItems.Add(item.Path, item);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console2.WriteLine(item.Path + " doesn't exist anymore. Removing it from the config.");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }
            _contentItems = checkedItems;

            Console2.WriteLine();

            var checkedReferences = new HashSet <string>();

            foreach (var reference in _references)
            {
                Console2.WriteLine("Checking reference: " + Path.Combine(rootPath, reference));
                if (File.Exists(Path.Combine(rootPath, reference)))
                {
                    checkedReferences.Add(reference);
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console2.WriteLine(reference + " wasn't found! Deleting it from the config.");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }
            _references = checkedReferences;
        }
示例#3
0
        public void Read(Content content, string MGCBConfigPath)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console2.WriteLine("Reading MGCB config " + MGCBConfigPath);
            Console2.WriteLine();
            Console.ForegroundColor = ConsoleColor.Gray;

            var configPath = MGCBConfigPath;
            var rootDir    = Path.GetDirectoryName(MGCBConfigPath);

            string line;
            var    collectionState = CollectionStates.Settings;
            Item   item            = null;

            using (var file = new StreamReader(MGCBConfigPath))
            {
                while ((line = file.ReadLine()) != null)
                {
                    // Reading settings.
                    if (collectionState == CollectionStates.Settings)
                    {
                        if (line.StartsWith(ContentStructure.ReferenceKeyword))
                        {
                            collectionState = CollectionStates.References;
                            Console2.WriteLine();
                        }
                        else
                        {
                            if (line.StartsWith(ContentStructure.ContentBeginKeyword))
                            {
                                collectionState = CollectionStates.Content;
                                Console2.WriteLine();
                            }
                            else
                            {
                                if (line.StartsWith(ContentStructure.KeywordStartingChar))
                                {
                                    Console2.WriteLine("Reading setting: " + line);
                                    content.AddGlobalSetting(line);
                                }
                            }
                        }
                    }
                    // Reading settings.


                    // Reading references.
                    if (collectionState == CollectionStates.References)
                    {
                        if (line.StartsWith(ContentStructure.ContentBeginKeyword))
                        {
                            collectionState = CollectionStates.Content;
                            Console2.WriteLine();
                        }
                        else
                        {
                            if (line.StartsWith(ContentStructure.ReferenceKeyword))
                            {
                                var reference = line.Substring(ContentStructure.ReferenceKeyword.Length);
                                content.AddReference(reference);

                                Console2.WriteLine("Reading reference: " + reference);
                            }
                        }
                    }
                    // Reading references.

                    // Reading content.
                    if (collectionState == CollectionStates.Content)
                    {
                        if (line.StartsWith(ContentStructure.ContentBeginKeyword))
                        {
                            item = new Item(line.Substring(ContentStructure.ContentBeginKeyword.Length + 1));

                            content.AddContentItem(item);
                            Console2.WriteLine("Reading content:" + item.Path);
                        }
                        else
                        {
                            item.Parameters.Add(line);
                        }
                    }
                    // Reading content.
                }
            }

            Console2.WriteLine("Finished reading MGCB config! Got " + content.ContentItemsCount + " items.");
        }
示例#4
0
        static void Run(string configPath)
        {
            // Read config file name from the input parameter.

            string MGCBConfigPath, NPLConfigPath;

            if (configPath.EndsWith(".mgcb"))
            {
                MGCBConfigPath = configPath;
                NPLConfigPath  = Path.ChangeExtension(configPath, ".npl");
            }
            else
            {
                NPLConfigPath  = configPath;
                MGCBConfigPath = Path.ChangeExtension(configPath, ".mgcb");
            }

            // Check if configuration file exists.
            if (!File.Exists(NPLConfigPath) || !File.Exists(NPLConfigPath))
            {
                Console2.WriteLine(NPLConfigPath + " not found!");
                PrintHelp();
                return;
            }

            var content = new Content();

            // Create MGCB object to read mgcb file.
            var MGCBReader = new MGCBConfigReader();

            MGCBReader.Read(content, MGCBConfigPath);

            Console2.WriteLine();
            Console2.WriteLine("-------------------------------------");
            Console2.WriteLine();

            // Create ContentProcessor object to read config file and update content
            // content will be overwrited from config file.
            var NPLReader = new NPLConfigReader();

            NPLReader.Read(content, NPLConfigPath);

            Console2.WriteLine("-------------------------------------");
            Console2.WriteLine();

            // Check all rules in content object and update timestamp of files if required.
            content.CheckIntegrity(Path.GetDirectoryName(MGCBConfigPath));

            // Saving MGCB file.

            Console2.WriteLine();
            Console2.WriteLine("-------------------------------------");
            Console2.WriteLine();

            Console2.WriteLine("Saving new config as " + MGCBConfigPath);
            Console2.WriteLine();

            File.WriteAllText(MGCBConfigPath, content.Build());

            Console.ForegroundColor = ConsoleColor.Green;
            Console2.WriteLine("Done! \\^u^/");
            Console.ForegroundColor = ConsoleColor.Gray;
        }