/// <inheritdoc /> public IEnumerable <string> GetResourceNamesByType(ResourceType resourceType) { HashSet <string> resources = new HashSet <string>(); string resourcePrefix = GetResourceTypePrefix(resourceType); if (string.IsNullOrWhiteSpace(resourcePrefix) || resourceType == ResourceType.Unknown) { throw new ArgumentException(nameof(resourceType)); } IEnumerable <string> resourcePacks = GetResourcePacks(); foreach (var resourcePack in resourcePacks) { SerializedManifestData manifest = _dataService.RetrieveManifest(_pathService.ResourcePackDirectory + resourcePack + ".rpk"); var names = manifest.Where(x => x.Key.StartsWith(resourcePrefix)).Select(x => x.Key.Remove(0, resourcePrefix.Length)); foreach (var name in names) { if (!resources.Contains(name)) { resources.Add(name); } } } return(resources); }
/// <inheritdoc /> public ResourceItemData GetResourceByName(ResourceType resourceType, string resourceName) { string fullResourceName = GetResourceTypePrefix(resourceType) + resourceName; IEnumerable <string> resourcePacks = GetResourcePacks(); foreach (var pack in resourcePacks) { string path = _pathService.ResourcePackDirectory + pack + ".rpk"; SerializedManifestData manifest = _dataService.RetrieveManifest(path); if (manifest.ContainsKey(fullResourceName)) { return(_dataService.RetrieveSingleFile <ResourceItemData>(path, fullResourceName)); } } return(null); }
public void PackageFile <T>(T data, Stream stream) { SerializedManifestData manifest = null; if (stream.Length > 0) { try { long currentPosition = stream.Position; stream.Position = 0; manifest = Serializer.DeserializeWithLengthPrefix <SerializedManifestData>(stream, PrefixStyle.Base128, 0); if (manifest == null) { throw new Exception($"An instance of {nameof(SerializedManifestData)} must be the first packaged file."); } stream.Position = currentPosition; } catch (Exception ex) { throw new Exception($"An instance of {nameof(SerializedManifestData)} must be the first packaged file.", ex); } } else { if (data.GetType() != typeof(SerializedManifestData)) { throw new Exception($"An instance of {nameof(SerializedManifestData)} must be the first packaged file."); } } int fieldNumber = 0; if (manifest != null) { fieldNumber = 1; } Serializer.SerializeWithLengthPrefix(stream, data, PrefixStyle.Base128, fieldNumber); }
/// <inheritdoc /> public void SaveResourcePack(IEnumerable <ResourceItemData> resources, string resourcePackName) { var resourceList = resources.ToList(); string backupFilePath = resourcePackName + ".rpbk"; resourcePackName = resourcePackName + ".rpk"; if (File.Exists(_pathService.ResourcePackDirectory + resourcePackName)) { File.Move(_pathService.ResourcePackDirectory + resourcePackName, _pathService.ResourcePackDirectory + backupFilePath); } else { backupFilePath = null; } try { using (var stream = File.Create(_pathService.ResourcePackDirectory + resourcePackName)) { SerializedManifestData manifest = new SerializedManifestData(); for (int index = 0; index < resourceList.Count(); index++) { var resource = resourceList[index]; manifest.Add(GetKeyOfResourceItem(resource), index); } _dataService.PackageFile(manifest, stream); foreach (var resource in resourceList) { // Load data - either from the file system or from the existing rpk file. if (string.IsNullOrWhiteSpace(resource.FilePath)) { var backupEntry = _dataService.RetrieveSingleFile <ResourceItemData>(_pathService.ResourcePackDirectory + backupFilePath, GetKeyOfResourceItem(resource)); resource.Data = backupEntry.Data; } else { resource.Data = File.ReadAllBytes(resource.FilePath); } resource.FilePath = null; _dataService.PackageFile(resource, stream); resource.Data = null; } } } catch (Exception ex) { // Restore backup, if any. if (backupFilePath != null) { if (File.Exists(_pathService.ResourcePackDirectory + resourcePackName)) { File.Delete(_pathService.ResourcePackDirectory + resourcePackName); } File.Move(_pathService.ResourcePackDirectory + backupFilePath, _pathService.ResourcePackDirectory + resourcePackName); } throw new Exception("Unable to save resource pack.", ex); } if (File.Exists(_pathService.ResourcePackDirectory + backupFilePath)) { File.Delete(_pathService.ResourcePackDirectory + backupFilePath); } }