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
2 years ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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<int> 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 = "AKIDvhEVXN4cv0ugIlFYiniV6Wk1McfkplYA"; //"云 API 密钥 SecretId";
  381. string secretKey = "YyGLGCJG4f5VsEUddnz9JSRPSSK8sYBo"; //"云 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<int> LoginToEEsast(HttpClient client, string useremail, string password)
  1009. {
  1010. string token = "";
  1011. try
  1012. {
  1013. using (var response = await client.PostAsync("https://api.eesast.com/users/login", JsonContent.Create(new
  1014. {
  1015. email = useremail,
  1016. password = password,
  1017. })))
  1018. {
  1019. switch (response.StatusCode)
  1020. {
  1021. case System.Net.HttpStatusCode.OK:
  1022. Console.WriteLine("Success login");
  1023. token = (System.Text.Json.JsonSerializer.Deserialize(await response.Content.ReadAsStreamAsync(), typeof(LoginResponse), new JsonSerializerOptions()
  1024. {
  1025. PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
  1026. }) as LoginResponse)
  1027. ?.Token ??
  1028. throw new Exception("no token!");
  1029. logintoken = token;
  1030. SaveToken();
  1031. var info = JsonConvert.DeserializeObject<Dictionary<string, string>>(await response.Content.ReadAsStringAsync());
  1032. Downloader.UserInfo._id = info["_id"];
  1033. Downloader.UserInfo.email = info["email"];
  1034. break;
  1035. default:
  1036. int code = ((int)response.StatusCode);
  1037. Console.WriteLine(code);
  1038. if (code == 401)
  1039. {
  1040. //Console.WriteLine("邮箱或密码错误!");
  1041. return -1;
  1042. }
  1043. break;
  1044. }
  1045. return 0;
  1046. }
  1047. }
  1048. catch
  1049. {
  1050. return -2;
  1051. }
  1052. }
  1053. /// <summary>
  1054. ///
  1055. /// </summary>
  1056. /// <param name="client">http client</param>
  1057. /// <param name="tarfile">代码源位置</param>
  1058. /// <param name="type">编程语言,格式为"cpp"或"python"</param>
  1059. /// <param name="plr">第x位玩家,格式为"player_x"</param>
  1060. /// <returns>-1:tokenFail;-2:FileNotExist;-3:CosFail;-4:loginTimeout;-5:Fail;-6:ReadFileFail;-7:networkError</returns>
  1061. async public Task<int> UploadFiles(HttpClient client, string tarfile, string type, string plr) //用来上传文件
  1062. {
  1063. if (!ReadToken()) //读取token失败
  1064. {
  1065. return -1;
  1066. }
  1067. try
  1068. {
  1069. string content;
  1070. client.DefaultRequestHeaders.Authorization = new("Bearer", logintoken);
  1071. if (!File.Exists(tarfile))
  1072. {
  1073. //Console.WriteLine("文件不存在!");
  1074. return -2;
  1075. }
  1076. using FileStream fs = new FileStream(tarfile, FileMode.Open, FileAccess.Read);
  1077. using StreamReader sr = new StreamReader(fs);
  1078. content = sr.ReadToEnd();
  1079. string targetUrl = $"https://api.eesast.com/static/player?team_id={await GetTeamId()}";
  1080. using (var response = await client.GetAsync(targetUrl))
  1081. {
  1082. switch (response.StatusCode)
  1083. {
  1084. case System.Net.HttpStatusCode.OK:
  1085. var res = JsonConvert.DeserializeObject<Dictionary<string, string>>(await response.Content.ReadAsStringAsync());
  1086. string appid = "1255334966"; // 设置腾讯云账户的账户标识(APPID)
  1087. string region = "ap-beijing"; // 设置一个默认的存储桶地域
  1088. CosXmlConfig config = new CosXmlConfig.Builder()
  1089. .IsHttps(true) // 设置默认 HTTPS 请求
  1090. .SetAppid(appid) // 设置腾讯云账户的账户标识 APPID
  1091. .SetRegion(region) // 设置一个默认的存储桶地域
  1092. .SetDebugLog(true) // 显示日志
  1093. .Build(); // 创建 CosXmlConfig 对象
  1094. string tmpSecretId = res["TmpSecretId"]; //"临时密钥 SecretId";
  1095. string tmpSecretKey = res["TmpSecretKey"]; //"临时密钥 SecretKey";
  1096. string tmpToken = res["SecurityToken"]; //"临时密钥 token";
  1097. long tmpExpiredTime = Convert.ToInt64(res["ExpiredTime"]);//临时密钥有效截止时间,精确到秒
  1098. QCloudCredentialProvider cosCredentialProvider = new DefaultSessionQCloudCredentialProvider(
  1099. tmpSecretId, tmpSecretKey, tmpExpiredTime, tmpToken
  1100. );
  1101. // 初始化 CosXmlServer
  1102. CosXmlServer cosXml = new CosXmlServer(config, cosCredentialProvider);
  1103. // 初始化 TransferConfig
  1104. TransferConfig transferConfig = new TransferConfig();
  1105. // 初始化 TransferManager
  1106. TransferManager transferManager = new TransferManager(cosXml, transferConfig);
  1107. string bucket = "eesast-1255334966"; //存储桶,格式:BucketName-APPID
  1108. string cosPath = $"/THUAI6/{GetTeamId()}/{type}/{plr}"; //对象在存储桶中的位置标识符,即称对象键
  1109. string srcPath = tarfile;//本地文件绝对路径
  1110. // 上传对象
  1111. COSXMLUploadTask uploadTask = new COSXMLUploadTask(bucket, cosPath);
  1112. uploadTask.SetSrcPath(srcPath);
  1113. uploadTask.progressCallback = delegate (long completed, long total)
  1114. {
  1115. Console.WriteLine(string.Format("progress = {0:##.##}%", completed * 100.0 / total));
  1116. };
  1117. try
  1118. {
  1119. COSXMLUploadTask.UploadTaskResult result = await transferManager.UploadAsync(uploadTask);
  1120. Console.WriteLine(result.GetResultInfo());
  1121. string eTag = result.eTag;
  1122. //到这里应该是成功了,但是因为我没有试过,也不知道具体情况,可能还要根据result的内容判断
  1123. }
  1124. catch (Exception)
  1125. {
  1126. return -3;
  1127. }
  1128. break;
  1129. case System.Net.HttpStatusCode.Unauthorized:
  1130. //Console.WriteLine("您未登录或登录过期,请先登录");
  1131. return -4;
  1132. default:
  1133. //Console.WriteLine("上传失败!");
  1134. return -5;
  1135. }
  1136. }
  1137. }
  1138. catch (IOException)
  1139. {
  1140. //Console.WriteLine("文件读取错误!请检查文件是否被其它应用占用!");
  1141. return -6;
  1142. }
  1143. catch
  1144. {
  1145. //Console.WriteLine("请求错误!请检查网络连接!");
  1146. return -7;
  1147. }
  1148. return 0;
  1149. }
  1150. async public Task UserDetails(HttpClient client) // 用来测试访问网站
  1151. {
  1152. if (!ReadToken()) // 读取token失败
  1153. {
  1154. return;
  1155. }
  1156. try
  1157. {
  1158. client.DefaultRequestHeaders.Authorization = new("Bearer", logintoken);
  1159. Console.WriteLine(logintoken);
  1160. using (var response = await client.GetAsync("https://api.eesast.com/application/info")) // JsonContent.Create(new
  1161. //{
  1162. //})))
  1163. {
  1164. switch (response.StatusCode)
  1165. {
  1166. case System.Net.HttpStatusCode.OK:
  1167. Console.WriteLine("Require OK");
  1168. Console.WriteLine(await response.Content.ReadAsStringAsync());
  1169. break;
  1170. default:
  1171. int code = ((int)response.StatusCode);
  1172. if (code == 401)
  1173. {
  1174. Console.WriteLine("您未登录或登录过期,请先登录");
  1175. }
  1176. return;
  1177. }
  1178. }
  1179. }
  1180. catch
  1181. {
  1182. Console.WriteLine("请求错误!请检查网络连接!");
  1183. }
  1184. }
  1185. public void SaveToken() // 保存token
  1186. {
  1187. string savepath = System.IO.Path.Combine(Data.dataPath, "THUAI6.json");
  1188. try
  1189. {
  1190. string json;
  1191. Dictionary<string, string> dict = new Dictionary<string, string>();
  1192. using FileStream fs = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  1193. using (StreamReader r = new StreamReader(fs))
  1194. {
  1195. json = r.ReadToEnd();
  1196. if (json == null || json == "")
  1197. {
  1198. json += @"{""THUAI6""" + ":" + @"""2023""}";
  1199. }
  1200. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  1201. if (dict.ContainsKey("token"))
  1202. {
  1203. dict.Remove("token");
  1204. }
  1205. dict?.Add("token", logintoken);
  1206. }
  1207. using FileStream fs2 = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  1208. using StreamWriter sw = new StreamWriter(fs2);
  1209. fs2.SetLength(0);
  1210. sw.Write(JsonConvert.SerializeObject(dict)); //将token写入文件
  1211. }
  1212. catch (DirectoryNotFoundException)
  1213. {
  1214. Console.WriteLine("保存token时未找到下载器地址!请检查下载器是否被移动!");
  1215. }
  1216. catch (PathTooLongException)
  1217. {
  1218. Console.WriteLine("下载器的路径名太长!请尝试移动下载器!");
  1219. }
  1220. catch (ArgumentNullException)
  1221. {
  1222. Console.WriteLine("下载器路径初始化失败!");
  1223. }
  1224. catch (IOException)
  1225. {
  1226. Console.WriteLine("写入token.dat发生冲突!请检查token.dat是否被其它程序占用!");
  1227. }
  1228. }
  1229. public bool ReadToken() // 读取token
  1230. {
  1231. try
  1232. {
  1233. string json;
  1234. Dictionary<string, string> dict = new Dictionary<string, string>();
  1235. string savepath = System.IO.Path.Combine(Data.dataPath, "THUAI6.json");
  1236. using FileStream fs = new FileStream(savepath, FileMode.Open, FileAccess.Read);
  1237. using StreamReader sr = new StreamReader(fs);
  1238. json = sr.ReadToEnd();
  1239. if (json == null || json == "")
  1240. {
  1241. json += @"{""THUAI6""" + ":" + @"""2023""}";
  1242. }
  1243. dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
  1244. if (!dict.ContainsKey("token"))
  1245. {
  1246. return false;
  1247. }
  1248. else
  1249. {
  1250. logintoken = dict["token"];
  1251. return true;
  1252. }
  1253. }
  1254. catch (DirectoryNotFoundException)
  1255. {
  1256. Console.WriteLine("读取token时未找到下载器地址!请检查下载器是否被移动!");
  1257. return false;
  1258. }
  1259. catch (FileNotFoundException)
  1260. {
  1261. //没有登陆
  1262. Console.WriteLine("请先登录!");
  1263. return false;
  1264. }
  1265. catch (PathTooLongException)
  1266. {
  1267. Console.WriteLine("下载器的路径名太长!请尝试移动下载器!");
  1268. return false;
  1269. }
  1270. catch (ArgumentNullException)
  1271. {
  1272. Console.WriteLine("下载器路径初始化失败!");
  1273. return false;
  1274. }
  1275. catch (IOException)
  1276. {
  1277. Console.WriteLine("写入token.dat发生冲突!请检查token.dat是否被其它程序占用!");
  1278. return false;
  1279. }
  1280. }
  1281. async public Task<string> GetTeamId()
  1282. {
  1283. var client = new HttpClient();
  1284. var request = new HttpRequestMessage(HttpMethod.Post, "https://api.eesast.com/dev/v1/graphql");
  1285. request.Headers.Add("x-hasura-admin-secret", "hasuraDevAdminSecret");
  1286. //var content = new StringContent($@"
  1287. // {{
  1288. // ""query"": ""query MyQuery {{contest_team_member(where: {{user_id: {{_eq: \""{Downloader.UserInfo._id}\""}}}}) {{ team_id }}}}"",
  1289. // ""variables"": {{}},
  1290. // }}", null, "application/json");
  1291. 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");
  1292. request.Content = content;
  1293. var response = await client.SendAsync(request);
  1294. response.EnsureSuccessStatusCode();
  1295. var info = await response.Content.ReadAsStringAsync();
  1296. var s1 = JsonConvert.DeserializeObject<Dictionary<string, object>>(info)["data"];
  1297. var s2 = JsonConvert.DeserializeObject<Dictionary<string, List<object>>>(s1.ToString())["contest_team_member"];
  1298. var sres = JsonConvert.DeserializeObject<Dictionary<string, string>>(s2[0].ToString())["team_id"];
  1299. return sres;
  1300. }
  1301. async public Task<string> GetUserId(string learnNumber)
  1302. {
  1303. var client = new HttpClient();
  1304. var request = new HttpRequestMessage(HttpMethod.Post, "https://api.eesast.com/dev/v1/graphql");
  1305. request.Headers.Add("x-hasura-admin-secret", "hasuraDevAdminSecret");
  1306. var content = new StringContent("{\"query\":\"query MyQuery {\r\n user(where: {id: {_eq: \""
  1307. + learnNumber + "\"}}) {\r\n _id\r\n }\r\n}\r\n\",\"variables\":{}}", null, "application/json");
  1308. request.Content = content;
  1309. var response = await client.SendAsync(request);
  1310. response.EnsureSuccessStatusCode();
  1311. return await response.Content.ReadAsStringAsync();
  1312. }
  1313. }
  1314. [Serializable]
  1315. record LoginResponse
  1316. {
  1317. // Map `Token` to `token` when serializing
  1318. public string Token { get; set; } = "";
  1319. }
  1320. }