Skip to content

Commit

Permalink
Upgraded project to SpaceWarp 0.4 and added configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-bures committed Mar 5, 2023
1 parent 8e02da3 commit 8eaf321
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 27 deletions.
3 changes: 2 additions & 1 deletion CommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="BepInEx.BaseLib" Version="5.4.21" />
<PackageReference Include="HarmonyX" Version="2.10.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /y &quot;$(TargetPath)&quot; &quot;$(ProjectDir)\dist\community_fixes\bin\&quot;&#xA;xcopy /y &quot;$(ProjectDir)\modinfo.json&quot; &quot;$(ProjectDir)\dist\community_fixes\&quot;&#xA;xcopy /y &quot;$(ProjectDir)\license&quot; &quot;$(ProjectDir)\dist\community_fixes\&quot;&#xA;echo f | xcopy /y &quot;$(ProjectDir)\README.md&quot; &quot;$(ProjectDir)\dist\community_fixes\README.txt&quot;&#xA;" />
<Exec Command="xcopy /y &quot;$(TargetPath)&quot; &quot;$(ProjectDir)\dist\community_fixes\&quot;&#xA;xcopy /y &quot;$(ProjectDir)\swinfo.json&quot; &quot;$(ProjectDir)\dist\community_fixes\&quot;&#xA;xcopy /y &quot;$(ProjectDir)\license&quot; &quot;$(ProjectDir)\dist\community_fixes\&quot;&#xA;echo f | xcopy /y &quot;$(ProjectDir)\README.md&quot; &quot;$(ProjectDir)\dist\community_fixes\README.txt&quot;&#xA;" />
</Target>
</Project>
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Community Fixes for KSP 2
This project aims to bring together community bug fixes for Kerbal Space Program 2 in one centralized place.

## Current fixes
## Compatibility
- Tested with Kerbal Space Program 2 v0.1.0.0.20892
- Requires **[SpaceWarp 0.4+](https://github.com/SpaceWarpDev/SpaceWarp/releases/tag/spacewarp-0.4.0)**

## Implemented fixes
- **Separation CommNet Fix** by [munix](https://github.com/jan-bures) - Fixes CommNet disconnecting after separating two controllable vessels.
- **Sticky Orbit Markers** by [munix](https://github.com/jan-bures) - Makes Ap/Pe and other orbit markers stay pinned when plotting a maneuver.

## Planned fixes
To see what fixes are planned to be implemented, you can visit the [Issues page](https://github.com/Bit-Studios/CommunityFixes/issues) on the project's GitHub.

## Development wiki
If you'd like to contribute to this project, please take a look at [our wiki](https://github.com/Bit-Studios/CommunityFixes/wiki/Adding-your-fix).
31 changes: 31 additions & 0 deletions src/CommunityFixesConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using BepInEx.Configuration;

namespace CommunityFixes;

public class CommunityFixesConfig
{
private const string TogglesSection = "Toggle fixes";

private Dictionary<Type, ConfigEntry<bool>> _fixesEnabled = new();
private ConfigFile _configFile;

public CommunityFixesConfig(ConfigFile configFileFile)
{
_configFile = configFileFile;
}


public bool LoadConfig(Type type, string name)
{
// If the toggle value for a fix class is already defined, we return it
if (_fixesEnabled.TryGetValue(type, out var isEnabled))
{
return isEnabled.Value;
}

// Otherwise create a new config entry for the fix class and return its default value (true)
var configEntry = _configFile.Bind(TogglesSection, type.Name, true, name);
_fixesEnabled.Add(type, configEntry);
return configEntry.Value;
}
}
52 changes: 42 additions & 10 deletions src/CommunityFixesMod.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
using System.Reflection;
using BepInEx;
using CommunityFixes.Fix;
using SpaceWarp.API.Logging;
using SpaceWarp;
using SpaceWarp.API.Mods;

namespace CommunityFixes;

[MainMod]
public class CommunityFixesMod : Mod
[BepInPlugin(ModGuid, ModName, ModVer)]
[BepInDependency(SpaceWarpPlugin.ModGuid, SpaceWarpPlugin.ModVer)]
public class CommunityFixesMod : BaseSpaceWarpPlugin
{
public const string ModGuid = "com.github.communityfixes";
public const string ModName = "Community Fixes";
public const string ModVer = "0.1.0";

private static readonly Assembly _assembly = typeof(CommunityFixesMod).Assembly;
private CommunityFixesConfig _config;

public override void OnInitialized()
{
Expand All @@ -19,10 +26,12 @@ public override void OnInitialized()
}
catch (Exception ex)
{
Logger.Error($"Could not get types: ${ex.Message}");
Logger.LogError($"Could not get types: ${ex.Message}");
return;
}

_config = new CommunityFixesConfig(Config);

foreach (var type in types)
{
if (type.IsAbstract || !type.GetInterfaces().Contains(typeof(IFix)))
Expand All @@ -32,26 +41,33 @@ public override void OnInitialized()

try
{
LoadFix(type);
Logger.Info($"Initialized fix {type.Name}.");
var isLoaded = LoadFix(type);
Logger.LogInfo($"Fix {type.Name} is " + (isLoaded ? "disabled" : "enabled"));
}
catch (Exception ex)
{
Logger.Error($"Error loading fix {type.FullName}: {ex.Message}");
Logger.LogError($"Error loading fix {type.FullName}: {ex.Message}");
}
}

Logger.Info($"{Info.name} finished loading.");
Logger.LogInfo($"{ModName} finished loading.");
}

internal void LoadFix(Type type)
internal bool LoadFix(Type type)
{
var fixName = GetFixName(type);

if (!_config.LoadConfig(type, fixName))
{
return false;
}

IFix fix;
if (type.BaseType == typeof(BaseFix))
{
var baseFix = gameObject.AddComponent(type) as BaseFix;
baseFix!.transform.parent = transform;
baseFix.Logger = new ModLogger($"{baseFix.Name}");
baseFix.Logger = BepInEx.Logging.Logger.CreateLogSource($"CF/{fixName}");
fix = baseFix;
}
else
Expand All @@ -65,5 +81,21 @@ internal void LoadFix(Type type)
}

fix.OnInitialized();

return true;
}

private static string GetFixName(Type type)
{
var attributes = Attribute.GetCustomAttributes(type);
foreach (var attribute in attributes)
{
if (attribute is FixAttribute fix)
{
return fix.Name;
}
}

throw new Exception($"The attribute {typeof(FixAttribute).FullName} has to be declared on a fix class.");
}
}
8 changes: 3 additions & 5 deletions src/Fix/BaseFix.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using HarmonyLib;
using BepInEx.Logging;
using HarmonyLib;
using KSP.Game;
using SpaceWarp.API.Logging;

namespace CommunityFixes.Fix;

public abstract class BaseFix : KerbalMonoBehaviour, IFix
{
public abstract string Name { get; }

public virtual void OnInitialized()
{
}

protected Harmony _harmony;
internal BaseModLogger Logger { get; set; }
internal ManualLogSource Logger { get; set; }

protected BaseFix()
{
Expand Down
12 changes: 12 additions & 0 deletions src/Fix/FixAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace CommunityFixes.Fix;

[AttributeUsage(AttributeTargets.Class)]
public class FixAttribute: Attribute
{
public string Name { get; }

public FixAttribute(string name)
{
Name = name;
}
}
1 change: 0 additions & 1 deletion src/Fix/IFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public interface IFix
{
public string Name { get; }
public void OnInitialized();
}
3 changes: 1 addition & 2 deletions src/Fix/SeparationCommNet/SeparationCommNetFix.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace CommunityFixes.Fix.SeparationCommNet;

[Fix("Separation CommNet Fix")]
public class SeparationCommNetFix: BaseFix
{
public override string Name => "Separation CommNet Fix";

public static SeparationCommNetFix Instance;

public SeparationCommNetFix()
Expand Down
4 changes: 2 additions & 2 deletions src/Fix/SeparationCommNet/TelemetryComponent_OnUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void Prefix(TelemetryComponent __instance)

var comManager = KSP.Game.GameManager.Instance.Game.SessionManager.CommNetManager;

SeparationCommNetFix.Instance.Logger.Info(
SeparationCommNetFix.Instance.Logger.LogInfo(
$"Refreshed CommNet from {node.MaxRange}/{node.IsActive} to {maxRange}/{isActive}"
);

Expand All @@ -40,7 +40,7 @@ public static void Prefix(TelemetryComponent __instance)
}
catch (Exception ex)
{
SeparationCommNetFix.Instance.Logger.Error($"Could not refresh: {ex.Message}");
SeparationCommNetFix.Instance.Logger.LogError($"Could not refresh: {ex.Message}");
}
}
}
3 changes: 1 addition & 2 deletions src/Fix/StickyOrbitMarkers/StickyOrbitMarkersFix.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace CommunityFixes.Fix.StickyOrbitMarkers;

[Fix("Sticky Orbit Markers")]
public class StickyOrbitMarkersFix : BaseFix
{
public override string Name => "Sticky Orbit Markers";

public override void OnInitialized()
{
_harmony.PatchAll(typeof(Map3DOrbitalMarker_OnManeuverGizmoStateChange));
Expand Down
6 changes: 3 additions & 3 deletions modinfo.json → swinfo.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"mod_id": "community_fixes",
"author": "ShadowDev#6969, supermurloc#1838",
"author": "ShadowDev#6969, supermurloc#1838 (munix)",
"name": "Community Fixes",
"description": "Fix stuff",
"description": "Community project that aims to bring together bug fixes for KSP 2.",
"source": "https://github.com/Bit-Studios/CommunityFixes",
"version": "0.0.1",
"version": "0.1.0",
"dependencies": [],
"ksp2_version": {
"min": "0.1.0",
Expand Down

0 comments on commit 8eaf321

Please sign in to comment.