mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
chore: moved debug classes to their own package
This commit is contained in:
parent
f81cd34318
commit
f41e988b2e
327
NewHorizons/Utility/DebugUtilities/DebugMenu.cs
Normal file
327
NewHorizons/Utility/DebugUtilities/DebugMenu.cs
Normal file
@ -0,0 +1,327 @@
|
||||
using NewHorizons.External;
|
||||
using NewHorizons.External.Configs;
|
||||
using NewHorizons.External.Modules;
|
||||
using OWML.Common;
|
||||
using OWML.Common.Menus;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NewHorizons.Utility.DebugUtilities
|
||||
{
|
||||
|
||||
//
|
||||
//
|
||||
// TODO: split this into two separate classes "DebugMenu" and "DebugPropPlacerMenu"
|
||||
//
|
||||
//
|
||||
|
||||
[RequireComponent(typeof(DebugRaycaster))]
|
||||
[RequireComponent(typeof(DebugPropPlacer))]
|
||||
class DebugMenu : MonoBehaviour
|
||||
{
|
||||
private static IModButton pauseMenuButton;
|
||||
|
||||
GUIStyle _editorMenuStyle;
|
||||
Vector2 EditorMenuSize = new Vector2(600, 900);
|
||||
bool menuOpen = false;
|
||||
static bool openMenuOnPause;
|
||||
static bool staticInitialized;
|
||||
|
||||
DebugPropPlacer _dpp;
|
||||
DebugRaycaster _drc;
|
||||
|
||||
// menu params
|
||||
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 static readonly string favoritePropsPlayerPrefKey = "FavoriteProps";
|
||||
|
||||
//private string workingModName = "";
|
||||
private static IModBehaviour loadedMod = null;
|
||||
private Dictionary<string, PlanetConfig> loadedConfigFiles = new Dictionary<string, PlanetConfig>();
|
||||
private bool saveButtonUnlocked = false;
|
||||
private Vector2 recentModListScrollPosition = Vector2.zero;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_dpp = this.GetRequiredComponent<DebugPropPlacer>();
|
||||
_drc = this.GetRequiredComponent<DebugRaycaster>();
|
||||
LoadFavoriteProps();
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
if (!Main.Debug) return;
|
||||
if (!staticInitialized)
|
||||
{
|
||||
staticInitialized = true;
|
||||
|
||||
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());
|
||||
}
|
||||
else
|
||||
{
|
||||
InitMenu();
|
||||
}
|
||||
|
||||
if (loadedMod != null)
|
||||
{
|
||||
LoadMod(loadedMod);
|
||||
}
|
||||
}
|
||||
|
||||
private void PauseMenuInitHook()
|
||||
{
|
||||
pauseMenuButton = Main.Instance.ModHelper.Menus.PauseMenu.OptionsButton.Duplicate("Toggle Prop Placer Menu".ToUpper());
|
||||
InitMenu();
|
||||
}
|
||||
private void RestoreMenuOpennessState() { menuOpen = openMenuOnPause; }
|
||||
private void ToggleMenu() { menuOpen = !menuOpen; openMenuOnPause = !openMenuOnPause; }
|
||||
|
||||
private void CloseMenu() { menuOpen = false; }
|
||||
|
||||
private void LoadFavoriteProps()
|
||||
{
|
||||
string favoritePropsPlayerPref = PlayerPrefs.GetString(favoritePropsPlayerPrefKey);
|
||||
|
||||
if (favoritePropsPlayerPref == null || favoritePropsPlayerPref == "") return;
|
||||
|
||||
var favoritePropPaths = favoritePropsPlayerPref.Split(separatorCharacter);
|
||||
foreach (string favoriteProp in favoritePropPaths)
|
||||
{
|
||||
DebugPropPlacer.RecentlyPlacedProps.Add(favoriteProp);
|
||||
this.favoriteProps.Add(favoriteProp);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
if (!menuOpen) return;
|
||||
if (!Main.Debug) return;
|
||||
|
||||
Vector2 menuPosition = new Vector2(10, 40);
|
||||
|
||||
GUILayout.BeginArea(new Rect(menuPosition.x, menuPosition.y, EditorMenuSize.x, EditorMenuSize.y), _editorMenuStyle);
|
||||
|
||||
//
|
||||
// DebugPropPlacer
|
||||
//
|
||||
GUILayout.Label("Recently placed objects");
|
||||
_dpp.SetCurrentObject(GUILayout.TextArea(_dpp.currentObject));
|
||||
|
||||
GUILayout.Space(5);
|
||||
|
||||
// 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 DebugPropPlacer.RecentlyPlacedProps)
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
var propPathElements = propPath.Split('/');
|
||||
string propName = propPathElements[propPathElements.Length-1];
|
||||
|
||||
string favoriteButtonIcon = favoriteProps.Contains(propPath) ? "★" : "☆";
|
||||
if (GUILayout.Button(favoriteButtonIcon, GUILayout.ExpandWidth(false)))
|
||||
{
|
||||
if (favoriteProps.Contains(propPath))
|
||||
{
|
||||
favoriteProps.Remove(propPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
favoriteProps.Add(propPath);
|
||||
}
|
||||
|
||||
string[] favoritePropsArray = favoriteProps.ToArray<string>();
|
||||
PlayerPrefs.SetString(favoritePropsPlayerPrefKey, string.Join(separatorCharacter+"", favoritePropsArray));
|
||||
}
|
||||
|
||||
if (GUILayout.Button(propName))
|
||||
{
|
||||
_dpp.SetCurrentObject(propPath);
|
||||
}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
GUILayout.EndScrollView();
|
||||
|
||||
GUILayout.Space(5);
|
||||
|
||||
// continue working on existing mod
|
||||
|
||||
GUILayout.Label("Name of your mod");
|
||||
if (loadedMod == null)
|
||||
{
|
||||
recentModListScrollPosition = GUILayout.BeginScrollView(recentModListScrollPosition, GUILayout.Width(EditorMenuSize.x), GUILayout.Height(100));
|
||||
|
||||
foreach (var mod in Main.MountedAddons)
|
||||
{
|
||||
if (GUILayout.Button(mod.ModHelper.Manifest.UniqueName))
|
||||
{
|
||||
LoadMod(mod);
|
||||
}
|
||||
}
|
||||
|
||||
GUILayout.EndScrollView();
|
||||
}
|
||||
else
|
||||
{
|
||||
GUILayout.Label(loadedMod.ModHelper.Manifest.UniqueName);
|
||||
}
|
||||
|
||||
GUILayout.Space(5);
|
||||
|
||||
// save your work
|
||||
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
if (GUILayout.Button(saveButtonUnlocked ? " O " : " | ", GUILayout.ExpandWidth(false)))
|
||||
{
|
||||
saveButtonUnlocked = !saveButtonUnlocked;
|
||||
}
|
||||
GUI.enabled = saveButtonUnlocked;
|
||||
if (GUILayout.Button("Update your mod's configs"))
|
||||
{
|
||||
SaveLoadedConfigsForRecentSystem();
|
||||
saveButtonUnlocked = false;
|
||||
}
|
||||
GUI.enabled = true;
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
|
||||
GUILayout.EndArea();
|
||||
}
|
||||
|
||||
private void LoadMod(IModBehaviour mod)
|
||||
{
|
||||
loadedMod = mod;
|
||||
DebugPropPlacer.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);
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveLoadedConfigsForRecentSystem()
|
||||
{
|
||||
UpdateLoadedConfigsForRecentSystem();
|
||||
|
||||
string backupFolderName = "configBackups\\" + DateTime.Now.ToString("yyyyMMddTHHmmss") + "\\";
|
||||
Logger.Log($"Potentially 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 config files. New Details Counts by planet: " + string.Join(", ", newDetails.Keys.Select(x => x + $" ({newDetails[x].Length})")));
|
||||
|
||||
Dictionary<string, string> planetToConfigPath = new Dictionary<string, string>();
|
||||
|
||||
// Get all configs
|
||||
foreach (var filePath in loadedConfigFiles.Keys)
|
||||
{
|
||||
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 astroObjectName = DebugPropPlacer.GetAstroObjectName(loadedConfigFiles[filePath].Name);
|
||||
planetToConfigPath[astroObjectName] = filePath;
|
||||
|
||||
if (!newDetails.ContainsKey(astroObjectName)) continue;
|
||||
|
||||
if (loadedConfigFiles[filePath].Props == null) loadedConfigFiles[filePath].Props = new External.Modules.PropModule();
|
||||
loadedConfigFiles[filePath].Props.Details = newDetails[astroObjectName];
|
||||
|
||||
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 astroObjectName in planetsThatDoNotHaveConfigFiles)
|
||||
{
|
||||
Logger.Log("Fabricating new config file for " + astroObjectName);
|
||||
|
||||
var filepath = "planets/" + Main.Instance.CurrentStarSystem + "/" + astroObjectName + ".json";
|
||||
PlanetConfig c = new PlanetConfig();
|
||||
c.StarSystem = Main.Instance.CurrentStarSystem;
|
||||
c.Name = astroObjectName;
|
||||
c.Props = new PropModule();
|
||||
c.Props.Details = newDetails[astroObjectName];
|
||||
|
||||
loadedConfigFiles[filepath] = c;
|
||||
}
|
||||
}
|
||||
|
||||
private void InitMenu()
|
||||
{
|
||||
if (_editorMenuStyle != null) return;
|
||||
|
||||
// TODO: figure out how to clear this event list so that we don't pile up useless instances of the DebugMenu that can't get garbage collected
|
||||
pauseMenuButton.OnClick += ToggleMenu;
|
||||
|
||||
_dpp = this.GetRequiredComponent<DebugPropPlacer>();
|
||||
_drc = this.GetRequiredComponent<DebugRaycaster>();
|
||||
|
||||
|
||||
Texture2D bgTexture = ImageUtilities.MakeSolidColorTexture((int)EditorMenuSize.x, (int)EditorMenuSize.y, Color.black);
|
||||
|
||||
_editorMenuStyle = new GUIStyle
|
||||
{
|
||||
normal =
|
||||
{
|
||||
background = bgTexture
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
265
NewHorizons/Utility/DebugUtilities/DebugPropPlacer.cs
Normal file
265
NewHorizons/Utility/DebugUtilities/DebugPropPlacer.cs
Normal file
@ -0,0 +1,265 @@
|
||||
using NewHorizons.Builder.Props;
|
||||
using NewHorizons.External.Configs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using static NewHorizons.External.Modules.PropModule;
|
||||
|
||||
namespace NewHorizons.Utility.DebugUtilities
|
||||
{
|
||||
|
||||
[RequireComponent(typeof(DebugRaycaster))]
|
||||
class DebugPropPlacer : MonoBehaviour
|
||||
{
|
||||
private struct PropPlacementData
|
||||
{
|
||||
public string body;
|
||||
public string system;
|
||||
public GameObject gameObject;
|
||||
public DetailInfo detailInfo;
|
||||
}
|
||||
|
||||
public static readonly string DEFAULT_OBJECT = "BrittleHollow_Body/Sector_BH/Sector_NorthHemisphere/Sector_NorthPole/Sector_HangingCity/Sector_HangingCity_District1/Props_HangingCity_District1/OtherComponentsGroup/Props_HangingCity_Building_10/Prefab_NOM_VaseThin";
|
||||
|
||||
public string currentObject { get; private set; }
|
||||
private bool hasAddedCurrentObjectToRecentsList = false;
|
||||
private List<PropPlacementData> props = new List<PropPlacementData>();
|
||||
private List<PropPlacementData> deletedProps = new List<PropPlacementData>();
|
||||
private DebugRaycaster _rc;
|
||||
|
||||
public static HashSet<string> RecentlyPlacedProps = new HashSet<string>();
|
||||
|
||||
public static bool active = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rc = this.GetRequiredComponent<DebugRaycaster>();
|
||||
currentObject = DEFAULT_OBJECT;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!Main.Debug) return;
|
||||
if (!active) return;
|
||||
|
||||
if (Keyboard.current[Key.G].wasReleasedThisFrame)
|
||||
{
|
||||
PlaceObject();
|
||||
}
|
||||
|
||||
if (Keyboard.current[Key.Minus].wasReleasedThisFrame)
|
||||
{
|
||||
DeleteLast();
|
||||
}
|
||||
|
||||
if (Keyboard.current[Key.Equals].wasReleasedThisFrame)
|
||||
{
|
||||
UndoDelete();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCurrentObject(string s)
|
||||
{
|
||||
currentObject = s;
|
||||
hasAddedCurrentObjectToRecentsList = false;
|
||||
}
|
||||
|
||||
internal void PlaceObject()
|
||||
{
|
||||
DebugRaycastData data = _rc.Raycast();
|
||||
PlaceObject(data, this.gameObject.transform.position);
|
||||
|
||||
if (!hasAddedCurrentObjectToRecentsList)
|
||||
{
|
||||
hasAddedCurrentObjectToRecentsList = true;
|
||||
|
||||
if (!RecentlyPlacedProps.Contains(currentObject))
|
||||
{
|
||||
RecentlyPlacedProps.Add(currentObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void PlaceObject(DebugRaycastData data, Vector3 playerAbsolutePosition)
|
||||
{
|
||||
// TODO: implement sectors
|
||||
// if this hits a sector, store that sector and add a config file option for it
|
||||
|
||||
if (!data.hitObject.name.EndsWith("_Body"))
|
||||
{
|
||||
Logger.Log("Cannot place object on non-body object: " + data.hitObject.name);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (currentObject == "" || currentObject == null)
|
||||
{
|
||||
SetCurrentObject(DEFAULT_OBJECT);
|
||||
}
|
||||
|
||||
GameObject prop = DetailBuilder.MakeDetail(data.hitObject, data.hitObject.GetComponentInChildren<Sector>(), currentObject, data.pos, data.norm, 1, false);
|
||||
PropPlacementData propData = RegisterProp_WithReturn(data.bodyName, prop);
|
||||
|
||||
// align with surface normal
|
||||
Vector3 alignToSurface = (Quaternion.LookRotation(data.norm) * Quaternion.FromToRotation(Vector3.up, Vector3.forward)).eulerAngles;
|
||||
prop.transform.localEulerAngles = alignToSurface;
|
||||
|
||||
// rotate facing dir towards player
|
||||
GameObject g = new GameObject();
|
||||
g.transform.parent = prop.transform.parent;
|
||||
g.transform.localPosition = prop.transform.localPosition;
|
||||
g.transform.localRotation = prop.transform.localRotation;
|
||||
|
||||
prop.transform.parent = g.transform;
|
||||
|
||||
var dirTowardsPlayer = prop.transform.parent.transform.InverseTransformPoint(playerAbsolutePosition) - prop.transform.localPosition;
|
||||
dirTowardsPlayer.y = 0;
|
||||
float rotation = Quaternion.LookRotation(dirTowardsPlayer).eulerAngles.y;
|
||||
prop.transform.localEulerAngles = new Vector3(0, rotation, 0);
|
||||
|
||||
prop.transform.parent = g.transform.parent;
|
||||
GameObject.Destroy(g);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Logger.Log($"Failed to place object {currentObject} on body ${data.hitObject} at location ${data.pos}.");
|
||||
}
|
||||
}
|
||||
|
||||
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(PlanetConfig 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 astroObjectName = GetAstroObjectName(config.Name);
|
||||
|
||||
foreach (var detail in config.Props.Details)
|
||||
{
|
||||
GameObject spawnedProp = DetailBuilder.GetSpawnedGameObjectByDetailInfo(detail);
|
||||
|
||||
if (spawnedProp == null)
|
||||
{
|
||||
Logger.LogError("No spawned prop found for " + detail.path);
|
||||
continue;
|
||||
}
|
||||
|
||||
PropPlacementData data = RegisterProp_WithReturn(astroObjectName, spawnedProp, detail.path, detail);
|
||||
|
||||
if (!RecentlyPlacedProps.Contains(data.detailInfo.path))
|
||||
{
|
||||
RecentlyPlacedProps.Add(data.detailInfo.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterProp(string bodyGameObjectName, GameObject prop)
|
||||
{
|
||||
RegisterProp_WithReturn(bodyGameObjectName, prop);
|
||||
}
|
||||
|
||||
private PropPlacementData RegisterProp_WithReturn(string bodyGameObjectName, GameObject prop, string propPath = null, DetailInfo detailInfo = null)
|
||||
{
|
||||
if (Main.Debug)
|
||||
{
|
||||
// TOOD: make this prop an item
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
|
||||
PropPlacementData data = new PropPlacementData
|
||||
{
|
||||
body = bodyName,
|
||||
gameObject = prop,
|
||||
system = Main.Instance.CurrentStarSystem,
|
||||
detailInfo = detailInfo
|
||||
};
|
||||
|
||||
props.Add(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
public Dictionary<string, DetailInfo[]> GetPropsConfigByBody()
|
||||
{
|
||||
var groupedProps = props
|
||||
.GroupBy(p => p.system + "." + p.body)
|
||||
.Select(grp => grp.ToList())
|
||||
.ToList();
|
||||
|
||||
Dictionary<string, DetailInfo[]> propConfigs = new Dictionary<string, DetailInfo[]>();
|
||||
|
||||
foreach (List<PropPlacementData> bodyProps in groupedProps)
|
||||
{
|
||||
if (bodyProps == null || bodyProps.Count == 0) continue;
|
||||
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[bodyName] = infoArray;
|
||||
|
||||
for(int i = 0; i < bodyProps.Count; i++)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return propConfigs;
|
||||
}
|
||||
|
||||
public void DeleteLast()
|
||||
{
|
||||
if (props.Count <= 0) return;
|
||||
|
||||
PropPlacementData last = props[props.Count-1];
|
||||
props.RemoveAt(props.Count-1);
|
||||
|
||||
last.gameObject.SetActive(false);
|
||||
|
||||
deletedProps.Add(last);
|
||||
}
|
||||
|
||||
public void UndoDelete()
|
||||
{
|
||||
if (deletedProps.Count <= 0) return;
|
||||
|
||||
PropPlacementData last = deletedProps[deletedProps.Count-1];
|
||||
deletedProps.RemoveAt(deletedProps.Count-1);
|
||||
|
||||
last.gameObject.SetActive(true);
|
||||
|
||||
props.Add(last);
|
||||
}
|
||||
}
|
||||
}
|
||||
20
NewHorizons/Utility/DebugUtilities/DebugRaycastData.cs
Normal file
20
NewHorizons/Utility/DebugUtilities/DebugRaycastData.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NewHorizons.Utility.DebugUtilities
|
||||
{
|
||||
struct DebugRaycastData
|
||||
{
|
||||
public bool hit;
|
||||
public Vector3 pos;
|
||||
public Vector3 norm;
|
||||
|
||||
public string bodyName;
|
||||
public string bodyPath;
|
||||
public GameObject hitObject;
|
||||
}
|
||||
}
|
||||
78
NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs
Normal file
78
NewHorizons/Utility/DebugUtilities/DebugRaycaster.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NewHorizons.Utility.DebugUtilities
|
||||
{
|
||||
[RequireComponent(typeof(OWRigidbody))]
|
||||
public class DebugRaycaster : MonoBehaviour
|
||||
{
|
||||
private OWRigidbody _rb;
|
||||
private GameObject _surfaceSphere;
|
||||
private GameObject _normalSphere1;
|
||||
private GameObject _normalSphere2;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rb = this.GetRequiredComponent<OWRigidbody>();
|
||||
}
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!Main.Debug) return;
|
||||
if (Keyboard.current == null) return;
|
||||
|
||||
if (Keyboard.current[Key.P].wasReleasedThisFrame)
|
||||
{
|
||||
PrintRaycast();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal void PrintRaycast()
|
||||
{
|
||||
DebugRaycastData data = Raycast();
|
||||
var posText = $"{{\"x\": {data.pos.x}, \"y\": {data.pos.y}, \"z\": {data.pos.z}}}";
|
||||
var normText = $"{{\"x\": {data.norm.x}, \"y\": {data.norm.y}, \"z\": {data.norm.z}}}";
|
||||
|
||||
if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere);
|
||||
if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1);
|
||||
if(_normalSphere2 != null) GameObject.Destroy(_normalSphere2);
|
||||
|
||||
_surfaceSphere = AddDebugShape.AddSphere(data.hitObject, 0.1f, Color.green);
|
||||
_normalSphere1 = AddDebugShape.AddSphere(data.hitObject, 0.01f, Color.red);
|
||||
_normalSphere2 = AddDebugShape.AddSphere(data.hitObject, 0.01f, Color.red);
|
||||
|
||||
_surfaceSphere.transform.localPosition = data.pos;
|
||||
_normalSphere1.transform.localPosition = data.pos + data.norm * 0.5f;
|
||||
_normalSphere2.transform.localPosition = data.pos + data.norm;
|
||||
|
||||
Logger.Log($"Raycast hit \"position\": {posText}, \"normal\": {normText} on [{data.bodyName}] at [{data.bodyPath}]");
|
||||
}
|
||||
internal DebugRaycastData Raycast()
|
||||
{
|
||||
DebugRaycastData data = new DebugRaycastData();
|
||||
|
||||
_rb.DisableCollisionDetection();
|
||||
int layerMask = OWLayerMask.physicalMask;
|
||||
var origin = Locator.GetActiveCamera().transform.position;
|
||||
var direction = Locator.GetActiveCamera().transform.TransformDirection(Vector3.forward);
|
||||
|
||||
data.hit = Physics.Raycast(origin, direction, out RaycastHit hitInfo, 100f, layerMask);
|
||||
if (data.hit)
|
||||
{
|
||||
data.pos = hitInfo.transform.InverseTransformPoint(hitInfo.point);
|
||||
data.norm = hitInfo.transform.InverseTransformDirection(hitInfo.normal);
|
||||
var o = hitInfo.transform.gameObject;
|
||||
|
||||
data.bodyName = o.name;
|
||||
data.bodyPath = SearchUtilities.GetPath(o.transform);
|
||||
data.hitObject = hitInfo.transform.gameObject;
|
||||
}
|
||||
_rb.EnableCollisionDetection();
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
55
NewHorizons/Utility/DebugUtilities/DebugReload.cs
Normal file
55
NewHorizons/Utility/DebugUtilities/DebugReload.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using NewHorizons.Handlers;
|
||||
using OWML.Common;
|
||||
using OWML.Common.Menus;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NewHorizons.Utility.DebugUtilities
|
||||
{
|
||||
public static class DebugReload
|
||||
{
|
||||
|
||||
private static IModButton _reloadButton;
|
||||
|
||||
public static void InitializePauseMenu()
|
||||
{
|
||||
_reloadButton = Main.Instance.ModHelper.Menus.PauseMenu.OptionsButton.Duplicate(TranslationHandler.GetTranslation("Reload Configs", TranslationHandler.TextType.UI).ToUpper());
|
||||
_reloadButton.OnClick += ReloadConfigs;
|
||||
UpdateReloadButton();
|
||||
}
|
||||
|
||||
public static void UpdateReloadButton()
|
||||
{
|
||||
if (_reloadButton != null)
|
||||
{
|
||||
if (Main.Debug) _reloadButton.Show();
|
||||
else _reloadButton.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
private static void ReloadConfigs()
|
||||
{
|
||||
Logger.Log("Begin reload of config files...", Logger.LogType.Log);
|
||||
|
||||
Main.ResetConfigs();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (IModBehaviour mountedAddon in Main.MountedAddons)
|
||||
{
|
||||
Main.Instance.LoadConfigs(mountedAddon);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Logger.LogWarning("Error While Reloading");
|
||||
}
|
||||
|
||||
GameObject.Find("/PauseMenu/PauseMenuManagers").GetComponent<PauseMenuManager>().OnSkipToNextTimeLoop();
|
||||
|
||||
Main.Instance.ChangeCurrentStarSystem(Main.Instance.CurrentStarSystem);
|
||||
|
||||
Main.SecondsLeftInLoop = -1f;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -293,5 +293,19 @@ namespace NewHorizons.Utility
|
||||
|
||||
return new Color(r / 255, g / 255, b / 255);
|
||||
}
|
||||
public static Texture2D MakeSolidColorTexture(int width, int height, Color color)
|
||||
{
|
||||
Color[] pixels = new Color[width*height];
|
||||
|
||||
for(int i = 0; i < pixels.Length; i++)
|
||||
{
|
||||
pixels[i] = color;
|
||||
}
|
||||
|
||||
Texture2D newTexture = new Texture2D(width, height);
|
||||
newTexture.SetPixels(pixels);
|
||||
newTexture.Apply();
|
||||
return newTexture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user