Skip to content

Commit

Permalink
Updaterの追加
Browse files Browse the repository at this point in the history
  • Loading branch information
puk06 committed Aug 7, 2024
1 parent 4e10bbf commit a4af7ba
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 30 deletions.
2 changes: 1 addition & 1 deletion App.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="./src/libs"/>
<probing privatePath="./src/libs" />
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
Expand Down
38 changes: 38 additions & 0 deletions Classes/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using Octokit;

namespace osu_launcher.Classes
{
internal class Helper
{
public static async Task<string> GetVersion(string currentVersion)
{
try
{
var releaseType = currentVersion.Split('-')[1];
var githubClient = new GitHubClient(new ProductHeaderValue("osu-Launcher"));
var tags = await githubClient.Repository.GetAllTags("puk06", "osu-Launcher");
string latestVersion = currentVersion;
foreach (var tag in tags)
{
if (releaseType == "Release")
{
if (tag.Name.Split('-')[1] != "Release") continue;
latestVersion = tag.Name;
break;
}

latestVersion = tag.Name;
break;
}

return latestVersion;
}
catch
{
throw new Exception("アップデートの取得に失敗しました");
}
}
}
}
153 changes: 124 additions & 29 deletions Forms/Main.Designer.cs

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions Forms/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
using Newtonsoft.Json.Linq;
using osu_launcher.Classes;
using Profile = osu_launcher.Classes.Profile;
using static osu_launcher.Classes.Helper;

namespace osu_launcher.Forms
{
public partial class Main : Form
{
public const string CurrentVersion = "v1.0.0-Release";

// Data Values
public JObject Data;

Expand Down Expand Up @@ -60,6 +63,7 @@ public Main()
Helper.ValidateRequiredFiles();
AddFontFile();
InitializeComponent();
GithubUpdateChecker();
InitializeDefaults();
InitializeWebBrowser();
LoadConfigFile();
Expand All @@ -82,6 +86,8 @@ private void InitializeDefaults()
MASTER_BAR.Value = 100;
EFFECT_BAR.Value = 100;
AUDIO_BAR.Value = 100;

CURRENT_VERSION_TEXT.Text = CurrentVersion;
}

private void InitializeWebBrowser()
Expand Down Expand Up @@ -852,5 +858,55 @@ private void OSUFOLDER_FOLDEROPEN_BUTTON_Click(object sender, EventArgs e)
OSUFOLDER_TEXTBOX.Text = folderBrowserDialog.SelectedPath;
}
}

public async void GithubUpdateChecker()
{
try
{
var latestRelease = await GetVersion(CurrentVersion);
LATEST_VERSION_TEXT.Text = latestRelease;
if (latestRelease == CurrentVersion)
{
UPDATE_BUTTON.Enabled = false;
LATEST_VERSION_TEXT.ForeColor = Color.Black;
return;
}
UPDATE_BUTTON.Enabled = true;
LATEST_VERSION_TEXT.ForeColor = Color.Green;
}
catch (Exception exception)
{
MessageBox.Show("アップデートチェック中にエラーが発生しました" + exception.Message, "エラー", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

private void UPDATE_BUTTON_Click(object sender, EventArgs e)
{
try
{
if (!File.Exists("./Updater/osu-launcher.Updater.exe"))
{
MessageBox.Show("アップデーターが見つかりませんでした。手動でダウンロードしてください。", "エラー", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}

string updaterPath = Path.GetFullPath("./Updater/osu-launcher.Updater.exe");
ProcessStartInfo args = new ProcessStartInfo()
{
FileName = $"\"{updaterPath}\"",
Arguments = CurrentVersion,
UseShellExecute = true
};

Process.Start(args);
}
catch (Exception exception)
{
MessageBox.Show("アップデーターを起動できませんでした" + exception.Message, "エラー", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}
3 changes: 3 additions & 0 deletions Forms/Main.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="contextMenuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
104 changes: 104 additions & 0 deletions osu-launcher.Updater/Classes/Updater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Text;

namespace osu_launcher.Updater.Classes
{
public class Updater
{
private readonly string _version;

public Updater(string version)
{
_version = version;
}

private const string Baseurl = "https://github.com/puk06/osu-Launcher/releases/download/";

public async Task Update()
{
var downloadUrl = $"{Baseurl}{_version}/osu-Launcher.zip";
var tempPath = Path.GetTempPath();
var tempFile = Path.Combine(tempPath, $"osu-Launcher.zip");
var extractPath = Path.Combine(tempPath, $"osu-Launcher.Temp");

Console.WriteLine("ファイルのダウンロードを開始しています...");
Console.WriteLine("ファイルのダウンロード中です...ソフトを終了しないでください!");

using var client = new WebClient();
await client.DownloadFileTaskAsync(new Uri(downloadUrl), tempFile);

Console.WriteLine("ダウンロードが完了しました!");
Console.WriteLine("ファイルの展開中です...");

ZipFile.ExtractToDirectory(tempFile, extractPath, Encoding.UTF8, true);
File.Delete(tempFile);

var folders = Directory.GetDirectories(extractPath);
folders = folders.Where(x => !x.Contains("Updater")).ToArray();
var files = Directory.GetFiles(extractPath);

var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (currentPath == null)
{
Console.WriteLine("カレントフォルダの取得に失敗しました。");
Thread.Sleep(3000);
return;
}
var softwarePath = Directory.GetParent(currentPath)?.FullName;
if (softwarePath == null)
{
Console.WriteLine("ソフトウェアのフォルダの取得に失敗しました。");
Thread.Sleep(3000);
return;
}

for (int i = 0; i < files.Length; i++)
{
var file = files[i];
var fileName = Path.GetFileName(file);
var currentFile = Path.Combine(softwarePath, fileName);
Console.WriteLine($"ファイルのコピー中です... {i + 1}/{files.Length}: {fileName}");
File.Copy(file, currentFile, true);
}

for (int i = 0; i < folders.Length; i++)
{
var folder = folders[i];
var folderName = Path.GetFileName(folder);
var currentFolder = Path.Combine(softwarePath, folderName);
if (!Directory.Exists(currentFolder)) Directory.CreateDirectory(currentFolder);
Console.WriteLine($"フォルダのコピー中です... {i + 1}/{folders.Length}: {folderName}");
DirectoryCopy(folder, currentFolder, true);
}

Directory.Delete(extractPath, true);
}

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
var dir = new DirectoryInfo(sourceDirName);
var dirs = dir.GetDirectories();

if (!dir.Exists) throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
if (!Directory.Exists(destDirName)) Directory.CreateDirectory(destDirName);


var files = dir.GetFiles();
foreach (var file in files)
{
var tempPath = Path.Combine(destDirName, file.Name);
if (file.Name == "data.json") continue;
file.CopyTo(tempPath, true);
}

if (!copySubDirs) return;
foreach (var subdir in dirs)
{
var tempPath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, tempPath, true);
}
}
}
}
92 changes: 92 additions & 0 deletions osu-launcher.Updater/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Diagnostics;
using Octokit;

namespace osu_launcher.Updater
{
internal class Program
{
private static async Task Main(string[] args)
{
try
{
if (args.Length != 1)
{
Console.WriteLine("バージョン情報が取得できませんでした。ソフト内から実行するようにしてください!");
Thread.Sleep(3000);
return;
}

var currentVersion = args[0];

if (string.IsNullOrEmpty(currentVersion))
{
Console.WriteLine("バージョン情報が取得できませんでした。ソフト内から実行するようにしてください!");
Thread.Sleep(3000);
return;
}

Console.WriteLine("アップデートを確認します。");

var latestVersion = await GithubUpdateChecker(currentVersion);

if (latestVersion == currentVersion)
{
Console.WriteLine("最新バージョンです!ソフトを使ってくれてありがとうございます!");
Thread.Sleep(3000);
return;
}

Console.WriteLine($"最新バージョンが見つかりました({currentVersion}{latestVersion})");
Console.ReadLine();
Console.WriteLine("osu-Launcher関係のソフトをすべて終了します。");

var processes = Process.GetProcessesByName("osu-launcher");
foreach (var process in processes)
{
process.Kill();
}

Console.WriteLine("osu-Launcherを終了しました。アップデートを開始します。");
var updater = new Classes.Updater(latestVersion);

await updater.Update();

Console.WriteLine("アップデートが完了しました!ソフトを使ってくれてありがとうございます!");
Thread.Sleep(3000);
}
catch (Exception e)
{
Console.WriteLine("アップデート中にエラーが発生しました: " + e.Message);
Thread.Sleep(3000);
}
}

private static async Task<string> GithubUpdateChecker(string currentVersion)
{
var latestRelease = await GetVersion(currentVersion);
return latestRelease == currentVersion ? currentVersion : latestRelease;
}

private static async Task<string> GetVersion(string currentVersion)
{
var releaseType = currentVersion.Split('-')[1];
var githubClient = new GitHubClient(new ProductHeaderValue("osu-Launcher"));
var tags = await githubClient.Repository.GetAllTags("puk06", "osu-Launcher");
string latestVersion = currentVersion;
foreach (var tag in tags)
{
if (releaseType == "Release")
{
if (tag.Name.Split("-")[1] != "Release") continue;
latestVersion = tag.Name;
break;
}

latestVersion = tag.Name;
break;
}

return latestVersion;
}
}
}
15 changes: 15 additions & 0 deletions osu-launcher.Updater/osu-launcher.Updater.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>osu_launcher.Updater</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Octokit" Version="13.0.1" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions osu-launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Octokit, Version=13.0.1.0, Culture=neutral, PublicKeyToken=0be8860aee462442, processorArchitecture=MSIL">
<HintPath>packages\Octokit.13.0.1\lib\netstandard2.0\Octokit.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
Expand Down Expand Up @@ -120,6 +123,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\Helper.cs" />
<Compile Include="Classes\Server.cs" />
<Compile Include="Forms\EditSoftware.cs">
<SubType>Form</SubType>
Expand Down
6 changes: 6 additions & 0 deletions osu-launcher.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "osu-launcher", "osu-launcher.csproj", "{85312C2E-4174-48A0-A8A0-75540300E4A4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "osu-launcher.Updater", "osu-launcher.Updater\osu-launcher.Updater.csproj", "{01CC0C1F-9029-42C2-B320-A723F3D87123}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{85312C2E-4174-48A0-A8A0-75540300E4A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85312C2E-4174-48A0-A8A0-75540300E4A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85312C2E-4174-48A0-A8A0-75540300E4A4}.Release|Any CPU.Build.0 = Release|Any CPU
{01CC0C1F-9029-42C2-B320-A723F3D87123}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01CC0C1F-9029-42C2-B320-A723F3D87123}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01CC0C1F-9029-42C2-B320-A723F3D87123}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01CC0C1F-9029-42C2-B320-A723F3D87123}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit a4af7ba

Please sign in to comment.