public static void ApplyVersions(JObject json, AvcVersion avc) { ModuleService.ApplyVersions(json, avc.ksp_version, avc.ksp_version_min, avc.ksp_version_max); 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()); } }
/// <summary> /// Processes an individual override stanza, altering the object's /// metadata in the process if applicable. /// </summary> private void ProcessOverrideStanza(JObject overrideStanza, JObject metadata) { JToken jBefore; JToken jAfter; string before = null; string after = null; if (overrideStanza.TryGetValue("before", out jBefore)) { if (jBefore.Type == JTokenType.String) { before = (string)jBefore; } else { throw new Kraken("override before property must be a string"); } } if (overrideStanza.TryGetValue("after", out jAfter)) { if (jAfter.Type == JTokenType.String) { after = (string)jAfter; } else { throw new Kraken("override after property must be a string"); } } if (_before.Contains(before) || _after.Contains(after)) { Log.InfoFormat("Processing override: {0}", overrideStanza); JToken stanzaConstraints; if (!overrideStanza.TryGetValue("version", out stanzaConstraints)) { throw new Kraken( string.Format( "Can't find version in override stanza {0}", overrideStanza)); } IEnumerable <string> constraints; // First let's get our constraints into a list of strings. if (stanzaConstraints is JValue) { constraints = new List <string> { stanzaConstraints.ToString() }; } else if (stanzaConstraints is JArray) { // Pop the constraints in 'constraints' constraints = ((JArray)stanzaConstraints).Values().Select(x => x.ToString()); } else { throw new Kraken( string.Format("Totally unexpected x_netkan_override - {0}", stanzaConstraints)); } // If the constraints don't apply, then do nothing. if (!ConstraintsApply(constraints, new ModuleVersion(metadata["version"].ToString()))) { return; } // All the constraints pass; let's replace the metadata we have with what's // in the override. JToken overrideBlock; if (overrideStanza.TryGetValue("override", out overrideBlock)) { var overrides = overrideBlock as JObject; if (gameVersionProperties.Any(p => overrides.ContainsKey(p))) { ModuleService.ApplyVersions( metadata, overrides.ContainsKey("ksp_version") ? GameVersion.Parse((string)overrides["ksp_version"]) : null, overrides.ContainsKey("ksp_version_min") ? GameVersion.Parse((string)overrides["ksp_version_min"]) : null, overrides.ContainsKey("ksp_version_max") ? GameVersion.Parse((string)overrides["ksp_version_max"]) : null ); foreach (var p in gameVersionProperties) { overrides.Remove(p); } } foreach (var property in overrides.Properties()) { metadata[property.Name] = property.Value; } } // And let's delete anything that needs deleting. JToken deleteList; if (overrideStanza.TryGetValue("delete", out deleteList)) { foreach (string key in (JArray)deleteList) { metadata.Remove(key); } } } }