Beispiel #1
0
        public static void ApplyVersions(JObject json, AvcVersion avc)
        {
            // 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.
            // http://ksp.cybutek.net/kspavc/Documents/README.htm
            // KSP-AVC allows (requires?) KSP_VERSION to be set
            // when KSP_VERSION_MIN/_MAX are set, but CKAN treats
            // its equivalent properties as mutually exclusive.
            // Only fallback if neither min nor max are defined,
            // for open ranges.
            KspVersion avcKspMin, avcKspMax;

            if (avc.ksp_version_min == null && avc.ksp_version_max == null)
            {
                // Use specific KSP version if min/max don't exist
                avcKspMin = avcKspMax = avc.ksp_version;
            }
            else
            {
                avcKspMin = avc.ksp_version_min;
                avcKspMax = avc.ksp_version_max;
            }

            // 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 (!KspVersion.IsNullOrAny(existingKspMin))
            {
                kspMins.Add(existingKspMin);
            }

            if (!KspVersion.IsNullOrAny(avcKspMin))
            {
                kspMins.Add(avcKspMin);
            }

            if (!KspVersion.IsNullOrAny(existingKspMax))
            {
                kspMaxes.Add(existingKspMax);
            }

            if (!KspVersion.IsNullOrAny(avcKspMax))
            {
                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());
            }
        }
Beispiel #2
0
        public static void ApplyVersions(JObject json, AvcVersion avc)
        {
            // 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 (!KspVersion.IsNullOrAny(existingKspMin))
            {
                kspMins.Add(existingKspMin);
            }

            if (!KspVersion.IsNullOrAny(avcKspMin))
            {
                kspMins.Add(avcKspMin);
            }

            if (!KspVersion.IsNullOrAny(existingKspMax))
            {
                kspMaxes.Add(existingKspMax);
            }

            if (!KspVersion.IsNullOrAny(avcKspMax))
            {
                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());
            }
        }