Package 버전에 따라 무삭제 기능 추가 시작점

This commit is contained in:
2025-05-21 14:47:39 +09:00
parent ff52d49800
commit d25e77e7f0
2 changed files with 161 additions and 169 deletions

View File

@@ -1,153 +1,135 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq;
namespace CommonLibrary namespace CommonLibrary
{ {
internal class ProgressStream : Stream
{
private readonly Stream insideStream;
private readonly IProgress<int> insideReadProgress;
private readonly IProgress<int> insideWriteProgress;
internal class ProgressStream : Stream public ProgressStream(Stream stream, IProgress<int> readProgress, IProgress<int> writeProgress)
{ {
private readonly Stream insideStream; insideStream = stream;
private readonly IProgress<int> insideReadProgress; insideReadProgress = readProgress;
private readonly IProgress<int> insideWriteProgress; insideWriteProgress = writeProgress;
}
public ProgressStream(Stream stream, IProgress<int> readProgress, IProgress<int> writeProgress) public override bool CanRead => insideStream.CanRead;
public override bool CanSeek => insideStream.CanSeek;
public override bool CanWrite => insideStream.CanWrite;
public override long Length => insideStream.Length;
public override long Position
{
get => insideStream.Position;
set => insideStream.Position = value;
}
public override void Flush() => insideStream.Flush();
public override long Seek(long offset, SeekOrigin origin) => insideStream.Seek(offset, origin);
public override void SetLength(long value) => insideStream.SetLength(value);
public override int Read(byte[] buffer, int offset, int count)
{
int bytesRead = insideStream.Read(buffer, offset, count);
insideReadProgress?.Report(bytesRead);
return bytesRead;
}
public override void Write(byte[] buffer, int offset, int count)
{
insideStream.Write(buffer, offset, count);
insideWriteProgress?.Report(count);
}
}
public class ProgressPacker
{
public void Pack(string sourceDirectoryName, string packFileName, IProgress<double> progress)
{
sourceDirectoryName = Path.GetFullPath(sourceDirectoryName);
FileInfo[] sourceFiles = new DirectoryInfo(sourceDirectoryName).GetFiles("*", SearchOption.AllDirectories);
double totalBytes = sourceFiles.Sum(f => f.Length);
long currentBytes = 0;
using (ZipArchive zipArchive = ZipFile.Open(packFileName, ZipArchiveMode.Create))
{ {
insideStream = stream; foreach (FileInfo file in sourceFiles)
insideReadProgress = readProgress; {
insideWriteProgress = writeProgress; // 경로 구분자를 '/' 로 통일 (Windows 탐색기 호환)
} string entryName = file.FullName.Substring(sourceDirectoryName.Length + 1)
.Replace(Path.DirectorySeparatorChar, '/');
public override bool CanRead { get { return insideStream.CanRead; } } ZipArchiveEntry entry = zipArchive.CreateEntry(entryName);
public override bool CanSeek { get { return insideStream.CanSeek; } } entry.LastWriteTime = file.LastWriteTime;
public override bool CanWrite { get { return insideStream.CanWrite; } }
public override long Length { get { return insideStream.Length; } }
public override long Position using (Stream inputStream = file.OpenRead())
{ using (Stream outputStream = entry.Open())
get { return insideStream.Position; } using (Stream progressStream = new ProgressStream(inputStream,
set { insideStream.Position = value; } new BasicProgress<int>(i =>
} {
currentBytes += i;
public override void Flush() progress?.Report(currentBytes / totalBytes);
{ }), null))
insideStream.Flush(); {
} // ✅ 방향 수정: inputStream → progressStream → outputStream
progressStream.CopyTo(outputStream);
public override long Seek(long offset, SeekOrigin origin) }
{ }
return insideStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
insideStream.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
int bytesRead = insideStream.Read(buffer, offset, count);
insideReadProgress?.Report(bytesRead);
return bytesRead;
}
public override void Write(byte[] buffer, int offset, int count)
{
insideStream.Write(buffer, offset, count);
insideWriteProgress?.Report(count);
} }
} }
public class ProgressPacker public void UnPack(string packFileName, string destinationDirectoryName, IProgress<double> progress)
{ {
public void Pack(string sourceDirectoryName, string packFileName, IProgress<double> progress) using (ZipArchive zipArchive = ZipFile.OpenRead(packFileName))
{ {
sourceDirectoryName = Path.GetFullPath(sourceDirectoryName); double totalBytes = zipArchive.Entries.Sum(f => f.Length);
FileInfo[] sourceFiles = new DirectoryInfo(sourceDirectoryName).GetFiles("*", SearchOption.AllDirectories);
double totalBytes = sourceFiles.Sum(f => f.Length);
long currentBytes = 0; long currentBytes = 0;
using (ZipArchive zipArchive = ZipFile.Open(packFileName, ZipArchiveMode.Create)) foreach (ZipArchiveEntry entry in zipArchive.Entries)
{ {
// 디렉터리 엔트리 건너뜀
if (string.IsNullOrEmpty(entry.Name))
continue;
foreach (FileInfo file in sourceFiles) string fileName = Path.Combine(destinationDirectoryName, entry.FullName);
{ Directory.CreateDirectory(Path.GetDirectoryName(fileName));
string entryName = file.FullName.Substring(sourceDirectoryName.Length + 1);
ZipArchiveEntry entry = zipArchive.CreateEntry(entryName);
entry.LastWriteTime = file.LastWriteTime; using (Stream inputStream = entry.Open())
using (Stream inputStream = file.OpenRead()) using (Stream outputStream = File.OpenWrite(fileName))
using (Stream progressStream = new ProgressStream(outputStream, null,
new BasicProgress<int>(i =>
{ {
using (Stream outputStream = entry.Open()) currentBytes += i;
{ progress?.Report(currentBytes / totalBytes);
Stream progressStream = new ProgressStream(inputStream, })))
new BasicProgress<int>(i =>
{
currentBytes += i;
progress.Report(currentBytes / totalBytes);
}), null);
progressStream.CopyTo(outputStream);
}
}
}
}
}
public void UnPack(string packFileName, string destinationDirectoryName, IProgress<double> progress)
{
using (ZipArchive zipArchive = ZipFile.OpenRead(packFileName))
{
double totalBytes = zipArchive.Entries.Sum(f => f.Length);
long currentBytes = 0;
foreach (ZipArchiveEntry entry in zipArchive.Entries)
{ {
string fileName = Path.Combine(destinationDirectoryName, entry.FullName); inputStream.CopyTo(progressStream);
if (entry.Name == "") continue;
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
using (Stream inputStream = entry.Open())
{
using (Stream outputStream = File.OpenWrite(fileName))
{
Stream progressStream = new ProgressStream(outputStream, null,
new BasicProgress<int>(i =>
{
currentBytes += i;
if (progress != null)
{
progress.Report(currentBytes / totalBytes);
}
}));
inputStream.CopyTo(progressStream);
}
File.SetLastWriteTime(fileName, entry.LastWriteTime.LocalDateTime);
}
} }
File.SetLastWriteTime(fileName, entry.LastWriteTime.LocalDateTime);
} }
} }
} }
public class BasicProgress<T> : IProgress<T>
{
private readonly Action<T> actionHandler;
public BasicProgress(Action<T> handler)
{
actionHandler = handler;
}
void IProgress<T>.Report(T value)
{
actionHandler(value);
}
}
} }
public class BasicProgress<T> : IProgress<T>
{
private readonly Action<T> actionHandler;
public BasicProgress(Action<T> handler)
{
actionHandler = handler;
}
public void Report(T value)
{
actionHandler(value);
}
}
}

View File

@@ -27,7 +27,7 @@ namespace Mitria_Minecraft_Launcher.Updater
// Version File Download // Version File Download
CommonLibrary.Log.INFO("download version file."); CommonLibrary.Log.INFO("download version file.");
var verionData = downloader.DownloadString(CommonLibrary.Extensions.PathCombineL(Settings.ServerBaseUrl,"Servers", Settings.NowProfile.ServerName, Settings.ServerDataPatchInformationFile)); var verionData = downloader.DownloadString(CommonLibrary.Extensions.PathCombineL(Settings.ServerBaseUrl, "Servers", Settings.NowProfile.ServerName, Settings.ServerDataPatchInformationFile));
// Versoin File 받기에 실패하였을때 업데이트 전체 실패처리 // Versoin File 받기에 실패하였을때 업데이트 전체 실패처리
if (verionData == string.Empty) if (verionData == string.Empty)
@@ -74,7 +74,7 @@ namespace Mitria_Minecraft_Launcher.Updater
CommonLibrary.Log.INFO("Runtime delete it for update."); CommonLibrary.Log.INFO("Runtime delete it for update.");
// 런타임 폴더 경로 가져오기 // 런타임 폴더 경로 가져오기
var rootDirectoryInfo = new System.IO.DirectoryInfo(System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.RuntimeLocation,Settings.NowProfile.ServerName))); var rootDirectoryInfo = new System.IO.DirectoryInfo(System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.RuntimeLocation, Settings.NowProfile.ServerName)));
// 런타임 폴더 삭제후 새로만들기 // 런타임 폴더 삭제후 새로만들기
if (!rootDirectoryInfo.Exists) if (!rootDirectoryInfo.Exists)
@@ -124,6 +124,7 @@ namespace Mitria_Minecraft_Launcher.Updater
CommonLibrary.Log.INFO("[Package] LocalVersion : " + thisVersion); CommonLibrary.Log.INFO("[Package] LocalVersion : " + thisVersion);
CommonLibrary.Log.INFO("[Package] RemoteVersion : " + remoteVersion); CommonLibrary.Log.INFO("[Package] RemoteVersion : " + remoteVersion);
result = remoteVersion.CompareTo(thisVersion); result = remoteVersion.CompareTo(thisVersion);
//TODO: 만약 Major 버전이 바뀌면 전부삭제, 아니면 있는 폴더면 선별후 삭제 //TODO: 만약 Major 버전이 바뀌면 전부삭제, 아니면 있는 폴더면 선별후 삭제
// 1 : 리모트가 큼, 0 : 같음, -1 리모트가 적음 // 1 : 리모트가 큼, 0 : 같음, -1 리모트가 적음
if (result == 0) if (result == 0)
@@ -133,10 +134,21 @@ namespace Mitria_Minecraft_Launcher.Updater
else else
{ {
CommonLibrary.Log CommonLibrary.Log
.INFO(string.Format("{0}", result == 1 ? "remote is the upper version" : "remote is the lower version")); .INFO(string.Format("{0}", result == 1 ? "remote is the upper version" : "remote is the lower version"));
CommonLibrary.Log.INFO("[Package] delete it for update.");
var rootDirectoryInfo = new System.IO.DirectoryInfo(System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory,Settings.NowProfile.ServerName))); bool packageInitialization = true;
if (thisVersion.Major < remoteVersion.Major ||
(thisVersion.Major == remoteVersion.Major && thisVersion.Minor < remoteVersion.Minor))
{
packageInitialization = false;
CommonLibrary.Log.INFO("[Package] update.");
}
else
{
CommonLibrary.Log.INFO("[Package] delete it for update.");
}
var rootDirectoryInfo = new System.IO.DirectoryInfo(System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName)));
if (!rootDirectoryInfo.Exists) if (!rootDirectoryInfo.Exists)
{ {
@@ -241,7 +253,7 @@ namespace Mitria_Minecraft_Launcher.Updater
} }
foreach (var directory in resultDirectorys) foreach (var directory in resultDirectorys)
{ {
var directoryPath = System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory,Settings.NowProfile.ServerName, directory)); var directoryPath = System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName, directory));
if (System.IO.Directory.Exists(directoryPath)) if (System.IO.Directory.Exists(directoryPath))
{ {
@@ -270,7 +282,6 @@ namespace Mitria_Minecraft_Launcher.Updater
} }
} }
} }
} }
foreach (var directory in dataPatchInformation.ComponentDirectorys) foreach (var directory in dataPatchInformation.ComponentDirectorys)
@@ -284,7 +295,6 @@ namespace Mitria_Minecraft_Launcher.Updater
} }
} }
var auditFile = new AuditFile(); var auditFile = new AuditFile();
var localFiles = auditFile.GetLocalFileList(dataPatchInformation.ComponentDirectorys); var localFiles = auditFile.GetLocalFileList(dataPatchInformation.ComponentDirectorys);
var removeFiles = auditFile.GetRemoveFiles(localFiles, dataPatchInformation.ComponentList); var removeFiles = auditFile.GetRemoveFiles(localFiles, dataPatchInformation.ComponentList);
@@ -294,8 +304,8 @@ namespace Mitria_Minecraft_Launcher.Updater
foreach (var fileDetail in removeFiles) foreach (var fileDetail in removeFiles)
{ {
var filePath = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory,Settings.NowProfile.ServerName, fileDetail.Directory, fileDetail.FileName); var filePath = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName, fileDetail.Directory, fileDetail.FileName);
FileInfo fileInfo= new FileInfo(filePath); FileInfo fileInfo = new FileInfo(filePath);
fileInfo.IsReadOnly = false; fileInfo.IsReadOnly = false;
if (fileInfo.Exists) if (fileInfo.Exists)
{ {
@@ -309,7 +319,7 @@ namespace Mitria_Minecraft_Launcher.Updater
for (int i = 0; i < needFiles.Count; i++) for (int i = 0; i < needFiles.Count; i++)
{ {
var url = CommonLibrary.Extensions.PathCombineL(Settings.ServerBaseUrl, "Servers", Settings.NowProfile.ServerName, dataPatchInformation.ComponentUrl, needFiles[i].Directory, needFiles[i].FileName); var url = CommonLibrary.Extensions.PathCombineL(Settings.ServerBaseUrl, "Servers", Settings.NowProfile.ServerName, dataPatchInformation.ComponentUrl, needFiles[i].Directory, needFiles[i].FileName);
var path = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory,Settings.NowProfile.ServerName, needFiles[i].Directory, needFiles[i].FileName); var path = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName, needFiles[i].Directory, needFiles[i].FileName);
downloader.DownloadFile(url, path); downloader.DownloadFile(url, path);
Log.INFO("[Component] +[F] " + path); Log.INFO("[Component] +[F] " + path);
} }
@@ -322,10 +332,11 @@ namespace Mitria_Minecraft_Launcher.Updater
// 커스텀 폴더, Config // 커스텀 폴더, Config
#region CustomData #region CustomData
// 삭제된 파일 찾기 // 삭제된 파일 찾기
string rootCustomPath = System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.CustomDataDirectory, Settings.NowProfile.ServerName)); string rootCustomPath = System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.CustomDataDirectory, Settings.NowProfile.ServerName));
if(!System.IO.Directory.Exists(CommonLibrary.Extensions.PathCombineW(rootCustomPath, "config"))) if (!System.IO.Directory.Exists(CommonLibrary.Extensions.PathCombineW(rootCustomPath, "config")))
{ {
System.IO.Directory.CreateDirectory(CommonLibrary.Extensions.PathCombineW(rootCustomPath, "config")); System.IO.Directory.CreateDirectory(CommonLibrary.Extensions.PathCombineW(rootCustomPath, "config"));
} }
@@ -339,14 +350,12 @@ namespace Mitria_Minecraft_Launcher.Updater
for (int i = 0; i < files.Count; i++) for (int i = 0; i < files.Count; i++)
{ {
files[i] = files[i].Replace(rootCustomPath + '\\', string.Empty); files[i] = files[i].Replace(rootCustomPath + '\\', string.Empty);
} }
foreach (var file in files) foreach (var file in files)
{ {
string sourcePath = CommonLibrary.Extensions.PathCombineW(rootCustomPath, file); string sourcePath = CommonLibrary.Extensions.PathCombineW(rootCustomPath, file);
string targetPath = CommonLibrary.Extensions.PathCombineW( Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName,file); string targetPath = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName, file);
if (System.IO.File.Exists(targetPath)) if (System.IO.File.Exists(targetPath))
{ {
@@ -354,9 +363,8 @@ namespace Mitria_Minecraft_Launcher.Updater
System.IO.FileInfo targetFile = new System.IO.FileInfo(targetPath); System.IO.FileInfo targetFile = new System.IO.FileInfo(targetPath);
if (sourceFile.GetFileHashCode() != targetFile.GetFileHashCode()) if (sourceFile.GetFileHashCode() != targetFile.GetFileHashCode())
{ {
System.IO.File.Copy(sourcePath, targetPath,true); System.IO.File.Copy(sourcePath, targetPath, true);
Log.INFO("[CustomData] +[F] " + targetPath); Log.INFO("[CustomData] +[F] " + targetPath);
} }
} }
else else
@@ -387,33 +395,35 @@ namespace Mitria_Minecraft_Launcher.Updater
GameUpdateManagerMessage?.Invoke(this, e); GameUpdateManagerMessage?.Invoke(this, e);
} }
} }
}
public enum GameUpdateStatus public enum GameUpdateStatus
{
Success,
Fail
}
public enum GameUpdateManagerMessageType
{
First,
Second,
Message
}
public class GameUpdateManagerMessageEventArgs : EventArgs
{
public GameUpdateManagerMessageType MessageType { get; set; }
public long MinValue { get; set; }
public long MaxValue { get; set; }
public string Message { get; set; }
public GameUpdateManagerMessageEventArgs(GameUpdateManagerMessageType messageType, long minValue, long maxValue, string message)
{ {
Success, MessageType = messageType;
Fail MinValue = minValue;
} MaxValue = maxValue;
Message = message;
public enum GameUpdateManagerMessageType
{
First,
Second,
Message
}
public class GameUpdateManagerMessageEventArgs : EventArgs
{
public GameUpdateManagerMessageType MessageType { get; set; }
public long MinValue { get; set; }
public long MaxValue { get; set; }
public string Message { get; set; }
public GameUpdateManagerMessageEventArgs(GameUpdateManagerMessageType messageType, long minValue, long maxValue, string message)
{
MessageType = messageType;
MinValue = minValue;
MaxValue = maxValue;
Message = message;
}
} }
} }
}