Skip to content

Commit

Permalink
Incepting LetsStayTogether
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisias committed Aug 7, 2021
1 parent 93066dc commit f590b7f
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CONFIG.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TARGETDIR="999_$PACKAGE"
TARGETBINDIR="$TARGETDIR/Plugins"
PROJECTSDIR="$PACKAGE/"
VERSIONFILE="KSP_Recall.version"
DLLS="KSP-Recall Resourceful Driftless Attached ChillingOut Refunding"
DLLS="KSP-Recall Resourceful Driftless Attached ChillingOut Refunding LetsStayTogether"

PROJECT_BRANCH=`git branch | grep \* | cut -d ' ' -f2`
if [ "$PROJECT_BRANCH" == "master" ] ; then
Expand Down
1 change: 1 addition & 0 deletions GameData/999_KSP-Recall/patches/100_Interventions.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
moduleName:NEEDS[KSPRECALL-DRIFTLESS] = Driftless
moduleName:NEEDS[KSPRECALL-REFUNDING] = RefundingForKSP111x
moduleName:NEEDS[KSPRECALL-RESOURCEFUL] = Resourceful
moduleName:NEEDS[KSPRECALL-STAYINGTOGETHER] = LetsStayTogether
}
}
7 changes: 7 additions & 0 deletions GameData/999_KSP-Recall/patches/stayingtogether.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@PART[*]:HAS[@MODULE[ModuleDockingNode],!MODULE[ModuleAsteroid],!MODULE[ModuleComet],!MODULE[KerbalEVA]]:NEEDS[KSPRECALL-STAYINGTOGETHER]
{
%MODULE[LetsStayTogether]
{
active = True
}
}
6 changes: 6 additions & 0 deletions KSP-Recall.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Resourceful", "Source\Resou
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Source\Tests\Tests.csproj", "{F3E6624A-9F1C-492A-93F0-9BB2E23CD6A6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LetsStayTogether", "Source\LetsStayTogether\LetsStayTogether.csproj", "{ABE56F6A-AB8A-498B-8968-B6FB61EBC1D3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -53,6 +55,10 @@ Global
{F3E6624A-9F1C-492A-93F0-9BB2E23CD6A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3E6624A-9F1C-492A-93F0-9BB2E23CD6A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3E6624A-9F1C-492A-93F0-9BB2E23CD6A6}.Release|Any CPU.Build.0 = Release|Any CPU
{ABE56F6A-AB8A-498B-8968-B6FB61EBC1D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABE56F6A-AB8A-498B-8968-B6FB61EBC1D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABE56F6A-AB8A-498B-8968-B6FB61EBC1D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABE56F6A-AB8A-498B-8968-B6FB61EBC1D3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
Expand Down
3 changes: 3 additions & 0 deletions Source/KSP-Recall/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Globals
public readonly bool Driftless;
public readonly bool Refunding;
public readonly bool Resourceful;
public readonly bool LetsStayTogether;

private Globals()
{
Expand All @@ -49,6 +50,7 @@ private Globals()
this.Driftless = cn.GetValue<bool>("Driftless");
this.Refunding = cn.GetValue<bool>("Refunding");
this.Resourceful = cn.GetValue<bool>("Resourceful");
this.LetsStayTogether = cn.GetValue<bool>("LetsStayTogether");
}
catch (Exception)
{
Expand All @@ -57,6 +59,7 @@ private Globals()
this.Driftless = false;
this.Refunding = false;
this.Resourceful = false;
this.LetsStayTogether = false;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Source/KSP-Recall/ModuleManagerSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static IEnumerable<string> ModuleManagerAddToModList()
if (checkForAttached()) tags.Add("KSPRECALL-ATTACHED");
if (checkForChillingOut()) tags.Add("KSPRECALL-CHILLINGOUT");
if (checkForRefunding()) tags.Add("KSPRECALL-REFUNDING");
if (checkForStayingTogether()) tags.Add("KSPRECALL-STAYINGTOGETHER");

return tags.ToArray();
}
Expand Down Expand Up @@ -74,5 +75,10 @@ private static bool checkForRefunding()
{
return KSPe.Util.KSP.Version.Current >= KSPe.Util.KSP.Version.FindByVersion(1,11,0);
}

private static bool checkForStayingTogether()
{
return KSPe.Util.KSP.Version.Current >= KSPe.Util.KSP.Version.FindByVersion(1,12,0);
}
}
}
27 changes: 26 additions & 1 deletion Source/KSP-Recall/SanityCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal class SanityCheck : MonoBehaviour
private const string ATTACHED_MODULE_NAME = "Attached";
private const string CHILLINGOUT_MODULE_NAME = "ChillingOut";
private const string REFUNDING_MODULE_NAME = "RefundingForKSP111x";
private const string STAYINGTOGETHER_MODULE_NAME = "LetsStayTogether";

internal static bool isConcluded = false;

Expand Down Expand Up @@ -77,6 +78,7 @@ private IEnumerator SanityCheckCoroutine()
int parts_with_attached_count = 0;
int parts_with_chillingout_count = 0;
int parts_with_refunding_count = 0;
int parts_with_stayingtogether_count = 0;
int showstoppers_count = 0;

foreach (AvailablePart p in PartLoader.LoadedPartsList)
Expand All @@ -98,6 +100,7 @@ private IEnumerator SanityCheckCoroutine()
bool containsAttached = false;
bool containsChillingOut = false;
bool containsRefunding = false;
bool containsStayingTogether = false;
Exception culprit = null;

prefab = p.partPrefab; // Reaching the prefab here in the case another Mod recreates it from zero. If such hypothecical mod recreates the whole part, we're doomed no matter what.
Expand All @@ -112,6 +115,7 @@ private IEnumerator SanityCheckCoroutine()
containsAttached = prefab.Modules.Contains(ATTACHED_MODULE_NAME);
containsChillingOut = prefab.Modules.Contains(CHILLINGOUT_MODULE_NAME);
containsRefunding = prefab.Modules.Contains(REFUNDING_MODULE_NAME);
containsStayingTogether = prefab.Modules.Contains(STAYINGTOGETHER_MODULE_NAME);
++total_count;
break; // Yeah. This while stunt was done just to be able to do this. All the rest is plain clutter! :D
}
Expand Down Expand Up @@ -170,6 +174,13 @@ private IEnumerator SanityCheckCoroutine()
prefab.RemoveModule(prefab.Modules[REFUNDING_MODULE_NAME]);
}
else ++parts_with_refunding_count;

if (containsStayingTogether) if (null != (due = this.checkForStayingTogether(prefab)))
{
Log.info("Removing {0} support for {1} ({2}) due {3}.", STAYINGTOGETHER_MODULE_NAME, p.name, p.title, due);
prefab.RemoveModule(prefab.Modules[STAYINGTOGETHER_MODULE_NAME]);
}
else ++parts_with_stayingtogether_count;
}
catch (Exception e)
{
Expand All @@ -187,13 +198,14 @@ private IEnumerator SanityCheckCoroutine()
#endif
}

Log.info("SanityCheck Concluded : {0} parts found ; {1} parts using {2} ; {3} parts using {4} ; {5} parts using {6} ; {7} parts using {8} ; {9} parts using {10}, {11} show stoppers detected ."
Log.info("SanityCheck Concluded : {0} parts found ; {1} parts using {2} ; {3} parts using {4} ; {5} parts using {6} ; {7} parts using {8} ; {9} parts using {10} ; {11} parts using {12} ; {13} show stoppers detected ."
, total_count
, parts_with_resourceful_count, RESOURCEFUL_MODULE_NAME
, parts_with_driftless_count, DRIFTLESS_MODULE_NAME
, parts_with_attached_count, ATTACHED_MODULE_NAME
, parts_with_chillingout_count, CHILLINGOUT_MODULE_NAME
, parts_with_refunding_count, REFUNDING_MODULE_NAME
, parts_with_stayingtogether_count, STAYINGTOGETHER_MODULE_NAME
, showstoppers_count);
SanityCheck.isConcluded = true;

Expand Down Expand Up @@ -282,6 +294,19 @@ private string checkForRefunding(Part p)
return this.checkForCommonUnsupportedParts(p);
}

private string checkForStayingTogether(Part p)
{
Log.dbg("Checking {0} Sanity for {1} at {2}", STAYINGTOGETHER_MODULE_NAME, p.name, p.partInfo.partUrl ?? "<NO URL>");

if ( KSPe.Util.KSP.Version.Current < KSPe.Util.KSP.Version.FindByVersion(1,12,0) )
{
if (Globals.Instance.Refunding) Log.warn(MSG_INSTALLATION_FORCED, STAYINGTOGETHER_MODULE_NAME);
else return MSG_PART_DOES_NOT_NEED ;
}

return this.checkForCommonUnsupportedParts(p);
}

private string checkForCommonUnsupportedParts(Part p)
{
if (p.name.StartsWith("kerbalEVA")) return MSG_PART_NOT_SUPPORTED;
Expand Down
113 changes: 113 additions & 0 deletions Source/LetsStayTogether/FlightHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
This file is part of LetsStayTogether, a component of KSP-Recall
(C) 2020-2021 Lisias T : http://lisias.net <support@lisias.net>
KSP-Recall is double licensed, as follows:
* SKL 1.0 : https://ksp.lisias.net/SKL-1_0.txt
* GPL 2.0 : https://www.gnu.org/licenses/gpl-2.0.txt
And you are allowed to choose the License that better suit your needs.
KSP-Recall is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the SKL Standard License 1.0
along with KSP-Recall. If not, see <https://ksp.lisias.net/SKL-1_0.txt>.
You should have received a copy of the GNU General Public License 2.0
along with KSP-Recall. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using UnityEngine;

namespace KSP_Recall { namespace StayingTogether

{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class FlightHelper : MonoBehaviour
{
private readonly FlightKscTrackingHelper helper = new FlightKscTrackingHelper();
private void Awake() { this.helper.Awake(); }
private void OnDestroy() { this.helper.OnDestroy(); }
}

[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
public class SpaceCentreHelper : MonoBehaviour
{
private readonly FlightKscTrackingHelper helper = new FlightKscTrackingHelper();
private void Awake() { this.helper.Awake(); }
private void OnDestroy() { this.helper.OnDestroy(); }
}

[KSPAddon(KSPAddon.Startup.TrackingStation, false)]
public class TrackingStationHelper : MonoBehaviour
{
private readonly FlightKscTrackingHelper helper = new FlightKscTrackingHelper();
private void Awake() { this.helper.Awake(); }
private void OnDestroy() { this.helper.OnDestroy(); }
}

internal class FlightKscTrackingHelper
{
#region Unity Life Cycle

internal void Awake()
{
Log.dbg("Awake on {0}", HighLogic.LoadedScene);
if (Globals.Instance.Refunding)
{
GameEvents.onVesselGoOnRails.Add(OnVesselGoOnRails);
GameEvents.onVesselGoOffRails.Add(OnVesselGoOffRails);
}
}

internal void OnDestroy()
{
Log.dbg("OnDestroy");
if (Globals.Instance.Refunding)
{
GameEvents.onVesselGoOffRails.Add(OnVesselGoOffRails);
GameEvents.onVesselGoOnRails.Remove(OnVesselGoOnRails);
}
}

#endregion

#region Events Handlers

private void OnVesselGoOnRails(Vessel vessel)
{
Log.dbg("OnVesselGo ON Rails {0}", vessel.vesselName);
this.ImmediateUpdate(vessel);
}

// Continuing from the the OnRails stunt, when the vessel is unpacked we need to remove the Resource from
// the stackable parts, so they could be stackable again.
//
// Since vessels are recovered while flying or while packed, I think this stunt my stick...
private void OnVesselGoOffRails(Vessel vessel)
{
Log.dbg("OnVesselGo OFF Rails {0}", vessel.vesselName);
this.ImediateRestore(vessel);
}

#endregion

private void ImmediateUpdate(Vessel vessel)
{
foreach (Part p in vessel.Parts) if (p.Modules.Contains<LetsStayTogether>())
p.Modules.GetModule<LetsStayTogether>().SynchronousUpdate();
}

private void ImediateRestore(Vessel vessel)
{
foreach (Part p in vessel.Parts) if (p.Modules.Contains<LetsStayTogether>())
p.Modules.GetModule<LetsStayTogether>().SynchronousRestore();
}
private static KSPe.Util.Log.Logger Log = KSPe.Util.Log.Logger.CreateForType<FlightHelper>("KSP-Recall", "StayingTogether-FlightHelper");
}
} }
54 changes: 54 additions & 0 deletions Source/LetsStayTogether/LetsStayTogether.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{ABE56F6A-AB8A-498B-8968-B6FB61EBC1D3}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>LetsStayTogether</RootNamespace>
<AssemblyName>LetsStayTogether</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<BaseIntermediateOutputPath>..\..\obj\$(AssemblyName)</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>..\..\bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="KSPe.Light.Recall">
<HintPath>..\..\..\..\..\..\..\..\..\..\Users\lisias\Workspaces\KSP\LIB\plugins\KSPe.Light.Recall.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\..\..\..\..\..\Users\lisias\Workspaces\KSP\LIB\managed\1.4.1\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\..\..\..\..\..\..\..\..\Users\lisias\Workspaces\KSP\LIB\managed\1.4.1\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FlightHelper.cs" />
<Compile Include="PartModule.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\KSP-Recall\KSP-Recall.csproj">
<Project>{40E7A5F2-C30E-43E9-B345-51971959149F}</Project>
<Name>KSP-Recall</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
Loading

0 comments on commit f590b7f

Please sign in to comment.