Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit b1e43e3

Browse files
committed
Reconnect to last world automatically
1 parent bb8a3ae commit b1e43e3

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

PluginLoader/LoaderTools.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
using VRage;
2222
using VRage.Audio;
2323
using Sandbox.Game.Gui;
24+
using System.Collections.Generic;
2425

2526
namespace avaness.PluginLoader
2627
{
2728
public static class LoaderTools
2829
{
2930
public static string PluginsDir => Path.GetFullPath(Path.Combine(MyFileSystem.ExePath, "Plugins"));
3031

32+
public const string AutoRejoinArg = "-auto_join_world";
33+
3134
public static DialogResult ShowMessageBox(string msg, MessageBoxButtons buttons = MessageBoxButtons.OK, MessageBoxIcon icon = MessageBoxIcon.None, MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1)
3235
{
3336
if (Application.OpenForms.Count > 0)
@@ -58,9 +61,15 @@ public static DialogResult ShowMessageBox(string msg, MessageBoxButtons buttons
5861
public static void AskToRestart()
5962
{
6063
if (MyGuiScreenGamePlay.Static != null)
61-
AskSave(delegate { UnloadAndRestart(); });
64+
AskSave(delegate {
65+
Unload();
66+
Restart();
67+
});
6268
else
63-
UnloadAndRestart();
69+
{
70+
Unload();
71+
Restart();
72+
}
6473
}
6574

6675
/// <summary>
@@ -127,21 +136,44 @@ void UnloadAndExitAfterScreenshotWasTaken(object sender, EventArgs e)
127136
}
128137
}
129138

130-
131-
public static void UnloadAndRestart()
139+
public static void Unload()
132140
{
133141
LogFile.Dispose();
134142
MySessionLoader.Unload();
135143
MySandboxGame.Config.ControllerDefaultOnStart = MyInput.Static.IsJoystickLastUsed;
136144
MySandboxGame.Config.Save();
137145
MyScreenManager.CloseAllScreensNowExcept(null);
138146
MyPlugins.Unload();
139-
Restart();
140147
}
141148

142-
public static void Restart()
149+
public static void Restart(bool autoRejoin = false)
150+
{
151+
Start(autoRejoin);
152+
Process.GetCurrentProcess().Kill();
153+
}
154+
155+
private static void Start(bool autoRejoin)
143156
{
144-
Application.Restart();
157+
// Regular app case
158+
StringBuilder sb = new StringBuilder();
159+
IEnumerable<string> args = Environment.GetCommandLineArgs().Skip(1).Where(x => x != AutoRejoinArg);
160+
if(autoRejoin)
161+
args = args.Append(AutoRejoinArg);
162+
foreach (string arg in args)
163+
{
164+
if (sb.Length > 0)
165+
sb.Append(' ');
166+
sb.Append('"');
167+
sb.Append(arg);
168+
sb.Append('"');
169+
}
170+
171+
ProcessStartInfo currentStartInfo = Process.GetCurrentProcess().StartInfo;
172+
currentStartInfo.FileName = Application.ExecutablePath;
173+
if (sb.Length > 0)
174+
currentStartInfo.Arguments = sb.ToString();
175+
176+
Process.Start(currentStartInfo);
145177
Process.GetCurrentProcess().Kill();
146178
}
147179

PluginLoader/Patch/Patch_CreateMenu.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using VRage.Game;
77
using VRage.Utils;
88
using VRageMath;
9+
using System;
910

1011
// ReSharper disable InconsistentNaming
1112

@@ -14,7 +15,9 @@ namespace avaness.PluginLoader.Patch
1415
[HarmonyPatch(typeof(MyGuiScreenMainMenu), "CreateMainMenu")]
1516
public static class Patch_CreateMainMenu
1617
{
17-
public static void Postfix(MyGuiScreenMainMenu __instance, Vector2 leftButtonPositionOrigin, ref Vector2 lastButtonPosition)
18+
private static bool usedAutoRejoin = false;
19+
20+
public static void Postfix(MyGuiScreenMainMenu __instance, Vector2 leftButtonPositionOrigin, ref Vector2 lastButtonPosition, MyGuiControlButton ___m_continueButton)
1821
{
1922
MyGuiControlButton lastBtn = null;
2023
foreach (var control in __instance.Controls)
@@ -45,6 +48,12 @@ public static void Postfix(MyGuiScreenMainMenu __instance, Vector2 leftButtonPos
4548
BorderColor = Vector4.Zero
4649
};
4750
__instance.Controls.Add(openBtn);
51+
52+
if (___m_continueButton != null && ___m_continueButton.Visible && !usedAutoRejoin && Environment.GetCommandLineArgs().Contains(LoaderTools.AutoRejoinArg))
53+
{
54+
___m_continueButton.PressButton();
55+
usedAutoRejoin = true;
56+
}
4857
}
4958
}
5059

@@ -54,7 +63,7 @@ public static class Patch_CreateInGameMenu
5463
{
5564
public static void Postfix(MyGuiScreenMainMenu __instance, Vector2 leftButtonPositionOrigin, ref Vector2 lastButtonPosition)
5665
{
57-
Patch_CreateMainMenu.Postfix(__instance, leftButtonPositionOrigin, ref lastButtonPosition);
66+
Patch_CreateMainMenu.Postfix(__instance, leftButtonPositionOrigin, ref lastButtonPosition, null);
5867
}
5968
}
6069
}

PluginLoader/Patch/Patch_IngameShortcuts.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ public static void ShowRestartMenu()
6262
private static void OnRestartMessageClosed(MyGuiScreenMessageBox.ResultEnum result)
6363
{
6464
if(result == MyGuiScreenMessageBox.ResultEnum.YES)
65-
LoaderTools.UnloadAndRestart();
65+
{
66+
LoaderTools.Unload();
67+
LoaderTools.Restart(true);
68+
}
6669
}
6770
}
6871
}

PluginLoader/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.12.2.0")]
35-
[assembly: AssemblyFileVersion("1.12.2.0")]
34+
[assembly: AssemblyVersion("1.12.3.0")]
35+
[assembly: AssemblyFileVersion("1.12.3.0")]

0 commit comments

Comments
 (0)