Skip to content

Commit 349c4c7

Browse files
author
VastraKai
committed
Prevent crashes from injecting too early
1 parent 274b9a0 commit 349c4c7

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Injector/Inject.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,26 @@ public static void LaunchMinecraft()
118118
UseShellExecute = true
119119
};
120120
Process.Start(startInfo);
121-
Thread.Sleep(2000);
121+
Process? mcProcess = null;
122+
123+
while (mcProcess == null)
124+
{
125+
mcProcess = Process.GetProcessesByName("Minecraft.Windows").FirstOrDefault();
126+
Thread.Sleep(100);
127+
}
128+
129+
Logger.Log("Inject", "Minecraft launched");
130+
// Wait for the module count to be more than 120 (the amount of modules loaded when the game is fully loaded)
131+
bool msgShown = false;
132+
while (mcProcess.Modules.Count < 120)
133+
{
134+
mcProcess.Refresh();
135+
Logger.LogWrite("Inject", $"Waiting for Minecraft to load... ({mcProcess.Modules.Count}/120) \r");
136+
Thread.Sleep(100);
137+
}
138+
139+
Console.WriteLine();
140+
Logger.Log("Inject", "Minecraft loaded");
122141
}
123142

124143
private static void ApplyAppPackages(string dllPath)

Injector/Logger.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,30 @@ public static void Log(string sender, string message, LType lType = LType.Info)
3434
Console.WriteLine(message);
3535
}
3636

37+
public static void LogWrite(string sender, string message, LType lType = LType.Info)
38+
{
39+
string time = DateTime.Now.ToString("HH:mm:ss");
40+
Console.ForegroundColor = ConsoleColor.Gray;
41+
Console.Write($"[{time}] ");
42+
43+
switch (lType)
44+
{
45+
case LType.Info:
46+
Console.ForegroundColor = ConsoleColor.Green;
47+
break;
48+
case LType.Warning:
49+
Console.ForegroundColor = ConsoleColor.Yellow;
50+
break;
51+
case LType.Error:
52+
Console.ForegroundColor = ConsoleColor.Red;
53+
break;
54+
}
55+
56+
Console.Write($"[{sender}] ");
57+
Console.ForegroundColor = ConsoleColor.White;
58+
Console.Write(message);
59+
}
60+
61+
62+
3763
}

0 commit comments

Comments
 (0)