public Metadata Transform(Metadata metadata) { if (metadata.Download != null) { var json = metadata.Json(); var file = _http.DownloadPackage(metadata.Download, metadata.Identifier); var internalJson = _moduleService.GetInternalCkan(file); if (internalJson != null) { Log.InfoFormat("Executing Internal CKAN transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); foreach (var property in internalJson.Properties()) { json.SafeAdd(property.Name, property.Value); } json["spec_version"] = Version.Max(metadata.SpecVersion, new Metadata(internalJson).SpecVersion) .ToSpecVersionJson(); Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); } return(new Metadata(json)); } return(metadata); }
public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts) { if (metadata.Download != null) { var json = metadata.Json(); string file = _http.DownloadPackage(metadata.Download, metadata.Identifier, metadata.RemoteTimestamp); var internalJson = _moduleService.GetInternalCkan(file); if (internalJson != null) { Log.InfoFormat("Executing internal CKAN transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); foreach (var property in internalJson.Properties()) { json.SafeAdd(property.Name, property.Value); } json["spec_version"] = ModuleVersion.Max(metadata.SpecVersion, new Metadata(internalJson).SpecVersion) .ToSpecVersionJson(); Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); } yield return(new Metadata(json)); } else { yield return(metadata); } }
public IEnumerable <Metadata> Transform(Metadata metadata) { if (metadata.Download != null) { var json = metadata.Json(); Log.InfoFormat("Executing Download attribute transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); string file = _http.DownloadPackage(metadata.Download, metadata.Identifier, metadata.RemoteTimestamp); if (file != null) { json["download_size"] = _fileService.GetSizeBytes(file); json["download_hash"] = new JObject(); var download_hashJson = (JObject)json["download_hash"]; download_hashJson.SafeAdd("sha1", _fileService.GetFileHashSha1(file)); download_hashJson.SafeAdd("sha256", _fileService.GetFileHashSha256(file)); json["download_content_type"] = _fileService.GetMimetype(file); } Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); yield return(new Metadata(json)); } else { yield return(metadata); } }
public void Validate(Metadata metadata) { var mod = CkanModule.FromJson(metadata.Json().ToString()); var file = _http.DownloadPackage(metadata.Download, metadata.Identifier, metadata.RemoteTimestamp); // Make sure this would actually generate an install. if (!_moduleService.HasInstallableFiles(mod, file)) { throw new Kraken(string.Format( "Module contains no files matching: {0}", mod.DescribeInstallStanzas() )); } // Make sure no paths include GameData other than at the start var gamedatas = _moduleService.FileDestinations(mod, file) .Where(p => p.StartsWith("GameData") && p.LastIndexOf("/GameData/") > 0) .ToList(); if (gamedatas.Any()) { var badPaths = string.Join("\r\n", gamedatas); throw new Kraken($"GameData directory found within GameData:\r\n{badPaths}"); } }
public void Validate(Metadata metadata) { var mod = CkanModule.FromJson(metadata.Json().ToString()); var file = _http.DownloadPackage(metadata.Download, metadata.Identifier); // Make sure this would actually generate an install. if (!_moduleService.HasInstallableFiles(mod, file)) { throw new Kraken("Module contains no files to install."); } }
public void Validate(Metadata metadata) { var mod = CkanModule.FromJson(metadata.Json().ToString()); var file = _http.DownloadPackage(metadata.Download, metadata.Identifier, metadata.RemoteTimestamp); // Make sure this would actually generate an install. if (!_moduleService.HasInstallableFiles(mod, file)) { throw new Kraken(string.Format( "Module contains no files matching: {0}", mod.DescribeInstallStanzas() )); } }
public Metadata Transform(Metadata metadata) { if (metadata.Download != null) { var json = metadata.Json(); Log.InfoFormat("Executing Download Size transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); var file = _http.DownloadPackage(metadata.Download, metadata.Identifier); if (file != null) { json["download_size"] = _fileService.GetSizeBytes(file); } Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); return new Metadata(json); } return metadata; }
public Metadata Transform(Metadata metadata) { if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc") { var json = metadata.Json(); Log.InfoFormat("Executing Interal AVC transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); var noVersion = metadata.Version == null; if (noVersion) { json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw } var mod = CkanModule.FromJson(json.ToString()); if (noVersion) { json.Remove("version"); } var file = _http.DownloadPackage(metadata.Download, metadata.Identifier); var avc = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id); if (avc != null) { Log.Info("Found internal AVC version file"); // Get the minimum and maximum KSP versions that already exist in the metadata. // Use specific KSP version if min/max don't exist. var existingKspMinStr = (string)json["ksp_version_min"] ?? (string)json["ksp_version"]; var existingKspMaxStr = (string)json["ksp_version_max"] ?? (string)json["ksp_version"]; var existingKspMin = existingKspMinStr == null ? null : new KSPVersion(existingKspMinStr); var existingKspMax = existingKspMaxStr == null ? null : new KSPVersion(existingKspMaxStr); // Get the minimum and maximum KSP versions that are in the AVC file. // Use specific KSP version if min/max don't exist. var avcKspMin = avc.ksp_version_min ?? avc.ksp_version; var avcKspMax = avc.ksp_version_max ?? avc.ksp_version; // Now calculate the minimum and maximum KSP versions between both the existing metadata and the // AVC file. var kspMins = new List <KSPVersion>(); var kspMaxes = new List <KSPVersion>(); if (existingKspMin != null) { kspMins.Add(existingKspMin); } if (avcKspMin != null) { kspMins.Add(avcKspMin); } if (existingKspMax != null) { kspMaxes.Add(existingKspMax); } if (avcKspMax != null) { kspMaxes.Add(avcKspMax); } var kspMin = kspMins.Any() ? kspMins.Min() : null; var kspMax = kspMaxes.Any() ? kspMaxes.Max() : null; if (kspMin != null || kspMax != null) { // If we have either a minimum or maximum KSP version, remove all existing KSP version // information from the metadata. json.Remove("ksp_version"); json.Remove("ksp_version_min"); json.Remove("ksp_version_max"); if (kspMin != null && kspMax != null) { // If we have both a minimum and maximum KSP version... if (kspMin.CompareTo(kspMax) == 0) { // ...and they are equal, then just set ksp_version json["ksp_version"] = kspMin.ToString(); } else { // ...otherwise set both ksp_version_min and ksp_version_max json["ksp_version_min"] = kspMin.ToString(); json["ksp_version_max"] = kspMax.ToString(); } } else { // If we have only one or the other then set which ever is applicable if (kspMin != null) { json["ksp_version_min"] = kspMin.ToString(); } if (kspMax != null) { json["ksp_version_max"] = kspMax.ToString(); } } } if (avc.version != null) { // In practice, the version specified in .version files tends to be unreliable, with authors // forgetting to update it when new versions are released. Therefore if we have a version // specified from another source such as SpaceDock or a GitHub tag, don't overwrite it. json.SafeAdd("version", avc.version.ToString()); } // It's cool if we don't have version info at all, it's optional in the AVC spec. Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); } return(new Metadata(json)); } else { return(metadata); } }
public IEnumerable <Metadata> Transform(Metadata metadata) { if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc") { var json = metadata.Json(); Log.InfoFormat("Executing internal AVC transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); var noVersion = metadata.Version == null; if (noVersion) { json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw } var mod = CkanModule.FromJson(json.ToString()); if (noVersion) { json.Remove("version"); } var file = _http.DownloadPackage(metadata.Download, metadata.Identifier, metadata.RemoteTimestamp); var avc = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id); if (avc != null) { Log.Info("Found internal AVC version file"); var remoteUri = GetRemoteAvcUri(avc); if (remoteUri != null) { try { var remoteJson = Net.DownloadText(remoteUri); var remoteAvc = JsonConvert.DeserializeObject <AvcVersion>(remoteJson); if (avc.version.CompareTo(remoteAvc.version) == 0) { // Local AVC and Remote AVC describe the same version, prefer Log.Info("Remote AVC version file describes same version as local AVC version file, using it preferrentially."); avc = remoteAvc; } } catch (Exception e) { Log.InfoFormat("An error occured fetching the remote AVC version file, ignoring: {0}", e.Message); Log.Debug(e); } } ApplyVersions(json, avc); // It's cool if we don't have version info at all, it's optional in the AVC spec. Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); } yield return(new Metadata(json)); } else { yield return(metadata); } }
public Metadata Transform(Metadata metadata) { if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc") { var json = metadata.Json(); Log.InfoFormat("Executing Interal AVC transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); var noVersion = metadata.Version == null; if (noVersion) { json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw } var mod = CkanModule.FromJson(json.ToString()); if (noVersion) { json.Remove("version"); } var file = _http.DownloadPackage(metadata.Download, metadata.Identifier); var avc = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id); if (avc != null) { Log.Info("Found internal AVC version file"); // The CKAN spec states that only a KSP version can be supplied, *or* a min/max can // be provided. Since min/max are more descriptive, we check and use them first. if (avc.ksp_version_min != null || avc.ksp_version_max != null) { json.Remove("ksp_version"); if (avc.ksp_version_min != null) { json["ksp_version_min"] = avc.ksp_version_min.ToString(); } if (avc.ksp_version_max != null) { json["ksp_version_max"] = avc.ksp_version_max.ToString(); } } else if (avc.ksp_version != null) { json["ksp_version"] = avc.ksp_version.ToString(); } if (avc.version != null) { json["version"] = avc.version.ToString(); } // It's cool if we don't have version info at all, it's optional in the AVC spec. Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); } return(new Metadata(json)); } else { return(metadata); } }
public Metadata Transform(Metadata metadata) { if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc") { var json = metadata.Json(); Log.InfoFormat("Executing internal AVC transformation with {0}", metadata.Kref); Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json); var noVersion = metadata.Version == null; if (noVersion) { json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw } var mod = CkanModule.FromJson(json.ToString()); if (noVersion) { json.Remove("version"); } var file = _http.DownloadPackage(metadata.Download, metadata.Identifier, metadata.RemoteTimestamp); var avc = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id); if (avc != null) { Log.Info("Found internal AVC version file"); var remoteUri = GetRemoteAvcUri(avc); if (remoteUri != null) { try { var remoteJson = Net.DownloadText(remoteUri); var remoteAvc = JsonConvert.DeserializeObject <AvcVersion>(remoteJson); if (avc.version.CompareTo(remoteAvc.version) == 0) { // Local AVC and Remote AVC describe the same version, prefer Log.Info("Remote AVC version file describes same version as local AVC version file, using it preferrentially."); avc = remoteAvc; } } catch (Exception e) { Log.InfoFormat("An error occured fetching the remote AVC version file, ignoring: {0}", e.Message); Log.Debug(e); } } // Get the minimum and maximum KSP versions that already exist in the metadata. // Use specific KSP version if min/max don't exist. var existingKspMinStr = (string)json["ksp_version_min"] ?? (string)json["ksp_version"]; var existingKspMaxStr = (string)json["ksp_version_max"] ?? (string)json["ksp_version"]; var existingKspMin = existingKspMinStr == null ? null : KspVersion.Parse(existingKspMinStr); var existingKspMax = existingKspMaxStr == null ? null : KspVersion.Parse(existingKspMaxStr); // Get the minimum and maximum KSP versions that are in the AVC file. // Use specific KSP version if min/max don't exist. var avcKspMin = avc.ksp_version_min ?? avc.ksp_version; var avcKspMax = avc.ksp_version_max ?? avc.ksp_version; // Now calculate the minimum and maximum KSP versions between both the existing metadata and the // AVC file. var kspMins = new List <KspVersion>(); var kspMaxes = new List <KspVersion>(); if (existingKspMin != null) { kspMins.Add(existingKspMin); } if (avcKspMin != null) { kspMins.Add(avcKspMin); } if (existingKspMax != null) { kspMaxes.Add(existingKspMax); } if (avcKspMax != null) { kspMaxes.Add(avcKspMax); } var kspMin = kspMins.Any() ? kspMins.Min() : null; var kspMax = kspMaxes.Any() ? kspMaxes.Max() : null; if (kspMin != null || kspMax != null) { // If we have either a minimum or maximum KSP version, remove all existing KSP version // information from the metadata. json.Remove("ksp_version"); json.Remove("ksp_version_min"); json.Remove("ksp_version_max"); if (kspMin != null && kspMax != null) { // If we have both a minimum and maximum KSP version... if (kspMin.CompareTo(kspMax) == 0) { // ...and they are equal, then just set ksp_version json["ksp_version"] = kspMin.ToString(); } else { // ...otherwise set both ksp_version_min and ksp_version_max json["ksp_version_min"] = kspMin.ToString(); json["ksp_version_max"] = kspMax.ToString(); } } else { // If we have only one or the other then set which ever is applicable if (kspMin != null) { json["ksp_version_min"] = kspMin.ToString(); } if (kspMax != null) { json["ksp_version_max"] = kspMax.ToString(); } } } if (avc.version != null) { // In practice, the version specified in .version files tends to be unreliable, with authors // forgetting to update it when new versions are released. Therefore if we have a version // specified from another source such as SpaceDock, curse or a GitHub tag, don't overwrite it // unless x_netkan_trust_version_file is true. if ((bool?)json["x_netkan_trust_version_file"] ?? false) { json.Remove("version"); } json.SafeAdd("version", avc.version.ToString()); } // It's cool if we don't have version info at all, it's optional in the AVC spec. Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); } return(new Metadata(json)); } else { return(metadata); } }