Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Electrocutor committed Nov 19, 2018
1 parent 506f35e commit 73d44f5
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 2 deletions.
31 changes: 31 additions & 0 deletions KSPF.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KSPF", "KSPF\KSPF.csproj", "{61A3CCC4-590F-4768-84A6-3639B36E6BDE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6FE29C42-37C4-4534-9946-18FF373AF8C2}"
ProjectSection(SolutionItems) = preProject
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{61A3CCC4-590F-4768-84A6-3639B36E6BDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61A3CCC4-590F-4768-84A6-3639B36E6BDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61A3CCC4-590F-4768-84A6-3639B36E6BDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61A3CCC4-590F-4768-84A6-3639B36E6BDE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F96BF906-2C12-4204-8CB7-628E00710760}
EndGlobalSection
EndGlobal
11 changes: 11 additions & 0 deletions KSPF/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KSPF
{
public class Class1
{
}
}
50 changes: 50 additions & 0 deletions KSPF/KSPF.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{61A3CCC4-590F-4768-84A6-3639B36E6BDE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>KSPF</RootNamespace>
<AssemblyName>KSPF</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\..\Games\KSP-1.5.1\KSP_x64_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="UnityEngine">
<HintPath>..\..\..\..\..\..\Games\KSP-1.5.1\KSP_x64_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Parts\Addons\PartVariantsEx.cs" />
<Compile Include="Parts\PartModules\ModulePartVariantsEx.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
144 changes: 144 additions & 0 deletions KSPF/Parts/Addons/PartVariantsEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace KSPF.Parts.Addons
{
/// <summary>
/// Addon used to hook the existing ModulePartVariants and extend it
/// </summary>
[KSPAddon(KSPAddon.Startup.Instantly, true)]
public class PartVariantsEx : MonoBehaviour
{
private bool _Debug = false;
private EventData<Part, PartVariant>.OnEvent VariantAppliedEvent;

public void Start()
{
this.VariantAppliedEvent = new EventData<Part, PartVariant>.OnEvent(VariantAppliedEventHandler);
GameEvents.onVariantApplied.Add(this.VariantAppliedEvent);
}

private void VariantAppliedEventHandler(Part oPart, PartVariant oVariant)
{
string sDebug = "";
string sValue;
float fValue;
double dValue;
double dValue2;

if (oPart == null || oVariant == null)
{ return; }

#region "Resources"

PartResource oResource;
ConfigNode oConfigNode;

foreach (PartResourceDefinition oResourceDef in PartResourceLibrary.Instance.resourceDefinitions)
{
sValue = oVariant.GetExtraInfoValue("resource/" + oResourceDef.name);

if (!string.IsNullOrEmpty(sValue))
{
oResource = oPart.Resources[oResourceDef.name];

if (oResource != null)
{
if (sValue.ToLower() == "none")
{ oPart.RemoveResource(oResource); }
else if (double.TryParse(sValue, out dValue))
{
dValue2 = oResource.amount / oResource.maxAmount;
oResource.maxAmount = dValue;

if (HighLogic.LoadedSceneIsEditor)
{ oResource.amount = dValue * dValue2; }
}
}
else if (double.TryParse(sValue, out dValue))
{
oConfigNode = new ConfigNode("RESOURCE");
oConfigNode.AddValue("name", oResourceDef.name);

if (HighLogic.LoadedSceneIsEditor)
{ oConfigNode.AddValue("amount", dValue); }
else
{ oConfigNode.AddValue("amount", 0.0); }

oConfigNode.AddValue("maxAmount", dValue);

oPart.AddResource(oConfigNode);
}
}
}

#endregion "Resources"

#region "Module Properties"

sValue = oVariant.GetExtraInfoValue("AddModule1");
for (int i = 2; !string.IsNullOrEmpty(sValue); i++)
{
oPart.AddModule(sValue);
sValue = oVariant.GetExtraInfoValue("AddModule" + i.ToString());
}

List<string> oRemoveModuleList = new List<string>();
sValue = oVariant.GetExtraInfoValue("RemoveModule1");
for (int i = 2; !string.IsNullOrEmpty(sValue); i++)
{
oRemoveModuleList.Add(sValue);
sValue = oVariant.GetExtraInfoValue("RemoveModule" + i.ToString());
}

foreach (PartModule oModule in oPart.Modules)
{
if (_Debug)
{ sDebug = "Module Fields [" + oModule.moduleName + "]:\r\n"; }

foreach (BaseField oField in oModule.Fields)
{
if (_Debug)
{ sDebug += oField.name + "\r\n"; }

sValue = oVariant.GetExtraInfoValue(oModule.moduleName + "/" + oField.name);

if (!string.IsNullOrEmpty(sValue))
{
oField.SetValue(Convert.ChangeType(sValue, oField.FieldInfo.FieldType), oField.host);

if (_Debug)
{ sDebug += " Appled: " + oField.name + " = " + sValue + "\r\n"; }
}
}

if (_Debug)
{ Debug.Log(sDebug); }
}

#endregion"Module Properties"

#region "Update UI"

if (HighLogic.LoadedSceneIsEditor)
{
GameEvents.onEditorPartEvent.Fire(ConstructionEventType.PartTweaked, oPart);
GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
}
else if (HighLogic.LoadedSceneIsFlight)
{
GameEvents.onVesselWasModified.Fire(oPart.vessel);
}

UIPartActionWindow[] oWindows = Resources.FindObjectsOfTypeAll<UIPartActionWindow>();
foreach (UIPartActionWindow oWindow in oWindows)
{
if (oWindow.part == oPart)
{ oWindow.displayDirty = true; }
}

#endregion "Update UI"
}
}
}
61 changes: 61 additions & 0 deletions KSPF/Parts/PartModules/ModulePartVariantsEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KSPF.Parts.PartModules
{
/// <summary>
/// Allows persisting of part-level properties per instance of part that are usually only at the AvailablePart definition level
/// </summary>
public class ModulePartVariantsEx : PartModule
{
[KSPField(isPersistant = true)]
public string internalModel = null;

[KSPField(isPersistant = true)]
public float rescaleFactor = 0.0f;

[KSPField(isPersistant = true)]
public float scale = 0.0f;

public override void OnAwake()
{
// rescaleFactor and scale must be set here, but KSP does not set PartModule fields until onLoad
}

public override void OnLoad(ConfigNode node)
{
this.part.partInfo = new AvailablePart(this.part.partInfo);

SetInternalModel();
}

private void SetInternalModel()
{
if (string.IsNullOrEmpty(this.internalModel))
{ return; }

ConfigNode oConfig = new ConfigNode("INTERNAL");
oConfig.AddValue("name", this.internalModel);

this.part.partInfo.internalConfig = oConfig;
}

private void SetRescaleFactor()
{
if (this.rescaleFactor == 0.0f)
{ return; }

this.part.rescaleFactor = this.rescaleFactor;
}

private void SetScale()
{
if (this.scale == 0.0f)
{ return; }

this.part.scaleFactor = this.scale;
}
}
}
36 changes: 36 additions & 0 deletions KSPF/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("KSPF")]
[assembly: AssemblyDescription("Kerbal Space Program Framework")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("KSPF")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("61a3ccc4-590f-4768-84a6-3639b36e6bde")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# KSPF
Kerbal Space Program Framework
# Kerbal Space Program Framework
A comprehensive, organized, and efficient API to KSP.

## Part Modules
* ModulePartVariantsEx

0 comments on commit 73d44f5

Please sign in to comment.