feat: implemented saving updated config files

This commit is contained in:
FreezeDriedMangoes 2022-05-13 22:33:22 -04:00
parent 7011b6ec3a
commit b25156cf0b
2 changed files with 102 additions and 50 deletions

View File

@ -1,4 +1,5 @@
using NewHorizons.External.Configs;
using NewHorizons.External;
using NewHorizons.External.Configs;
using OWML.Common;
using System;
using System.Collections.Generic;
@ -29,7 +30,7 @@ namespace NewHorizons.Utility
//private string workingModName = "";
private IModBehaviour loadedMod = null;
private Dictionary<string, IPlanetConfig> loadedConfigFiles = new Dictionary<string, IPlanetConfig>();
private Dictionary<string, PlanetConfig> loadedConfigFiles = new Dictionary<string, PlanetConfig>();
private bool saveButtonUnlocked = false;
private bool propsHaveBeenLoaded = false;
private Vector2 recentModListScrollPosition = Vector2.zero;
@ -136,6 +137,21 @@ namespace NewHorizons.Utility
if (GUILayout.Button(mod.ModHelper.Manifest.UniqueName))
{
loadedMod = mod;
propsHaveBeenLoaded = true;
var folder = loadedMod.ModHelper.Manifest.ModFolderPath;
if (System.IO.Directory.Exists(folder + "planets"))
{
foreach (var file in System.IO.Directory.GetFiles(folder + @"planets\", "*.json", System.IO.SearchOption.AllDirectories))
{
Logger.Log("READING FROM CONFIG @ " + file);
var relativeDirectory = file.Replace(folder, "");
var bodyConfig = loadedMod.ModHelper.Storage.Load<PlanetConfig>(relativeDirectory);
loadedConfigFiles[file] = bodyConfig;
_dpp.FindAndRegisterPropsFromConfig(bodyConfig);
}
}
}
}
@ -149,25 +165,25 @@ namespace NewHorizons.Utility
GUI.enabled = !propsHaveBeenLoaded && loadedMod != null;
if (GUILayout.Button("Load Detail Props from Configs", GUILayout.ExpandWidth(false)))
{
propsHaveBeenLoaded = true;
var folder = loadedMod.ModHelper.Manifest.ModFolderPath;
//GUI.enabled = !propsHaveBeenLoaded && loadedMod != null;
//if (GUILayout.Button("Load Detail Props from Configs", GUILayout.ExpandWidth(false)))
//{
// propsHaveBeenLoaded = true;
// var folder = loadedMod.ModHelper.Manifest.ModFolderPath;
if (System.IO.Directory.Exists(folder + "planets"))
{
foreach (var file in System.IO.Directory.GetFiles(folder + @"planets\", "*.json", System.IO.SearchOption.AllDirectories))
{
Logger.Log("READING FROM CONFIG @ " + file);
var relativeDirectory = file.Replace(folder, "");
var bodyConfig = loadedMod.ModHelper.Storage.Load<PlanetConfig>(relativeDirectory);
loadedConfigFiles[file] = bodyConfig;
_dpp.FindAndRegisterPropsFromConfig(bodyConfig);
}
}
}
GUI.enabled = true;
// if (System.IO.Directory.Exists(folder + "planets"))
// {
// foreach (var file in System.IO.Directory.GetFiles(folder + @"planets\", "*.json", System.IO.SearchOption.AllDirectories))
// {
// Logger.Log("READING FROM CONFIG @ " + file);
// var relativeDirectory = file.Replace(folder, "");
// var bodyConfig = loadedMod.ModHelper.Storage.Load<PlanetConfig>(relativeDirectory);
// loadedConfigFiles[file] = bodyConfig;
// _dpp.FindAndRegisterPropsFromConfig(bodyConfig);
// }
// }
//}
//GUI.enabled = true;
GUILayout.Space(5);
@ -182,10 +198,12 @@ namespace NewHorizons.Utility
{
UpdateLoadedConfigs();
Logger.Log($"(count) Saving {loadedConfigFiles.Keys.Count} files");
foreach (var filePath in loadedConfigFiles.Keys)
{
Logger.Log("Saving... " + filePath);
Main.Instance.ModHelper.Storage.Save<IPlanetConfig>(loadedConfigFiles[filePath], filePath);
var relativePath = filePath.Replace(loadedMod.ModHelper.Manifest.ModFolderPath, "");
Logger.Log("Saving... " + relativePath + " to " + filePath);
loadedMod.ModHelper.Storage.Save(loadedConfigFiles[filePath], relativePath);
}
saveButtonUnlocked = false;
}
@ -199,7 +217,7 @@ namespace NewHorizons.Utility
foreach (var filePath in loadedConfigFiles.Keys)
{
Logger.Log("Updated copy of " + filePath);
Logger.Log("The updated copy of " + filePath);
Logger.Log(Newtonsoft.Json.JsonConvert.SerializeObject(loadedConfigFiles[filePath], Newtonsoft.Json.Formatting.Indented));
}
//_dpp.PrintConfigs();
@ -226,25 +244,57 @@ namespace NewHorizons.Utility
//var allConfigsForMod = Main.Instance.BodyDict[Main.CurrentStarSystem].Where(x => x.Mod == mod).Select(x => x.Config)
//var allConfigs = Main.BodyDict.Values.SelectMany(x => x).Where(x => x.Mod == loadedMod).Select(x => x.Config);
Logger.Log("updating configs");
Logger.Log("New Details keys: " + string.Join(", ", newDetails.Keys));
Dictionary<string, string> planetToConfigPath = new Dictionary<string, string>();
// Get all configs
foreach (var filePath in loadedConfigFiles.Keys)
{
if (loadedConfigFiles[filePath].Name == null || AstroObjectLocator.GetAstroObject(loadedConfigFiles[filePath].Name) == null) continue;
Logger.Log("potentially updating copy of config at " + filePath);
if (loadedConfigFiles[filePath].Name == null || AstroObjectLocator.GetAstroObject(loadedConfigFiles[filePath].Name) == null) { Logger.Log("Failed to update copy of config at " + filePath); continue; }
var bodyName = loadedConfigFiles[filePath].Name;
var astroObjectName = AstroObjectLocator.GetAstroObject(bodyName).name;
if (astroObjectName.EndsWith("_Body")) astroObjectName = astroObjectName.Substring(0, astroObjectName.Length-"_Body".Length);
var systemName = loadedConfigFiles[filePath].StarSystem;
var composedName = systemName + separatorCharacter + astroObjectName;
planetToConfigPath[composedName] = filePath;
Logger.Log("made composed name from copy of config file for " + composedName + " " + newDetails.ContainsKey(composedName));
if (!newDetails.ContainsKey(composedName)) continue;
if (loadedConfigFiles[filePath].Props == null)
{
(loadedConfigFiles[filePath] as PlanetConfig).Props = new External.PropModule();
loadedConfigFiles[filePath].Props = new External.PropModule();
}
loadedConfigFiles[filePath].Props.Details = newDetails[composedName];
Logger.Log("successfully updated copy of config file for " + composedName);
}
// find all new planets that do not yet have config paths
var planetsThatDoNotHaveConfigFiles = newDetails.Keys.Where(x => !planetToConfigPath.ContainsKey(x)).ToList();
foreach (var planetAndSystem in planetsThatDoNotHaveConfigFiles)
{
Logger.Log("Fabricating new config file for " + planetAndSystem);
var filepath = "planets/" + planetAndSystem + ".json";
PlanetConfig c = new PlanetConfig(null);
c.StarSystem = planetAndSystem.Split(separatorCharacter)[0];
c.Name = planetAndSystem.Split(separatorCharacter)[1];
c.Props = new PropModule();
c.Props.Details = newDetails[planetAndSystem];
// loadedConfigFiles[filepath] = c;
}
}

View File

@ -20,14 +20,16 @@ namespace NewHorizons.Utility
public string body;
public string system;
public string propPath;
//public string propPath;
public GameObject gameObject;
public Vector3 pos { get { return gameObject.transform.localPosition; } }
public Vector3 rotation { get { return gameObject.transform.localEulerAngles; } }
//public Vector3 pos { get { return gameObject.transform.localPosition; } }
//public Vector3 rotation { get { return gameObject.transform.localEulerAngles; } }
public string assetBundle;
public string[] removeChildren;
//public string assetBundle;
//public string[] removeChildren;
public DetailInfo detailInfo;
}
// DreamWorld_Body/Sector_DreamWorld/Sector_DreamZone_1/Props_DreamZone_1/OtherComponentsGroup/Trees_Z1/DreamHouseIsland/Tree_DW_M_Var
@ -172,14 +174,12 @@ namespace NewHorizons.Utility
}
Transform spawnedProp = potentialProps[0];
PropPlacementData data = RegisterProp_WithReturn(config.Name, spawnedProp.gameObject, detail.path);
data.assetBundle = detail.assetBundle;
data.removeChildren = detail.removeChildren;
PropPlacementData data = RegisterProp_WithReturn(config.Name, spawnedProp.gameObject, detail.path, config.StarSystem, detail);
potentialProps.Remove(spawnedProp);
if (!RecentlyPlacedProps.Contains(data.propPath))
if (!RecentlyPlacedProps.Contains(data.detailInfo.path))
{
RecentlyPlacedProps.Add(data.propPath);
RecentlyPlacedProps.Add(data.detailInfo.path);
}
}
}
@ -189,22 +189,28 @@ namespace NewHorizons.Utility
RegisterProp_WithReturn(bodyGameObjectName, prop);
}
private PropPlacementData RegisterProp_WithReturn(string bodyGameObjectName, GameObject prop, string propPath = null, string systemName = null)
private PropPlacementData RegisterProp_WithReturn(string bodyGameObjectName, GameObject prop, string propPath = null, string systemName = null, DetailInfo detailInfo = null)
{
if (Main.Debug)
{
// TOOD: make this prop an item
}
// TODO: add a DetailInfo param to this function and PropPlacementData, and use that as a base in GetPropsConfigByBody
// eg data.DetailInfo.position = data.gameObject.transform.localPosition; return data.DetailInfo;
string bodyName = bodyGameObjectName.EndsWith("_Body")
? bodyGameObjectName.Substring(0, bodyGameObjectName.Length-"_Body".Length)
: bodyGameObjectName;
detailInfo = detailInfo == null ? new DetailInfo() : detailInfo;
detailInfo.path = propPath == null ? currentObject : propPath;
PropPlacementData data = new PropPlacementData
{
body = bodyName,
propPath = propPath == null ? currentObject : propPath,
gameObject = prop,
system = systemName
system = systemName == null ? "SolarSystem" : systemName,
detailInfo = detailInfo
};
props.Add(data);
@ -282,22 +288,18 @@ namespace NewHorizons.Utility
if (bodyProps == null || bodyProps.Count == 0) continue;
if ( AstroObjectLocator.GetAstroObject(bodyProps[0].body) == null ) continue;
string bodyName = useAstroObjectName ? AstroObjectLocator.GetAstroObject(bodyProps[0].body).name : bodyProps[0].body;
if (bodyName.EndsWith("_Body")) bodyName = bodyName.Substring(0, bodyName.Length-"_Body".Length);
DetailInfo[] infoArray = new DetailInfo[bodyProps.Count];
propConfigs[bodyProps[0].system + DebugMenu.separatorCharacter + bodyName] = infoArray;
for(int i = 0; i < bodyProps.Count; i++)
{
infoArray[i] = new DetailInfo()
{
path = bodyProps[i].propPath,
assetBundle = bodyProps[i].assetBundle,
position = bodyProps[i].gameObject.transform.localPosition,
rotation = bodyProps[i].gameObject.transform.localEulerAngles,
scale = bodyProps[i].gameObject.transform.localScale.x,
//public bool alignToNormal; // TODO: figure out how to recover this (or actually, rotation should cover it)
removeChildren = bodyProps[i].removeChildren
};
bodyProps[i].detailInfo.position = bodyProps[i].gameObject.transform.localPosition;
bodyProps[i].detailInfo.rotation = bodyProps[i].gameObject.transform.localEulerAngles;
bodyProps[i].detailInfo.scale = bodyProps[i].gameObject.transform.localScale.x;
infoArray[i] = bodyProps[i].detailInfo;
}
}