From 3803a3efcad68b0218226f35b01b8c96a127cb2e Mon Sep 17 00:00:00 2001 From: taiseiue Date: Mon, 8 Jul 2024 09:30:52 +0900 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alice/Properties/Resources.Designer.cs | 22 +- alice/Resources/update.txt | 319 +++++++++++++++++++------ 2 files changed, 251 insertions(+), 90 deletions(-) diff --git a/alice/Properties/Resources.Designer.cs b/alice/Properties/Resources.Designer.cs index 93a7faf..1089b5c 100644 --- a/alice/Properties/Resources.Designer.cs +++ b/alice/Properties/Resources.Designer.cs @@ -163,24 +163,22 @@ internal static string shell { } /// - /// // 実行ファイルのファイル名 - ///// ファイル名が異なる場合はこの部分を編集してください。{0}には現在の実行ファイルのディレクトリが代入されます。また、Windowsの場合でも末尾の.exeは不要です - ///var target_filename = "{0}alice"; - /// - ///using Alice.IO; + /// using Alice.IO; ///using Alice.Net; + ///using Alice.Regex; ///using Alice.Console; - ///using Alice.Environment; + ///using Alice.Security; ///using Alice.Diagnostics; + ///using Alice.Environment; /// - ///const version_get_api = "https://api.wsoft.ws/download/detail?id={0}&feature=version"; + ///const string update_get_api = "https://api.wsoft.ws/download/detail?id={0}"; ///const download_url = "https://download.wsoft.ws/{0}/Download"; /// - ///var download_id=""; - ///var isWin=false; - /// - ///var platform= env_impl_target(); - ///var arch= env_impl_architecture [残りの文字列は切り詰められました]"; に類似しているローカライズされた文字列を検索します。 + ///// OS名とアーキテクチャ名から、該当するバイナリのダウンロードIDを取得します + ///// os = OS名。Windows,Linux,OSXのいずれか + ///// arch = アーキテクチャ名。x64,x86,ARM32,ARM64のいずれか + ///// return = 該当するバイナリが見つかった場合はID、それ以外の場合は空文字列 + ///string GetDownloadId(string os, string arch) [残りの文字列は切り詰められました]"; に類似しているローカライズされた文字列を検索します。 /// internal static string update { get { diff --git a/alice/Resources/update.txt b/alice/Resources/update.txt index f4b2724..5dd9479 100644 --- a/alice/Resources/update.txt +++ b/alice/Resources/update.txt @@ -1,126 +1,289 @@ -// st@C̃t@C -// t@CقȂꍇ͂̕ҏWĂB{0}ɂ݂͌̎st@C̃fBNg܂B܂AWindows̏ꍇł.exe͕svł -var target_filename = "{0}alice"; - using Alice.IO; using Alice.Net; +using Alice.Regex; using Alice.Console; -using Alice.Environment; +using Alice.Security; using Alice.Diagnostics; +using Alice.Environment; -const version_get_api = "https://api.wsoft.ws/download/detail?id={0}&feature=version"; +const string update_get_api = "https://api.wsoft.ws/download/detail?id={0}"; const download_url = "https://download.wsoft.ws/{0}/Download"; -var download_id=""; -var isWin=false; +// OSƃA[LeN`AYoCĩ_E[hID擾܂ +// os = OSBWindows,Linux,OSX̂ꂩ +// arch = A[LeN`Bx64,x86,ARM32,ARM64̂ꂩ +// return = YoCi‚ꍇIDAȊȌꍇ͋󕶎 +string GetDownloadId(string os, string arch) +{ + switch(os) + { + case "Windows": + { + switch(arch) + { + case "x64": + { + return "WS330"; + } + case "x86": + { + return "WS331"; + } + case "ARM64": + { + return "WS332"; + } + case "ARM32": + { + return "WS333"; + } + default: + { + return ""; + } + } + break; + } + case "OSX": + { + switch(arch) + { + case "x64": + { + return "WS320"; + } + case "ARM64": + { + return "WS321"; + } + default: + { + return ""; + } + } + break; + } + case "Linux": + { + switch(arch) + { + case "x64": + { + return "WS310"; + } + case "ARM32": + { + return "WS311"; + } + case "ARM64": + { + return "WS312"; + } + default: + { + return ""; + } + } + break; + } + default: + { + return ""; + } + } -var platform= env_impl_target(); -var arch= env_impl_architecture(); +} -// vbgtH[̎ -if(platform=="Windows") +string GetDownloadData(string download_id) { - isWin=true; - if(arch=="x64") + string update_url = update_get_api.format(download_id); + write($"{update_url} ŐV擾Ă܂..."); + string result = ""; + try + { + result = web_download_text(update_url); + }catch { - download_id="WS148"; + print("s"); + env_Exit(1); } - if(arch=="x86") + print(""); + return result; +} + +bool CheckFound(string data) +{ + return GetValue(data, "Status") == "Found"; +} + +string GetValue(string json, string key) +{ + var kv = regex_match(json,$'"{key}":"(.*?)"').split(":"); + + if(kv.length > 1) { - download_id="WS149"; + string r = kv[1]; + return r.SubString(1, r.Length - 2); } - if(arch=="ARM32") + return ""; +} + +string GetNewlyVersion(string data) +{ + return GetValue(data, "Version"); +} +string GetFileSize(string data) +{ + return GetValue(data, "FileSize"); +} +string GetTitle(string data) +{ + return GetValue(data, "Title"); +} + +bool CompareVersion(string new, string now) +{ + var news = new.Split("."); + var nows = now.Split("."); + if(news.Length != nows.Length) { - download_id="WS151"; + return true; } - if(arch=="ARM64") + for(var i = 0; i < news.Length; i++) { - download_id="WS150"; + string n = news[i]; + string m = nows[i]; + try + { + if(m.Convert(number) > n.Convert(number)) + { + return false; + } + }catch{} } + return true; } -if(platform=="OSX") + +bool VerifyCheckSum(bytes data, string sum) { - download_id="WS147"; + string checksum = sha512_getHash(data).ToBase64(); + sum = sum.Trim(); + return checksum == sum; } -if(platform=="Linux") + +bool AskYN(string message) { - if(arch=="x64") + write($"{message} [Y/n]"); + + string ans = Console_ReadKey().ToUpperInvariant(); + print(); + + if(ans == "Y") { - download_id="WS144"; + return true; } - if(arch=="ARM32") + else if(ans == "N") { - download_id="WS145"; + return false; } - if(arch=="ARM64") + else { - download_id="WS146"; + return AskYN(message); } } -if(download_id!="") -{ +string platform = env_impl_target(); +string arch = env_impl_architecture(); -// Windows̏ꍇ̓t@C.exe} -if(isWin&& !target_filename.EndsWith(".exe")) +string download_id = GetDownloadId(platform, arch); + +if(download_id == null) { - target_filename+=".exe"; + print("̃vbgtH[ł͍XV̓T|[gĂ܂"); + return; } -print("‹̔: OS:{0},A[LeN`:{1}",platform,arch); +string data = GetDownloadData(download_id); -var version_url = version_get_api.format(download_id); -print("{0} ŐVo[W擾Ă܂...",version_url); -var new_version = web_download_text(version_url); -print("ŐVo[W : {0}",new_version); +write("ŐVǂݍł܂..."); +string now_version = env_impl_version(); +string new_version = GetNewlyVersion(data); +string fileSize = GetFileSize(data); +string title = GetTitle(data); -var force=env_commandLineArgs().Contains("force"); -var check=env_commandLineArgs().Contains("check"); +bool force = env_commandLineArgs().Contains("force"); +bool check = env_commandLineArgs().Contains("check"); +bool need = CompareVersion(new_version, now_version); +print(""); + +print(); -if(check) +if(need || force) { - print("̎̃o[W : {0}",env_impl_version()); - if(new_version != env_impl_version()) - { - print("AliceScript̎̍XV܂"); - } + print("ȉ̍XVgp”\ł"); + print($"- {title} {now_version} -> {new_version} [{fileSize}]"); +} +else +{ + print("ݎgp”\ȍXV͂܂"); return; } +print(); +bool need_new = (need || force) && !check; -if(new_version != env_impl_version() || force) +if(need_new) { - print("̎VJĂ܂B"); - Console_Write("XVs܂H(Y/N)>>"); - if(Console_ReadKey()=="y") - { - print(); - var path=target_filename.format(env_impl_location()); - var tmp =path+".old"; - file_delete(tmp); - file_move(path,tmp); - - var url = download_url.format(download_id); - print("{0} ŐVoCi_E[hĂ܂...",url); - web_download_file(url,path); - print("_E[h܂B"); - - if(!isWin) - { - print("t@C : {0} Ɏst^Ă܂...",path); - process_start("chmod","+x "+path).WaitForExit(); - } + print($"XVsɂ {fileSize} ̃t@C_E[hKv܂"); - print("{0} ɍXV܂",new_version); + if(!AskYN("XV܂H")) + { + print("XV̓[U[ɂĎ܂"); return; } + print(); + bytes? bin = null; + try + { + string url = download_url.format(download_id); + write($"{url} XV_E[hĂ܂..."); + bin = web_download_data(url); + print(""); + } + catch(e) + { + print("s"); + print("XṼ_E[hɎs܂BXV͍s܂łB"); + print($": {e.Message}"); + env_Exit(1); + } + + write("t@C؂Ă܂..."); + bool verify = VerifyCheckSum(bin, GetValue(data, "Hash")); + print(""); + if(!verify) + { + print("t@Č؂Ɏs܂BXV͍s܂łB"); + env_Exit(1); + } + + string fileName = env_processPath(); + string tmpFile = fileName + ".old"; + write("t@CobNAbvĂ܂..."); print(); - print("XV̓[U[ɂĎ܂"); - return; -} -print(); -print("ݗpłXV͂܂"); -} + file_delete(tmpFile); + file_move(fileName, tmpFile); + write(""); -print("̃vbgtH[ł͍XV̓T|[gĂ܂"); -return; \ No newline at end of file + write("t@Cł܂..."); + file_write_data(fileName, bin); + print(""); + + print("XV܂"); + print($"{tmpFile} ɃobNAbv܂"); + if(platform != "Windows") + { + print("ŌɎ̃R}hsĂ"); + print($" chmod +x {fileName}"); + } +}