Skip to content

Commit 1c477e9

Browse files
committed
adding git support: add timestamp to reponame if already used, #218
add .gitignore and .gitattributes to Resources, update versions cache json, add missing copy for cached versions json (to get updates faster on first run)
1 parent efac18b commit 1c477e9

7 files changed

Lines changed: 376 additions & 40 deletions

File tree

UnityLauncherPro/GetUnityUpdates.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,54 @@ private static string GetStringValue(string source, string propertyName)
385385

386386
private static List<UnityVersion> LoadCachedVersions()
387387
{
388-
string configFilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
388+
string configFilePath = ConfigurationManager
389+
.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)
390+
.FilePath;
391+
389392
string configDirectory = Path.GetDirectoryName(configFilePath);
390-
if (configDirectory == null) return new List<UnityVersion>();
393+
if (string.IsNullOrEmpty(configDirectory))
394+
return new List<UnityVersion>();
395+
396+
Directory.CreateDirectory(configDirectory);
391397

392398
string cacheFilePath = Path.Combine(configDirectory, CacheFileName);
393-
if (!File.Exists(cacheFilePath)) return new List<UnityVersion>();
394399

395-
Console.WriteLine("cache file: " + cacheFilePath);
400+
// copy from embedded resource on first run
401+
if (!File.Exists(cacheFilePath))
402+
{
403+
try
404+
{
405+
string resourceName = $"{typeof(GetUnityUpdates).Namespace}.Resources.{CacheFileName}";
406+
using (var stream = typeof(GetUnityUpdates).Assembly.GetManifestResourceStream(resourceName))
407+
{
408+
if (stream != null)
409+
{
410+
using (var fileStream = File.Create(cacheFilePath))
411+
{
412+
stream.CopyTo(fileStream);
413+
}
414+
}
415+
else
416+
{
417+
return new List<UnityVersion>();
418+
}
419+
}
420+
}
421+
catch
422+
{
423+
return new List<UnityVersion>();
424+
}
425+
}
396426

397-
string json = File.ReadAllText(cacheFilePath);
398-
return ParseCachedUnityVersions(json);
427+
try
428+
{
429+
string json = File.ReadAllText(cacheFilePath);
430+
return ParseCachedUnityVersions(json);
431+
}
432+
catch
433+
{
434+
return new List<UnityVersion>();
435+
}
399436
}
400437

401438
private static void SaveCachedVersions(List<UnityVersion> versions)

UnityLauncherPro/NewProject.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xmlns:data="clr-namespace:UnityLauncherPro.Data"
88
d:DataContext="{d:DesignInstance Type=local:NewProject}"
99
mc:Ignorable="d"
10-
Title="Create New Project" Height="650" Width="650" Background="{DynamicResource ThemeDarkestBackground}" PreviewKeyDown="Window_PreviewKeyDown" ResizeMode="NoResize" WindowStartupLocation="CenterOwner" ShowInTaskbar="True">
10+
Title="Create New Project" Height="660" Width="650" Background="{DynamicResource ThemeDarkestBackground}" PreviewKeyDown="Window_PreviewKeyDown" ResizeMode="NoResize" WindowStartupLocation="CenterOwner" ShowInTaskbar="True">
1111

1212
<Grid>
1313
<Grid.ColumnDefinitions>
@@ -87,7 +87,7 @@
8787
<Label Content="Connected as:" Foreground="{DynamicResource ThemeButtonForeground}" Margin="0,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
8888
<Label x:Name="lblGithubUsername" Content="(not connected)" Foreground="{DynamicResource ThemeButtonForeground}" Margin="5,0,0,0" VerticalAlignment="Center" FontStyle="Italic" MaxWidth="92"/>
8989
<Button x:Name="btnCreateToken" Style="{StaticResource CustomButton}" Content="Get Token 🔗" ToolTip="Open Github to create a token" Height="22" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,4,6,0" Grid.Column="1" FontSize="10" Width="66" Click="btnCreateToken_Click" />
90-
<TextBox x:Name="txtTokenInput" ToolTip="Enter your Github token here" VerticalAlignment="Center" IsUndoEnabled="True" Margin="0,2,0,2" HorizontalAlignment="Left" Width="100" TextChanged="txtTokenInput_TextChanged"/>
90+
<PasswordBox x:Name="txtTokenInput" ToolTip="Enter your Github token here" VerticalAlignment="Center" Margin="0,2,0,2" HorizontalAlignment="Left" Width="100" PasswordChanged="txtTokenInput_PasswordChanged"/>
9191
<Button x:Name="btnAuthorizeToken" Style="{StaticResource CustomButton}" Content="Authorize" ToolTip="Open Github to create a token" Height="22" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="6,4,0,0" IsEnabled="False" Width="60" Click="btnAuthorizeToken_Click" />
9292
<Button x:Name="btnDisconnectToken" Style="{StaticResource CustomButton}" Content="Disconnect" ToolTip="Disconnect from Github AND delete saved token" Height="22" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="6,4,0,0" IsEnabled="False" Width="64" Visibility="Collapsed" Click="btnDisconnectToken_Click" />
9393
</StackPanel>
@@ -140,7 +140,7 @@
140140
</Grid>
141141
</Expander>
142142

143-
<Grid UseLayoutRounding="False" Margin="0,4,0,0">
143+
<Grid UseLayoutRounding="False" Margin="0,2,0,0">
144144
<StatusBar VerticalAlignment="Center" Background="{x:Null}" Foreground="{DynamicResource ThemeStatusText}">
145145
<StatusBarItem>
146146
<TextBlock x:Name="txtNewProjectStatus" VerticalAlignment="Center" Text=""/>

UnityLauncherPro/NewProject.xaml.cs

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private async void LoadSettings()
165165
GitHubTokenStore.DeleteToken();
166166
ShowGitAuthorizedUI(false);
167167
}
168-
}
168+
} // LoadSettings
169169

170170
void UpdateTemplatesDropDown(string unityPath)
171171
{
@@ -290,6 +290,12 @@ private async void BtnCreateNewProject_Click(object sender, RoutedEventArgs e)
290290
{
291291
string token = GitHubTokenStore.LoadToken();
292292

293+
// if invalid repo, add DDMMYYY_HHMMSS
294+
if (lblRepoNameInvalid.Visibility == Visibility.Visible)
295+
{
296+
txtRepoName.Text += "_" + DateTime.Now.ToString("ddMMyyyy_HHmmss");
297+
}
298+
293299
GitHubCreateRepoResult result = await GithubActions.CreateRepositoryAsync(token: token, repoName: txtRepoName.Text, description: txtRepoDescription.Text, isPrivate: rbPrivate.IsChecked == true, autoInit: false);
294300

295301
if (result.Success)
@@ -330,12 +336,24 @@ private async void BtnCreateNewProject_Click(object sender, RoutedEventArgs e)
330336
// create .gitattributes if LFS enabled?
331337
if (chkEnableLfs.IsChecked == true)
332338
{
333-
var attributesUrl = "https://raw.githubusercontent.com/gitattributes/gitattributes/refs/heads/master/Unity.gitattributes";
334-
var res = await Tools.DownloadFileAsync(attributesUrl, Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitattributes"));
335-
if (res == false)
339+
//var attributesUrl = "https://raw.githubusercontent.com/gitattributes/gitattributes/refs/heads/master/Unity.gitattributes";
340+
//var res = await Tools.DownloadFileAsync(attributesUrl, Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitattributes"));
341+
//if (res == false)
342+
//{
343+
// Tools.SetStatus("Failed to download .gitattributes file for this project.");
344+
//}
345+
346+
// load from resources
347+
try
348+
{
349+
var attributesPath = Path.Combine(txtNewProjectFolder.Text, txtNewProjectName.Text, ".gitattributes");
350+
//File.WriteAllText(attributesPath, attributesContent);
351+
}
352+
catch (Exception ex)
336353
{
337-
Tools.SetStatus("Failed to download .gitattributes file for this project.");
354+
Tools.SetStatus("Failed to create .gitattributes file for this project: " + ex.Message);
338355
}
356+
339357
}
340358

341359
// download .gitignore if enabled
@@ -586,10 +604,19 @@ private void chkForceDX11_Checked(object sender, RoutedEventArgs e)
586604

587605
private void chkEnableVersionControl_Checked(object sender, RoutedEventArgs e)
588606
{
589-
if (isInitializing) return;
590-
591607
bool state = chkEnableVersionControl.IsChecked == true;
592608

609+
if (state == true)
610+
{
611+
btnCreateNewProject.Content = "Create Project + GitHub Repo";
612+
}
613+
else
614+
{
615+
btnCreateNewProject.Content = "Create Project";
616+
}
617+
618+
if (isInitializing) return;
619+
593620
// lock controls
594621
rbPrivate.IsEnabled = state;
595622
rbPublic.IsEnabled = state;
@@ -991,30 +1018,9 @@ private void btnCreateToken_Click(object sender, RoutedEventArgs e)
9911018

9921019

9931020

994-
private void txtTokenInput_TextChanged(object sender, TextChangedEventArgs e)
995-
{
996-
bool tokenSeemsOK = false;
997-
998-
if (string.IsNullOrWhiteSpace(txtTokenInput.Text)) return;
999-
1000-
if (txtTokenInput.Text.StartsWith("ghp_") && txtTokenInput.Text.Length == 40)
1001-
{
1002-
btnAuthorizeToken.IsEnabled = true;
1003-
txtTokenInput.BorderBrush = Brushes.Green;
1004-
tokenSeemsOK = true;
1005-
}
1006-
else
1007-
{
1008-
btnAuthorizeToken.IsEnabled = false;
1009-
txtTokenInput.BorderBrush = Brushes.Red;
1010-
}
1011-
1012-
btnAuthorizeToken.IsEnabled = tokenSeemsOK;
1013-
}
1014-
10151021
private void ShowGitAuthorizedUI(bool show)
10161022
{
1017-
txtTokenInput.Text = null;
1023+
txtTokenInput.Password = null;
10181024

10191025
if (show)
10201026
{
@@ -1050,7 +1056,7 @@ private void ShowGitAuthorizedUI(bool show)
10501056

10511057
private async void btnAuthorizeToken_Click(object sender, RoutedEventArgs e)
10521058
{
1053-
string token = txtTokenInput.Text.Trim();
1059+
string token = txtTokenInput.Password.Trim();
10541060

10551061
btnAuthorizeToken.IsEnabled = false;
10561062
btnDisconnectToken.IsEnabled = true;
@@ -1135,11 +1141,13 @@ private async void txtRepoName_TextChanged(object sender, TextChangedEventArgs e
11351141
txtNewProjectStatus.Text = res;
11361142
lblRepoNameValid.Visibility = Visibility.Collapsed;
11371143
lblRepoNameInvalid.Visibility = Visibility.Visible;
1144+
lblRepoNameInvalid.ToolTip = res;
11381145
}
11391146
else
11401147
{
11411148
lblRepoNameValid.Visibility = Visibility.Visible;
11421149
lblRepoNameInvalid.Visibility = Visibility.Collapsed;
1150+
lblRepoNameInvalid.ToolTip = "";
11431151
}
11441152
}
11451153

@@ -1157,5 +1165,25 @@ private void btnDisconnectToken_Click(object sender, RoutedEventArgs e)
11571165
ShowGitAuthorizedUI(false);
11581166
}
11591167

1168+
private void txtTokenInput_PasswordChanged(object sender, RoutedEventArgs e)
1169+
{
1170+
bool tokenSeemsOK = false;
1171+
1172+
if (string.IsNullOrWhiteSpace(txtTokenInput.Password)) return;
1173+
1174+
if (txtTokenInput.Password.StartsWith("ghp_") && txtTokenInput.Password.Length == 40)
1175+
{
1176+
btnAuthorizeToken.IsEnabled = true;
1177+
txtTokenInput.BorderBrush = Brushes.Green;
1178+
tokenSeemsOK = true;
1179+
}
1180+
else
1181+
{
1182+
btnAuthorizeToken.IsEnabled = false;
1183+
txtTokenInput.BorderBrush = Brushes.Red;
1184+
}
1185+
1186+
btnAuthorizeToken.IsEnabled = tokenSeemsOK;
1187+
}
11601188
} // class NewProject
11611189
} // namespace UnityLauncherPro

0 commit comments

Comments
 (0)