JD2022-TU1/main/tools/services/JD.MapFileFontDownloader.Service/MapFileFontDownloaderService.cs

107 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JD.MapFileFontDownloader.Service.Configuration;
using JD.Vault;
using Newtonsoft.Json;
using Plume.Core.Logging;
using Plume.Interfaces.Services;
using Ubisoft.Perforce;
using Ubisoft.Perforce.Helper;
namespace JD.MapFileFontDownloader.Service
{
public class MapFileFontDownloaderService : IMapFileFontDownloaderService
{
private ILogger _logger;
private FontDownloaderConfiguration _configuration;
public string TempWorkspacePath { get; private set; }
public int MapCount { get; private set; }
public IEnumerable<string> GetFontFiles(string localFilesWorkspacePath, string serverFilesPath)
{
var _fontFiles = new List<string>(GetMapFiles(serverFilesPath));
_fontFiles.Add(Path.Combine(localFilesWorkspacePath, _configuration.CreditsComponentFilePath));
_fontFiles.Add(Path.Combine(localFilesWorkspacePath, _configuration.TextsBaseSetFilePath));
var localisationFiles = Directory.EnumerateFiles(Path.Combine(localFilesWorkspacePath, _configuration.LocalisationFolderPath), _configuration.LocalisationFileSearchPattern);
_fontFiles.AddRange(localisationFiles);
return _fontFiles;
}
private IEnumerable<string> GetMapFiles(string serverFilesPath)
{
var mapFilesOnServer = new List<string>()
{
$"{serverFilesPath}/{_configuration.MapsSubDirectory}/{_configuration.LyricsFileSearchPattern}",
$"{serverFilesPath}/{_configuration.MapsSubDirectory}/{_configuration.SongDescFileSearchPattern}",
};
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss_ffff");
string tempClientName = _configuration.TempWorkspaceFolder + timestamp;
TempWorkspacePath = Path.Combine(Path.GetTempPath(), _configuration.TempWorkspaceFolder, timestamp);
Directory.CreateDirectory(TempWorkspacePath);
_logger?.Info("Getting credentials for the service account used to connect to Perforce");
string robotPassword = ServiceAccounts.GetServiceAccountPassword(_configuration.RobotP4Login);
_logger?.Info("Service account credentials retrieved");
// We use views to be able to get files not only from streams and because there are some issues with virtual streams
PerforceWrapper perforceWrapper = new PerforceWrapper("", _configuration.RobotP4Login, robotPassword, _configuration.DefaultP4JdData, true);
List<string> clientViews = new List<string>() { $"{serverFilesPath}/... //{tempClientName}/..." };
perforceWrapper.CreateClient(tempClientName, TempWorkspacePath, "", clientViews);
if (!perforceWrapper.AutoConnect(TempWorkspacePath))
{
return Enumerable.Empty<string>();
}
// Setting allwrite option to avoid having files readonly, in order to delete them easily later
Dictionary<string, string> fields = new Dictionary<string, string>
{
["Options"] = _configuration.P4Fields
};
perforceWrapper.CreateOrModifyClientFromTemplateAndFields(tempClientName, tempClientName, fields, null);
perforceWrapper.Sync(mapFilesOnServer, null, SyncOptions.Parallel);
perforceWrapper.DeleteClient(tempClientName);
string tempMapDirectory = Path.Combine(TempWorkspacePath, _configuration.MapsSubDirectory);
if (Directory.Exists(tempMapDirectory))
{
MapCount = Directory.EnumerateDirectories(tempMapDirectory).Count();
}
return Directory.EnumerateFiles(TempWorkspacePath, "*", SearchOption.AllDirectories);
}
private void InitializeConfiguration()
{
var configFilePath = @"Configuration/FontDownloaderConfiguration.json";
if (!File.Exists(configFilePath))
{
throw new FileNotFoundException("Unable to load the configuration file", configFilePath);
}
_configuration = JsonConvert.DeserializeObject<FontDownloaderConfiguration>(File.ReadAllText(configFilePath));
}
public void Initialize(IServiceContext serviceContext)
{
InitializeConfiguration();
_logger = serviceContext.Logger;
}
public void Dispose()
{
}
}
}