Use JSON serialization settings

This commit is contained in:
Nick 2022-05-22 00:47:01 -04:00
parent 134ba469d3
commit 0e545850cc

View File

@ -1,10 +1,12 @@
using NewHorizons.External; using NewHorizons.External;
using NewHorizons.External.Configs; using NewHorizons.External.Configs;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using Newtonsoft.Json;
using OWML.Common; using OWML.Common;
using OWML.Common.Menus; using OWML.Common.Menus;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -47,6 +49,12 @@ namespace NewHorizons.Utility.DebugUtilities
private bool saveButtonUnlocked = false; private bool saveButtonUnlocked = false;
private Vector2 recentModListScrollPosition = Vector2.zero; private Vector2 recentModListScrollPosition = Vector2.zero;
private static JsonSerializerSettings jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
Formatting = Formatting.Indented,
};
private void Awake() private void Awake()
{ {
@ -54,6 +62,7 @@ namespace NewHorizons.Utility.DebugUtilities
_drc = this.GetRequiredComponent<DebugRaycaster>(); _drc = this.GetRequiredComponent<DebugRaycaster>();
LoadFavoriteProps(); LoadFavoriteProps();
} }
private void Start() private void Start()
{ {
if (!Main.Debug) return; if (!Main.Debug) return;
@ -85,6 +94,7 @@ namespace NewHorizons.Utility.DebugUtilities
pauseMenuButton = Main.Instance.ModHelper.Menus.PauseMenu.OptionsButton.Duplicate("Toggle Prop Placer Menu".ToUpper()); pauseMenuButton = Main.Instance.ModHelper.Menus.PauseMenu.OptionsButton.Duplicate("Toggle Prop Placer Menu".ToUpper());
InitMenu(); InitMenu();
} }
private void RestoreMenuOpennessState() { menuOpen = openMenuOnPause; } private void RestoreMenuOpennessState() { menuOpen = openMenuOnPause; }
private void ToggleMenu() { menuOpen = !menuOpen; openMenuOnPause = !openMenuOnPause; } private void ToggleMenu() { menuOpen = !menuOpen; openMenuOnPause = !openMenuOnPause; }
@ -109,7 +119,7 @@ namespace NewHorizons.Utility.DebugUtilities
if (!menuOpen) return; if (!menuOpen) return;
if (!Main.Debug) return; if (!Main.Debug) return;
Vector2 menuPosition = new Vector2(10, 40); Vector2 menuPosition = new Vector2(10, 40);
GUILayout.BeginArea(new Rect(menuPosition.x, menuPosition.y, EditorMenuSize.x, EditorMenuSize.y), _editorMenuStyle); GUILayout.BeginArea(new Rect(menuPosition.x, menuPosition.y, EditorMenuSize.x, EditorMenuSize.y), _editorMenuStyle);
@ -123,13 +133,13 @@ namespace NewHorizons.Utility.DebugUtilities
// List of recently placed objects // List of recently placed objects
GUILayout.Label("Recently placed objects"); GUILayout.Label("Recently placed objects");
recentPropsScrollPosition = GUILayout.BeginScrollView(recentPropsScrollPosition, GUILayout.Width(EditorMenuSize.x), GUILayout.Height(100)); recentPropsScrollPosition = GUILayout.BeginScrollView(recentPropsScrollPosition, GUILayout.Width(EditorMenuSize.x), GUILayout.Height(100));
foreach (string propPath in DebugPropPlacer.RecentlyPlacedProps) foreach (string propPath in DebugPropPlacer.RecentlyPlacedProps)
{ {
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
var propPathElements = propPath.Split('/'); var propPathElements = propPath.Split('/');
string propName = propPathElements[propPathElements.Length-1]; string propName = propPathElements[propPathElements.Length - 1];
string favoriteButtonIcon = favoriteProps.Contains(propPath) ? "★" : "☆"; string favoriteButtonIcon = favoriteProps.Contains(propPath) ? "★" : "☆";
if (GUILayout.Button(favoriteButtonIcon, GUILayout.ExpandWidth(false))) if (GUILayout.Button(favoriteButtonIcon, GUILayout.ExpandWidth(false)))
@ -144,7 +154,7 @@ namespace NewHorizons.Utility.DebugUtilities
} }
string[] favoritePropsArray = favoriteProps.ToArray<string>(); string[] favoritePropsArray = favoriteProps.ToArray<string>();
PlayerPrefs.SetString(favoritePropsPlayerPrefKey, string.Join(separatorCharacter+"", favoritePropsArray)); PlayerPrefs.SetString(favoritePropsPlayerPrefKey, string.Join(separatorCharacter + "", favoritePropsArray));
} }
if (GUILayout.Button(propName)) if (GUILayout.Button(propName))
@ -163,7 +173,7 @@ namespace NewHorizons.Utility.DebugUtilities
GUILayout.Label("Name of your mod"); GUILayout.Label("Name of your mod");
if (loadedMod == null) if (loadedMod == null)
{ {
recentModListScrollPosition = GUILayout.BeginScrollView(recentModListScrollPosition, GUILayout.Width(EditorMenuSize.x), GUILayout.Height(100)); recentModListScrollPosition = GUILayout.BeginScrollView(recentModListScrollPosition, GUILayout.Width(EditorMenuSize.x), GUILayout.Height(100));
foreach (var mod in Main.MountedAddons) foreach (var mod in Main.MountedAddons)
{ {
@ -237,24 +247,30 @@ namespace NewHorizons.Utility.DebugUtilities
var relativePath = filePath.Replace(loadedMod.ModHelper.Manifest.ModFolderPath, ""); var relativePath = filePath.Replace(loadedMod.ModHelper.Manifest.ModFolderPath, "");
var json = JsonConvert.SerializeObject(loadedConfigFiles[filePath], jsonSettings);
// Add the schema line
json = "{\n\t\"$schema\": \"https://raw.githubusercontent.com/xen-42/outer-wilds-new-horizons/main/NewHorizons/Schemas/body_schema.json\"," + json.Substring(1);
try try
{ {
Logger.Log("Saving... " + relativePath + " to " + filePath); Logger.Log("Saving... " + relativePath + " to " + filePath);
var directoryName = System.IO.Path.GetDirectoryName(loadedMod.ModHelper.Manifest.ModFolderPath + relativePath); var path = loadedMod.ModHelper.Manifest.ModFolderPath + relativePath;
System.IO.Directory.CreateDirectory(directoryName); var directoryName = Path.GetDirectoryName(path);
Directory.CreateDirectory(directoryName);
loadedMod.ModHelper.Storage.Save(loadedConfigFiles[filePath], relativePath); File.WriteAllText(path, json);
} }
catch (Exception e) { Logger.LogError("Failed to save file " + backupFolderName+relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); } catch (Exception e) { Logger.LogError("Failed to save file " + backupFolderName + relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); }
try try
{ {
var directoryName = System.IO.Path.GetDirectoryName(Main.Instance.ModHelper.Manifest.ModFolderPath + backupFolderName + relativePath); var path = Main.Instance.ModHelper.Manifest.ModFolderPath + backupFolderName + relativePath;
System.IO.Directory.CreateDirectory(directoryName); var directoryName = Path.GetDirectoryName(path);
Directory.CreateDirectory(directoryName);
Main.Instance.ModHelper.Storage.Save(loadedConfigFiles[filePath], backupFolderName+relativePath); File.WriteAllText(path, json);
} }
catch (Exception e) { Logger.LogError("Failed to save backup file " + backupFolderName+relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); } catch (Exception e) { Logger.LogError("Failed to save backup file " + backupFolderName + relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); }
} }
} }
@ -312,7 +328,6 @@ namespace NewHorizons.Utility.DebugUtilities
_dpp = this.GetRequiredComponent<DebugPropPlacer>(); _dpp = this.GetRequiredComponent<DebugPropPlacer>();
_drc = this.GetRequiredComponent<DebugRaycaster>(); _drc = this.GetRequiredComponent<DebugRaycaster>();
Texture2D bgTexture = ImageUtilities.MakeSolidColorTexture((int)EditorMenuSize.x, (int)EditorMenuSize.y, Color.black); Texture2D bgTexture = ImageUtilities.MakeSolidColorTexture((int)EditorMenuSize.x, (int)EditorMenuSize.y, Color.black);
_editorMenuStyle = new GUIStyle _editorMenuStyle = new GUIStyle