From be81f2cc2e1411bbaeee9ccdedc772c2e77f0c1b Mon Sep 17 00:00:00 2001 From: Richard Lake Date: Thu, 28 May 2015 20:13:46 +0930 Subject: [PATCH 1/3] RegistryManager.SerializeCurrentInstall changes Change depends to recommends. Allows user customization of the install in the GUI Do not output provided versions. They are no real mods. --- Core/Registry/RegistryManager.cs | 58 ++++++++++++++++---------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/Core/Registry/RegistryManager.cs b/Core/Registry/RegistryManager.cs index c3fb830572..a237d08c8e 100644 --- a/Core/Registry/RegistryManager.cs +++ b/Core/Registry/RegistryManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using ChinhDo.Transactions; using log4net; @@ -38,7 +39,8 @@ private RegistryManager(string path, KSP ksp) // We don't cause an inconsistency error to stop the registry from being loaded, // because then the user can't do anything to correct it. However we're // sure as hell going to complain if we spot one! - try { + try + { registry.CheckSanity(); } catch (InconsistentKraken kraken) @@ -65,7 +67,6 @@ public static RegistryManager Instance(KSP ksp) private void Load() { - // Our registry needs to know our KSP install when upgrading from older // registry formats. This lets us encapsulate that to make it available // after deserialisation. @@ -74,7 +75,7 @@ private void Load() Context = new System.Runtime.Serialization.StreamingContext( System.Runtime.Serialization.StreamingContextStates.Other, ksp - ) + ) }; string json = File.ReadAllText(path); @@ -133,7 +134,7 @@ private string Serialize() StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); - using (JsonTextWriter writer = new JsonTextWriter (sw)) + using (JsonTextWriter writer = new JsonTextWriter(sw)) { writer.Formatting = Formatting.Indented; writer.Indentation = 4; @@ -152,35 +153,34 @@ private string SerializeCurrentInstall() string kspInstanceName = "default"; string name = "installed-" + kspInstanceName; - JObject installed = new JObject (); - installed["spec_version"] = "v1.6"; - installed["identifier"] = name; - installed["version"] = DateTime.UtcNow.ToString("yyyy.MM.dd.hh.mm.ss"); - - installed["license"] = "unknown"; - installed["name"] = name; - installed["abstract"] = "A list of modules installed on the " + kspInstanceName + " KSP instance"; - installed["kind"] = "metapackage"; + JObject installed = new JObject + { + ["spec_version"] = "v1.6", + ["identifier"] = name, + ["version"] = DateTime.UtcNow.ToString("yyyy.MM.dd.hh.mm.ss"), + ["license"] = "unknown", + ["name"] = name, + ["abstract"] = "A list of modules installed on the " + kspInstanceName + " KSP instance", + ["kind"] = "metapackage" + }; - JArray depends = new JArray (); - installed["depends"] = depends; - foreach(KeyValuePair module in registry.Installed()) - { - if (!(CKAN.Version.AutodetectedDllString.Equals(module.Value.ToString()))) + var recommends = new JArray(); + foreach (var module in registry.Installed(). + Where(mod => !(mod.Value is ProvidesVersion) && !(mod.Value is DllVersion)). + Select(mod => new JObject { - JObject moduleJson = new JObject(); - moduleJson["name"] = module.Key; - moduleJson["version"] = module.Value.ToString(); - - depends.Add(moduleJson); - } + ["name"] = mod.Key, + ["version"] = mod.Value.ToString() + })) + { + recommends.Add(module); } - StringBuilder sb = new StringBuilder(); - StringWriter sw = new StringWriter(sb); + installed["recommends"] = recommends; - using (JsonTextWriter writer = new JsonTextWriter (sw)) + var sw = new StringWriter(new StringBuilder()); + using (JsonTextWriter writer = new JsonTextWriter(sw)) { writer.Formatting = Formatting.Indented; writer.Indentation = 4; @@ -190,7 +190,7 @@ private string SerializeCurrentInstall() serializer.Serialize(writer, installed); } - return sw.ToString() + Environment.NewLine; + return sw + Environment.NewLine; } public void Save(bool enforceConsistency = true) @@ -220,7 +220,7 @@ public void Save(bool enforceConsistency = true) // TODO how do we obtain the name of the current KSP instance? string kspInstanceName = "default"; - string installedModsPath = Path.Combine (directoryPath, "installed-" + kspInstanceName + ".ckan"); + string installedModsPath = Path.Combine(directoryPath, "installed-" + kspInstanceName + ".ckan"); file_transaction.WriteAllText(installedModsPath, SerializeCurrentInstall()); } } From 22ed6ccbd05020db82f6e8bc2b9c465be48bec3e Mon Sep 17 00:00:00 2001 From: Richard Lake Date: Fri, 29 May 2015 10:11:14 +0930 Subject: [PATCH 2/3] Create CurrentInstallAsCKAN method. Will allow clients to avoid having to use installed-default.ckan and has greater flexibility. --- Core/Registry/RegistryManager.cs | 61 ++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/Core/Registry/RegistryManager.cs b/Core/Registry/RegistryManager.cs index a237d08c8e..b89ef0c6ab 100644 --- a/Core/Registry/RegistryManager.cs +++ b/Core/Registry/RegistryManager.cs @@ -23,7 +23,7 @@ public class RegistryManager // when deserialising, and *it* only needs it to do registry upgrades. // We could get rid of all of this if we declare we no longer wish to support // older registry formats. - private KSP ksp; + private readonly KSP ksp; public Registry registry; @@ -65,6 +65,18 @@ public static RegistryManager Instance(KSP ksp) return singleton[directory]; } + /// + /// Returns the currently installed modules in json format suitable for outputting to a ckan file. + /// Defaults to using depends and with version numbers. + /// + /// If the json should use a recommends field instead of depends + /// If version numbers should be included + /// String containing a valid ckan file + public string CurrentInstallAsCKAN(bool recommmends, bool with_versions) + { + return SerializeCurrentInstall(recommmends, with_versions); + } + private void Load() { // Our registry needs to know our KSP install when upgrading from older @@ -118,8 +130,7 @@ private void AscertainDefaultRepo() { repositories = new SortedDictionary(); } - - // if (!(repositories.ContainsKey(Repository.default_ckan_repo_name))) + if (repositories.Count == 0) { repositories.Add(Repository.default_ckan_repo_name, @@ -137,23 +148,23 @@ private string Serialize() using (JsonTextWriter writer = new JsonTextWriter(sw)) { writer.Formatting = Formatting.Indented; - writer.Indentation = 4; - writer.IndentChar = ' '; + writer.Indentation = 1; + writer.IndentChar = '\t'; JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, registry); } - return sw.ToString() + Environment.NewLine; + return sw + Environment.NewLine; } - private string SerializeCurrentInstall() + private string SerializeCurrentInstall(bool recommmends = false, bool with_versions = true) { // TODO how do we obtain the name of the current KSP instance? string kspInstanceName = "default"; string name = "installed-" + kspInstanceName; - JObject installed = new JObject + var installed = new JObject { ["spec_version"] = "v1.6", ["identifier"] = name, @@ -164,40 +175,38 @@ private string SerializeCurrentInstall() ["kind"] = "metapackage" }; - - var recommends = new JArray(); - foreach (var module in registry.Installed(). - Where(mod => !(mod.Value is ProvidesVersion) && !(mod.Value is DllVersion)). - Select(mod => new JObject - { - ["name"] = mod.Key, - ["version"] = mod.Value.ToString() - })) + var mods = new JArray(); + foreach (var mod in registry.Installed() + .Where(mod => !(mod.Value is ProvidesVersion || mod.Value is DllVersion))) { - recommends.Add(module); + var module = new JObject {["name"] = mod.Key}; + if (with_versions) + { + module["version"] = mod.Value.ToString(); + } + mods.Add(module); } - installed["recommends"] = recommends; + installed[recommmends ? "recommends" : "depends"] = mods; var sw = new StringWriter(new StringBuilder()); - using (JsonTextWriter writer = new JsonTextWriter(sw)) + using (var writer = new JsonTextWriter(sw)) { writer.Formatting = Formatting.Indented; - writer.Indentation = 4; - writer.IndentChar = ' '; + writer.Indentation = 1; + writer.IndentChar = '\t'; - JsonSerializer serializer = new JsonSerializer(); - serializer.Serialize(writer, installed); + new JsonSerializer().Serialize(writer, installed); } return sw + Environment.NewLine; } - public void Save(bool enforceConsistency = true) + public void Save(bool enforce_consistency = true) { log.DebugFormat("Saving CKAN registry at {0}", path); - if (enforceConsistency) + if (enforce_consistency) { // No saving the registry unless it's in a sane state. registry.CheckSanity(); From f9138fd9bf8db626e8f8e17e5a4793d742bba843 Mon Sep 17 00:00:00 2001 From: Richard Lake Date: Tue, 2 Jun 2015 12:26:57 +0930 Subject: [PATCH 3/3] Fix for old compilers. --- Core/Registry/RegistryManager.cs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Core/Registry/RegistryManager.cs b/Core/Registry/RegistryManager.cs index b89ef0c6ab..66061bf248 100644 --- a/Core/Registry/RegistryManager.cs +++ b/Core/Registry/RegistryManager.cs @@ -67,7 +67,7 @@ public static RegistryManager Instance(KSP ksp) /// /// Returns the currently installed modules in json format suitable for outputting to a ckan file. - /// Defaults to using depends and with version numbers. + /// Defaults to using depends and with version numbers. /// /// If the json should use a recommends field instead of depends /// If version numbers should be included @@ -130,7 +130,7 @@ private void AscertainDefaultRepo() { repositories = new SortedDictionary(); } - + if (repositories.Count == 0) { repositories.Add(Repository.default_ckan_repo_name, @@ -164,22 +164,21 @@ private string SerializeCurrentInstall(bool recommmends = false, bool with_versi string kspInstanceName = "default"; string name = "installed-" + kspInstanceName; - var installed = new JObject - { - ["spec_version"] = "v1.6", - ["identifier"] = name, - ["version"] = DateTime.UtcNow.ToString("yyyy.MM.dd.hh.mm.ss"), - ["license"] = "unknown", - ["name"] = name, - ["abstract"] = "A list of modules installed on the " + kspInstanceName + " KSP instance", - ["kind"] = "metapackage" - }; + var installed = new JObject(); + installed["kind"] = "metapackage"; + installed["abstract"] = "A list of modules installed on the " + kspInstanceName + " KSP instance"; + installed["name"] = name; + installed["license"] = "unknown"; + installed["version"] = DateTime.UtcNow.ToString("yyyy.MM.dd.hh.mm.ss"); + installed["identifier"] = name; + installed["spec_version"] = "v1.6"; var mods = new JArray(); foreach (var mod in registry.Installed() .Where(mod => !(mod.Value is ProvidesVersion || mod.Value is DllVersion))) { - var module = new JObject {["name"] = mod.Key}; + var module = new JObject(); + module["name"] = mod.Key; if (with_versions) { module["version"] = mod.Value.ToString();