From 8eaf32119c8882896826f443f1458828568b595b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bure=C5=A1?= Date: Sun, 5 Mar 2023 23:14:21 +0100 Subject: [PATCH] Upgraded project to SpaceWarp 0.4 and added configuration --- CommunityFixes.csproj | 3 +- README.md | 9 +++- src/CommunityFixesConfig.cs | 31 +++++++++++ src/CommunityFixesMod.cs | 52 +++++++++++++++---- src/Fix/BaseFix.cs | 8 ++- src/Fix/FixAttribute.cs | 12 +++++ src/Fix/IFix.cs | 1 - .../SeparationCommNet/SeparationCommNetFix.cs | 3 +- .../TelemetryComponent_OnUpdate.cs | 4 +- .../StickyOrbitMarkersFix.cs | 3 +- modinfo.json => swinfo.json | 6 +-- 11 files changed, 105 insertions(+), 27 deletions(-) create mode 100644 src/CommunityFixesConfig.cs create mode 100644 src/Fix/FixAttribute.cs rename modinfo.json => swinfo.json (57%) diff --git a/CommunityFixes.csproj b/CommunityFixes.csproj index 5b5a276..63c3fca 100644 --- a/CommunityFixes.csproj +++ b/CommunityFixes.csproj @@ -285,10 +285,11 @@ + - + \ No newline at end of file diff --git a/README.md b/README.md index 80353b3..3e8277c 100644 --- a/README.md +++ b/README.md @@ -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). \ No newline at end of file diff --git a/src/CommunityFixesConfig.cs b/src/CommunityFixesConfig.cs new file mode 100644 index 0000000..392462c --- /dev/null +++ b/src/CommunityFixesConfig.cs @@ -0,0 +1,31 @@ +using BepInEx.Configuration; + +namespace CommunityFixes; + +public class CommunityFixesConfig +{ + private const string TogglesSection = "Toggle fixes"; + + private Dictionary> _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; + } +} \ No newline at end of file diff --git a/src/CommunityFixesMod.cs b/src/CommunityFixesMod.cs index 7e5c85d..65783f3 100644 --- a/src/CommunityFixesMod.cs +++ b/src/CommunityFixesMod.cs @@ -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() { @@ -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))) @@ -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 @@ -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."); } } \ No newline at end of file diff --git a/src/Fix/BaseFix.cs b/src/Fix/BaseFix.cs index ef0e15e..25e8801 100644 --- a/src/Fix/BaseFix.cs +++ b/src/Fix/BaseFix.cs @@ -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() { diff --git a/src/Fix/FixAttribute.cs b/src/Fix/FixAttribute.cs new file mode 100644 index 0000000..20389f3 --- /dev/null +++ b/src/Fix/FixAttribute.cs @@ -0,0 +1,12 @@ +namespace CommunityFixes.Fix; + +[AttributeUsage(AttributeTargets.Class)] +public class FixAttribute: Attribute +{ + public string Name { get; } + + public FixAttribute(string name) + { + Name = name; + } +} \ No newline at end of file diff --git a/src/Fix/IFix.cs b/src/Fix/IFix.cs index 3f87c89..da12bd2 100644 --- a/src/Fix/IFix.cs +++ b/src/Fix/IFix.cs @@ -2,6 +2,5 @@ public interface IFix { - public string Name { get; } public void OnInitialized(); } \ No newline at end of file diff --git a/src/Fix/SeparationCommNet/SeparationCommNetFix.cs b/src/Fix/SeparationCommNet/SeparationCommNetFix.cs index bc84f40..fd6af54 100644 --- a/src/Fix/SeparationCommNet/SeparationCommNetFix.cs +++ b/src/Fix/SeparationCommNet/SeparationCommNetFix.cs @@ -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() diff --git a/src/Fix/SeparationCommNet/TelemetryComponent_OnUpdate.cs b/src/Fix/SeparationCommNet/TelemetryComponent_OnUpdate.cs index 113a2b5..fbbff56 100644 --- a/src/Fix/SeparationCommNet/TelemetryComponent_OnUpdate.cs +++ b/src/Fix/SeparationCommNet/TelemetryComponent_OnUpdate.cs @@ -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}" ); @@ -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}"); } } } \ No newline at end of file diff --git a/src/Fix/StickyOrbitMarkers/StickyOrbitMarkersFix.cs b/src/Fix/StickyOrbitMarkers/StickyOrbitMarkersFix.cs index c0f4dbc..c514089 100644 --- a/src/Fix/StickyOrbitMarkers/StickyOrbitMarkersFix.cs +++ b/src/Fix/StickyOrbitMarkers/StickyOrbitMarkersFix.cs @@ -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)); diff --git a/modinfo.json b/swinfo.json similarity index 57% rename from modinfo.json rename to swinfo.json index c4d7a3d..8563f02 100644 --- a/modinfo.json +++ b/swinfo.json @@ -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",