Skip to content

Commit

Permalink
big changes to launching
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Jan 1, 2024
1 parent d1cc7d4 commit 5dcf88c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
62 changes: 49 additions & 13 deletions JiayiLauncher/Features/Game/Minecraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Timers;
using JiayiLauncher.Features.Launch;
using JiayiLauncher.Features.Mods;
using JiayiLauncher.Features.Stats;
using JiayiLauncher.Settings;
Expand All @@ -13,18 +15,11 @@ namespace JiayiLauncher.Features.Game;

public static class Minecraft
{
private static readonly List<Mod> _modsLoaded = new();
private static readonly Timer _timer = new(1000);
private static bool _callbackSet;

public static List<Mod> ModsLoaded { get; } = new();

public static List<Mod> ModsLoaded
{
get
{
if (!IsOpen) _modsLoaded.Clear();
return _modsLoaded;
}
}

public static bool IsOpen
{
get
Expand All @@ -48,28 +43,68 @@ public static async Task Open()
Process = Process.GetProcessesByName("Minecraft.Windows")[0];
}

public static void TrackGameTime()
public static void StartUpdate()
{
if (_timer.Enabled) return; // i think this fixes duplicating game time
if (_timer.Enabled) return;

if (_callbackSet)
{
_timer.Start();
return;
}

_timer.Elapsed += (_, _) =>
{
if (!IsOpen)
{
_timer.Stop();
ModsLoaded.Clear();
}
else
{
JiayiStats.Instance!.TotalPlayTime += TimeSpan.FromSeconds(1);

foreach (var mod in _modsLoaded)
foreach (var mod in ModsLoaded)
{
mod.PlayTime += TimeSpan.FromSeconds(1);

var path = mod.RealPath ?? mod.Path;

bool external;
if (path.EndsWith(".exe")) external = true;
else if (path.EndsWith(".dll")) external = false;
else
{
Log.Write(nameof(Minecraft),
$"Loaded mod {mod.Name} mysteriously disappeared. Removing from list.", Log.LogLevel.Warning);
ModsLoaded.Remove(mod);
break; // throws an exception if we don't break
}

// check if the mod is still loaded
if (external)
{
if (Process.GetProcessesByName(Path.GetFileNameWithoutExtension(path)).Length != 0) continue;

ModsLoaded.Remove(mod);
Log.Write(nameof(Minecraft), $"{mod.Name} is no longer loaded");
break;
}

// internal mod
if (Injector.IsInjected(path)) continue;

ModsLoaded.Remove(mod);
Log.Write(nameof(Minecraft), $"{mod.Name} is no longer loaded");
break;
}
}

JiayiStats.Save();
ModCollection.Current!.Save();
};

_callbackSet = true;
_timer.Start();
}

Expand All @@ -87,6 +122,7 @@ await Task.Run(() =>
&& Process.Modules.Count > JiayiSettings.Instance.ModuleRequirement[2])
break;

if (!IsOpen) break;
if (Process.Modules.Count > 165) break;

if (JiayiSettings.Instance.AccelerateGameLoading) AccelerateGameLoading();
Expand Down
6 changes: 4 additions & 2 deletions JiayiLauncher/Features/Launch/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static async Task<LaunchResult> Launch(Mod mod)
LaunchProgress += 35;
LaunchProgressChanged?.Invoke(null, EventArgs.Empty);
}

mod.RealPath = path;

// either wait for the game's modules to load
// or if the user has injection delay enabled, wait for the time they specified
Expand Down Expand Up @@ -135,7 +137,7 @@ public static async Task<LaunchResult> Launch(Mod mod)
Launching = false;

Minecraft.ModsLoaded.Add(mod);
Minecraft.TrackGameTime();
Minecraft.StartUpdate();
JiayiStats.Instance!.MostRecentMod = mod;

return LaunchResult.Success;
Expand All @@ -156,7 +158,7 @@ public static async Task<LaunchResult> Launch(Mod mod)
if (injected)
{
Minecraft.ModsLoaded.Add(mod);
Minecraft.TrackGameTime();
Minecraft.StartUpdate();
JiayiStats.Instance!.MostRecentMod = mod;
}

Expand Down
2 changes: 2 additions & 0 deletions JiayiLauncher/Features/Mods/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class Mod
public List<string> SupportedVersions { get; set; }
public string Arguments { get; set; } = string.Empty;
public TimeSpan PlayTime { get; set; } = TimeSpan.Zero;

[JsonIgnore] public bool FromInternet => Path.StartsWith("http");
[JsonIgnore] public string? RealPath;

// for serialization
public Mod() { }
Expand Down
3 changes: 3 additions & 0 deletions JiayiLauncher/Features/Mods/ModCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public void Save()

using var stream = File.OpenWrite(indexPath);
JsonSerializer.Serialize(stream, this, _options);

#if DEBUG
Log.Write(this, "Saved mod collection.");
#endif
}

public static void Load(string path)
Expand Down

0 comments on commit 5dcf88c

Please sign in to comment.