Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions OpenKh.Tools.ModsManager/Services/ModsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ public static Task InstallMod(
bool isLuaFile,
Action<string> progressOutput = null,
Action<float> progressNumber = null,
string platformUrl = null,
string branchName = null)
{
if (!isZipFile && !isLuaFile)
{
return Task.Run(() => InstallModFromGithub(name, progressOutput, progressNumber, branchName));
return Task.Run(() => InstallModFromGit(name, progressOutput, progressNumber, platformUrl, branchName));
}
else if (isZipFile && !isLuaFile)
{
Expand Down Expand Up @@ -277,10 +278,11 @@ public static void InstallModFromZip(
}
}

public static async Task InstallModFromGithub(
public static async Task InstallModFromGit(
string repositoryName,
Action<string> progressOutput = null,
Action<float> progressNumber = null,
string platformName = null,
string branchName = null)
{
var parts = repositoryName.Split('/');
Expand All @@ -294,15 +296,15 @@ public static async Task InstallModFromGithub(
if (!string.IsNullOrWhiteSpace(branchName))
{
progressOutput?.Invoke($"Fetching file {ModMetadata} from branch {branchName}");
var isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata);
var isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata, platformName);
if (!isValidMod)
throw new BranchNotValidException(repositoryName, branchName);
}
else
{
branchName = DefaultGitBranch;
progressOutput?.Invoke($"Fetching file {ModMetadata} from {branchName}");
var isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata);
var isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata, platformName);
if (!isValidMod)
{
progressOutput?.Invoke($"{ModMetadata} not found, fetching default branch name");
Expand All @@ -311,7 +313,7 @@ public static async Task InstallModFromGithub(
throw new RepositoryNotFoundException(repositoryName);

progressOutput?.Invoke($"Fetching file {ModMetadata} from {branchName}");
isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata);
isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata, platformName);
}

if (!isValidMod)
Expand Down Expand Up @@ -363,7 +365,16 @@ await Task.Run(() =>
return true;
};

Repository.Clone($"https://github.com/{repositoryName}", modPath, options);
var _fetchPlatformUrl = new Uri("https://github.com/");
var _fetchRelativeUri = new Uri(_fetchPlatformUrl, $"{repositoryName}");

if (!String.IsNullOrWhiteSpace(platformName))
{
_fetchPlatformUrl = new Uri(platformName.Contains("http") ? platformName : "https://" + platformName);
_fetchRelativeUri = new Uri(_fetchPlatformUrl, $"{repositoryName}");
}

Repository.Clone(_fetchRelativeUri.ToString(), modPath, options);
});
if (IsCollection(repositoryName))
await MoveToCollection(repositoryName);
Expand Down
50 changes: 48 additions & 2 deletions OpenKh.Tools.ModsManager/Services/RepositoryService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using LibGit2Sharp;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -38,8 +40,52 @@ public static async Task<string> GetMainBranchFromRepository(string repositoryNa
return JsonConvert.DeserializeObject<ReposResponse>(await response.Content.ReadAsStringAsync())?.DefaultBranch;
}

public static Task<bool> IsFileExists(string repoName, string branch, string filePath) =>
IsFileExists($"https://raw.githubusercontent.com/{repoName}/{branch}/{filePath}");
public static Task<bool> IsFileExists(string repoName, string branch, string filePath, string platformUrl = null)
{
if (!string.IsNullOrEmpty(platformUrl))
{
var _fetchBaseUri = new Uri(platformUrl.Contains("http") ? platformUrl : "https://" + platformUrl);
var _fetchRelativeUri = new Uri(_fetchBaseUri, $"{repoName}");

var _fetchRepoDirectory = $"gitfetch/{repoName}/{branch}";

var _cloneOptions = new CloneOptions
{
Checkout = false,
IsBare = true,
BranchName = branch
};

_cloneOptions.FetchOptions.Prune = true;

Repository.Clone(_fetchRelativeUri.ToString(), _fetchRepoDirectory, _cloneOptions);
var _fetchRepository = new Repository(_fetchRepoDirectory);

var _fetchCommit = _fetchRepository.Head.Tip;
var _doesModFileExist = _fetchCommit["mod.yml"] != null;

_fetchRepository.Dispose();

if (Directory.Exists(_fetchRepoDirectory))
{
var _fetchAllFiles = Directory.GetFiles(_fetchRepoDirectory, "*", SearchOption.AllDirectories);
var _fetchAllDirectories = Directory.GetDirectories(_fetchRepoDirectory, "*", SearchOption.AllDirectories);

foreach (var _fetchFile in _fetchAllFiles)
File.SetAttributes(_fetchFile, FileAttributes.Normal);

foreach (string _fetchDirectory in _fetchAllDirectories)
File.SetAttributes(_fetchDirectory, FileAttributes.Normal);

Directory.Delete(_fetchRepoDirectory, true);
}

return Task.FromResult(_doesModFileExist);
}

else
return IsFileExists($"https://raw.githubusercontent.com/{repoName}/{branch}/{filePath}");
}

public static async Task<bool> IsFileExists(string url)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private async void InstallMod()
});

// Install mod with progress updates
await Task.Run(() => ModsService.InstallModFromGithub(Repo,
await Task.Run(() => ModsService.InstallModFromGit(Repo,
progress => {
Application.Current.Dispatcher.Invoke(() => progressWindow.ProgressText = progress);
},
Expand Down
3 changes: 2 additions & 1 deletion OpenKh.Tools.ModsManager/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ public MainViewModel()
var name = view.RepositoryName;
var isZipFile = view.IsZipFile;
var isLuaFile = view.IsLuaFile;
var platformUrl = view.PlatformURL;
var branchName = view.BranchName;
progressWindow = Application.Current.Dispatcher.Invoke(() =>
{
Expand All @@ -512,7 +513,7 @@ await ModsService.InstallMod(name, isZipFile, isLuaFile, progress =>
}, nProgress =>
{
Application.Current.Dispatcher.Invoke(() => progressWindow.ProgressValue = nProgress);
}, branchName);
}, platformUrl, branchName);
var repoName = name;
if (!isZipFile && !isLuaFile)
{
Expand Down
16 changes: 13 additions & 3 deletions OpenKh.Tools.ModsManager/ViewModels/ModViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using LibGit2Sharp;
using OpenKh.Common;
using OpenKh.Tools.ModsManager.Models;
using OpenKh.Tools.ModsManager.Services;
Expand All @@ -7,6 +8,7 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
Expand Down Expand Up @@ -41,6 +43,14 @@ public ModViewModel(ModModel model, IChangeModEnableState changeModEnableState)
{
Author = Source[0..nameIndex];
Name = Source[(nameIndex + 1)..];

var _fetchRepository = new Repository(_model.Path);
var _fetchRemoteUrl = _fetchRepository.Network.Remotes.ElementAt(0).Url.TrimEnd('/');

SourceUrl = _fetchRemoteUrl;
ReportBugUrl = _fetchRemoteUrl + "/issues";

AuthorUrl = _fetchRemoteUrl.Substring(0, _fetchRemoteUrl.LastIndexOf('/') + 1);
}
else
{
Expand Down Expand Up @@ -175,9 +185,9 @@ public CollectionSettingsViewModel CollectionSelectedValue
public string Name { get; }
public string Author { get; }
public string Source => _model.Name;
public string AuthorUrl => $"https://github.com/{Author}";
public string SourceUrl => $"https://github.com/{Source}";
public string ReportBugUrl => $"https://github.com/{Source}/issues";
public string AuthorUrl { get; set; }
public string SourceUrl { get; set; }
public string ReportBugUrl { get; set; }
public string FilesToPatch
{
get
Expand Down
37 changes: 28 additions & 9 deletions OpenKh.Tools.ModsManager/Views/InstallModView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
FocusManager.FocusedElement="{Binding ElementName=txtSourceModUrl}"
Title="Install a new mod or Lua Script..." Height="160" Width="300" SizeToContent="Height" Background="{Binding ColorTheme.BackgroundColor}" Foreground="{Binding ColorTheme.TextColor}">
Title="Install a new mod or Lua Script..." Height="183" Width="300" SizeToContent="Height" Background="{Binding ColorTheme.BackgroundColor}" Foreground="{Binding ColorTheme.TextColor}">
<Window.InputBindings>
<KeyBinding Key="Esc" Command="{Binding CloseCommand}"/>
</Window.InputBindings>
Expand All @@ -29,16 +29,35 @@
Foreground="Gray" Visibility="{Binding ElementName=txtSourceModUrl, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" IsHitTestVisible="False"/>
<TextBox Name="txtSourceModUrl" Background="Transparent" Text="{Binding RepositoryName, UpdateSourceTrigger=PropertyChanged}" KeyUp="txtSourceModUrl_KeyUp" />
</Grid>

<TextBlock Margin="0 0 0 3">Branch (optional)</TextBlock>
<Grid Margin="0 0 0 5" Background="White">
<TextBlock Margin="5,1" MinWidth="50" Text="eg. main"
Foreground="Gray" Visibility="{Binding ElementName=txtBranchName, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" IsHitTestVisible="False"/>
<TextBox Name="txtBranchName" Background="Transparent" Text="{Binding BranchName, UpdateSourceTrigger=PropertyChanged}" KeyUp="txtSourceModUrl_KeyUp" />
</Grid>


<TextBlock Margin="0 0 0 3">Install a Mod Archive or Lua Script</TextBlock>
<Button Margin="0 0 0 5" Click="InstallLocalFile_Click">Select and install Mod Archive or Lua Script</Button>

<Expander Header="More Options" Foreground="{Binding ColorTheme.TextColor}">
<StackPanel>
<TextBlock Margin="0 0 0 3">Hosted Platform (optional)</TextBlock>
<Grid Margin="0 0 0 5" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="211*"/>
<ColumnDefinition Width="70*"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5,1,5,1" MinWidth="50" Text="eg. https://github.com/"
Foreground="Gray" Visibility="{Binding ElementName=txtPlatformUrl, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" IsHitTestVisible="False" Grid.ColumnSpan="2"/>
<TextBox Name="txtPlatformUrl" Background="Transparent" Text="{Binding PlatformURL, UpdateSourceTrigger=PropertyChanged}" KeyUp="txtSourceModUrl_KeyUp" Grid.ColumnSpan="2" />
</Grid>

<TextBlock Margin="0 0 0 3">Branch (optional)</TextBlock>
<Grid Margin="0 0 0 5" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="211*"/>
<ColumnDefinition Width="70*"/>
</Grid.ColumnDefinitions>
<TextBlock Margin="5,1,5,1" MinWidth="50" Text="eg. main"
Foreground="Gray" Visibility="{Binding ElementName=txtBranchName, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" IsHitTestVisible="False" Grid.ColumnSpan="2"/>
<TextBox Name="txtBranchName" Background="Transparent" Text="{Binding BranchName, UpdateSourceTrigger=PropertyChanged}" KeyUp="txtSourceModUrl_KeyUp" Grid.ColumnSpan="2" />
</Grid>
</StackPanel>
</Expander>
</StackPanel>

<Grid Grid.Row="2">
Expand Down
1 change: 1 addition & 0 deletions OpenKh.Tools.ModsManager/Views/InstallModView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class InstallModView : Window

public RelayCommand CloseCommand { get; }
public string RepositoryName { get; set; }
public string PlatformURL { get; set; }
public string BranchName { get; set; }
public bool IsZipFile { get; private set; }
public bool IsLuaFile { get; private set; } = false;
Expand Down
Loading