diff --git a/.gitignore b/.gitignore index af49890..9f673cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,365 +1,51 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore - -external_dlls/ - -# User-specific files -*.rsuser +# General C# project *.suo *.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Mono auto generated files -mono_crash.* - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -[Ww][Ii][Nn]32/ -[Aa][Rr][Mm]/ -[Aa][Rr][Mm]64/ -bld/ +.vs/ [Bb]in/ [Oo]bj/ -[Oo]ut/ -[Ll]og/ -[Ll]ogs/ - -# Visual Studio 2015/2017 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# Visual Studio 2017 auto generated files -Generated\ Files/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUnit -*.VisualState.xml -TestResult.xml -nunit-*.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# Benchmark Results -BenchmarkDotNet.Artifacts/ - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ - -# ASP.NET Scaffolding -ScaffoldingReadMe.txt - -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_h.h -*.ilk -*.meta -*.obj -*.iobj -*.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*_wpftmp.csproj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# Visual Studio Trace Files -*.e2e - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# AxoCover is a Code Coverage Tool -.axoCover/* -!.axoCover/settings.json - -# Coverlet is a free, cross platform Code Coverage Tool -coverage*.json -coverage*.xml -coverage*.info - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# Note: Comment the next line if you want to checkin your web deploy settings, -# but database connection strings (with potential passwords) will be unencrypted -*.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# NuGet Symbol Packages -*.snupkg -# The packages folder can be ignored because of Package Restore -**/[Pp]ackages/* -# except build/, which is used as an MSBuild target. -!**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt -*.appx -*.appxbundle -*.appxupload - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!?*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Including strong name files can present a security risk -# (https://github.com/github/gitignore/pull/2483#issue-259490424) -#*.snk - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) _UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm -ServiceFabricBackup/ -*.rptproj.bak - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings -*.rptproj.rsuser -*- [Bb]ackup.rdl -*- [Bb]ackup ([0-9]).rdl -*- [Bb]ackup ([0-9][0-9]).rdl - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# CodeRush personal settings -.cr/personal - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -# OpenCover UI analysis results -OpenCover/ - -# Azure Stream Analytics local run output -ASALocalRun/ - -# MSBuild Binary and Structured Log -*.binlog - -# NVidia Nsight GPU debugger configuration file -*.nvuser - -# MFractors (Xamarin productivity tool) working folder -.mfractor/ - -# Local History for Visual Studio -.localhistory/ - -# BeatPulse healthcheck temp database -healthchecksdb - -# Backup folder for Package Reference Convert tool in Visual Studio 2017 -MigrationBackup/ - -# Ionide (cross platform F# VS Code tools) working folder -.ionide/ - -# Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +/[Pp]ackages/ + +# Mod template folders +/[Bb]uild/ +/[Dd]ist/ +/[Nn]uget/ + +# Rider + +# User specific +**/.idea/**/workspace.xml +**/.idea/**/tasks.xml +**/.idea/shelf/* +**/.idea/dictionaries +**/.idea/httpRequests/ + +# Sensitive or high-churn files +**/.idea/**/dataSources/ +**/.idea/**/dataSources.ids +**/.idea/**/dataSources.xml +**/.idea/**/dataSources.local.xml +**/.idea/**/sqlDataSources.xml +**/.idea/**/dynamic.xml + +# Rider auto-generates .iml files, and contentModel.xml +**/.idea/**/*.iml +**/.idea/**/contentModel.xml +**/.idea/**/modules.xml + +# VS Code files +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# OS-specific +Thumbs.db +Desktop.ini +.DS_Store diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..14d4d06 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,59 @@ + + + + netstandard2.1 + true + latest + true + + + + + C:/Program Files (x86)/Steam/steamapps/common/Kerbal Space Program 2 + + + + $(MSBuildThisFileDirectory) + $(SolutionDir)build/bin/plugin/$(Configuration) + $(SolutionDir)build/obj/plugin/$(Configuration) + $(PluginBinPath)/$(MSBuildProjectName) + $(PluginObjPath)/$(MSBuildProjectName) + $(MSBuildProjectName) + $(AssemblyName) + Debug;Release;Deploy;DeployAndRun + AnyCPU + $(NoWarn);CS0436 + + + + + https://nuget.spacewarp.org/v3/index.json + + + + + + + + + + + + + + + + @(Swinfo -> '%(mod_id)') + @(Swinfo -> '%(version)') + $(Version.Substring(0, $(Version.IndexOf('-')))) + @(Swinfo -> '%(name)') + @(Swinfo -> '%(author)') + @(Swinfo -> '%(description)') + git + @(Swinfo -> '%(source)') + $(ModId) + $(Product) + $(Version) + + + diff --git a/HideOrbits.sln b/HideOrbits.sln index 6be1b63..8b3a4e1 100644 --- a/HideOrbits.sln +++ b/HideOrbits.sln @@ -1,20 +1,25 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 -VisualStudioVersion = 17.5.33424.131 +VisualStudioVersion = 17.5.33502.453 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HideOrbits", "HideOrbitsProject\HideOrbits.csproj", "{D734C8FD-21D3-4C99-8722-3324D9A63F34}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HideOrbits", "src\HideOrbits\HideOrbits.csproj", "{747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Deploy|Any CPU = Deploy|Any CPU + DeployAndRun|Any CPU = DeployAndRun|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D734C8FD-21D3-4C99-8722-3324D9A63F34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D734C8FD-21D3-4C99-8722-3324D9A63F34}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D734C8FD-21D3-4C99-8722-3324D9A63F34}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D734C8FD-21D3-4C99-8722-3324D9A63F34}.Release|Any CPU.Build.0 = Release|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.Deploy|Any CPU.Build.0 = Deploy|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {747F4A4F-2AE2-4C45-AB24-E56E7BF3673C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/HideOrbitsProject/HideOrbits.csproj b/HideOrbitsProject/HideOrbits.csproj deleted file mode 100644 index c407559..0000000 --- a/HideOrbitsProject/HideOrbits.csproj +++ /dev/null @@ -1,42 +0,0 @@ - - - - netstandard2.0 - true - latest - true - com.github.colinzeidler.hide_distant_orbits - Hide Orbits - Automatically hide distant orbits while zoomed in - 0.4.0 - - https://api.nuget.org/v3/index.json; - https://nuget.bepinex.dev/v3/index.json - - HideOrbits - hide_orbits - - - - ..\external_dlls\Assembly-CSharp.dll - true - false - - - ..\external_dlls\SpaceWarp.dll - true - false - - - - - - - - - - - - - - diff --git a/HideOrbitsProject/OrbitHiderPatch.cs b/HideOrbitsProject/OrbitHiderPatch.cs deleted file mode 100644 index b424dfc..0000000 --- a/HideOrbitsProject/OrbitHiderPatch.cs +++ /dev/null @@ -1,118 +0,0 @@ -using HarmonyLib; -using KSP.Game; -using KSP.Map; -using KSP.Sim.impl; -using UnityEngine; - -namespace HideOrbits -{ - internal class OrbitHiderPatch - { - public static Color hiddenOrbit = new(0, 0, 0, 0); - - [HarmonyPatch(typeof(OrbitRenderer), nameof(OrbitRenderer.UpdateOrbitStyling))] - [HarmonyPostfix] - public static void OrbitRenderer_UpdateOrbitStyling(Dictionary ____orbitRenderData) - { - CelestialBodyComponent vesselParentBody = null; - VesselComponent activeVessel = GameManager.Instance.Game.ViewController.GetActiveSimVessel(true); - if (activeVessel != null) - { - //HideOrbitsPlugin.Instance.logger.LogInfo($"Vessel Guid {activeVessel.Guid}"); - vesselParentBody = activeVessel.mainBody; - //HideOrbitsPlugin.Instance.logger.LogInfo($"Vessel parent Guid {vesselParentBody.SimulationObject.GlobalId}"); - } - - if (HideOrbitsPlugin.Instance != null && HideOrbitsPlugin.Instance.AutoHideOrbits) { - //HideOrbitsPlugin.Instance.logger.LogInfo("Updating orbits"); - - //Collect bodies related to local planet (and its moons) - Dictionary vesselParentChildren = new(); - foreach (OrbitRenderer.OrbitRenderData orbitRenderData in ____orbitRenderData.Values) - { - if (orbitRenderData.Segments != null) - { - if (orbitRenderData.IsCelestialBody) - { - CelestialBodyComponent orbitBody = GameManager.Instance.Game.SpaceSimulation.GetSimulationObjectComponent(orbitRenderData.ParentGuid); - if (orbitBody.HasChild(vesselParentBody) || orbitBody.SimulationObject.GlobalId == vesselParentBody.SimulationObject.GlobalId) - { - if (!orbitBody.IsStar) - { - foreach (CelestialBodyComponent child in orbitBody.orbitingBodies) - { - if (child != null) - { - vesselParentChildren.Add(child.SimulationObject.GlobalId, child); - } - } - } - } - } - } - } - - // Hide unimportant orbits - foreach (OrbitRenderer.OrbitRenderData orbitRenderData in ____orbitRenderData.Values) - { - if (orbitRenderData.Segments != null) - { - if (orbitRenderData.IsCelestialBody) - { - // orbitRenderData.ParentGuid is the Guid of the planet the orbit is for. - if (orbitRenderData.ParentGuid == vesselParentBody?.SimulationObject.GlobalId) - { - continue; - } - - // check if current orbitRenderData body is a child of vesselParentBody - CelestialBodyComponent orbitBody = GameManager.Instance.Game.SpaceSimulation.GetSimulationObjectComponent(orbitRenderData.ParentGuid); - if (vesselParentChildren.ContainsKey(orbitRenderData.ParentGuid)) - { - continue; - } - - if ((bool)orbitBody?.HasParent(vesselParentBody)) - { - continue; - } - - // TODO what happens if we orbit Mun, will we see Minmus Orbit? Kerbin Orbit? - if (orbitBody.HasChild(vesselParentBody)) - { - continue; - } - - bool targetOrbit = activeVessel != null && activeVessel.TargetObjectId == orbitRenderData.Orbiter.SimulationObject.GlobalId; - if (targetOrbit) - { - continue; - // TODO change color of target orbits? - } - - foreach (OrbitRenderSegment segment in orbitRenderData.Segments) - { - segment.SetColors(hiddenOrbit, hiddenOrbit); - } - } - else - { - if (!HideOrbitsPlugin.Instance.HideVesselOrbits) - { - continue; - } - if (orbitRenderData.Vessel.GlobalId != activeVessel.GlobalId && orbitRenderData.Vessel.GlobalId != activeVessel.TargetObjectId) - { - foreach (OrbitRenderSegment segment in orbitRenderData.Segments) - { - segment.SetColors(hiddenOrbit, hiddenOrbit); - } - } - } - } - } // end Hide orbits - - } - } - } -} diff --git a/README.md b/README.md index 2f371b1..d4f4ed8 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ Automatically hide distant orbits while zoomed in ## Installing Extract and copy into your KSP2 install directory -Requires SpaceWarp 0.4.0: https://github.com/SpaceWarpDev/SpaceWarp +Requires SpaceWarp 1.7.0: https://github.com/SpaceWarpDev/SpaceWarp +Requires UITKForKSP2 ## Issues - Distant orbits are always hidden (can be displayed using the toggle in the mods UI menu) @@ -12,5 +13,8 @@ Requires SpaceWarp 0.4.0: https://github.com/SpaceWarpDev/SpaceWarp Please report any issues at https://github.com/ColinZeidler/KSP2-HideDistantOrbits/issues ## Changelog + ### v0.5.0 + - Support for ksp 0.2.0 + ### v0.3.0 - Local moons should now always show their orbits \ No newline at end of file diff --git a/build-debug.bat b/build-debug.bat deleted file mode 100644 index 72e1aa4..0000000 --- a/build-debug.bat +++ /dev/null @@ -1,2 +0,0 @@ -@echo off -dotnet build HideOrbits.sln -c Debug \ No newline at end of file diff --git a/build-release.bat b/build-release.bat deleted file mode 100644 index 20cd6e2..0000000 --- a/build-release.bat +++ /dev/null @@ -1,2 +0,0 @@ -@echo off -dotnet build HideOrbits.sln -c Release \ No newline at end of file diff --git a/hide_orbits/assets/images/icon.png b/hide_orbits/assets/images/icon.png deleted file mode 100644 index 19fa71a..0000000 Binary files a/hide_orbits/assets/images/icon.png and /dev/null differ diff --git a/hide_orbits/swinfo.json b/hide_orbits/swinfo.json deleted file mode 100644 index 86fe05e..0000000 --- a/hide_orbits/swinfo.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "mod_id": "hide_orbits", - "author": "Strykker2", - "name": "Hide Orbits", - "description": "Automatically hide distant orbits while zoomed in", - "source": "https://github.com/ColinZeidler/KSP2-HideDistantOrbits", - "version": "0.4.0", - "dependencies": [ - { - "id": "SpaceWarp", - "version": { - "min": "0.4.0", - "max": "*" - } - } - ], - "ksp2_version": { - "min": "0.1.1.0", - "max": "*" - } -} \ No newline at end of file diff --git a/plugin_template/assets/images/icon.png b/plugin_template/assets/images/icon.png new file mode 100644 index 0000000..614f4aa Binary files /dev/null and b/plugin_template/assets/images/icon.png differ diff --git a/plugin_template/swinfo.json b/plugin_template/swinfo.json new file mode 100644 index 0000000..c781902 --- /dev/null +++ b/plugin_template/swinfo.json @@ -0,0 +1,23 @@ +{ + "spec": "2.0", + "mod_id": "HideOrbits", + "author": "Strykker2", + "name": "HideOrbits", + "description": "UI to toggle distant orbits if they get in the way", + "source": "https://github.com/ColinZeidler/KSP2-HideDistantOrbits", + "version": "0.5.0", + "version_check": "", + "ksp2_version": { + "min": "0.2.0", + "max": "*" + }, + "dependencies": [ + { + "id": "com.github.x606.spacewarp", + "version": { + "min": "1.7.0", + "max": "*" + } + }, + ] +} diff --git a/scripts/build-debug.bat b/scripts/build-debug.bat new file mode 100644 index 0000000..4fc25fd --- /dev/null +++ b/scripts/build-debug.bat @@ -0,0 +1,2 @@ +@echo off +dotnet build "../HideOrbits.sln" -c Debug diff --git a/scripts/build-deploy.bat b/scripts/build-deploy.bat new file mode 100644 index 0000000..98e79ef --- /dev/null +++ b/scripts/build-deploy.bat @@ -0,0 +1,2 @@ +@echo off +dotnet build "../HideOrbits.sln" -c Deploy diff --git a/scripts/build-release.bat b/scripts/build-release.bat new file mode 100644 index 0000000..1b474ee --- /dev/null +++ b/scripts/build-release.bat @@ -0,0 +1,2 @@ +@echo off +dotnet build "../HideOrbits.sln" -c Release diff --git a/scripts/build-run.bat b/scripts/build-run.bat new file mode 100644 index 0000000..95dbbc4 --- /dev/null +++ b/scripts/build-run.bat @@ -0,0 +1,2 @@ +@echo off +dotnet build "../HideOrbits.sln" -c DeployAndRun diff --git a/src/HideOrbits/Directory.Build.targets b/src/HideOrbits/Directory.Build.targets new file mode 100644 index 0000000..bdefac5 --- /dev/null +++ b/src/HideOrbits/Directory.Build.targets @@ -0,0 +1,78 @@ + + + + + powershell + pwsh + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/HideOrbits/HideOrbits.csproj b/src/HideOrbits/HideOrbits.csproj new file mode 100644 index 0000000..c3653e0 --- /dev/null +++ b/src/HideOrbits/HideOrbits.csproj @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/HideOrbitsProject/HideOrbitsPlugin.cs b/src/HideOrbits/HideOrbitsPlugin.cs similarity index 63% rename from HideOrbitsProject/HideOrbitsPlugin.cs rename to src/HideOrbits/HideOrbitsPlugin.cs index 7f23847..8880432 100644 --- a/HideOrbitsProject/HideOrbitsPlugin.cs +++ b/src/HideOrbits/HideOrbitsPlugin.cs @@ -1,13 +1,16 @@ -using BepInEx; -using BepInEx.Logging; +using BepInEx; using HarmonyLib; +using JetBrains.Annotations; using KSP.UI.Binding; using SpaceWarp; using SpaceWarp.API.Assets; using SpaceWarp.API.Mods; +using SpaceWarp.API.Game; +using SpaceWarp.API.Game.Extensions; using SpaceWarp.API.UI; using SpaceWarp.API.UI.Appbar; using UnityEngine; +using BepInEx.Logging; namespace HideOrbits; @@ -15,21 +18,28 @@ namespace HideOrbits; [BepInDependency(SpaceWarpPlugin.ModGuid, SpaceWarpPlugin.ModVer)] public class HideOrbitsPlugin : BaseSpaceWarpPlugin { - // These are useful in case some other mod wants to add a dependency to this one - public const string ModGuid = MyPluginInfo.PLUGIN_GUID; - public const string ModName = MyPluginInfo.PLUGIN_NAME; - public const string ModVer = MyPluginInfo.PLUGIN_VERSION; - + // Useful in case some other mod wants to use this mod a dependency + [PublicAPI] public const string ModGuid = MyPluginInfo.PLUGIN_GUID; + [PublicAPI] public const string ModName = MyPluginInfo.PLUGIN_NAME; + [PublicAPI] public const string ModVer = MyPluginInfo.PLUGIN_VERSION; + + // Singleton instance of the plugin class + [PublicAPI] public static HideOrbitsPlugin Instance { get; set; } + + // UI window state private bool _isWindowOpen; private Rect _windowRect; public bool AutoHideOrbits { get; private set; } public bool HideVesselOrbits { get; private set; } - public ManualLogSource logger { get; private set; } + public ManualLogSource MyLogger { get; private set; } - private const string ToolbarFlightButtonID = "BTN-HideOrbitsFlight"; + public string ShipID { get; set; } - public static HideOrbitsPlugin Instance { get; set; } + // AppBar button IDs + private const string ToolbarFlightButtonID = "BTN-HideOrbitsFlight"; + private const string ToolbarOabButtonID = "BTN-HideOrbitsOAB"; + private const string ToolbarKscButtonID = "BTN-HideOrbitsKSC"; /// /// Runs when the mod is first initialized. @@ -42,17 +52,56 @@ public override void OnInitialized() // Register Flight AppBar button Appbar.RegisterAppButton( - "Hide Orbits", + ModName, ToolbarFlightButtonID, - AssetManager.GetAsset($"{SpaceWarpMetadata.ModID}/images/icon.png"), - ToggleGuiButton + AssetManager.GetAsset($"{ModGuid}/images/icon.png"), + isOpen => + { + _isWindowOpen = isOpen; + GameObject.Find(ToolbarFlightButtonID)?.GetComponent()?.SetValue(isOpen); + } + ); + + // Register OAB AppBar Button + Appbar.RegisterOABAppButton( + ModName, + ToolbarOabButtonID, + AssetManager.GetAsset($"{ModGuid}/images/icon.png"), + isOpen => + { + _isWindowOpen = isOpen; + GameObject.Find(ToolbarOabButtonID)?.GetComponent()?.SetValue(isOpen); + } + ); + + // Register KSC AppBar Button + Appbar.RegisterKSCAppButton( + ModName, + ToolbarKscButtonID, + AssetManager.GetAsset($"{ModGuid}/images/icon.png"), + () => + { + _isWindowOpen = !_isWindowOpen; + } ); // Register all Harmony patches in the project var harmony = new Harmony(ModGuid); harmony.PatchAll(typeof(OrbitHiderPatch)); //Harmony.CreateAndPatchAll(typeof(OrbitHiderPatch).Assembly); - + + // Try to get the currently active vessel, set its throttle to 100% and toggle on the landing gear + try + { + var currentVessel = Vehicle.ActiveVesselVehicle; + if (currentVessel != null) + { + currentVessel.SetMainThrottle(1.0f); + currentVessel.SetGearState(true); + } + } + catch (Exception){} + // Fetch a configuration value or create a default one if it does not exist var defaultValue = true; var configValue = Config.Bind("Orbits", "Enable Orbit Hiding", defaultValue, "Enables automatic hiding of distant orbits"); @@ -61,16 +110,11 @@ public override void OnInitialized() var hideVessels = Config.Bind("Orbits", "Enable Vessel Orbit Hiding", defaultValue, "Hides non active or target vessel orbits by default"); HideVesselOrbits = hideVessels.Value; - + ShipID = "No ship yet"; + // Log the config value into /BepInEx/LogOutput.log Logger.LogInfo($"OrbitHiding: {configValue.Value}"); - logger = Logger; - } - - void ToggleGuiButton(bool toggle) - { - _isWindowOpen = toggle; - GameObject.Find(ToolbarFlightButtonID)?.GetComponent()?.SetValue(toggle); + MyLogger = Logger; } /// @@ -87,12 +131,17 @@ private void OnGUI() GUIUtility.GetControlID(FocusType.Passive), _windowRect, FillWindow, - "Hide Orbits", + "HideOrbits", GUILayout.Height(350), GUILayout.Width(350) ); } } + void ToggleGuiButton(bool toggle) + { + _isWindowOpen = toggle; + GameObject.Find(ToolbarFlightButtonID)?.GetComponent()?.SetValue(toggle); + } /// /// Defines the content of the UI window drawn in the OnGui method. @@ -122,6 +171,9 @@ private void FillWindow(int windowID) GUILayout.Label("Hide Orbits - Automatically hide distant planet orbits while zoomed in"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); + GUILayout.Label($"Ship ID: {ShipID}"); + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); GUILayout.Label($"Auto hiding orbits: {AutoHideOrbits}"); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); diff --git a/src/HideOrbits/OrbitHiderPatch.cs b/src/HideOrbits/OrbitHiderPatch.cs new file mode 100644 index 0000000..8d8c20b --- /dev/null +++ b/src/HideOrbits/OrbitHiderPatch.cs @@ -0,0 +1,123 @@ +using HarmonyLib; +using KSP.Game; +using KSP.Map; +using KSP.Sim.impl; +using UnityEngine; + +namespace HideOrbits +{ + internal class OrbitHiderPatch + { + public static Color hiddenOrbit = new(0, 0, 0, 0); + + [HarmonyPatch(typeof(OrbitRenderer), nameof(OrbitRenderer.UpdateOrbitStyling))] + [HarmonyPostfix] + public static void OrbitRenderer_UpdateOrbitStyling(Dictionary ____orbitRenderData) + { + HideOrbitsPlugin.Instance.ShipID = "Test value"; + CelestialBodyComponent vesselParentBody = null; + VesselComponent activeVessel = GameManager.Instance.Game.ViewController.GetActiveSimVessel(true); + if (activeVessel != null) + { + HideOrbitsPlugin.Instance.MyLogger.LogInfo($"Vessel Guid {activeVessel.Guid}"); + HideOrbitsPlugin.Instance.ShipID = activeVessel.Guid; + vesselParentBody = activeVessel.mainBody; + //HideOrbitsPlugin.Instance.logger.LogInfo($"Vessel parent Guid {vesselParentBody.SimulationObject.GlobalId}"); + } + + if (HideOrbitsPlugin.Instance == null || !HideOrbitsPlugin.Instance.AutoHideOrbits) + { + //Do nothing if not hiding orbits + return; + } + //HideOrbitsPlugin.Instance.MyLogger.LogInfo("Updating orbits"); + + //Collect bodies related to local planet (and its moons) + Dictionary vesselParentChildren = new(); + foreach (OrbitRenderer.OrbitRenderData orbitRenderData in ____orbitRenderData.Values) + { + if (orbitRenderData.Segments != null) + { + if (orbitRenderData.IsCelestialBody) + { + CelestialBodyComponent orbitBody = GameManager.Instance.Game.SpaceSimulation.GetSimulationObjectComponent(orbitRenderData.ParentGuid); + if (orbitBody.HasChild(vesselParentBody) || orbitBody.SimulationObject.GlobalId == vesselParentBody.SimulationObject.GlobalId) + { + if (!orbitBody.IsStar) + { + foreach (CelestialBodyComponent child in orbitBody.orbitingBodies) + { + if (child != null) + { + vesselParentChildren.Add(child.SimulationObject.GlobalId, child); + } + } + } + } + } + } + } + + // Hide unimportant orbits + foreach (OrbitRenderer.OrbitRenderData orbitRenderData in ____orbitRenderData.Values) + { + if (orbitRenderData.Segments != null) + { + if (orbitRenderData.IsCelestialBody) + { + // orbitRenderData.ParentGuid is the Guid of the planet the orbit is for. + if (orbitRenderData.ParentGuid == vesselParentBody?.SimulationObject.GlobalId) + { + continue; + } + + // check if current orbitRenderData body is a child of vesselParentBody + CelestialBodyComponent orbitBody = GameManager.Instance.Game.SpaceSimulation.GetSimulationObjectComponent(orbitRenderData.ParentGuid); + if (vesselParentChildren.ContainsKey(orbitRenderData.ParentGuid)) + { + continue; + } + + if ((bool)orbitBody?.HasParent(vesselParentBody)) + { + continue; + } + + // TODO what happens if we orbit Mun, will we see Minmus Orbit? Kerbin Orbit? + if (orbitBody.HasChild(vesselParentBody)) + { + continue; + } + + bool targetOrbit = activeVessel != null && activeVessel.TargetObjectId == orbitRenderData.Orbiter.SimulationObject.GlobalId; + if (targetOrbit) + { + continue; + // TODO change color of target orbits? + } + + foreach (OrbitRenderSegment segment in orbitRenderData.Segments) + { + segment.SetColors(hiddenOrbit, hiddenOrbit); + } + } + else + { + if (!HideOrbitsPlugin.Instance.HideVesselOrbits) + { + continue; + } + if (orbitRenderData.Vessel.GlobalId != activeVessel.GlobalId && orbitRenderData.Vessel.GlobalId != activeVessel.TargetObjectId) + { + foreach (OrbitRenderSegment segment in orbitRenderData.Segments) + { + segment.SetColors(hiddenOrbit, hiddenOrbit); + } + } + } + } + } // end Hide orbits + + } + } +}