using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ValheimStatsToDiscord")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ValheimStatsToDiscord")]
[assembly: AssemblyTitle("ValheimStatsToDiscord")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace ValheimStatsToDiscord
{
[BepInPlugin("com.gamemaster.valheimonlinetracker", "Valheim Online Tracker", "1.1.2")]
public class ValheimOnlineTrackerPlugin : BaseUnityPlugin
{
public static ManualLogSource Log;
private static readonly HttpClient http = new HttpClient();
private float onlineTimer;
private ConfigEntry<string> WebhookURL;
private ConfigEntry<int> PostInterval;
private ConfigEntry<string> StartupMessage;
private ConfigEntry<string> ShutdownMessage;
private ConfigEntry<string> OnlineTitle;
public void Awake()
{
//IL_002c: Unknown result type (might be due to invalid IL or missing references)
//IL_0032: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
string text = Path.Combine(Paths.ConfigPath, "OnlineTracker");
Directory.CreateDirectory(text);
ConfigFile config = new ConfigFile(Path.Combine(text, "ValheimOnlineTracker.cfg"), true);
SetupConfig(config);
Task.Run(async delegate
{
if (!string.IsNullOrWhiteSpace(WebhookURL.Value))
{
await SendDiscord(StartupMessage.Value);
}
});
}
public void OnApplicationQuit()
{
Task.Run(async delegate
{
if (!string.IsNullOrWhiteSpace(WebhookURL.Value))
{
await SendDiscord(ShutdownMessage.Value);
}
});
}
public void Update()
{
if (!((Object)(object)ZNet.instance == (Object)null))
{
onlineTimer += Time.deltaTime;
if (onlineTimer >= (float)PostInterval.Value)
{
onlineTimer = 0f;
Task.Run((Func<Task?>)PostOnlinePlayers);
}
}
}
private void SetupConfig(ConfigFile config)
{
WebhookURL = config.Bind<string>("General", "WebhookURL", "", "Your Discord webhook URL to post online player messages. Default: (none)");
PostInterval = config.Bind<int>("General", "PostInterval", 600, "Interval in seconds between player online posts. Default: 600 (10 minutes)");
StartupMessage = config.Bind<string>("Messages", "StartupMessage", "\ud83d\udfe2 Server is now starting!", "Message posted when the server starts. Default: \ud83d\udfe2 Server is now starting!");
ShutdownMessage = config.Bind<string>("Messages", "ShutdownMessage", "\ud83d\udd34 Server is shutting down...", "Message posted when the server stops. Default: \ud83d\udd34 Server is shutting down...");
OnlineTitle = config.Bind<string>("Messages", "OnlineTitle", "\ud83d\udfe2 Online Players", "Header shown above the list of online players. Default: \ud83d\udfe2 Online Players");
}
private async Task PostOnlinePlayers()
{
if (!((Object)(object)ZNet.instance == (Object)null) && !string.IsNullOrWhiteSpace(WebhookURL.Value))
{
List<string> list = (from p in ZNet.instance.GetConnectedPeers()
where !string.IsNullOrEmpty(p.m_playerName)
select "- " + p.m_playerName).ToList();
if (list.Count != 0)
{
string message = $"{OnlineTitle.Value} ({list.Count})\n" + string.Join("\n", list);
await SendDiscord(message);
}
}
}
private async Task SendDiscord(string message)
{
string content = JsonConvert.SerializeObject((object)new
{
username = "Online Tracker",
content = message
});
try
{
HttpResponseMessage httpResponseMessage = await http.PostAsync(WebhookURL.Value, new StringContent(content, Encoding.UTF8, "application/json"));
if (!httpResponseMessage.IsSuccessStatusCode)
{
ManualLogSource log = Log;
string text = httpResponseMessage.StatusCode.ToString();
log.LogWarning((object)("[OnlineTracker] Discord response: " + text + " - " + await httpResponseMessage.Content.ReadAsStringAsync()));
}
}
catch (Exception ex)
{
Log.LogError((object)("[OnlineTracker] Failed to send Discord message: " + ex.Message));
}
}
}
}