Skip to content

Commit

Permalink
Stock Bug Fix now optionally can lock out the Mods button until the r…
Browse files Browse the repository at this point in the history
…isk of the race condition that reinstalls all mods is very low.

Fix several terrible bugs in PLib AVC. Now exclude all the broken older versions which might never continue the list, and show a warning in the main menu properly.
  • Loading branch information
peterhaneve committed Feb 9, 2024
1 parent 74dffc8 commit a806808
Show file tree
Hide file tree
Showing 22 changed files with 483 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
<DistributeMod>true</DistributeMod>
<ArchivedVersionPath></ArchivedVersionPath>
<UsePublicized>false</UsePublicized>
<PLibVersion>4.13.0.0</PLibVersion>
<PLibVersion>4.14.0.0</PLibVersion>
</PropertyGroup>
</Project>
24 changes: 13 additions & 11 deletions FastTrack/PathPatches/DeferredTriggers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,22 @@ internal void Process() {
src.DestroySelf();
}
}
while (cacheCellPending.TryDequeue(out var item)) {
int cell = Grid.PosToCell(item.transform.position), oldCell = item.cachedCell;
if (cell != oldCell) {
while (cacheCellPending.TryDequeue(out var item))
// Pickupables can be destroyed mid-frame
if (item != null) {
int cell = Grid.PosToCell(item.transform.position), oldCell = item.cachedCell;
if (cell != oldCell) {
#if DEBUG
PUtil.LogDebug("Adjusted bugged item {0} from {1:D} to {2:D}".F(
item.name, oldCell, cell));
PUtil.LogDebug("Adjusted bugged item {0} from {1:D} to {2:D}".F(
item.name, oldCell, cell));
#endif
item.UpdateCachedCell(cell);
gsp.UpdatePosition(item.solidPartitionerEntry, cell);
gsp.UpdatePosition(item.partitionerEntry, cell);
item.NotifyChanged(oldCell);
item.NotifyChanged(cell);
item.UpdateCachedCell(cell);
gsp.UpdatePosition(item.solidPartitionerEntry, cell);
gsp.UpdatePosition(item.partitionerEntry, cell);
item.NotifyChanged(oldCell);
item.NotifyChanged(cell);
}
}
}
while (offsetPending.TryDequeue(out var offset))
offset.offsets.GetOffsets(offset.newCell);
}
Expand Down
55 changes: 55 additions & 0 deletions FastTrack/VisualPatches/BuildWatermarkPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 Peter Han
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using HarmonyLib;

namespace PeterHan.FastTrack.VisualPatches {
/// <summary>
/// Applied to BuildWatermark to modify the text if the Fast Track version should be shown.
/// </summary>
[HarmonyPatch(typeof(BuildWatermark), nameof(BuildWatermark.GetBuildText))]
public static class BuildWatermark_GetBuildText_Patch {
internal static bool Prepare() => FastTrackOptions.Instance.VersionInWatermark ==
FastTrackOptions.WatermarkMessage.Show;

/// <summary>
/// Applied after GetBuildText runs.
/// </summary>
internal static void Postfix(ref string __result) {
__result = __result + " (FT-" + ModVersion.FILE_VERSION + ")";
}
}

/// <summary>
/// Applied to BuildWatermark to hide the text if the option is set to remove it
/// completely. Only applies in game.
/// </summary>
[HarmonyPatch(typeof(BuildWatermark), nameof(BuildWatermark.RefreshText))]
public static class BuildWatermark_RefreshText_Patch {
internal static bool Prepare() => FastTrackOptions.Instance.VersionInWatermark ==
FastTrackOptions.WatermarkMessage.Off;

/// <summary>
/// Applied after RefreshText runs.
/// </summary>
internal static void Postfix(BuildWatermark __instance) {
if (Game.Instance != null)
__instance.Show(false);
}
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Code in this repository is licensed under the MIT License:

Copyright (c) 2023 Peter Han
Copyright (c) 2024 Peter Han

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions PLib/PLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<PackageIcon>icon.png</PackageIcon>
<PackageTags>Game, OxygenNotIncluded, Oxygen, PLib, Library</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright 2023 Peter Han</Copyright>
<Copyright>Copyright 2024 Peter Han</Copyright>
<Description>PLib - Peter Han's library used for creating mods for Oxygen Not Included, a simulation game by Klei Entertainment.

Contains methods aimed at improving cross-mod compatibility, in-game user interfaces, and game-wide functions such as Actions and Lighting. An easy-to-use Mod Options menu is also included.</Description>
<PackageReleaseNotes>Fix a potential crash when running PLib AVC on the Epic Game Store or WeGame editions.
<PackageReleaseNotes>PLib AVC will not warn for installed mods which are newer than the online version, if the mod version uses a standard semantic version.

Silence a warning emitted when buildings are registered with PLib.</PackageReleaseNotes>
PLib AVC now displays a main menu warning if mods are outdated.</PackageReleaseNotes>
<RootNamespace>PeterHan.PLib</RootNamespace>
<AssemblyVersion>$(PLibVersion)</AssemblyVersion>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
Expand Down
2 changes: 1 addition & 1 deletion PLibAVC/JsonURLVersionChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private ModVersionCheckResults ParseModVersion(Mod mod, ModVersions versions) {
if (string.IsNullOrEmpty(newVersion))
result = new ModVersionCheckResults(id, true);
else
result = new ModVersionCheckResults(id, newVersion !=
result = new ModVersionCheckResults(id, newVersion ==
PVersionCheck.GetCurrentVersion(mod), newVersion);
break;
}
Expand Down
112 changes: 112 additions & 0 deletions PLibAVC/ModOutdatedWarning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2024 Peter Han
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using PeterHan.PLib.Core;
using PeterHan.PLib.Detours;
using UnityEngine;

namespace PeterHan.PLib.AVC {
/// <summary>
/// Added to the main menu to warn users if mods are out of date.
/// </summary>
internal sealed class ModOutdatedWarning : KMonoBehaviour {
/// <summary>
/// The singleton (should be!) instance of this class.
/// </summary>
internal static ModOutdatedWarning Instance { get; private set; }

// Detour the "Resume Game" button
private static readonly IDetouredField<MainMenu, KButton> RESUME_GAME = PDetours.
DetourFieldLazy<MainMenu, KButton>(nameof(MainMenu.Button_ResumeGame));

/// <summary>
/// The button used to open the Mods screen.
/// </summary>
private GameObject modsButton;

internal ModOutdatedWarning() {
modsButton = null;
}

/// <summary>
/// Finds the "Mods" button and stores it in the modsButton field.
/// </summary>
/// <param name="buttonParent">The parent of all the main menu buttons.</param>
private void FindModsButton(Transform buttonParent) {
int n = buttonParent.childCount;
for (int i = 0; i < n; i++) {
var button = buttonParent.GetChild(i).gameObject;
// Match by text... sucks but unlikely to have changed this early
if (button != null && button.GetComponentInChildren<LocText>()?.text ==
STRINGS.UI.FRONTEND.MODS.TITLE) {
modsButton = button;
break;
}
}
if (modsButton == null)
PUtil.LogWarning("Unable to find Mods menu button, main menu update warning will not be functional");
}

protected override void OnCleanUp() {
Instance = null;
base.OnCleanUp();
}

protected override void OnPrefabInit() {
base.OnPrefabInit();
var mm = GetComponent<MainMenu>();
Instance = this;
try {
Transform buttonParent;
KButton resumeButton;
// "Resume game" is in the same panel as "Mods"
if (mm != null && (resumeButton = RESUME_GAME.Get(mm)) != null &&
(buttonParent = resumeButton.transform.parent) != null)
FindModsButton(buttonParent);
} catch (DetourException) { }
UpdateText();
}

/// <summary>
/// Updates the Mods button text.
/// </summary>
private void UpdateText() {
var inst = PVersionCheck.Instance;
if (modsButton != null && inst != null) {
var modsText = modsButton.GetComponentInChildren<LocText>();
int outdated = inst.OutdatedMods;
if (outdated > 0 && modsText != null) {
string text = STRINGS.UI.FRONTEND.MODS.TITLE;
if (outdated == 1)
text += PLibStrings.MAINMENU_UPDATE_1;
else
text += string.Format(PLibStrings.MAINMENU_UPDATE, outdated);
modsText.text = text;
}
}
}

/// <summary>
/// Updates the button text in a coroutine after one frame.
/// </summary>
public System.Collections.IEnumerator UpdateTextThreaded() {
yield return null;
UpdateText();
}
}
}
Loading

0 comments on commit a806808

Please sign in to comment.