Skip to content

Commit a8a49f7

Browse files
committedDec 9, 2024
add disable unity hub from launching on Editor start #95
1 parent fd47430 commit a8a49f7

File tree

4 files changed

+146
-10
lines changed

4 files changed

+146
-10
lines changed
 

‎UnityLauncherPro/MainWindow.xaml

+1
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@
811811
<CheckBox x:Name="chkAllowSingleInstanceOnly" Content="Allow single instance only" Checked="ChkAllowSingleInstanceOnly_CheckedChanged" Unchecked="ChkAllowSingleInstanceOnly_CheckedChanged" ToolTip="Activates already running instance, instead of starting new exe (not working if app is minized to taskbar)" HorizontalAlignment="Left"/>
812812
<CheckBox x:Name="useAlphaReleaseNotesSite" Content="Use Unity Alpha Release Notes Site (only for final versions) " ToolTip="Use the superior (but alpha) Unity Release Notes (https://alpha.release-notes.ds.unity3d.com/) site when clicking on the ReleaseNotes button. Otherwise will default to the normal build page." Checked="UseAlphaReleaseNotes_Checked" Unchecked="UseAlphaReleaseNotes_Checked"/>
813813
<CheckBox x:Name="useUnofficialReleaseList" Content="Use Unofficial Release Watch List (for latest downloads)" ToolTip="Checks latest releases from https://github.com/unitycoder/UnofficialUnityReleasesWatcher (if not yet available in The Unity Releases API)" Checked="useUnofficialReleaseList_Checked" Unchecked="useUnofficialReleaseList_Checked"/>
814+
<CheckBox x:Name="chkDisableUnityHubLaunch" Content="Disable UnityHub launch at Editor start" ToolTip="Overrides UnityHub IPC port. Note: You will be logged out in Editor!" Checked="chkDisableUnityHubLaunch_Checked" Unchecked="chkDisableUnityHubLaunch_Checked"/>
814815
<CheckBox x:Name="chkStreamerMode" Content="Streamer Mode (hide project names and folders)" ToolTip="Hide project names and folders in main view" Checked="ChkStreamerMode_Checked" Unchecked="ChkStreamerMode_Checked" HorizontalAlignment="Left"/>
815816
<!--<StackPanel Orientation="Horizontal" Margin="0,0,0,4">
816817
<TextBox x:Name="txtTemplatePackagesFolder" BorderBrush="Transparent" CaretBrush="{DynamicResource ThemeSearchCaret}" Background="{DynamicResource ThemeTextBoxBackground}" SelectionBrush="{DynamicResource ThemeSearchSelection}" Foreground="{DynamicResource ThemeSearchForeground}" ToolTip="Folder for your custom unitypackage templates (for new project)" Padding="0,3,0,0" Width="110" TextChanged="TxtTemplatePackagesFolder_TextChanged" />

‎UnityLauncherPro/MainWindow.xaml.cs

+130-10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public partial class MainWindow : Window
5555
string _filterString = null;
5656
bool multiWordSearch = false;
5757
string[] searchWords;
58+
bool isDirtyCell = false;
5859

5960
int lastSelectedProjectIndex = 0;
6061
Mutex myMutex;
@@ -73,8 +74,9 @@ public partial class MainWindow : Window
7374
List<BuildReport> buildReports = new List<BuildReport>(); // multiple reports, each contains their own stats and items
7475
int currentBuildReport = 0;
7576

76-
private NamedPipeServerStream pipeServer;
77-
private const string PipeName = appName;
77+
private NamedPipeServerStream launcherPipeServer;
78+
private const string launcherPipeName = appName;
79+
readonly string unityHubPipeName = "Unity-hubIPCService";
7880

7981
[DllImport("user32", CharSet = CharSet.Unicode)]
8082
static extern IntPtr FindWindow(string cls, string win);
@@ -190,9 +192,61 @@ void Start()
190192
//themeEditorWindow = new ThemeEditor();
191193
//themeEditorWindow.Show();
192194

195+
// test override IPC so that unityhub doesnt start
196+
// open "Unity-hubIPCService" pipe, if not already open
197+
198+
if (Settings.Default.disableUnityHubLaunch == true) StartHubPipe();
199+
193200
isInitializing = false;
194201
}
195202

203+
private static NamedPipeServerStream hubPipeServer;
204+
private CancellationTokenSource _hubCancellationTokenSource;
205+
206+
private async Task StartPipeServerAsync(string pipeName, Action<string> onMessageReceived, CancellationToken cancellationToken)
207+
{
208+
try
209+
{
210+
while (!cancellationToken.IsCancellationRequested)
211+
{
212+
using (var pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
213+
{
214+
await pipeServer.WaitForConnectionAsync(cancellationToken);
215+
Console.WriteLine($"Client connected to pipe '{pipeName}'!");
216+
217+
using (var reader = new StreamReader(pipeServer))
218+
{
219+
while (!cancellationToken.IsCancellationRequested)
220+
{
221+
string message = await reader.ReadLineAsync();
222+
if (message == null) break; // End of stream
223+
onMessageReceived(message);
224+
}
225+
}
226+
}
227+
}
228+
}
229+
catch (OperationCanceledException)
230+
{
231+
// Expected when the cancellation token is triggered
232+
Console.WriteLine("Pipe server operation canceled.");
233+
}
234+
catch (Exception ex)
235+
{
236+
Console.WriteLine($"Error in pipe server: {ex.Message}");
237+
}
238+
finally
239+
{
240+
Console.WriteLine("Named pipe server stopped.");
241+
}
242+
}
243+
244+
245+
private void OnHubMessageReceived(string message)
246+
{
247+
//Console.WriteLine(message);
248+
}
249+
196250
private void DataGridUpdates_SelectionChanged(object sender, SelectionChangedEventArgs e)
197251
{
198252
var selectedUp = GetSelectedUpdate();
@@ -460,6 +514,7 @@ void LoadSettings()
460514
txtCustomThemeFile.Text = Settings.Default.themeFile;
461515
useAlphaReleaseNotesSite.IsChecked = Settings.Default.useAlphaReleaseNotes;
462516
useUnofficialReleaseList.IsChecked = Settings.Default.useUnofficialReleaseList;
517+
chkDisableUnityHubLaunch.IsChecked = Settings.Default.disableUnityHubLaunch;
463518

464519
chkEnablePlatformSelection.IsChecked = Settings.Default.enablePlatformSelection;
465520
chkRunAutomatically.IsChecked = Settings.Default.runAutomatically;
@@ -1137,11 +1192,11 @@ private void Window_Closing(object sender, CancelEventArgs e)
11371192
{
11381193
// (TODO force) close theme editor, if still open, TODO NEED to cancel all changes
11391194
CloseThemeEditor();
1140-
11411195
SaveSettingsOnExit();
1196+
CloseHubPipeAsync();
11421197
}
11431198

1144-
private void CloseThemeEditor()
1199+
private void CloseThemeEditor()
11451200
{
11461201
if (themeEditorWindow != null) themeEditorWindow.Close();
11471202
}
@@ -2150,7 +2205,6 @@ public void CanExecute_Copy(object sender, CanExecuteRoutedEventArgs e)
21502205
e.CanExecute = true;
21512206
}
21522207

2153-
bool isDirtyCell = false;
21542208
private void GridRecent_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
21552209
{
21562210
isDirtyCell = true;
@@ -3730,7 +3784,7 @@ private void ActivateRunningInstance()
37303784
{
37313785
try
37323786
{
3733-
using (var pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
3787+
using (var pipeClient = new NamedPipeClientStream(".", launcherPipeName, PipeDirection.Out))
37343788
{
37353789
pipeClient.Connect(1000); // Wait for 1 second to connect
37363790
using (var writer = new StreamWriter(pipeClient))
@@ -3749,18 +3803,18 @@ private void ActivateRunningInstance()
37493803

37503804
private void StartPipeServer()
37513805
{
3752-
pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
3753-
pipeServer.BeginWaitForConnection(OnPipeConnection, null);
3806+
launcherPipeServer = new NamedPipeServerStream(launcherPipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
3807+
launcherPipeServer.BeginWaitForConnection(OnPipeConnection, null);
37543808
}
37553809

37563810
private void OnPipeConnection(IAsyncResult result)
37573811
{
37583812
try
37593813
{
3760-
pipeServer.EndWaitForConnection(result);
3814+
launcherPipeServer.EndWaitForConnection(result);
37613815

37623816
// Read the message
3763-
using (var reader = new StreamReader(pipeServer))
3817+
using (var reader = new StreamReader(launcherPipeServer))
37643818
{
37653819
string message = reader.ReadLine();
37663820
if (message == "WakeUp")
@@ -3841,6 +3895,72 @@ private void useUnofficialReleaseList_Checked(object sender, RoutedEventArgs e)
38413895
Settings.Default.useUnofficialReleaseList = (bool)useUnofficialReleaseList.IsChecked;
38423896
Settings.Default.Save();
38433897
}
3898+
3899+
private async void chkDisableUnityHubLaunch_Checked(object sender, RoutedEventArgs e)
3900+
{
3901+
if (!this.IsActive) return; // Don't run code during window initialization
3902+
3903+
Console.WriteLine((bool)chkDisableUnityHubLaunch.IsChecked);
3904+
3905+
if ((bool)chkDisableUnityHubLaunch.IsChecked)
3906+
{
3907+
await CloseHubPipeAsync(); // Ensure old task is closed before starting a new one
3908+
StartHubPipe();
3909+
}
3910+
else
3911+
{
3912+
await CloseHubPipeAsync();
3913+
}
3914+
3915+
Settings.Default.disableUnityHubLaunch = (bool)chkDisableUnityHubLaunch.IsChecked;
3916+
Settings.Default.Save();
3917+
}
3918+
3919+
private void StartHubPipe()
3920+
{
3921+
if (_hubCancellationTokenSource != null && !_hubCancellationTokenSource.IsCancellationRequested)
3922+
{
3923+
Console.WriteLine("Pipe server already running.");
3924+
return; // Avoid starting multiple instances
3925+
}
3926+
3927+
_hubCancellationTokenSource = new CancellationTokenSource();
3928+
Task.Run(() => StartPipeServerAsync("Unity-hubIPCService", OnHubMessageReceived, _hubCancellationTokenSource.Token));
3929+
Console.WriteLine("StartHubPipe");
3930+
}
3931+
3932+
private async Task CloseHubPipeAsync()
3933+
{
3934+
if (_hubCancellationTokenSource == null || _hubCancellationTokenSource.IsCancellationRequested)
3935+
{
3936+
Console.WriteLine("Pipe server already stopped.");
3937+
return;
3938+
}
3939+
3940+
Console.WriteLine("CloseHubPipe..");
3941+
3942+
// Cancel the token to stop the server task
3943+
_hubCancellationTokenSource.Cancel();
3944+
3945+
try
3946+
{
3947+
// Allow the server to shut down gracefully
3948+
await Task.Delay(100); // Optional: Give some time for clean-up
3949+
}
3950+
catch (Exception ex)
3951+
{
3952+
Console.WriteLine($"Error during pipe server shutdown: {ex.Message}");
3953+
}
3954+
finally
3955+
{
3956+
_hubCancellationTokenSource.Dispose();
3957+
_hubCancellationTokenSource = null;
3958+
Console.WriteLine("Pipe server stopped.");
3959+
}
3960+
}
3961+
3962+
3963+
38443964
//private void menuProjectProperties_Click(object sender, RoutedEventArgs e)
38453965
//{
38463966
// var proj = GetSelectedProject();

‎UnityLauncherPro/Properties/Settings.Designer.cs

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎UnityLauncherPro/Properties/Settings.settings

+3
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,8 @@
151151
<Setting Name="useUnofficialReleaseList" Type="System.Boolean" Scope="User">
152152
<Value Profile="(Default)">False</Value>
153153
</Setting>
154+
<Setting Name="disableUnityHubLaunch" Type="System.Boolean" Scope="User">
155+
<Value Profile="(Default)">False</Value>
156+
</Setting>
154157
</Settings>
155158
</SettingsFile>

0 commit comments

Comments
 (0)