public static void TryAddToVersionManifest(VersionManifest manifest) { foreach (var entryKvp in NewManifestEntries) { var id = entryKvp.Key; var newEntry = entryKvp.Value; if (newEntry.ShouldMergeJSON && manifest.Contains(id, newEntry.Type)) { // read the manifest pointed entry and hash the contents JsonHashToId.Add(File.ReadAllText(manifest.Get(id, newEntry.Type).FilePath).GetHashCode(), id); // The manifest already contains this information, so we need to queue it to be merged var partialJson = File.ReadAllText(newEntry.Path); if (!JsonMerges.ContainsKey(id)) { JsonMerges.Add(id, new List <string>()); } Log($"\tAdding id {id} to JSONMerges"); JsonMerges[id].Add(partialJson); } else { // This is a new definition or a replacement that doesn't get merged, so add or update the manifest Log($"\tAddOrUpdate({id}, {newEntry.Path}, {newEntry.Type}, {DateTime.Now}, {newEntry.AssetBundleName}, {newEntry.AssetBundlePersistent})"); manifest.AddOrUpdate(id, newEntry.Path, newEntry.Type, DateTime.Now, newEntry.AssetBundleName, newEntry.AssetBundlePersistent); } } }
public static void TryMergeJsonInto(string jsonIn, ref string jsonOut) { var jsonHash = jsonIn.GetHashCode(); var jsonCopy = jsonOut; if (!JsonHashToId.ContainsKey(jsonHash)) { return; } var id = JsonHashToId[jsonHash]; if (!JsonMerges.ContainsKey(id)) { return; } try { var ontoJObj = JObject.Parse(jsonCopy); foreach (var jsonMerge in JsonMerges[id]) { var inJObj = JObject.Parse(jsonMerge); ontoJObj.Merge(inJObj); } jsonOut = ontoJObj.ToString(); } catch (JsonReaderException e) { Log($"Error merging JSON ${e}"); } }
internal static void TryAddToVersionManifest(VersionManifest manifest) { if (!hasLoadedMods) { LoadMods(); } var breakMyGame = File.Exists(Path.Combine(ModDirectory, "break.my.game")); LogWithDate("Adding in mod manifests!"); if (breakMyGame) { var mddPath = Path.Combine(Path.Combine(StreamingAssetsDirectory, "MDD"), "MetadataDatabase.db"); var mddBackupPath = mddPath + ".orig"; Log($"\tBreak my game mode enabled! All new modded content (doesn't currently support merges) will be added to the DB."); if (!File.Exists(mddBackupPath)) { Log($"\t\tBacking up metadata database to {Path.GetFileName(mddBackupPath)}"); File.Copy(mddPath, mddBackupPath); } } foreach (var modName in modLoadOrder) { if (!ModManifest.ContainsKey(modName)) { continue; } Log($"\t{modName}:"); foreach (var modEntry in ModManifest[modName]) { var existingEntry = manifest.Find(x => x.Id == modEntry.Id); VersionManifestAddendum addendum = null; if (!string.IsNullOrEmpty(modEntry.AddToAddendum)) { addendum = manifest.GetAddendumByName(modEntry.AddToAddendum); // create the addendum if it doesn't exist if (addendum == null) { Log($"\t\tCreated addendum {modEntry.AddToAddendum}:"); addendum = new VersionManifestAddendum(modEntry.AddToAddendum); manifest.ApplyAddendum(addendum); } } if (modEntry.Type == null) { // null type means that we have to find existing entry with the same rel path to fill in the entry // TODO: + 16 is a little bizzare looking, it's the length of the substring + 1 because we want to get rid of it and the \ var relPath = modEntry.Path.Substring(modEntry.Path.LastIndexOf("StreamingAssets", StringComparison.Ordinal) + 16); var fakeStreamingAssetsPath = Path.Combine(StreamingAssetsDirectory, relPath); existingEntry = manifest.Find(x => Path.GetFullPath(x.FilePath) == Path.GetFullPath(fakeStreamingAssetsPath)); if (existingEntry == null) { continue; } modEntry.Id = existingEntry.Id; modEntry.Type = existingEntry.Type; } if (Path.GetExtension(modEntry.Path).ToLower() == ".json" && modEntry.ShouldMergeJSON && existingEntry != null) { // read the manifest pointed entry and hash the contents JsonHashToId[File.ReadAllText(existingEntry.FilePath).GetHashCode()] = modEntry.Id; // The manifest already contains this information, so we need to queue it to be merged var partialJson = File.ReadAllText(modEntry.Path); if (!JsonMerges.ContainsKey(modEntry.Id)) { JsonMerges.Add(modEntry.Id, new List <string>()); } if (JsonMerges[modEntry.Id].Contains(partialJson)) { Log($"\t\tAlready added {modEntry.Id} to JsonMerges"); continue; } Log($"\t\tAdding {modEntry.Id} to JsonMerges"); JsonMerges[modEntry.Id].Add(partialJson); continue; } if (breakMyGame && Path.GetExtension(modEntry.Path).ToLower() == ".json") { var type = (BattleTechResourceType)Enum.Parse(typeof(BattleTechResourceType), modEntry.Type); using (var metadataDatabase = new MetadataDatabase()) { VersionManifestHotReload.InstantiateResourceAndUpdateMDDB(type, modEntry.Path, metadataDatabase); Log($"\t\tAdding to MDDB! {type} {modEntry.Path}"); } } if (!string.IsNullOrEmpty(modEntry.AddToAddendum)) { Log($"\t\tAddOrUpdate {modEntry.Type} {modEntry.Id} to addendum {addendum.Name}"); addendum.AddOrUpdate(modEntry.Id, modEntry.Path, modEntry.Type, DateTime.Now, modEntry.AssetBundleName, modEntry.AssetBundlePersistent); continue; } // This is a new definition or a replacement that doesn't get merged, so add or update the manifest Log($"\t\tAddOrUpdate {modEntry.Type} {modEntry.Id}"); manifest.AddOrUpdate(modEntry.Id, modEntry.Path, modEntry.Type, DateTime.Now, modEntry.AssetBundleName, modEntry.AssetBundlePersistent); } } Log(""); }
internal static void TryMergeIntoInterceptedJson(string jsonIn, ref string jsonOut) { var jsonHash = jsonIn.GetHashCode(); var jsonCopy = jsonOut; if (!JsonHashToId.ContainsKey(jsonHash)) { return; } var id = JsonHashToId[jsonHash]; if (!JsonMerges.ContainsKey(id)) { return; } LogWithDate($"Merging json into ID: {id}"); JObject ontoJObj; try { ontoJObj = JObject.Parse(jsonCopy); } catch (Exception e) { try { Log("\tParent JSON has an JSON parse error, attempting to fix missing commas with regex"); jsonCopy = FixMissingCommas(jsonCopy); ontoJObj = JObject.Parse(jsonCopy); } catch (Exception e2) { Log("\tParent JSON has an error preventing merges that couldn't be fixed with missing comma regex"); Log($"\t\t Exception before regex: {e.Message}"); Log($"\t\t Exception after regex: {e2.Message}"); return; } Log("\tFixed missing commas in parent JSON."); } foreach (var jsonMerge in JsonMerges[id]) { try { var inJObj = JObject.Parse(jsonMerge); ontoJObj.Merge(inJObj, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Replace }); } catch (Exception e) { Log($"\tError merging particular JSON merge onto {id}, skipping just this single merge"); Log($"\t\t{e.Message}"); } } jsonOut = ontoJObj.ToString(); }