Пример #1
0
        public void LoadEntries(Switch device = null)
        {
            lock (_lock)
            {
                _contentDictionary = new SortedDictionary <(ulong, NcaContentType), string>();
                _locationEntries   = new Dictionary <StorageId, LinkedList <LocationEntry> >();

                foreach (StorageId storageId in Enum.GetValues(typeof(StorageId)))
                {
                    string contentDirectory    = null;
                    string contentPathString   = null;
                    string registeredDirectory = null;

                    try
                    {
                        contentPathString   = LocationHelper.GetContentRoot(storageId);
                        contentDirectory    = LocationHelper.GetRealPath(_virtualFileSystem, contentPathString);
                        registeredDirectory = Path.Combine(contentDirectory, "registered");
                    }
                    catch (NotSupportedException)
                    {
                        continue;
                    }

                    Directory.CreateDirectory(registeredDirectory);

                    LinkedList <LocationEntry> locationList = new LinkedList <LocationEntry>();

                    void AddEntry(LocationEntry entry)
                    {
                        locationList.AddLast(entry);
                    }

                    foreach (string directoryPath in Directory.EnumerateDirectories(registeredDirectory))
                    {
                        if (Directory.GetFiles(directoryPath).Length > 0)
                        {
                            string ncaName = new DirectoryInfo(directoryPath).Name.Replace(".nca", string.Empty);

                            using (FileStream ncaFile = File.OpenRead(Directory.GetFiles(directoryPath)[0]))
                            {
                                Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());

                                string switchPath = contentPathString + ":/" + ncaFile.Name.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar);

                                // Change path format to switch's
                                switchPath = switchPath.Replace('\\', '/');

                                LocationEntry entry = new LocationEntry(switchPath,
                                                                        0,
                                                                        (long)nca.Header.TitleId,
                                                                        nca.Header.ContentType);

                                AddEntry(entry);

                                _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
                            }
                        }
                    }

                    foreach (string filePath in Directory.EnumerateFiles(contentDirectory))
                    {
                        if (Path.GetExtension(filePath) == ".nca")
                        {
                            string ncaName = Path.GetFileNameWithoutExtension(filePath);

                            using (FileStream ncaFile = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                            {
                                Nca nca = new Nca(_virtualFileSystem.KeySet, ncaFile.AsStorage());

                                string switchPath = contentPathString + ":/" + filePath.Replace(contentDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar);

                                // Change path format to switch's
                                switchPath = switchPath.Replace('\\', '/');

                                LocationEntry entry = new LocationEntry(switchPath,
                                                                        0,
                                                                        (long)nca.Header.TitleId,
                                                                        nca.Header.ContentType);

                                AddEntry(entry);

                                _contentDictionary.Add((nca.Header.TitleId, nca.Header.ContentType), ncaName);
                            }
                        }
                    }

                    if (_locationEntries.ContainsKey(storageId) && _locationEntries[storageId]?.Count == 0)
                    {
                        _locationEntries.Remove(storageId);
                    }

                    if (!_locationEntries.ContainsKey(storageId))
                    {
                        _locationEntries.Add(storageId, locationList);
                    }
                }

                if (device != null)
                {
                    TimeManager.Instance.InitializeTimeZone(device);
                    device.System.Font.Initialize(this);
                }
            }
        }