You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Model.cs 58 kB

2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. using COSXML.Auth;
  2. using COSXML.CosException;
  3. using COSXML.Model.Object;
  4. using COSXML;
  5. using Newtonsoft.Json;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System;
  12. using COSXML.Model.Bucket;
  13. using System.Runtime.InteropServices;
  14. using System.Text.Json.Nodes;
  15. using System.Net.Http.Json;
  16. using System.Text.Json;
  17. using System.Xml.Schema;
  18. using static Downloader.Program;
  19. using System.Threading.Tasks;
  20. using System.Net.Http;
  21. using System.Windows;
  22. using System.Windows.Shapes;
  23. //using System.Windows.Forms;
  24. using MessageBox = System.Windows.MessageBox;
  25. using Downloader;
  26. using COSXML.Transfer;
  27. namespace starter.viewmodel.settings
  28. {
  29. /// <summary>
  30. /// Settings Window Model
  31. /// </summary>
  32. public class SettingsModel
  33. {
  34. /// <summary>
  35. /// downloader function
  36. /// </summary>
  37. private Data configData = new Data("");
  38. private Tencent_cos_download cloud = new Tencent_cos_download();
  39. private HttpClient client = new HttpClient();
  40. private WebConnect.Web web = new WebConnect.Web();
  41. public SettingsModel()
  42. {
  43. Route = Data.FilePath;
  44. Username = "";
  45. Password = "";
  46. updates = "";
  47. CodeRoute = "";
  48. PlayerNum = "nSelect";
  49. UploadReady = false;
  50. LoginFailed = false;
  51. }
  52. /// <summary>
  53. /// save settings
  54. /// </summary>
  55. public bool install()
  56. {
  57. if (Tencent_cos_download.CheckAlreadyDownload())
  58. {
  59. MessageBoxResult repeatOption = MessageBox.Show($"文件已存在于{Downloader.Program.Data.FilePath},是否移动到新位置?", "重复安装", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
  60. // ask if abort install, with warning sign, defalut move instead of abort;
  61. if (repeatOption == MessageBoxResult.No)
  62. {
  63. Route = Data.FilePath;
  64. return false;
  65. }
  66. else
  67. {
  68. Downloader.Program.Tencent_cos_download.MoveProgram(Route);
  69. return true;
  70. }
  71. }
  72. else
  73. {
  74. Data.ResetFilepath(Route);
  75. Tencent_cos_download.DownloadAll();
  76. return true;
  77. }
  78. }
  79. public int move()
  80. {
  81. int state = Tencent_cos_download.MoveProgram(Route);
  82. if (state != 0)
  83. Route = Data.FilePath;
  84. return state;
  85. }
  86. ///<summary>
  87. ///check for update
  88. /// </summary>
  89. static bool ProfileAvailable
  90. {
  91. get; set;
  92. }
  93. /// <summary>
  94. /// 检查更新
  95. /// </summary>
  96. /// <returns></returns>
  97. public Status checkUpdate()
  98. {
  99. UpdateInfo updateInfo = Tencent_cos_download.Check();
  100. if (updateInfo.newFileCount == -1)
  101. {
  102. if (updateInfo.changedFileCount == -1)
  103. {
  104. return Status.error;
  105. }
  106. else
  107. {
  108. return Status.disconnected;
  109. }
  110. }
  111. else
  112. {
  113. if (updateInfo.changedFileCount != 0 || updateInfo.newFileCount != 0)
  114. {
  115. Updates = "发现新版本";
  116. }
  117. return Status.menu;
  118. }
  119. }
  120. public async Task<bool> Login()
  121. {
  122. return await web.LoginToEEsast(client, Username, Password);
  123. }
  124. public bool Update()
  125. {
  126. return Tencent_cos_download.Update();
  127. }
  128. public int Uninst()
  129. {
  130. return Tencent_cos_download.DeleteAll();
  131. }
  132. public bool Launch()
  133. {
  134. if (Tencent_cos_download.CheckAlreadyDownload())
  135. {
  136. Process.Start(System.IO.Path.Combine(Data.FilePath, startName));
  137. return true;
  138. }
  139. else
  140. {
  141. MessageBox.Show($"文件还不存在,请安装主体文件", "文件不存在", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  142. return false;
  143. }
  144. }
  145. public async Task<int> Upload()
  146. {
  147. switch (CodeRoute.Substring(CodeRoute.LastIndexOf('.') + 1))
  148. {
  149. case "cpp":
  150. Language = "cpp";
  151. break;
  152. case "h":
  153. Language = "cpp";
  154. break;
  155. case "py":
  156. Language = "python";
  157. break;
  158. default:
  159. return -8;
  160. }
  161. if (PlayerNum.Equals("nSelect"))
  162. return -9;
  163. return await web.UploadFiles(client, CodeRoute, Language, PlayerNum);
  164. }
  165. /// <summary>
  166. /// Route of files
  167. /// </summary>
  168. public string Route
  169. {
  170. get; set;
  171. }
  172. public string Username
  173. {
  174. get; set;
  175. }
  176. public string Password
  177. {
  178. get; set;
  179. }
  180. public string CodeRoute
  181. {
  182. get; set;
  183. }
  184. public string Language
  185. {
  186. get; set;
  187. }
  188. public string PlayerNum
  189. {
  190. get; set;
  191. }
  192. /// <summary>
  193. /// 关于更新的屏幕显示信息
  194. /// </summary>
  195. private string updates;
  196. public string Updates
  197. {
  198. get
  199. {
  200. return updates;
  201. }
  202. set
  203. {
  204. updates = value;
  205. }
  206. }
  207. /// <summary>
  208. /// 关于介绍的屏幕显示信息
  209. /// </summary>
  210. public enum Status { newUser, menu, move, working, disconnected, error, successful, login, web };
  211. public Status status
  212. {
  213. get; set;
  214. }
  215. public bool Working
  216. {
  217. get; set;
  218. }
  219. /// <summary>
  220. /// if an update is planned
  221. /// </summary>
  222. public bool UpdatePlanned
  223. {
  224. get
  225. {
  226. return Program.UpdatePlanned;
  227. }
  228. }
  229. public bool CombatCompleted
  230. {
  231. get
  232. {
  233. return false;
  234. }
  235. }
  236. public bool LoginFailed
  237. {
  238. get; set;
  239. }
  240. public bool UploadReady
  241. {
  242. get; set;
  243. }
  244. }
  245. }
  246. namespace Downloader
  247. {
  248. class UserInfo
  249. {
  250. static public string _id = "";
  251. static public string email = "";
  252. }
  253. class Program
  254. {
  255. static List<string> newFileName = new List<string>(); // 新文件名
  256. static List<string> updateFileName = new List<string>(); // 更新文件名
  257. public static string ProgramName = "THUAI6"; // 要运行或下载的程序名称
  258. public static string playerFolder = "player"; // 选手代码保存文件夹路径
  259. public static string startName = "maintest.exe"; // 启动的程序名
  260. public struct UpdateInfo // 更新信息,包括新版本版本号、更改文件数和新文件数
  261. {
  262. public string status;
  263. public int changedFileCount;
  264. public int newFileCount;
  265. }
  266. public static bool UpdatePlanned
  267. {
  268. get; set;
  269. }
  270. static int filenum = 0; // 总文件个数
  271. public class Data
  272. {
  273. public static string path = ""; // 标记路径记录文件THUAI6.json的路径
  274. public static string FilePath = ""; // 最后一级为THUAI6文件夹所在目录
  275. public static string dataPath = ""; // C盘的文档文件夹
  276. public Data(string path)
  277. {
  278. // dataPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  279. dataPath = new DirectoryInfo(".").FullName;
  280. Data.path = System.IO.Path.Combine(dataPath, "THUAI6.json");
  281. if (File.Exists(Data.path))
  282. {
  283. var dict = new Dictionary<string, string>();
  284. using (StreamReader r = new StreamReader(Data.path))
  285. {
  286. string json = r.ReadToEnd();
  287. if (json == null || json == "")
  288. {
  289. json += @"{""THUAI6""" + ":" + @"""2023""}";
  290. }
  291. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  292. if (dict != null && dict.ContainsKey("installpath"))
  293. {
  294. FilePath = dict["installpath"].Replace('\\', '/');
  295. } //读取安装路径
  296. }
  297. dict?.TryAdd("installpath", @path);
  298. using FileStream fs = new FileStream(Data.path, FileMode.Create, FileAccess.ReadWrite);
  299. using StreamWriter sw = new StreamWriter(fs);
  300. sw.Write(JsonConvert.SerializeObject(dict));
  301. sw.Flush();
  302. }
  303. else
  304. {
  305. FilePath = System.IO.Path.GetDirectoryName(@path);
  306. //将dat文件写入程序运行路径
  307. string json;
  308. Dictionary<string, string> dict = new Dictionary<string, string>();
  309. using FileStream fs = new FileStream(Data.path, FileMode.Create, FileAccess.ReadWrite);
  310. using (StreamReader r = new StreamReader(fs))
  311. {
  312. json = r.ReadToEnd();
  313. if (json == null || json == "")
  314. {
  315. json += @"{""THUAI6""" + ":" + @"""2023""}";
  316. }
  317. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  318. dict?.Add("installpath", path);
  319. }
  320. using FileStream fs2 = new FileStream(Data.path, FileMode.Create, FileAccess.ReadWrite);
  321. using StreamWriter sw = new StreamWriter(fs2);
  322. sw.Write(JsonConvert.SerializeObject(dict));
  323. sw.Flush();
  324. }
  325. }
  326. public static void ResetFilepath(string newPath)
  327. {
  328. string json;
  329. Dictionary<string, string> dict = new Dictionary<string, string>();
  330. FilePath = newPath.Replace('\\', '/');
  331. path = System.IO.Path.Combine(dataPath, "THUAI6.json");
  332. using FileStream fs = new FileStream(Data.path, FileMode.Create, FileAccess.ReadWrite);
  333. using (StreamReader r = new StreamReader(fs))
  334. {
  335. json = r.ReadToEnd();
  336. if (json == null || json == "")
  337. {
  338. json += @"{""THUAI6""" + ":" + @"""2023""}";
  339. }
  340. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  341. if (dict != null && dict.ContainsKey("installpath"))
  342. {
  343. dict["installpath"] = newPath;
  344. }
  345. else
  346. {
  347. dict.Add("installpath", newPath);
  348. }
  349. if (dict == null || !dict.ContainsKey("download"))
  350. {
  351. dict?.Add("download", "true");
  352. }
  353. else
  354. {
  355. dict["download"] = "true";
  356. }
  357. }
  358. using FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
  359. using StreamWriter sw = new StreamWriter(fs2);
  360. fs2.SetLength(0);
  361. sw.Write(JsonConvert.SerializeObject(dict));
  362. sw.Flush();
  363. }
  364. }
  365. public class Tencent_cos_download
  366. {
  367. public void download(string download_dir, string key)
  368. {
  369. // download_dir标记根文件夹路径,key为相对根文件夹的路径(不带./)
  370. // 初始化CosXmlConfig(提供配置SDK接口)
  371. string appid = "1314234950"; // 设置腾讯云账户的账户标识(APPID)
  372. string region = "ap-beijing"; // 设置一个默认的存储桶地域
  373. CosXmlConfig config = new CosXmlConfig.Builder()
  374. .IsHttps(true) // 设置默认 HTTPS 请求
  375. .SetAppid(appid) // 设置腾讯云账户的账户标识 APPID
  376. .SetRegion(region) // 设置一个默认的存储桶地域
  377. .SetDebugLog(true) // 显示日志
  378. .Build(); // 创建 CosXmlConfig 对象
  379. // 永久密钥访问凭证
  380. string secretId = "***"; //"云 API 密钥 SecretId";
  381. string secretKey = "***"; //"云 API 密钥 SecretKey";
  382. long durationSecond = 1000; // 每次请求签名有效时长,单位为秒
  383. QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
  384. secretId, secretKey, durationSecond
  385. );
  386. // 初始化 CosXmlServer
  387. CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider);
  388. // 创建存储桶
  389. try
  390. {
  391. string bucket = "thuai6-1314234950"; // 格式:BucketName-APPID
  392. string localDir = System.IO.Path.GetDirectoryName(download_dir); // 本地文件夹
  393. string localFileName = System.IO.Path.GetFileName(download_dir); // 指定本地保存的文件名
  394. GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);
  395. Dictionary<string, string> test = request.GetRequestHeaders();
  396. request.SetCosProgressCallback(delegate (long completed, long total)
  397. {
  398. Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
  399. });
  400. // 执行请求
  401. GetObjectResult result = cosXml.GetObject(request);
  402. // 请求成功
  403. }
  404. catch (CosClientException clientEx)
  405. {
  406. throw clientEx;
  407. }
  408. catch (CosServerException serverEx)
  409. {
  410. throw serverEx;
  411. }
  412. }
  413. public static void GetNewHash()
  414. {
  415. Tencent_cos_download Downloader = new Tencent_cos_download();
  416. Downloader.download(System.IO.Path.Combine(Data.FilePath, "hash.json"), "hash.json");
  417. }
  418. public static string GetFileMd5Hash(string strFileFullPath)
  419. {
  420. FileStream fst = null;
  421. try
  422. {
  423. fst = new FileStream(strFileFullPath, FileMode.Open);
  424. byte[] data = MD5.Create().ComputeHash(fst);
  425. StringBuilder sBuilder = new StringBuilder();
  426. for (int i = 0; i < data.Length; i++)
  427. {
  428. sBuilder.Append(data[i].ToString("x2"));
  429. }
  430. fst.Close();
  431. return sBuilder.ToString().ToLower();
  432. }
  433. catch (Exception)
  434. {
  435. if (fst != null)
  436. fst.Close();
  437. return "";
  438. }
  439. finally
  440. {
  441. }
  442. }
  443. public static UpdateInfo Check()
  444. {
  445. string json, MD5, jsonName;
  446. int newFile = 0, updateFile = 0;
  447. newFileName.Clear();
  448. updateFileName.Clear();
  449. jsonName = "hash.json";
  450. UpdateInfo updateInfo;
  451. Tencent_cos_download Downloader = new Tencent_cos_download();
  452. try
  453. {
  454. // 如果json存在就删了重新下
  455. if (File.Exists(System.IO.Path.Combine(Data.FilePath, jsonName)))
  456. {
  457. File.Delete(System.IO.Path.Combine(Data.FilePath, jsonName));
  458. Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName), jsonName);
  459. }
  460. else
  461. {
  462. Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName), jsonName);
  463. }
  464. }
  465. catch (CosClientException clientEx)
  466. {
  467. // 请求失败
  468. updateInfo.status = "ClientEx: " + clientEx.ToString();
  469. updateInfo.newFileCount = -1;
  470. updateInfo.changedFileCount = 0;
  471. return updateInfo;
  472. }
  473. catch (CosServerException serverEx)
  474. {
  475. // 请求失败
  476. updateInfo.status = "ServerEx: " + serverEx.ToString();
  477. updateInfo.newFileCount = -1;
  478. updateInfo.changedFileCount = 0;
  479. return updateInfo;
  480. }
  481. using (StreamReader r = new StreamReader(System.IO.Path.Combine(Data.FilePath, jsonName)))
  482. json = r.ReadToEnd();
  483. json = json.Replace("\r", string.Empty).Replace("\n", string.Empty);
  484. Dictionary<string, string> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  485. foreach (KeyValuePair<string, string> pair in jsonDict)
  486. {
  487. MD5 = GetFileMd5Hash(System.IO.Path.Combine(Data.FilePath, pair.Key));
  488. if (MD5.Length == 0) // 文档不存在
  489. newFileName.Add(pair.Key);
  490. else if (MD5 != pair.Value) // MD5不匹配
  491. updateFileName.Add(pair.Key);
  492. }
  493. newFile = newFileName.Count;
  494. updateFile = updateFileName.Count;
  495. filenum = newFile + updateFile;
  496. //Console.WriteLine("----------------------" + Environment.NewLine);
  497. if (newFile + updateFile == 0)
  498. {
  499. updateInfo.status = "latest";
  500. updateInfo.newFileCount = 0;
  501. updateInfo.changedFileCount = 0;
  502. newFileName.Clear();
  503. updateFileName.Clear();
  504. }
  505. else
  506. {
  507. updateInfo.status = "old";
  508. //TODO:获取版本号
  509. updateInfo.newFileCount = newFile;
  510. /*
  511. foreach (string filename in newFileName)
  512. {
  513. Console.WriteLine(filename);
  514. }
  515. */
  516. updateInfo.changedFileCount = updateFile;
  517. /*
  518. foreach (string filename in updateFileName)
  519. {
  520. Console.WriteLine(filename);
  521. }
  522. Console.Write(Environment.NewLine + "是否下载新文件? y/n:");
  523. if (Console.Read() != 'y')
  524. Console.WriteLine("下载取消!");
  525. else
  526. Download();
  527. */
  528. UpdatePlanned = true;
  529. }
  530. return updateInfo;
  531. }
  532. public static bool Update()
  533. {
  534. if (UpdatePlanned)
  535. {
  536. Download();
  537. UpdatePlanned = false;
  538. return true;
  539. }
  540. return false;
  541. }
  542. private static void Download()
  543. {
  544. Tencent_cos_download Downloader = new Tencent_cos_download();
  545. int newFile = 0, updateFile = 0;
  546. int totalnew = newFileName.Count, totalupdate = updateFileName.Count;
  547. filenum = totalnew + totalupdate;
  548. if (newFileName.Count > 0 || updateFileName.Count > 0)
  549. {
  550. try
  551. {
  552. foreach (string filename in newFileName)
  553. {
  554. Console.WriteLine(newFile + 1 + "/" + totalnew + ":开始下载" + filename);
  555. Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename);
  556. Console.WriteLine(filename + "下载完毕!" + Environment.NewLine);
  557. newFile++;
  558. }
  559. foreach (string filename in updateFileName)
  560. {
  561. Console.WriteLine(updateFile + 1 + "/" + totalupdate + ":开始下载" + filename);
  562. File.Delete(System.IO.Path.Combine(@Data.FilePath, filename));
  563. Downloader.download(System.IO.Path.Combine(@Data.FilePath, filename), filename);
  564. Console.WriteLine(filename + "下载完毕!" + Environment.NewLine);
  565. updateFile++;
  566. }
  567. }
  568. catch (CosClientException clientEx)
  569. {
  570. // 请求失败
  571. Console.WriteLine("CosClientException: " + clientEx.ToString() + Environment.NewLine);
  572. return;
  573. }
  574. catch (CosServerException serverEx)
  575. {
  576. // 请求失败
  577. Console.WriteLine("CosClientException: " + serverEx.ToString() + Environment.NewLine);
  578. return;
  579. }
  580. catch (Exception)
  581. {
  582. throw;
  583. }
  584. }
  585. else
  586. Console.WriteLine("当前平台已是最新版本!" + Environment.NewLine);
  587. newFileName.Clear();
  588. updateFileName.Clear();
  589. }
  590. public static bool CheckAlreadyDownload() // 检查是否已经下载
  591. {
  592. string existpath = System.IO.Path.Combine(Data.dataPath, "THUAI6.json");
  593. if (!File.Exists(existpath)) // 文件不存在
  594. {
  595. using FileStream fs = new FileStream(existpath, FileMode.Create, FileAccess.ReadWrite);
  596. return false;
  597. }
  598. else // 文件存在
  599. {
  600. using FileStream fs = new FileStream(existpath, FileMode.Open, FileAccess.Read);
  601. using StreamReader sr = new StreamReader(fs);
  602. string json = sr.ReadToEnd();
  603. if (json == null || json == "")
  604. {
  605. json += @"{""THUAI6""" + ":" + @"""2023""}";
  606. }
  607. var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  608. if (dict == null || !dict.ContainsKey("download") || "false" == dict["download"])
  609. {
  610. return false;
  611. }
  612. else if (dict["download"] == "true")
  613. {
  614. return true;
  615. }
  616. else
  617. {
  618. return false;
  619. }
  620. }
  621. }
  622. public static void DownloadAll() // 下载全部文件
  623. {
  624. string jsonName = "hash.json";
  625. string json;
  626. Tencent_cos_download Downloader = new Tencent_cos_download();
  627. try
  628. {
  629. // 如果json存在就删了重新下
  630. if (File.Exists(System.IO.Path.Combine(Data.FilePath, jsonName)))
  631. {
  632. File.Delete(System.IO.Path.Combine(Data.FilePath, jsonName));
  633. Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName), jsonName);
  634. }
  635. else
  636. {
  637. Downloader.download(System.IO.Path.Combine(Data.FilePath, jsonName), jsonName);
  638. }
  639. }
  640. catch (CosClientException clientEx)
  641. {
  642. // 请求失败
  643. Console.WriteLine("CosClientException: " + clientEx.ToString() + Environment.NewLine);
  644. return;
  645. }
  646. catch (CosServerException serverEx)
  647. {
  648. // 请求失败
  649. Console.WriteLine("CosClientException: " + serverEx.ToString() + Environment.NewLine);
  650. return;
  651. }
  652. using (StreamReader r = new StreamReader(System.IO.Path.Combine(Data.FilePath, jsonName)))
  653. json = r.ReadToEnd();
  654. json = json.Replace("\r", string.Empty).Replace("\n", string.Empty);
  655. Dictionary<string, string> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  656. newFileName.Clear();
  657. updateFileName.Clear();
  658. foreach (KeyValuePair<string, string> pair in jsonDict)
  659. {
  660. newFileName.Add(pair.Key);
  661. }
  662. Download();
  663. string json2;
  664. Dictionary<string, string> dict = new Dictionary<string, string>();
  665. string existpath = System.IO.Path.Combine(Data.dataPath, "THUAI6.json");
  666. using FileStream fs = new FileStream(existpath, FileMode.Open, FileAccess.ReadWrite);
  667. using (StreamReader r = new StreamReader(fs))
  668. {
  669. json2 = r.ReadToEnd();
  670. if (json2 == null || json2 == "")
  671. {
  672. json2 += @"{""THUAI6""" + ":" + @"""2023""}";
  673. }
  674. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json2);
  675. if (dict == null || !dict.ContainsKey("download"))
  676. {
  677. dict?.Add("download", "true");
  678. }
  679. else
  680. {
  681. dict["download"] = "true";
  682. }
  683. }
  684. using FileStream fs2 = new FileStream(existpath, FileMode.Open, FileAccess.ReadWrite);
  685. using StreamWriter sw = new StreamWriter(fs2);
  686. fs2.SetLength(0);
  687. sw.Write(JsonConvert.SerializeObject(dict));
  688. }
  689. public static void Change_all_hash(string topDir, Dictionary<string, string> jsonDict) // 更改HASH
  690. {
  691. DirectoryInfo theFolder = new DirectoryInfo(@topDir);
  692. bool ifexist = false;
  693. // 遍历文件
  694. foreach (FileInfo NextFile in theFolder.GetFiles())
  695. {
  696. string filepath = topDir + @"/" + NextFile.Name; // 文件路径
  697. Console.WriteLine(filepath);
  698. foreach (KeyValuePair<string, string> pair in jsonDict)
  699. {
  700. if (System.IO.Path.Equals(filepath, System.IO.Path.Combine(Data.FilePath, pair.Key).Replace('\\', '/')))
  701. {
  702. ifexist = true;
  703. string MD5 = GetFileMd5Hash(filepath);
  704. jsonDict[pair.Key] = MD5;
  705. }
  706. }
  707. if (!ifexist && NextFile.Name != "hash.json")
  708. {
  709. string MD5 = GetFileMd5Hash(filepath);
  710. string relapath = filepath.Replace(Data.FilePath + '/', string.Empty);
  711. jsonDict.Add(relapath, MD5);
  712. }
  713. ifexist = false;
  714. }
  715. // 遍历文件夹
  716. foreach (DirectoryInfo NextFolder in theFolder.GetDirectories())
  717. {
  718. if (System.IO.Path.Equals(NextFolder.FullName, System.IO.Path.GetFullPath(System.IO.Path.Combine(Data.FilePath, playerFolder))))
  719. {
  720. foreach (FileInfo NextFile in NextFolder.GetFiles())
  721. {
  722. if (NextFile.Name == "README.md")
  723. {
  724. string MD5 = GetFileMd5Hash(NextFile.FullName);
  725. string relapath = NextFile.FullName.Replace('\\', '/').Replace(Data.FilePath + '/', string.Empty);
  726. jsonDict.Add(relapath, MD5);
  727. }
  728. }
  729. continue; // 如果是选手文件夹就忽略
  730. }
  731. Change_all_hash(NextFolder.FullName.Replace('\\', '/'), jsonDict);
  732. }
  733. }
  734. public static void UpdateHash()
  735. {
  736. while (true)
  737. {
  738. if (Directory.Exists(Data.FilePath))
  739. {
  740. string json;
  741. if (!File.Exists(System.IO.Path.Combine(Data.FilePath, "hash.json")))
  742. {
  743. Console.WriteLine("hash.json文件丢失!即将重新下载该文件!");
  744. GetNewHash();
  745. }
  746. using (StreamReader r = new StreamReader(System.IO.Path.Combine(Data.FilePath, "hash.json")))
  747. json = r.ReadToEnd();
  748. json = json.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("/", @"\\");
  749. Dictionary<string, string> jsonDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  750. Change_all_hash(Data.FilePath, jsonDict);
  751. OverwriteHash(jsonDict);
  752. break;
  753. }
  754. else
  755. {
  756. Console.WriteLine("读取路径失败!请重新输入文件路径:");
  757. Data.ResetFilepath(Console.ReadLine());
  758. }
  759. }
  760. }
  761. public static int DeleteAll()
  762. {
  763. DirectoryInfo di = new DirectoryInfo(Data.FilePath);
  764. DirectoryInfo player = new DirectoryInfo(System.IO.Path.GetFullPath(System.IO.Path.Combine(Data.FilePath, playerFolder)));
  765. try
  766. {
  767. foreach (FileInfo file in di.GetFiles())
  768. {
  769. file.Delete();
  770. }
  771. foreach (FileInfo file in player.GetFiles())
  772. {
  773. if (file.Name == "README.md")
  774. {
  775. continue;
  776. }
  777. string filename = System.IO.Path.GetFileName(file.FullName);
  778. file.MoveTo(System.IO.Path.Combine(Data.FilePath, filename));
  779. }
  780. foreach (DirectoryInfo subdi in di.GetDirectories())
  781. {
  782. subdi.Delete(true);
  783. }
  784. }
  785. catch (UnauthorizedAccessException)
  786. {
  787. Console.WriteLine("权限不足,无法删除!");
  788. return -2;
  789. }
  790. catch (DirectoryNotFoundException)
  791. {
  792. Console.WriteLine("文件夹没有找到,请检查是否已经手动更改路径");
  793. return -3;
  794. }
  795. catch (IOException)
  796. {
  797. Console.WriteLine("文件已经打开,请关闭后再删除");
  798. return -1;
  799. }
  800. string json2;
  801. Dictionary<string, string> dict = new Dictionary<string, string>();
  802. string existpath = System.IO.Path.Combine(Data.dataPath, "THUAI6.json");
  803. using FileStream fs = new FileStream(existpath, FileMode.Open, FileAccess.ReadWrite);
  804. using (StreamReader r = new StreamReader(fs))
  805. {
  806. json2 = r.ReadToEnd();
  807. if (json2 == null || json2 == "")
  808. {
  809. json2 += @"{""THUAI6""" + ":" + @"""2023""}";
  810. }
  811. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json2);
  812. if (dict == null || !dict.ContainsKey("download"))
  813. {
  814. dict?.Add("download", "false");
  815. }
  816. else
  817. {
  818. dict["download"] = "false";
  819. }
  820. }
  821. using FileStream fs2 = new FileStream(existpath, FileMode.Open, FileAccess.ReadWrite);
  822. using StreamWriter sw = new StreamWriter(fs2);
  823. fs2.SetLength(0);
  824. sw.Write(JsonConvert.SerializeObject(dict));
  825. sw.Close();
  826. fs2.Close();
  827. try
  828. {
  829. File.Delete(Data.path);
  830. }
  831. catch (UnauthorizedAccessException)
  832. {
  833. Console.WriteLine("权限不足,无法删除!");
  834. return -2;
  835. }
  836. catch (DirectoryNotFoundException)
  837. {
  838. Console.WriteLine("文件夹没有找到,请检查是否已经手动更改路径");
  839. return -3;
  840. }
  841. catch (IOException)
  842. {
  843. Console.WriteLine("文件已经打开,请关闭后再删除");
  844. return -1;
  845. }
  846. Console.WriteLine($"删除成功!player文件夹中的文件已经放在{ProgramName}的根目录下");
  847. return 0;
  848. }
  849. public static void OverwriteHash(Dictionary<string, string> jsonDict)
  850. {
  851. string Contentjson = JsonConvert.SerializeObject(jsonDict);
  852. Contentjson = Contentjson.Replace("\r", String.Empty).Replace("\n", String.Empty).Replace(@"\\", "/");
  853. File.WriteAllText(@System.IO.Path.Combine(Data.FilePath, "hash.json"), Contentjson);
  854. }
  855. public static int MoveProgram(string newPath)
  856. {
  857. DirectoryInfo newdi = new DirectoryInfo(newPath);
  858. DirectoryInfo olddi = new DirectoryInfo(Data.FilePath);
  859. try
  860. {
  861. foreach (DirectoryInfo direct in olddi.GetDirectories())
  862. {
  863. direct.MoveTo(System.IO.Path.Combine(newPath, direct.Name));
  864. }
  865. foreach (FileInfo file in olddi.GetFiles())
  866. {
  867. file.MoveTo(System.IO.Path.Combine(newPath, file.Name));
  868. }
  869. }
  870. catch (DirectoryNotFoundException)
  871. {
  872. Console.WriteLine("原路径未找到!请检查文件是否损坏");
  873. foreach (DirectoryInfo newdirect in newdi.GetDirectories())
  874. {
  875. newdirect.MoveTo(System.IO.Path.Combine(Data.FilePath, newdirect.Name));
  876. }
  877. foreach (FileInfo file in newdi.GetFiles())
  878. {
  879. file.MoveTo(System.IO.Path.Combine(Data.FilePath, file.Name));
  880. }
  881. Console.WriteLine("移动失败!");
  882. return -2;
  883. }
  884. catch (IOException)
  885. {
  886. Console.WriteLine("文件已打开或者目标路径下有同名文件!");
  887. foreach (DirectoryInfo newdirect in newdi.GetDirectories())
  888. {
  889. newdirect.MoveTo(System.IO.Path.Combine(Data.FilePath, newdirect.Name));
  890. }
  891. foreach (FileInfo file in newdi.GetFiles())
  892. {
  893. file.MoveTo(System.IO.Path.Combine(Data.FilePath, file.Name));
  894. }
  895. Console.WriteLine("移动失败!");
  896. return -1;
  897. }
  898. Data.ResetFilepath(newPath);
  899. Console.WriteLine("更改路径成功!");
  900. return 0;
  901. }
  902. public static async Task main(string[] args)
  903. {
  904. var client = new HttpClient();
  905. var web = new WebConnect.Web();
  906. Data date = new Data("");
  907. while (true)
  908. {
  909. Console.WriteLine($"1. 更新hash.json 2. 检查更新 3.下载{ProgramName} 4.删除{ProgramName} 5.启动进程 6.移动{ProgramName}到其它路径");
  910. string choose = Console.ReadLine();
  911. if (choose == "1")
  912. {
  913. if (!CheckAlreadyDownload())
  914. {
  915. Console.WriteLine($"未下载{ProgramName},请先执行下载操作!");
  916. continue;
  917. }
  918. UpdateHash();
  919. }
  920. else if (choose == "2")
  921. {
  922. if (!CheckAlreadyDownload())
  923. {
  924. Console.WriteLine($"未下载{ProgramName},请先执行下载操作!");
  925. continue;
  926. }
  927. while (true)
  928. {
  929. if (Data.FilePath != null && Directory.Exists(Data.FilePath))
  930. {
  931. Check();
  932. break;
  933. }
  934. else
  935. {
  936. Console.WriteLine("读取路径失败!请重新输入文件路径:");
  937. Data.ResetFilepath(Console.ReadLine());
  938. }
  939. }
  940. }
  941. else if (choose == "3")
  942. {
  943. if (CheckAlreadyDownload())
  944. {
  945. Console.WriteLine($"已经将{ProgramName}下载到{Data.FilePath}!若要重新下载请先完成删除操作!");
  946. }
  947. else
  948. {
  949. string newpath;
  950. Console.WriteLine("请输入下载路径:");
  951. newpath = Console.ReadLine();
  952. Data.ResetFilepath(newpath);
  953. DownloadAll();
  954. }
  955. }
  956. else if (choose == "4")
  957. {
  958. DeleteAll();
  959. }
  960. else if (choose == "5")
  961. {
  962. if (CheckAlreadyDownload())
  963. {
  964. Process.Start(System.IO.Path.Combine(Data.FilePath, startName));
  965. }
  966. else
  967. {
  968. Console.WriteLine($"未下载{ProgramName},请先执行下载操作!");
  969. }
  970. }
  971. else if (choose == "6")
  972. {
  973. string newPath;
  974. newPath = Console.ReadLine();
  975. MoveProgram(newPath);
  976. }
  977. else if (choose == "7")
  978. {
  979. Console.WriteLine("请输入email:");
  980. string username = Console.ReadLine();
  981. Console.WriteLine("请输入密码:");
  982. string password = Console.ReadLine();
  983. await web.LoginToEEsast(client, username, password);
  984. }
  985. else if (choose == "8")
  986. {
  987. await web.UserDetails(client);
  988. }
  989. else if (choose == "9")
  990. {
  991. await web.UploadFiles(client, "", "", "");
  992. }
  993. else if (choose == "exit")
  994. {
  995. return;
  996. }
  997. }
  998. }
  999. }
  1000. }
  1001. }
  1002. namespace WebConnect
  1003. {
  1004. class Web
  1005. {
  1006. public enum language { cpp, py };
  1007. public static string logintoken = "";
  1008. async public Task<bool> LoginToEEsast(HttpClient client, string useremail, string password)
  1009. {
  1010. string token = "";
  1011. using (var response = await client.PostAsync("https://api.eesast.com/users/login", JsonContent.Create(new
  1012. {
  1013. email = useremail,
  1014. password = password,
  1015. })))
  1016. {
  1017. switch (response.StatusCode)
  1018. {
  1019. case System.Net.HttpStatusCode.OK:
  1020. Console.WriteLine("Success login");
  1021. token = (System.Text.Json.JsonSerializer.Deserialize(await response.Content.ReadAsStreamAsync(), typeof(LoginResponse), new JsonSerializerOptions()
  1022. {
  1023. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  1024. }) as LoginResponse)
  1025. ?.Token ??
  1026. throw new Exception("no token!");
  1027. logintoken = token;
  1028. SaveToken();
  1029. var info = JsonConvert.DeserializeObject<Dictionary<string, string>>(await response.Content.ReadAsStringAsync());
  1030. Downloader.UserInfo._id = info["_id"];
  1031. Downloader.UserInfo.email = info["email"];
  1032. break;
  1033. default:
  1034. int code = ((int)response.StatusCode);
  1035. Console.WriteLine(code);
  1036. if (code == 401)
  1037. {
  1038. //Console.WriteLine("邮箱或密码错误!");
  1039. return false;
  1040. }
  1041. break;
  1042. }
  1043. return true;
  1044. }
  1045. }
  1046. /// <summary>
  1047. ///
  1048. /// </summary>
  1049. /// <param name="client">http client</param>
  1050. /// <param name="tarfile">代码源位置</param>
  1051. /// <param name="type">编程语言,格式为"cpp"或"python"</param>
  1052. /// <param name="plr">第x位玩家,格式为"player_x"</param>
  1053. /// <returns>-1:tokenFail;-2:FileNotExist;-3:CosFail;-4:loginTimeout;-5:Fail;-6:ReadFileFail;-7:networkError</returns>
  1054. async public Task<int> UploadFiles(HttpClient client, string tarfile, string type, string plr) //用来上传文件
  1055. {
  1056. if (!ReadToken()) //读取token失败
  1057. {
  1058. return -1;
  1059. }
  1060. try
  1061. {
  1062. string content;
  1063. client.DefaultRequestHeaders.Authorization = new("bearertoken", logintoken);
  1064. if (!File.Exists(tarfile))
  1065. {
  1066. //Console.WriteLine("文件不存在!");
  1067. return -2;
  1068. }
  1069. using FileStream fs = new FileStream(tarfile, FileMode.Open, FileAccess.Read);
  1070. using StreamReader sr = new StreamReader(fs);
  1071. content = sr.ReadToEnd();
  1072. string targetUrl = $"https://api.eesast.com/static/player?team_id={await GetTeamId()}";
  1073. using (var response = await client.GetAsync(targetUrl))
  1074. {
  1075. switch (response.StatusCode)
  1076. {
  1077. case System.Net.HttpStatusCode.OK:
  1078. var res = JsonConvert.DeserializeObject<Dictionary<string, string>>(await response.Content.ReadAsStringAsync());
  1079. string appid = "1255334966"; // 设置腾讯云账户的账户标识(APPID)
  1080. string region = "ap-beijing"; // 设置一个默认的存储桶地域
  1081. CosXmlConfig config = new CosXmlConfig.Builder()
  1082. .IsHttps(true) // 设置默认 HTTPS 请求
  1083. .SetAppid(appid) // 设置腾讯云账户的账户标识 APPID
  1084. .SetRegion(region) // 设置一个默认的存储桶地域
  1085. .SetDebugLog(true) // 显示日志
  1086. .Build(); // 创建 CosXmlConfig 对象
  1087. string tmpSecretId = res["TmpSecretId"]; //"临时密钥 SecretId";
  1088. string tmpSecretKey = res["TmpSecretKey"]; //"临时密钥 SecretKey";
  1089. string tmpToken = res["SecurityToken"]; //"临时密钥 token";
  1090. long tmpExpiredTime = Convert.ToInt64(res["ExpiredTime"]);//临时密钥有效截止时间,精确到秒
  1091. QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(
  1092. tmpSecretId, tmpSecretKey, tmpExpiredTime, tmpToken
  1093. );
  1094. // 初始化 CosXmlServer
  1095. CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider);
  1096. // 初始化 TransferConfig
  1097. TransferConfig transferConfig = new TransferConfig();
  1098. // 初始化 TransferManager
  1099. TransferManager transferManager = new TransferManager(cosXml, transferConfig);
  1100. string bucket = "eesast-1255334966"; //存储桶,格式:BucketName-APPID
  1101. string cosPath = $"/THUAI6/{GetTeamId()}/{type}/{plr}"; //对象在存储桶中的位置标识符,即称对象键
  1102. string srcPath = tarfile;//本地文件绝对路径
  1103. // 上传对象
  1104. COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
  1105. uploadTask.SetSrcPath(srcPath);
  1106. uploadTask.progressCallback = delegate (long completed, long total)
  1107. {
  1108. Console.WriteLine(string.Format("progress = {0:##.##}%", completed * 100.0 / total));
  1109. };
  1110. try
  1111. {
  1112. COSXMLUploadTask.UploadTaskResult result = await transferManager.UploadAsync(uploadTask);
  1113. Console.WriteLine(result.GetResultInfo());
  1114. string eTag = result.eTag;
  1115. //到这里应该是成功了,但是因为我没有试过,也不知道具体情况,可能还要根据result的内容判断
  1116. }
  1117. catch (Exception)
  1118. {
  1119. return -3;
  1120. }
  1121. break;
  1122. case System.Net.HttpStatusCode.Unauthorized:
  1123. //Console.WriteLine("您未登录或登录过期,请先登录");
  1124. return -4;
  1125. default:
  1126. //Console.WriteLine("上传失败!");
  1127. return -5;
  1128. }
  1129. }
  1130. }
  1131. catch (IOException)
  1132. {
  1133. //Console.WriteLine("文件读取错误!请检查文件是否被其它应用占用!");
  1134. return -6;
  1135. }
  1136. catch
  1137. {
  1138. //Console.WriteLine("请求错误!请检查网络连接!");
  1139. return -7;
  1140. }
  1141. return 0;
  1142. }
  1143. async public Task UserDetails(HttpClient client) // 用来测试访问网站
  1144. {
  1145. if (!ReadToken()) // 读取token失败
  1146. {
  1147. return;
  1148. }
  1149. try
  1150. {
  1151. client.DefaultRequestHeaders.Authorization = new("Bearer", logintoken);
  1152. Console.WriteLine(logintoken);
  1153. using (var response = await client.GetAsync("https://api.eesast.com/application/info")) // JsonContent.Create(new
  1154. //{
  1155. //})))
  1156. {
  1157. switch (response.StatusCode)
  1158. {
  1159. case System.Net.HttpStatusCode.OK:
  1160. Console.WriteLine("Require OK");
  1161. Console.WriteLine(await response.Content.ReadAsStringAsync());
  1162. break;
  1163. default:
  1164. int code = ((int)response.StatusCode);
  1165. if (code == 401)
  1166. {
  1167. Console.WriteLine("您未登录或登录过期,请先登录");
  1168. }
  1169. return;
  1170. }
  1171. }
  1172. }
  1173. catch
  1174. {
  1175. Console.WriteLine("请求错误!请检查网络连接!");
  1176. }
  1177. }
  1178. public void SaveToken() // 保存token
  1179. {
  1180. string savepath = System.IO.Path.Combine(Data.dataPath, "THUAI6.json");
  1181. try
  1182. {
  1183. string json;
  1184. Dictionary<string, string> dict = new Dictionary<string, string>();
  1185. using FileStream fs = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  1186. using (StreamReader r = new StreamReader(fs))
  1187. {
  1188. json = r.ReadToEnd();
  1189. if (json == null || json == "")
  1190. {
  1191. json += @"{""THUAI6""" + ":" + @"""2023""}";
  1192. }
  1193. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  1194. if (dict.ContainsKey("token"))
  1195. {
  1196. dict.Remove("token");
  1197. }
  1198. dict?.Add("token", logintoken);
  1199. }
  1200. using FileStream fs2 = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  1201. using StreamWriter sw = new StreamWriter(fs2);
  1202. fs2.SetLength(0);
  1203. sw.Write(JsonConvert.SerializeObject(dict)); //将token写入文件
  1204. }
  1205. catch (DirectoryNotFoundException)
  1206. {
  1207. Console.WriteLine("保存token时未找到下载器地址!请检查下载器是否被移动!");
  1208. }
  1209. catch (PathTooLongException)
  1210. {
  1211. Console.WriteLine("下载器的路径名太长!请尝试移动下载器!");
  1212. }
  1213. catch (ArgumentNullException)
  1214. {
  1215. Console.WriteLine("下载器路径初始化失败!");
  1216. }
  1217. catch (IOException)
  1218. {
  1219. Console.WriteLine("写入token.dat发生冲突!请检查token.dat是否被其它程序占用!");
  1220. }
  1221. }
  1222. public bool ReadToken() // 读取token
  1223. {
  1224. try
  1225. {
  1226. string json;
  1227. Dictionary<string, string> dict = new Dictionary<string, string>();
  1228. string savepath = System.IO.Path.Combine(Data.dataPath, "THUAI6.json");
  1229. using FileStream fs = new FileStream(savepath, FileMode.Open, FileAccess.Read);
  1230. using StreamReader sr = new StreamReader(fs);
  1231. json = sr.ReadToEnd();
  1232. if (json == null || json == "")
  1233. {
  1234. json += @"{""THUAI6""" + ":" + @"""2023""}";
  1235. }
  1236. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  1237. if (!dict.ContainsKey("token"))
  1238. {
  1239. return false;
  1240. }
  1241. else
  1242. {
  1243. logintoken = dict["token"];
  1244. return true;
  1245. }
  1246. }
  1247. catch (DirectoryNotFoundException)
  1248. {
  1249. Console.WriteLine("读取token时未找到下载器地址!请检查下载器是否被移动!");
  1250. return false;
  1251. }
  1252. catch (FileNotFoundException)
  1253. {
  1254. //没有登陆
  1255. Console.WriteLine("请先登录!");
  1256. return false;
  1257. }
  1258. catch (PathTooLongException)
  1259. {
  1260. Console.WriteLine("下载器的路径名太长!请尝试移动下载器!");
  1261. return false;
  1262. }
  1263. catch (ArgumentNullException)
  1264. {
  1265. Console.WriteLine("下载器路径初始化失败!");
  1266. return false;
  1267. }
  1268. catch (IOException)
  1269. {
  1270. Console.WriteLine("写入token.dat发生冲突!请检查token.dat是否被其它程序占用!");
  1271. return false;
  1272. }
  1273. }
  1274. async public Task<string> GetTeamId()
  1275. {
  1276. var client = new HttpClient();
  1277. var request = new HttpRequestMessage(HttpMethod.Post, "https://api.eesast.com/dev/v1/graphql");
  1278. request.Headers.Add("x-hasura-admin-secret", "hasuraDevAdminSecret");
  1279. //var content = new StringContent($@"
  1280. // {{
  1281. // ""query"": ""query MyQuery {{contest_team_member(where: {{user_id: {{_eq: \""{Downloader.UserInfo._id}\""}}}}) {{ team_id }}}}"",
  1282. // ""variables"": {{}},
  1283. // }}", null, "application/json");
  1284. var content = new StringContent("{\"query\":\"query MyQuery {\\r\\n contest_team_member(where: {user_id: {_eq: \\\"" + Downloader.UserInfo._id + "\\\"}}) {\\r\\n team_id\\r\\n }\\r\\n}\",\"variables\":{}}", null, "application/json");
  1285. request.Content = content;
  1286. var response = await client.SendAsync(request);
  1287. response.EnsureSuccessStatusCode();
  1288. var info = await response.Content.ReadAsStringAsync();
  1289. var s1 = JsonConvert.DeserializeObject<Dictionary<string, object>>(info)["data"];
  1290. var s2 = JsonConvert.DeserializeObject<Dictionary<string, List<object>>>(s1.ToString())["contest_team_member"];
  1291. var sres = JsonConvert.DeserializeObject<Dictionary<string, string>>(s2[0].ToString())["team_id"];
  1292. return sres;
  1293. }
  1294. async public Task<string> GetUserId(string learnNumber)
  1295. {
  1296. var client = new HttpClient();
  1297. var request = new HttpRequestMessage(HttpMethod.Post, "https://api.eesast.com/dev/v1/graphql");
  1298. request.Headers.Add("x-hasura-admin-secret", "hasuraDevAdminSecret");
  1299. var content = new StringContent("{\"query\":\"query MyQuery {\r\n user(where: {id: {_eq: \""
  1300. + learnNumber + "\"}}) {\r\n _id\r\n }\r\n}\r\n\",\"variables\":{}}", null, "application/json");
  1301. request.Content = content;
  1302. var response = await client.SendAsync(request);
  1303. response.EnsureSuccessStatusCode();
  1304. return await response.Content.ReadAsStringAsync();
  1305. }
  1306. }
  1307. [Serializable]
  1308. record LoginResponse
  1309. {
  1310. // Map `Token` to `token` when serializing
  1311. public string Token { get; set; } = "";
  1312. }
  1313. }