Skip to content

Commit

Permalink
Merge pull request #3 from KSP2Community/dev
Browse files Browse the repository at this point in the history
Some small changes
  • Loading branch information
cheese3660 authored Feb 24, 2024
2 parents 5979991 + 6f6c8a8 commit afe2a80
Show file tree
Hide file tree
Showing 122 changed files with 9,451 additions and 112 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ _UpgradeReport_Files/
/[Dd]ist/
/[Nn]uget/


# Rider

# User specific
Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@
</PropertyGroup>
</Target>
</Project>

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# V-SwiFT
Variant Switching & Feature Tweaking, a part switch mod for KSP2

65 changes: 38 additions & 27 deletions VSwift.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{DEEC0CDD-D14F-4753-BA48-E01099B12D73}") = "VSwift", "src/VSwift/VSwift.csproj", "{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}"
Project("{FB4DAA24-BC26-449C-BCE6-6B4DAD5EB25B}") = "VSwift", "src/VSwift/VSwift.csproj", "{83BF5B74-8D31-49D1-8156-9624BA45BAF3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VSwift.Modules", "src\VSwift.Modules\VSwift.Modules.csproj", "{00FDF80C-F331-4655-B93D-0672C472A009}"
Project("{FB4DAA24-BC26-449C-BCE6-6B4DAD5EB25B}") = "VSwift.Unity", "src/VSwift.Unity/VSwift.Unity.csproj", "{B87B0672-E61B-46C3-A466-0212FA127063}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VSwift.Preload", "src\VSwift.Preload\VSwift.Preload.csproj", "{B62A47FF-849E-4AA2-9526-93B2CAE3E353}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VSwift.Modules", "src\VSwift.Modules\VSwift.Modules.csproj", "{042AE33A-D79A-451D-83E4-7939233037B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VSwift.Preload", "src\VSwift.Preload\VSwift.Preload.csproj", "{D59E2FFB-5631-417E-99A3-4514672A0E6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -14,30 +16,39 @@ Global
DeployAndRun|Any CPU = DeployAndRun|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.Release|Any CPU.Build.0 = Release|Any CPU
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{B047C83D-8AFE-4D2D-94A8-20D1A559BBC5}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.Release|Any CPU.Build.0 = Release|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{00FDF80C-F331-4655-B93D-0672C472A009}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.Release|Any CPU.Build.0 = Release|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{B62A47FF-849E-4AA2-9526-93B2CAE3E353}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.Release|Any CPU.Build.0 = Release|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{83BF5B74-8D31-49D1-8156-9624BA45BAF3}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.Release|Any CPU.Build.0 = Release|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{B87B0672-E61B-46C3-A466-0212FA127063}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.Release|Any CPU.Build.0 = Release|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{042AE33A-D79A-451D-83E4-7939233037B2}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.Release|Any CPU.Build.0 = Release|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{D59E2FFB-5631-417E-99A3-4514672A0E6B}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
EndGlobalSection
EndGlobal


Empty file.
Binary file added plugin_template/assets/bundles/vswift_ui.bundle
Binary file not shown.
Binary file added plugin_template/assets/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion plugin_template/localizations/english.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
Key,Type,Desc,English
VSwift/Variants,Text,,Variants
VSwift/PartSwitch,Text,,Part Switch
VSwift/PartSwitch,Text,,Part Switch
VSwift/Requires,Text,,Requires - {0}
VSwift/Propellant/SingleMode,Text,,Propellant
VSwift/Propellant/MultiMode,Text,,Propellant - {0}
VSwift/Thrust/SingleMode,Text,,Thrust
VSwift/Thrust/MultiMode,Text,,Thrust - {0}
VSwift/Thrust/SeaLevel,Text,,Sea Level - {0} kN
VSwift/Thrust/Vacuum,Text,,Vacuum - {0} kN
VSwift/ISP/SingleMode,Text,,ISP
VSwift/ISP/MultiMode,Text,,ISP - {0}
VSwift/ISP/SeaLevel,Text,,Sea Level - {0}s
VSwift/ISP/Vacuum,Text,,Vacuum - {0}s
VSwift/Unlockable,Text,,Unlockable
VSwift/UnmetRequirements,Text,,Requirements not met
VSwift/CurrentlySelected,Text,,Currently selected
VSwift/Select,Text,,Select
VSwift/Mass,Text,,Mass
VSwift/MassModifier,Text,,Mass
VSwift/Mass/Description,Text,,{0} t
2 changes: 1 addition & 1 deletion plugin_template/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"version": "0.2.0",
"version_check": "https://raw.githubusercontent.com/KSP2Community/V-SwiFT/main/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.2.0",
"min": "0.3.0",
"max": "*"
},
"dependencies": [
Expand Down
83 changes: 61 additions & 22 deletions src/VSwift.Modules/Behaviours/Module_PartSwitch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Castle.Core.Internal;
using I2.Loc;
using KSP.Game;
using KSP.OAB;
using KSP.Sim.Definitions;
using KSP.UI.Binding;
using Newtonsoft.Json.Linq;
Expand All @@ -10,6 +11,7 @@
using VSwift.Modules.Logging;
using VSwift.Modules.Reverters;
using VSwift.Modules.Transformers;
using VSwift.Modules.UI;
using VSwift.Modules.Variants;

namespace VSwift.Modules.Behaviours;
Expand All @@ -20,14 +22,14 @@ public class Module_PartSwitch : PartBehaviourModule
private class StoredState
{
// public readonly List<(GameObject gameObject, bool state)> OriginalTransforms = [];
public readonly Dictionary<IReverter, object> OriginalTransformerData = [];
public readonly Dictionary<IReverter, object?> OriginalTransformerData = [];
}

public override Type PartComponentModuleType => typeof(PartComponentModule_PartSwitch);
private Data_PartSwitch? _dataPartSwitch;
private StoredState? _storedState;

public Data_PartSwitch? DataPartSwitch => _dataPartSwitch;

public override void AddDataModules()
{
base.AddDataModules();
Expand All @@ -46,8 +48,8 @@ public override void OnInitialize()

private void HandleInOabInitialization()
{
var j = _dataPartSwitch!.VariantSets.Aggregate(0, HandleVariantSetInOab);
ApplyInOab();
_dataPartSwitch!.VariantSets.Aggregate(0, HandleVariantSetInOab);
ApplyInOab(true);
}

private int HandleVariantSetInOab(int j, VariantSet variantSet)
Expand All @@ -62,23 +64,49 @@ private int HandleVariantSetInOab(int j, VariantSet variantSet)
_dataPartSwitch.ActiveVariants[j] = variantSet.Variants.First().VariantId;
}

var variantSetDropdown = new ModuleProperty<string>(_dataPartSwitch.ActiveVariants[j])
if (variantSet.IsPopout)
{
GenerateVariantSetButton(variantSet);
}
else
{
GenerateVariantSetDropdown(j, variantSet);
}

j += 1;
return j;
}

private void GenerateVariantSetButton(VariantSet variantSet)
{
var moduleAction = new ModuleAction(delegate() { IVSwiftUI.Instance.ShowUIFor(this, variantSet); });
DataPartSwitch!.AddAction(LocalizationManager.GetTranslation(
variantSet.VariantSetLocalizationKey.IsNullOrEmpty()
? variantSet.VariantSetId
: variantSet.VariantSetLocalizationKey),
moduleAction);
}

private void GenerateVariantSetDropdown(int j, VariantSet variantSet)
{
var variantSetDropdown = new ModuleProperty<string>(_dataPartSwitch!.ActiveVariants[j])
{
ContextKey = variantSet.VariantSetId
};
_dataPartSwitch.AddProperty(
LocalizationManager.GetTranslation(variantSet.VariantSetLocalizationKey.IsNullOrEmpty() ? variantSet.VariantSetId : variantSet.VariantSetLocalizationKey),
LocalizationManager.GetTranslation(variantSet.VariantSetLocalizationKey.IsNullOrEmpty()
? variantSet.VariantSetId
: variantSet.VariantSetLocalizationKey),
variantSetDropdown
);
var j1 = j;
variantSetDropdown.SetValue(_dataPartSwitch.ActiveVariants[j]);
variantSetDropdown.OnChangedValue += newVariant =>
{
IVSwiftLogger.Instance.LogInfo($"{OABPart.Name} switched variant to {newVariant}");
try
{
_dataPartSwitch.ActiveVariants[j1] = newVariant;
ApplyInOab();
_dataPartSwitch.ActiveVariants[j] = newVariant;
ApplyInOab(false,variantSet);
}
catch (Exception e)
{
Expand All @@ -97,9 +125,8 @@ private int HandleVariantSetInOab(int j, VariantSet variantSet)
: variant.VariantLocalizationKey)
});
}
_dataPartSwitch.SetDropdownData(variantSetDropdown,list);
j += 1;
return j;

_dataPartSwitch.SetDropdownData(variantSetDropdown, list);
}

private void HandleInFlightInitialization()
Expand All @@ -116,8 +143,10 @@ private void HandleInFlightInitialization()
{
_dataPartSwitch.ActiveVariants[i] = variant.Variants.First().VariantId;
}

i += 1;
}

ApplyInFlight();
}

Expand All @@ -135,19 +164,21 @@ private void ApplyCommon()
{
_dataPartSwitch.ActiveVariants[i] = variantSet.Variants.First().VariantId;
}

ApplyVariantCommon(variantSet.Variants.First(variant =>
_dataPartSwitch.ActiveVariants[i] == variant.VariantId));
i++;
}
}

private void ApplyVariantCommon(Variant variant)
{
foreach (var transformer in variant.Transformers)
{
transformer.ApplyCommon(this);
}
}

private void ApplyInFlight()
{
ApplyCommon();
Expand All @@ -163,12 +194,13 @@ private void ApplyInFlight()
{
_dataPartSwitch.ActiveVariants[i] = variantSet.Variants.First().VariantId;
}

ApplyVariantInFlight(variantSet.Variants.First(variant =>
_dataPartSwitch.ActiveVariants[i] == variant.VariantId));
i++;
}
}

private void ApplyVariantInFlight(Variant variant)
{
foreach (var transformer in variant.Transformers)
Expand All @@ -178,10 +210,10 @@ private void ApplyVariantInFlight(Variant variant)
}


private void ApplyInOab()
public void ApplyInOab(bool isStarting,VariantSet? swapped=null)
{
if (_storedState == null) StoreOriginalState();
ResetToOriginalState();
ResetToOriginalState(isStarting,swapped);
ApplyCommon();
var i = 0;
foreach (var variantSet in _dataPartSwitch!.VariantSets)
Expand All @@ -190,10 +222,12 @@ private void ApplyInOab()
{
_dataPartSwitch.ActiveVariants.Add(variantSet.Variants.First().VariantId);
}

ApplyVariantInOab(variantSet.Variants.First(variant =>
_dataPartSwitch.ActiveVariants[i] == variant.VariantId));
i++;
}
(OABPart as ObjectAssemblyPart)?.UpdateMassValues();
}

private void ApplyVariantInOab(Variant variant)
Expand All @@ -203,12 +237,17 @@ private void ApplyVariantInOab(Variant variant)
transformer.ApplyInOab(this);
}
}
private void ResetToOriginalState() {

private void ResetToOriginalState(bool isStarting=false,VariantSet? swapped=null)
{
_dataPartSwitch!.MassModifier = 0.0f;
// IVSwiftLogger.Instance.LogInfo("ResetToOriginalState() called");
foreach (var (instance, data) in _storedState!.OriginalTransformerData)
{
// IVSwiftLogger.Instance.LogInfo($"Reverting {instance} with data {data}");
instance.Revert(this, data);
if (!instance.RequiresInVariantSet ||
(swapped != null && swapped.Variants.Any(x => x.Transformers.Any(y => y.Reverter == instance))))
instance.Revert(this, data, isStarting);
}
}

Expand All @@ -218,11 +257,11 @@ private void StoreOriginalState()
foreach (var transformer in from variantSet in _dataPartSwitch!.VariantSets
from variant in variantSet.Variants
from transformer in variant.Transformers
where !_storedState.OriginalTransformerData.ContainsKey(transformer.Reverter)
where transformer.Reverter != null && !_storedState.OriginalTransformerData.ContainsKey(transformer.Reverter)
select transformer)
{
var reverter = transformer.Reverter;
_storedState.OriginalTransformerData[reverter] = reverter.Store(this);
_storedState.OriginalTransformerData[reverter!] = reverter!.Store(this);
}
}

Expand Down
Loading

0 comments on commit afe2a80

Please sign in to comment.