Skip to content

Commit 79352e8

Browse files
Initial Commit
0 parents  commit 79352e8

File tree

7 files changed

+351
-0
lines changed

7 files changed

+351
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
bin
3+
obj
4+
RandomFailuresLegacy.sln.DotSettings.user

EngineModulePatch.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Collections.Generic;
2+
using HarmonyLib;
3+
using SFS;
4+
using SFS.Parts.Modules;
5+
using SFS.UI;
6+
using Random = System.Random;
7+
8+
// ReSharper disable InconsistentNaming // Harmony double underscore
9+
10+
namespace RandomFailuresLegacy
11+
{
12+
internal static class Data
13+
{
14+
// Key: EngineModule::GetInstanceID
15+
public static readonly Dictionary<int, FailureData> FailureTable = new();
16+
}
17+
18+
internal class FailureData
19+
{
20+
private static readonly Random _random = new();
21+
22+
private readonly bool _failedAtStart = _random.Next(0, Settings.RFSettings.XInitialStartFailureChance) == 0;
23+
private bool _failedInFlight;
24+
private int _failureTicks;
25+
26+
public bool CanStart => !_failedAtStart && !_failedInFlight;
27+
28+
public FailureData(bool isIonEngine = false)
29+
{
30+
_failureTicks = Settings.RFSettings.XSustainedFailureChance *
31+
(isIonEngine ? Settings.RFSettings.XIonMultiplier : 1);
32+
}
33+
34+
public bool EngineTick()
35+
{
36+
if (_failureTicks == 0) return true;
37+
38+
_failureTicks--;
39+
return _random.Next(0, _failureTicks) == 0;
40+
}
41+
42+
public void DisablePermanent()
43+
{
44+
_failedInFlight = true;
45+
_failureTicks = 0;
46+
}
47+
}
48+
49+
[HarmonyPatch(typeof(EngineModule), "EnableEngine")]
50+
public class EnableEngineEngineModulePatch
51+
{
52+
[HarmonyPriority(Priority.First)]
53+
private static bool Prefix(EngineModule __instance, I_MsgLogger logger)
54+
{
55+
if (!Data.FailureTable.ContainsKey(__instance.GetInstanceID()))
56+
{
57+
Data.FailureTable.Add(__instance.GetInstanceID(), new FailureData());
58+
}
59+
60+
FailureData data = Data.FailureTable[__instance.GetInstanceID()];
61+
62+
if (!data.CanStart) MsgDrawer.main.Log("Engine failed to start!");
63+
return data.CanStart;
64+
}
65+
}
66+
67+
[HarmonyPatch(typeof(EngineModule), "FixedUpdate")]
68+
public class FixedUpdateEngineModulePatch
69+
{
70+
[HarmonyPriority(Priority.First)]
71+
private static void Prefix(EngineModule __instance)
72+
{
73+
if (!__instance.engineOn.Value) return;
74+
75+
if (!Data.FailureTable.ContainsKey(__instance.GetInstanceID()))
76+
{
77+
Data.FailureTable.Add(__instance.GetInstanceID(), new FailureData());
78+
}
79+
80+
FailureData data = Data.FailureTable[__instance.GetInstanceID()];
81+
82+
if (!data.CanStart) return;
83+
if (!data.EngineTick()) return;
84+
85+
MsgDrawer.main.Log("An engine failed mid-flight!");
86+
data.DisablePermanent();
87+
__instance.ToggleEngine();
88+
}
89+
}
90+
}

Properties/AssemblyInfo.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("RandomFailuresLegacy")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("RandomFailuresLegacy")]
12+
[assembly: AssemblyCopyright("Copyright © 2024")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("D9FBEB89-43CB-4CBB-9F66-6157CCFA3B2E")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]

RFLegacyMod.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.Generic;
2+
using ModLoader;
3+
using HarmonyLib;
4+
using ModLoader.Helpers;
5+
6+
namespace RandomFailuresLegacy
7+
{
8+
public class RFLegacyMod : Mod
9+
{
10+
public const string RfModId = "random_failures_legacy";
11+
public const string RfModName = "Random Failures Legacy";
12+
public const string RfAuthor = "TheAlgorithm476";
13+
14+
private static Harmony? _patcher;
15+
16+
public static RFLegacyMod Instance { get; private set; } = null!;
17+
18+
public override string ModNameID => RfModId;
19+
public override string DisplayName => RfModName;
20+
public override string Author => RfAuthor;
21+
public override string MinimumGameVersionNecessary => "1.5.10.2";
22+
public override string ModVersion => "1.0.0";
23+
public override Dictionary<string, string> Dependencies { get; } = new() { { "UITools", "1.1.5" } };
24+
public override string Description =>
25+
"Adds a random small chance of an engine failing to ignite, or having an in-flight failure.";
26+
27+
public RFLegacyMod() => Instance = this;
28+
29+
public override void Early_Load()
30+
{
31+
_patcher = new Harmony($"me.thealgorithm476.{RfModId}");
32+
_patcher.PatchAll();
33+
}
34+
35+
public override void Load()
36+
{
37+
Settings.Load();
38+
ConfigurationMenuTab.Load();
39+
40+
SceneHelper.OnHubSceneLoaded += () =>
41+
{
42+
Data.FailureTable.Clear();
43+
};
44+
}
45+
}
46+
}

RandomFailuresLegacy.csproj

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
4+
Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
5+
<PropertyGroup>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{D9FBEB89-43CB-4CBB-9F66-6157CCFA3B2E}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>RandomFailuresLegacy</RootNamespace>
12+
<AssemblyName>RandomFailuresLegacy</AssemblyName>
13+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<LangVersion>9</LangVersion>
16+
<Nullable>enable</Nullable>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19+
<PlatformTarget>AnyCPU</PlatformTarget>
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<PlatformTarget>AnyCPU</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="0Harmony">
39+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Contents\Resources\Data\Managed\0Harmony.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Assembly-CSharp">
42+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Contents\Resources\Data\Managed\Assembly-CSharp.dll</HintPath>
43+
</Reference>
44+
<Reference Include="System"/>
45+
<Reference Include="System.Core"/>
46+
<Reference Include="System.Data"/>
47+
<Reference Include="System.Xml"/>
48+
<Reference Include="UITools">
49+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Mods\UITools\UITools.dll</HintPath>
50+
</Reference>
51+
<Reference Include="Unity.TextMeshPro">
52+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Contents\Resources\Data\Managed\Unity.TextMeshPro.dll</HintPath>
53+
</Reference>
54+
<Reference Include="UnityEngine">
55+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Contents\Resources\Data\Managed\UnityEngine.dll</HintPath>
56+
</Reference>
57+
<Reference Include="UnityEngine.CoreModule">
58+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Contents\Resources\Data\Managed\UnityEngine.CoreModule.dll</HintPath>
59+
</Reference>
60+
<Reference Include="UnityEngine.TextRenderingModule">
61+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Contents\Resources\Data\Managed\UnityEngine.TextRenderingModule.dll</HintPath>
62+
</Reference>
63+
<Reference Include="UnityEngine.UI">
64+
<HintPath>..\..\..\..\Volumes\512 GB NVMe\SteamLibrary\steamapps\common\Spaceflight Simulator\SpaceflightSimulatorGame.app\Contents\Resources\Data\Managed\UnityEngine.UI.dll</HintPath>
65+
</Reference>
66+
</ItemGroup>
67+
<ItemGroup>
68+
<Compile Include="Settings.cs" />
69+
<Compile Include="EngineModulePatch.cs" />
70+
<Compile Include="RFLegacyMod.cs" />
71+
<Compile Include="Properties\AssemblyInfo.cs"/>
72+
</ItemGroup>
73+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
74+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
75+
Other similar extension points exist, see Microsoft.Common.targets.
76+
<Target Name="BeforeBuild">
77+
</Target>
78+
<Target Name="AfterBuild">
79+
</Target>
80+
-->
81+
82+
</Project>

RandomFailuresLegacy.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomFailuresLegacy", "RandomFailuresLegacy.csproj", "{D9FBEB89-43CB-4CBB-9F66-6157CCFA3B2E}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{D9FBEB89-43CB-4CBB-9F66-6157CCFA3B2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{D9FBEB89-43CB-4CBB-9F66-6157CCFA3B2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{D9FBEB89-43CB-4CBB-9F66-6157CCFA3B2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{D9FBEB89-43CB-4CBB-9F66-6157CCFA3B2E}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

Settings.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using SFS.IO;
3+
using SFS.UI.ModGUI;
4+
using SFS.Variables;
5+
using TMPro;
6+
using UITools;
7+
using UnityEngine;
8+
using Type = SFS.UI.ModGUI.Type;
9+
10+
namespace RandomFailuresLegacy
11+
{
12+
public class Settings : ModSettings<Settings.RFSettings>
13+
{
14+
private static Settings? _instance;
15+
16+
public static void Load()
17+
{
18+
_instance = new Settings();
19+
_instance.Initialize();
20+
}
21+
22+
protected override void RegisterOnVariableChange(Action onChange)
23+
{
24+
settings.InitialStartFailureChance.OnChange += onChange;
25+
settings.SustainedFailureChance.OnChange += onChange;
26+
settings.IonMultiplier.OnChange += onChange;
27+
28+
Application.quitting += onChange;
29+
}
30+
31+
protected override FilePath SettingsFile => new($"{RFLegacyMod.Instance.ModFolder}/rfl_config.txt");
32+
33+
public class RFSettings
34+
{
35+
public static int XInitialStartFailureChance => settings.InitialStartFailureChance.Value;
36+
public static int XSustainedFailureChance => settings.SustainedFailureChance.Value;
37+
public static int XIonMultiplier => settings.IonMultiplier.Value;
38+
39+
public Int_Local InitialStartFailureChance = new() { Value = 100 }; // 1% fails at initial start
40+
public Int_Local SustainedFailureChance = new() { Value = 216000 }; // Fails after 1 hour of firing
41+
public Int_Local IonMultiplier = new() { Value = 8760 }; // Fails after 1 year of firing (given that a normal engine fails after an hour)
42+
}
43+
}
44+
45+
public static class ConfigurationMenuTab
46+
{
47+
public static void Load()
48+
{
49+
ConfigurationMenu.Add(null, new (string, Func<Transform, GameObject>)[] { ("RFLegacy", CreateTab) });
50+
}
51+
52+
private static GameObject CreateTab(Transform transform)
53+
{
54+
Vector2Int size = ConfigurationMenu.ContentSize;
55+
Box box = Builder.CreateBox(transform, size.x, size.y);
56+
57+
box.CreateLayoutGroup(Type.Vertical, TextAnchor.UpperCenter, padding: new RectOffset(5, 5, 5, 5));
58+
59+
CreateNumberInput(box, size.x - 50, 50, "Initial Start Failure Chance (1 in x)", Settings.settings.InitialStartFailureChance, 1, value => Settings.settings.InitialStartFailureChance.Value = value);
60+
CreateNumberInput(box, size.x - 50, 50, "Sustained Engine Failure Time (ticks)", Settings.settings.SustainedFailureChance, 1, value => Settings.settings.SustainedFailureChance.Value = value);
61+
CreateNumberInput(box, size.x - 50, 50, "Ion Multiplier (Sustained Engine Failure Time x this value)", Settings.settings.IonMultiplier, 1, value => Settings.settings.IonMultiplier.Value = value);
62+
63+
return box.gameObject;
64+
}
65+
66+
private static void CreateNumberInput(Transform transform, int width, int height, string label, int value,
67+
int step, Action<int> onChange)
68+
{
69+
Container container = Builder.CreateContainer(transform);
70+
container.CreateLayoutGroup(Type.Horizontal);
71+
72+
Label title = Builder.CreateLabel(container, (int)(width * 0.55f), height, text: label);
73+
title.TextAlignment = TextAlignmentOptions.MidlineLeft;
74+
75+
UIToolsBuilder.CreateNumberInput(container, (int)(width * 0.5f), height, value, step).OnValueChangedEvent += it => onChange((int) it);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)