Skip to content

Commit

Permalink
Merge pull request #5 from jonathan-robertson/dev
Browse files Browse the repository at this point in the history
Improve Performance, Reduce Overhead
  • Loading branch information
jonathan-robertson authored Jul 2, 2023
2 parents e5e9ce0 + bcff7f2 commit d5c15fc
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 96 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- update trader restock on login
- update trader restock ui to days remaining

## [1.0.2] - 2023-07-02

- fix notification color
- improve remaining rental time calculations
- improve time trigger frequency

## [1.0.1] - 2023-06-30

- update to support a21 b324 (stable)
Expand Down
4 changes: 2 additions & 2 deletions Config/Localization.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Key,File,Type,UsedInMainMenu,NoTranslate,english,Context / Alternate Text,german,latam,french,italian,japanese,koreana,polish,brazilian,russian,turkish,schinese,tchinese,spanish
buffDaysRemainingVendingExpirationFormat,UI,XUI,,?,{0:0} Days,,{0:0} Tage,{0:0} Días,{0:0} Jours,{0:0} Giorni,{0:0} 日,{0:0} 일,{0:0} Dni,{0:0} Dias,{0:0} Дней,{0:0} Gün,{0:0} 天数,{0:0} 天,{0:0} Días
buffDaysRemainingVendingExpirationName,buffs,Buff,,x,Vending Machine,,Automat,Máquina expendedora,Distributeur automatique,Distributore automatico,自動販売機,자판기,Automat sprzedający,Máquina de Vendas,Торговый автомат,Otomat,自动贩卖机,自動販賣機,Máquina expendedora
buffDaysRemainingVendingExpirationDesc,buffs,Buff,,x,Your vending machine rental will expire in [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] days.,,Ihre Automatenmiete läuft in [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] Tagen ab.,El alquiler de su máquina expendedora caducará en [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] días.,Votre location de distributeur expirera dans [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] jours.,Il noleggio del tuo distributore automatico scadrà tra [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] giorni.,自動販売機のレンタルは [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] 日後に期限切れになります.,자판기 대여가 [007fff]{cvar(daysRemainingVendingExpiration:0)}일 후에 만료됩니다.,Wypożyczenie automatu wygaśnie za [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] dni.,O aluguel da máquina de venda automática expira em [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] dias.,Срок аренды вашего торгового автомата истекает через [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] дней.,Otomat kiralama süreniz [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] gün içinde sona erecek.,您的自动售货机租赁将在 [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,您的自動售貨機租賃將在 [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,El alquiler de su máquina expendedora caducará en [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] días.
buffDaysRemainingVendingExpirationTooltip,buffs,Buff,,x,Your vending machine rental will expire in [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] days.,,Ihre Automatenmiete läuft in [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] Tagen ab.,El alquiler de su máquina expendedora caducará en [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] días.,Votre location de distributeur expirera dans [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] jours.,Il noleggio del tuo distributore automatico scadrà tra [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] giorni.,自動販売機のレンタルは [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] 日後に期限切れになります.,자판기 대여가 [007fff]{cvar(daysRemainingVendingExpiration:0)}일 후에 만료됩니다.,Wypożyczenie automatu wygaśnie za [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] dni.,O aluguel da máquina de venda automática expira em [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] dias.,Срок аренды вашего торгового автомата истекает через [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] дней.,Otomat kiralama süreniz [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] gün içinde sona erecek.,您的自动售货机租赁将在 [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,您的自動售貨機租賃將在 [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,El alquiler de su máquina expendedora caducará en [007fff]{cvar(daysRemainingVendingExpiration:0)}[-] días.
buffDaysRemainingVendingExpirationDesc,buffs,Buff,,x,Your vending machine rental will expire in [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] days.,,Ihre Automatenmiete läuft in [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] Tagen ab.,El alquiler de su máquina expendedora caducará en [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] días.,Votre location de distributeur expirera dans [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] jours.,Il noleggio del tuo distributore automatico scadrà tra [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] giorni.,自動販売機のレンタルは [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] 日後に期限切れになります.,자판기 대여가 [ffff00]{cvar(daysRemainingVendingExpiration:0)}일 후에 만료됩니다.,Wypożyczenie automatu wygaśnie za [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] dni.,O aluguel da máquina de venda automática expira em [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] dias.,Срок аренды вашего торгового автомата истекает через [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] дней.,Otomat kiralama süreniz [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] gün içinde sona erecek.,您的自动售货机租赁将在 [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,您的自動售貨機租賃將在 [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,El alquiler de su máquina expendedora caducará en [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] días.
buffDaysRemainingVendingExpirationTooltip,buffs,Buff,,x,Your vending machine rental will expire in [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] days.,,Ihre Automatenmiete läuft in [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] Tagen ab.,El alquiler de su máquina expendedora caducará en [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] días.,Votre location de distributeur expirera dans [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] jours.,Il noleggio del tuo distributore automatico scadrà tra [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] giorni.,自動販売機のレンタルは [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] 日後に期限切れになります.,자판기 대여가 [ffff00]{cvar(daysRemainingVendingExpiration:0)}일 후에 만료됩니다.,Wypożyczenie automatu wygaśnie za [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] dni.,O aluguel da máquina de venda automática expira em [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] dias.,Срок аренды вашего торгового автомата истекает через [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] дней.,Otomat kiralama süreniz [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] gün içinde sona erecek.,您的自动售货机租赁将在 [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,您的自動售貨機租賃將在 [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] 天后到期.,El alquiler de su máquina expendedora caducará en [ffff00]{cvar(daysRemainingVendingExpiration:0)}[-] días.
Binary file modified DaysRemaining.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion ModInfo.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<xml>
<Name value="kanaverum-days-remaining" />
<DisplayName value="Days Remaining" />
<Version value="1.0.1" />
<Version value="1.0.2" />
<Description value="Update trader restock date and vending expiration date to show days until vs date of." />
<Author value="Jonathan Robertson (Kanaverum)" />
<Website value="https://github.com/jonathan-robertson/days-remaining" />
Expand Down
22 changes: 18 additions & 4 deletions src/ModApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyLib;
using DaysRemaining.Utilities;
using HarmonyLib;
using System;
using System.Reflection;

Expand All @@ -8,7 +9,7 @@ internal class ModApi : IModApi
{
private static readonly ModLog<ModApi> _log = new ModLog<ModApi>();

public static bool DebugMode { get; set; } = false; // TODO: disable before release
public static bool DebugMode { get; set; } = true; // TODO: disable before release

public void InitMod(Mod _modInstance)
{
Expand All @@ -19,13 +20,26 @@ public void InitMod(Mod _modInstance)
// TODO: on rental agreement changed, calculate and send cvar

ModEvents.PlayerSpawnedInWorld.RegisterHandler(OnPlayerSpawnedInWorld);
ModEvents.GameUpdate.RegisterHandler(OnGameUpdate);
}

private void OnGameUpdate()
{
try
{
DayMonitor.OnGameUpdate();
}
catch (Exception e)
{
_log.Error($"OnGameUpdate", e);
}
}

private void OnPlayerSpawnedInWorld(ClientInfo _cInfo, RespawnType _respawnReason, Vector3i _pos)
{
try
{
if (_cInfo == null) // is client local?
if (_cInfo == null) // local client
{
// TODO: var ppId = ((_cInfo != null) ? _cInfo.InternalId : null) ?? PlatformManager.InternalLocalUserIdentifier;
return;
Expand All @@ -36,7 +50,7 @@ private void OnPlayerSpawnedInWorld(ClientInfo _cInfo, RespawnType _respawnReaso
case RespawnType.JoinMultiplayer:
if (GameManager.Instance.World.Players.dict.TryGetValue(_cInfo.entityId, out var player))
{
Helpers.SetExpirationDaysRemaining(_cInfo, player);
DayMonitor.SetExpirationDaysRemaining(_cInfo, player);
}
return;
}
Expand Down
55 changes: 0 additions & 55 deletions src/Patches/GameManager.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/Patches/NetPackageTileEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyLib;
using DaysRemaining.Utilities;
using HarmonyLib;
using System;

namespace DaysRemaining.Patches
Expand Down Expand Up @@ -31,7 +32,7 @@ public static void Postfix(TileEntity _te, Vector3i ___teWorldPos)
clientInfo.latestPlayerData.rentalEndDay = tileEntityVendingMachine.RentalEndDay;
// close the open trader (vending machine) window because the cvar cannot auto-refresh its ui value on its own
clientInfo.SendPackage(NetPackageManager.GetPackage<NetPackageConsoleCmdClient>().Setup("xui close trader", true));
Helpers.SetExpirationDaysRemaining(clientInfo, player);
DayMonitor.SetExpirationDaysRemaining(clientInfo, player);
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions src/Utilities/DayMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using DaysRemaining.Patches;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DaysRemaining.Utilities
{
internal class DayMonitor
{
private const string CVAR_VENDING_EXPIRATION = "daysRemainingVendingExpiration";
private const string BUFF_VENDING_EXPIRATION = "buffDaysRemainingVendingExpiration";

internal static int CurrentDay { get; private set; } = -1;

private static readonly ModLog<DayMonitor> _log = new ModLog<DayMonitor>();

internal static void OnGameUpdate()
{
if (IsNewDay(out var day))
{
CurrentDay = day;
SetExpirationDaysRemaining();
}
}

internal static void SetExpirationDaysRemaining()
{
var players = GameManager.Instance.World.Players.list;
for (var i = 0; i < players.Count; i++)
{
if (Helpers.TryGetClientInfo(players[i].entityId, out var clientInfo))
{
SetExpirationDaysRemaining(clientInfo, players[i]);
}
}
}

/// <summary>
/// Call this to update the player's client-side data related to vending expiration date.
/// </summary>
/// <param name="clientInfo">ClientInfo containing the current rental information.</param>
/// <param name="player">EntityPlayer to update.</param>
/// <remarks>Would've loved to use rentalEndTime here, but it isn't reported to the server (just rentalEndDay). Maybe rentalEndTime is deprecated?</remarks>
internal static void SetExpirationDaysRemaining(ClientInfo clientInfo, EntityPlayer player)
{
if (clientInfo == null || player == null)
{
_log.Warn($"ClientInfo and EntityPlayer params must not be null; ClientInfo {(clientInfo != null ? "exists" : "does not exist")}, EntityPlayer {(player != null ? "exists" : "does not exist")}.");
return;
}

if (clientInfo.latestPlayerData.rentedVMPosition == Vector3i.zero)
{
return; // player does not have a vending machine rental
}
var daysRemaining = Math.Max(clientInfo.latestPlayerData.rentalEndDay - GameUtils.WorldTimeToDays(GameManager.Instance.World.worldTime), 0);
if (daysRemaining != player.GetCVar(CVAR_VENDING_EXPIRATION))
{
player.SetCVar(CVAR_VENDING_EXPIRATION, daysRemaining);
_ = player.Buffs.AddBuff(BUFF_VENDING_EXPIRATION);
}
}

private static bool IsNewDay(out int day)
{
day = GameUtils.WorldTimeToDays(GameManager.Instance.World.worldTime);
return day != CurrentDay;
}
}
}
29 changes: 1 addition & 28 deletions src/Helpers.cs → src/Utilities/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;

namespace DaysRemaining
namespace DaysRemaining.Utilities
{
internal class Helpers
{
private const string CVAR_VENDING_EXPIRATION = "daysRemainingVendingExpiration";
private const string BUFF_VENDING_EXPIRATION = "buffDaysRemainingVendingExpiration";

private static readonly ModLog<Helpers> _log = new ModLog<Helpers>();

public static bool TryGetTileEntityVendingMachine(Vector3i blockPos, out TileEntityVendingMachine tileEntityVendingMachine)
Expand Down Expand Up @@ -64,29 +61,5 @@ public static bool TryGetVendingMachineRentalData(Vector3i blockPos, out Platfor
rentalEndDay = tileEntityVendingMachine.RentalEndDay;
return true;
}

/// <summary>
/// Call this to update the player's client-side data related to vending expiration date.
/// </summary>
/// <param name="clientInfo">ClientInfo containing the current rental information.</param>
/// <param name="player">EntityPlayer to update.</param>
public static void SetExpirationDaysRemaining(ClientInfo clientInfo, EntityPlayer player)
{
if (clientInfo == null || player == null)
{
_log.Warn($"ClientInfo and EntityPlayer params must not be null; ClientInfo {(clientInfo != null ? "exists" : "does not exist")}, EntityPlayer {(player != null ? "exists" : "does not exist")}.");
return;
}
if (clientInfo.latestPlayerData.rentalEndDay == 0)
{
return;
}
var daysRemaining = Math.Max(clientInfo.latestPlayerData.rentalEndDay - GameUtils.WorldTimeToDays(GameManager.Instance.World.worldTime), 0);
if (daysRemaining != player.GetCVar(CVAR_VENDING_EXPIRATION))
{
player.SetCVar(CVAR_VENDING_EXPIRATION, daysRemaining);
_ = player.Buffs.AddBuff(BUFF_VENDING_EXPIRATION);
}
}
}
}
2 changes: 1 addition & 1 deletion src/ModLog.cs → src/Utilities/ModLog.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace DaysRemaining
namespace DaysRemaining.Utilities
{
internal class ModLog<T>
{
Expand Down
6 changes: 3 additions & 3 deletions src/src.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers.cs" />
<Compile Include="ModLog.cs" />
<Compile Include="Utilities\DayMonitor.cs" />
<Compile Include="Utilities\Helpers.cs" />
<Compile Include="Utilities\ModLog.cs" />
<Compile Include="ModApi.cs" />
<Compile Include="Patches\GameManager.cs" />
<Compile Include="Patches\NetPackageTileEntity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down

0 comments on commit d5c15fc

Please sign in to comment.