fix: various bug fixes including a major one in GetAstroObjectName, and a minor one causing a crash on a save attempt when saving props placed in a new system that previously had 0 props in the loaded mod

This commit is contained in:
FreezeDriedMangoes 2022-05-14 22:33:21 -04:00
parent 4e82e7f142
commit dac61f86fc
2 changed files with 122 additions and 103 deletions

View File

@ -18,7 +18,8 @@ namespace NewHorizons.Utility
GUIStyle _editorMenuStyle;
Vector2 EditorMenuSize = new Vector2(600, 900);
bool menuOpen = false;
bool openMenuOnPause = false;
static bool openMenuOnPause = false;
static bool staticInitialized = false;
DebugPropPlacer _dpp;
DebugRaycaster _drc;
@ -27,10 +28,10 @@ namespace NewHorizons.Utility
private Vector2 recentPropsScrollPosition = Vector2.zero;
private HashSet<string> favoriteProps = new HashSet<string>();
public static readonly char separatorCharacter = '☧'; // since no chars are illegal in game object names, I picked one that's extremely unlikely to be used to be a separator
private string favoritePropsPlayerPrefKey = "FavoriteProps";
private static readonly string favoritePropsPlayerPrefKey = "FavoriteProps";
//private string workingModName = "";
private IModBehaviour loadedMod = null;
private static IModBehaviour loadedMod = null;
private Dictionary<string, PlanetConfig> loadedConfigFiles = new Dictionary<string, PlanetConfig>();
private bool saveButtonUnlocked = false;
private Vector2 recentModListScrollPosition = Vector2.zero;
@ -44,13 +45,26 @@ namespace NewHorizons.Utility
}
private void Start()
{
if (Main.Debug)
if (!Main.Debug) return;
if (!staticInitialized)
{
staticInitialized = true;
Logger.Log("STATIC INITILIZATION IN DEBUG MENU");
Main.Instance.ModHelper.Menus.PauseMenu.OnInit += PauseMenuInitHook;
Main.Instance.ModHelper.Menus.PauseMenu.OnClosed += CloseMenu;
Main.Instance.ModHelper.Menus.PauseMenu.OnOpened += RestoreMenuOpennessState;
PauseMenuInitHook();
Main.Instance.OnChangeStarSystem.AddListener((string s) => SaveLoadedConfigsForRecentSystem());
}
if (loadedMod != null)
{
Logger.Log("THERE WAS A MOD ALREADY LOADED");
LoadMod(loadedMod);
}
}
@ -74,7 +88,7 @@ namespace NewHorizons.Utility
var favoritePropPaths = favoritePropsPlayerPref.Split(separatorCharacter);
foreach (string favoriteProp in favoritePropPaths)
{
_dpp.RecentlyPlacedProps.Add(favoriteProp);
DebugPropPlacer.RecentlyPlacedProps.Add(favoriteProp);
this.favoriteProps.Add(favoriteProp);
}
}
@ -99,7 +113,7 @@ namespace NewHorizons.Utility
// List of recently placed objects
GUILayout.Label("Recently placed objects");
recentPropsScrollPosition = GUILayout.BeginScrollView(recentPropsScrollPosition, GUILayout.Width(EditorMenuSize.x), GUILayout.Height(100));
foreach (string propPath in _dpp.RecentlyPlacedProps)
foreach (string propPath in DebugPropPlacer.RecentlyPlacedProps)
{
GUILayout.BeginHorizontal();
@ -144,22 +158,7 @@ namespace NewHorizons.Utility
{
if (GUILayout.Button(mod.ModHelper.Manifest.UniqueName))
{
loadedMod = mod;
_dpp.active = true;
var folder = loadedMod.ModHelper.Manifest.ModFolderPath;
List<NewHorizonsBody> bodiesForThisMod = Main.BodyDict.Values.SelectMany(x => x).Where(x => x.Mod == loadedMod).ToList();
foreach (NewHorizonsBody body in bodiesForThisMod)
{
if (body.RelativePath == null)
{
Logger.Log("Error loading config for " + body.Config.Name + " in " + body.Config.StarSystem);
}
loadedConfigFiles[folder + body.RelativePath] = (body.Config as PlanetConfig);
_dpp.FindAndRegisterPropsFromConfig(body.Config);
}
LoadMod(mod);
}
}
@ -183,58 +182,78 @@ namespace NewHorizons.Utility
GUI.enabled = saveButtonUnlocked;
if (GUILayout.Button("Update your mod's configs"))
{
UpdateLoadedConfigs();
string backupFolderName = "configBackups\\" + DateTime.Now.ToString("yyyyMMddTHHmmss") + "\\";
Logger.Log($"(count) Saving {loadedConfigFiles.Keys.Count} files");
foreach (var filePath in loadedConfigFiles.Keys)
{
var relativePath = filePath.Replace(loadedMod.ModHelper.Manifest.ModFolderPath, "");
Logger.Log("Saving... " + relativePath + " to " + filePath);
loadedMod.ModHelper.Storage.Save(loadedConfigFiles[filePath], relativePath);
try
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Main.Instance.ModHelper.Manifest.ModFolderPath + backupFolderName + relativePath));
Main.Instance.ModHelper.Storage.Save(loadedConfigFiles[filePath], backupFolderName+relativePath);
}
catch (Exception e) { Logger.LogError("Failed to save backup file " + backupFolderName+relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); }
}
SaveLoadedConfigsForRecentSystem();
saveButtonUnlocked = false;
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
//if (GUILayout.Button("Print your mod's updated configs"))
//{
// UpdateLoadedConfigs();
// foreach (var filePath in loadedConfigFiles.Keys)
// {
// Logger.Log("The updated copy of " + filePath);
// Logger.Log(Newtonsoft.Json.JsonConvert.SerializeObject(loadedConfigFiles[filePath], Newtonsoft.Json.Formatting.Indented));
// }
// // _dpp.PrintConfigs();
//}
GUILayout.EndArea();
}
private void UpdateLoadedConfigs()
private void LoadMod(IModBehaviour mod)
{
// for each keyvalue in _dpp.GetPropsConfigByBody()
// find the matching entry loadedConfigFiles
// entry matches if the value of AstroOBjectLocator.FindBody(key) matches
loadedMod = mod;
DebugPropPlacer.active = true;
var newDetails = _dpp.GetPropsConfigByBody(true);
var folder = loadedMod.ModHelper.Manifest.ModFolderPath;
List<NewHorizonsBody> bodiesForThisMod = Main.BodyDict.Values.SelectMany(x => x).Where(x => x.Mod == loadedMod).ToList();
foreach (NewHorizonsBody body in bodiesForThisMod)
{
if (body.RelativePath == null)
{
Logger.Log("Error loading config for " + body.Config.Name + " in " + body.Config.StarSystem);
}
loadedConfigFiles[folder + body.RelativePath] = (body.Config as PlanetConfig);
_dpp.FindAndRegisterPropsFromConfig(body.Config);
}
}
private void SaveLoadedConfigsForRecentSystem()
{
UpdateLoadedConfigsForRecentSystem();
string backupFolderName = "configBackups\\" + DateTime.Now.ToString("yyyyMMddTHHmmss") + "\\";
Logger.Log($"(count) Saving {loadedConfigFiles.Keys.Count} files");
foreach (var filePath in loadedConfigFiles.Keys)
{
Logger.Log("Possibly Saving... " + loadedConfigFiles[filePath].Name + " @ " + filePath);
if (loadedConfigFiles[filePath].StarSystem != Main.Instance.CurrentStarSystem) continue;
var relativePath = filePath.Replace(loadedMod.ModHelper.Manifest.ModFolderPath, "");
try
{
Logger.Log("Saving... " + relativePath + " to " + filePath);
var directoryName = System.IO.Path.GetDirectoryName(loadedMod.ModHelper.Manifest.ModFolderPath + relativePath);
System.IO.Directory.CreateDirectory(directoryName);
loadedMod.ModHelper.Storage.Save(loadedConfigFiles[filePath], relativePath);
}
catch (Exception e) { Logger.LogError("Failed to save file " + backupFolderName+relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); }
try
{
var directoryName = System.IO.Path.GetDirectoryName(Main.Instance.ModHelper.Manifest.ModFolderPath + backupFolderName + relativePath);
System.IO.Directory.CreateDirectory(directoryName);
Main.Instance.ModHelper.Storage.Save(loadedConfigFiles[filePath], backupFolderName+relativePath);
}
catch (Exception e) { Logger.LogError("Failed to save backup file " + backupFolderName+relativePath); Logger.LogError(e.Message + "\n" + e.StackTrace); }
}
}
private void UpdateLoadedConfigsForRecentSystem()
{
var newDetails = _dpp.GetPropsConfigByBody();
Logger.Log("updating configs");
Logger.Log("New Details Counts by planet: " + string.Join(", ", newDetails.Keys.Select(x => x + $" ({newDetails[x].Length})")));
// TODO: looks like placing the first prop on a given planet in a given session clears out all existing props on that planet
Dictionary<string, string> planetToConfigPath = new Dictionary<string, string>();
// Get all configs
@ -242,43 +261,32 @@ namespace NewHorizons.Utility
{
Logger.Log("potentially updating copy of config at " + filePath);
if (loadedConfigFiles[filePath].StarSystem != Main.Instance.CurrentStarSystem) return;
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;
var astroObjectName = DebugPropPlacer.GetAstroObjectName(loadedConfigFiles[filePath].Name);
planetToConfigPath[astroObjectName] = filePath;
planetToConfigPath[composedName] = filePath;
if (!newDetails.ContainsKey(astroObjectName)) continue;
Logger.Log("made composed name from copy of config file for " + composedName + " " + newDetails.ContainsKey(composedName));
if (loadedConfigFiles[filePath].Props == null) loadedConfigFiles[filePath].Props = new External.PropModule();
loadedConfigFiles[filePath].Props.Details = newDetails[astroObjectName];
if (!newDetails.ContainsKey(composedName)) continue;
if (loadedConfigFiles[filePath].Props == null)
{
loadedConfigFiles[filePath].Props = new External.PropModule();
}
loadedConfigFiles[filePath].Props.Details = newDetails[composedName];
Logger.Log("successfully updated copy of config file for " + composedName);
Logger.Log("successfully updated copy of config file for " + astroObjectName);
}
// 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)
foreach (var astroObjectName in planetsThatDoNotHaveConfigFiles)
{
Logger.Log("Fabricating new config file for " + planetAndSystem);
Logger.Log("Fabricating new config file for " + astroObjectName);
var filepath = "planets/" + planetAndSystem + ".json";
var filepath = "planets/" + Main.Instance.CurrentStarSystem + "/" + astroObjectName + ".json";
PlanetConfig c = new PlanetConfig(null);
c.StarSystem = planetAndSystem.Split(separatorCharacter)[0];
c.Name = planetAndSystem.Split(separatorCharacter)[1];
c.StarSystem = Main.Instance.CurrentStarSystem;
c.Name = astroObjectName;
c.Props = new PropModule();
c.Props.Details = newDetails[planetAndSystem];
c.Props.Details = newDetails[astroObjectName];
loadedConfigFiles[filepath] = c;
}

View File

@ -31,9 +31,9 @@ namespace NewHorizons.Utility
private List<PropPlacementData> deletedProps = new List<PropPlacementData>();
private DebugRaycaster _rc;
public HashSet<string> RecentlyPlacedProps = new HashSet<string>();
public static HashSet<string> RecentlyPlacedProps = new HashSet<string>();
public bool active = false;
public static bool active = false;
private void Awake()
{
@ -114,7 +114,6 @@ namespace NewHorizons.Utility
g.transform.localPosition = prop.transform.localPosition;
g.transform.localRotation = prop.transform.localRotation;
System.Random r = new System.Random();
prop.transform.parent = g.transform;
var dirTowardsPlayer = prop.transform.parent.transform.InverseTransformPoint(playerAbsolutePosition) - prop.transform.localPosition;
@ -131,16 +130,29 @@ namespace NewHorizons.Utility
}
}
public static string GetAstroObjectName(string bodyName)
{
if (bodyName.EndsWith("_Body")) bodyName = bodyName.Substring(0, bodyName.Length-"_Body".Length);
var astroObject = AstroObjectLocator.GetAstroObject(bodyName);
if (astroObject == null) return null;
var astroObjectName = astroObject.name;
if (astroObjectName.EndsWith("_Body")) astroObjectName = astroObjectName.Substring(0, astroObjectName.Length-"_Body".Length);
return astroObjectName;
}
public void FindAndRegisterPropsFromConfig(IPlanetConfig config)
{
if (config.StarSystem != Main.Instance.CurrentStarSystem) return;
AstroObject planet = AstroObjectLocator.GetAstroObject(config.Name);
if (planet == null) return;
if (config.Props == null || config.Props.Details == null) return;
var bodyName = config.Name;
var astroObjectName = AstroObjectLocator.GetAstroObject(bodyName).name;
if (astroObjectName.EndsWith("_Body")) astroObjectName = astroObjectName.Substring(0, astroObjectName.Length-"_Body".Length);
var astroObjectName = GetAstroObjectName(config.Name);
foreach (var detail in config.Props.Details)
{
@ -152,7 +164,7 @@ namespace NewHorizons.Utility
continue;
}
PropPlacementData data = RegisterProp_WithReturn(astroObjectName, spawnedProp, detail.path, config.StarSystem, detail);
PropPlacementData data = RegisterProp_WithReturn(astroObjectName, spawnedProp, detail.path, detail);
if (!RecentlyPlacedProps.Contains(data.detailInfo.path))
{
@ -166,19 +178,19 @@ namespace NewHorizons.Utility
RegisterProp_WithReturn(bodyGameObjectName, prop);
}
private PropPlacementData RegisterProp_WithReturn(string bodyGameObjectName, GameObject prop, string propPath = null, string systemName = null, DetailInfo detailInfo = null)
private PropPlacementData RegisterProp_WithReturn(string bodyGameObjectName, GameObject prop, string propPath = 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;
string bodyName = GetAstroObjectName(bodyGameObjectName);
Logger.Log("Adding prop to " + Main.Instance.CurrentStarSystem + "::" + bodyName);
detailInfo = detailInfo == null ? new DetailInfo() : detailInfo;
detailInfo.path = propPath == null ? currentObject : propPath;
@ -186,7 +198,7 @@ namespace NewHorizons.Utility
{
body = bodyName,
gameObject = prop,
system = systemName == null ? Main.Instance.CurrentStarSystem : systemName,
system = Main.Instance.CurrentStarSystem,
detailInfo = detailInfo
};
@ -194,7 +206,7 @@ namespace NewHorizons.Utility
return data;
}
public Dictionary<string, DetailInfo[]> GetPropsConfigByBody(bool useAstroObjectName = false)
public Dictionary<string, DetailInfo[]> GetPropsConfigByBody()
{
var groupedProps = props
.GroupBy(p => p.system + "." + p.body)
@ -205,14 +217,13 @@ namespace NewHorizons.Utility
foreach (List<PropPlacementData> bodyProps in groupedProps)
{
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);
Logger.Log("getting prop group for body " + bodyProps[0].body);
if (AstroObjectLocator.GetAstroObject(bodyProps[0].body) == null) continue;
string bodyName = GetAstroObjectName(bodyProps[0].body);
DetailInfo[] infoArray = new DetailInfo[bodyProps.Count];
propConfigs[bodyProps[0].system + DebugMenu.separatorCharacter + bodyName] = infoArray;
propConfigs[bodyName] = infoArray;
for(int i = 0; i < bodyProps.Count; i++)
{