Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion NTimeManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
Expand Down Expand Up @@ -66,7 +67,18 @@ private void ApplyDurations()
new[] { "NightLengthInMinutes", "NightLength", "nightLengthInMinutes" },
_config.NightDurationMinutes);

if (!dayApplied || !nightApplied)
if (dayApplied || nightApplied)
{
TryRefreshCycle(sky.Cycle);
}

var envDayAvailable = HasConsoleCommand("env.daylength");
var envNightAvailable = HasConsoleCommand("env.nightlength");
var envDayApplied = envDayAvailable && TrySetEnvLength("env.daylength", _config.DayDurationMinutes);
var envNightApplied = envNightAvailable && TrySetEnvLength("env.nightlength", _config.NightDurationMinutes);

if ((!dayApplied || !nightApplied)
&& ((envDayAvailable && !envDayApplied) || (envNightAvailable && !envNightApplied)))
{
PrintWarning("Could not apply day/night lengths via TOD cycle parameters.");
}
Expand Down Expand Up @@ -205,5 +217,71 @@ private static bool TrySetCycleValue(object cycle, string[] memberNames, float v

return false;
}

private static void TryRefreshCycle(object cycle)
{
if (cycle == null)
{
return;
}

var cycleType = cycle.GetType();
var refreshMethod = cycleType.GetMethod("Refresh");
if (refreshMethod != null)
{
refreshMethod.Invoke(cycle, null);
return;
}

var updateMethod = cycleType.GetMethod("Update");
if (updateMethod != null)
{
updateMethod.Invoke(cycle, null);
}
}

private static bool TrySetEnvLength(string command, float value)
{
if (!HasConsoleCommand(command))
{
return false;
}

try
{
ConsoleSystem.Run(ConsoleSystem.Option.Server, command,
value.ToString(CultureInfo.InvariantCulture));
return true;
}
catch (Exception)
{
return false;
}
}

private static bool HasConsoleCommand(string command)
{
var indexProperty = typeof(ConsoleSystem).GetProperty("Index");
if (indexProperty == null)
{
return false;
}

var index = indexProperty.GetValue(null);
if (index == null)
{
return false;
}

var indexType = index.GetType();
var findMethod = indexType.GetMethod("FindCommand", new[] { typeof(string) })
?? indexType.GetMethod("GetCommand", new[] { typeof(string) });
if (findMethod == null)
{
return false;
}

return findMethod.Invoke(index, new object[] { command }) != null;
}
}
}