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
15 changes: 12 additions & 3 deletions OpenKh.Tools.ModsManager/Services/ModsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ public static Task InstallMod(
string platformUrl = null,
string branchName = null)
{

if (name.Contains("@"))
{
var _fetchNameWithPlatform = name.Split("@");

name = _fetchNameWithPlatform[0];
platformUrl = _fetchNameWithPlatform[1];
}

if (!isZipFile && !isLuaFile)
{
return Task.Run(() => InstallModFromGit(name, progressOutput, progressNumber, platformUrl, branchName));
Expand Down Expand Up @@ -296,15 +305,15 @@ public static async Task InstallModFromGit(
if (!string.IsNullOrWhiteSpace(branchName))
{
progressOutput?.Invoke($"Fetching file {ModMetadata} from branch {branchName}");
var isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata, platformName);
var isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata, progressOutput, progressNumber, 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, platformName);
var isValidMod = await RepositoryService.IsFileExists(repositoryName, branchName, ModMetadata, progressOutput, progressNumber, platformName);
if (!isValidMod)
{
progressOutput?.Invoke($"{ModMetadata} not found, fetching default branch name");
Expand All @@ -313,7 +322,7 @@ public static async Task InstallModFromGit(
throw new RepositoryNotFoundException(repositoryName);

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

if (!isValidMod)
Expand Down
25 changes: 18 additions & 7 deletions OpenKh.Tools.ModsManager/Services/RepositoryService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
using Newtonsoft.Json;
using System;
using System.IO;
Expand Down Expand Up @@ -40,24 +41,34 @@ 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, string platformUrl = null)
{
public static Task<bool> IsFileExists(string repoName, string branch, string filePath, Action<string> progressOutput = null, Action<float> progressNumber = null, string platformUrl = null)
{
if (!string.IsNullOrEmpty(platformUrl))
{
var _fetchBaseUri = new Uri(platformUrl.Contains("http") ? platformUrl : "https://" + platformUrl);
var _fetchRelativeUri = new Uri(_fetchBaseUri, $"{repoName}");

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

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

_cloneOptions.FetchOptions.Depth = 1;
_cloneOptions.FetchOptions.Prune = true;

_cloneOptions.FetchOptions.OnTransferProgress = new TransferProgressHandler((progress) =>
{
progressOutput.Invoke($"Checking if Git is a Mod: {progress.ReceivedObjects} / {progress.TotalObjects}");
progressNumber.Invoke((float)progress.ReceivedObjects / (float)progress.TotalObjects);

return true;
});

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

Expand All @@ -66,18 +77,18 @@ public static Task<bool> IsFileExists(string repoName, string branch, string fil

_fetchRepository.Dispose();

if (Directory.Exists(_fetchRepoDirectory))
if (Directory.Exists(_fetchRepoParent))
{
var _fetchAllFiles = Directory.GetFiles(_fetchRepoDirectory, "*", SearchOption.AllDirectories);
var _fetchAllDirectories = Directory.GetDirectories(_fetchRepoDirectory, "*", SearchOption.AllDirectories);
var _fetchAllFiles = Directory.GetFiles(_fetchRepoParent, "*", SearchOption.AllDirectories);
var _fetchAllDirectories = Directory.GetDirectories(_fetchRepoParent, "*", 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);
Directory.Delete(_fetchRepoParent, true);
}

return Task.FromResult(_doesModFileExist);
Expand Down
27 changes: 27 additions & 0 deletions OpenKh.Tools.ModsManager/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,21 @@ public bool IsBuilding

public MainViewModel()
{
if (Directory.Exists($"gitfetch"))
{
var _fetchAllFiles = Directory.GetFiles($"gitfetch", "*", SearchOption.AllDirectories);
var _fetchAllDirectories = Directory.GetDirectories($"gitfetch", "*", SearchOption.AllDirectories);

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

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

Directory.Delete($"gitfetch", true);
}


if (ConfigurationService.GameEdition == SetupWizardViewModel.PC)
{
PC = true;
Expand Down Expand Up @@ -772,6 +787,18 @@ await ModsService.InstallMod(name, isZipFile, isLuaFile, progress =>
}
);

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

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

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

_pcsx2Injector = new Pcsx2Injector(new OperationDispatcher());
_ = FetchUpdates();

Expand Down
Loading