cgmsv引擎论坛

 找回密码
 注册/Sign up
搜索
查看: 1978|回复: 0

[源码] 魔力寶貝台服更新程式Delphi源代碼

[复制链接]

Rank: 1

发表于 2020-11-14 20:12:14 | 显示全部楼层 |阅读模式
  1. unit Unit1;
  2. interface
  3. uses
  4.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  5.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IniFiles, Vcl.StdCtrls, IdBaseComponent,
  6.   IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StrUtils, IdHashCRC,
  7.   Vcl.ComCtrls;
  8. type
  9.   TForm1 = class(TForm)
  10.     IdHTTP1: TIdHTTP;
  11.     Memo1: TMemo;
  12.     ProgressBar1: TProgressBar;
  13.     procedure FormCreate(Sender: TObject);
  14.   private
  15.     function GetFile_CRC(const iFileName: String): String;
  16.     { Private declarations }
  17.   public
  18.     { Public declarations }
  19.   end;
  20. var
  21.   Form1: TForm1;
  22. implementation
  23. {$R *.dfm}
  24. type
  25.   inifmt = record
  26.     path: String;
  27.     url: String;
  28.     filename: String;
  29.     size: String;
  30.     crc: String;
  31.     method: String;
  32.   end;
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. Var
  35.   RealCRC, BufferCRC: String;
  36.   myinifile:Tinifile;
  37.   Section: TStrings; // 存放ini中的節點資料 [exe] [file_1] [file_2] 這類節點標記
  38.   Path: String;
  39.   Temp, ScrCRC: inifmt;
  40.   i: Integer;
  41.   Buffer: TFileStream;
  42.   HttpClient: TIdHttp;
  43.   UpdateFileName: String;
  44.   ScriptCRC, NewScript: TMemoryStream;
  45. begin
  46.   Memo1.Clear;
  47.   ScriptCRC:= TMemoryStream.Create;
  48.   HttpClient:= TIdHttp.Create(nil);
  49.   HttpClient.Get('http://patch.joypark.com.tw/cg_test/script_CRC.ini', ScriptCRC);
  50.   ScriptCRC.Seek(0, 0);
  51.   ScriptCRC.SaveToFile('Script_CRC.ini');
  52.   ScriptCRC.Free;
  53.   HttpClient.Free;
  54.   Path:= Extractfilepath(Application.ExeName) + 'Script_CRC.ini';
  55.   myinifile:= Tinifile.create(Path);
  56.   BufferCRC:= myinifile.ReadString('CRC', 'crc','');
  57.   myinifile.Free;
  58.   RealCRC:= GetFile_CRC(Extractfilepath(Application.ExeName) + 'script.ini');
  59.   if RealCRC = BufferCRC then
  60.   begin
  61.     Application.Terminate; // CRC值相同退出流程
  62.     Exit;
  63.   end;
  64.   NewScript:= TMemoryStream.Create;
  65.   HttpClient:= TIdHttp.Create(nil);
  66.   HttpClient.Get('http://patch.joypark.com.tw/cg_test/script.ini', NewScript);
  67.   NewScript.SaveToFile('script.ini');
  68.   NewScript.Free;
  69.   Path:= Extractfilepath(Application.ExeName) + 'script.ini';
  70.   Section := TStringList.Create;
  71.   myinifile:= Tinifile.create(Path);
  72.   Section.Clear;
  73.   Section.LoadFromFile(Path);
  74.   myinifile.ReadSections(Section);
  75. //  Memo1.Lines.Text:= Section.Text;
  76.   progressbar1.Max:= Section.Count;
  77.   for i := 1 to Section.Count-1 do
  78.     begin
  79.       Temp.path:= myinifile.ReadString(Section[i], 'path', '');
  80.       Temp.filename:= myinifile.ReadString(Section[i], 'filename', '');
  81.       Temp.url:= myinifile.ReadString(Section[i], 'url', '');
  82.       Temp.size:= myinifile.ReadString(Section[i], 'size', '');
  83.       Temp.crc:= myinifile.ReadString(Section[i], 'crc', '');
  84.       Temp.method:= myinifile.ReadString(Section[i], 'method', '');
  85.       if (Temp.method = '') or (Temp.method = 'replace') then
  86.         begin
  87.           if not (FileExists(ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename)) then
  88.             continue; // 如果文件不存在跳過該文件
  89.           Buffer := TFileStream.Create(
  90.           ExtractFilePath(Application.ExeName) + '\' + Temp.path + Temp.filename,
  91.           fmOpenWrite or fmShareDenyNone);
  92.             try
  93.               HttpClient := TIdHttp.Create(nil);
  94.               HttpClient.Get(Temp.url, Buffer); // wait until it is done
  95.             finally
  96.               HttpClient.Free;
  97.               Buffer.Free;
  98.             end;
  99.           end
  100.        else if Temp.method = 'append' then
  101.          begin
  102.           if not (FileExists(ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename)) then
  103.               continue; // 如果文件不存在跳過該文件
  104.             Buffer := TFileStream.Create(
  105.               ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename, +
  106.               fmOpenWrite or fmShareDenyNone);
  107.             Buffer.Seek(0, soFromEnd); // append模式
  108.             try
  109.               HttpClient := TIdHttp.Create(nil);
  110.               HttpClient.Get(Temp.url, Buffer);
  111.             finally
  112.               HttpClient.Free;
  113.               Buffer.Free;
  114.               UpdateFileName:= RightStr(Temp.url, Length(Temp.filename));
  115.               RenameFile(ExtractFilePath(Application.ExeName) + Temp.path + Temp.filename,
  116.                 PCHAR(ExtractFilePath(Application.ExeName) + Temp.path + UpdateFileName));
  117.             end;
  118.           end;
  119.           Memo1.Lines.Add('更新文件: ' + ExtractFilePath(Application.ExeName) + Temp.path + UpdateFileName);
  120.           ProgressBar1.Position:= i + 1;
  121.     end;
  122.   myinifile.Free;
  123.   Section.Free;
  124. end;
  125. function TForm1.GetFile_CRC(const iFileName: String): String;
  126.   var
  127.   crc: TIdHashCRC32;
  128.   fileStream: TMemoryStream;
  129. begin
  130.   crc:= TIdHashCRC32.Create;
  131.   fileStream:= TMemoryStream.Create;
  132.   try
  133.     fileStream.LoadFromFile(iFileName);
  134.     Result:= crc.HashStreamAsHex(fileStream);
  135.   finally
  136.     fileStream.Free;
  137.     crc.Free;
  138.   end;
  139. end;
  140. end.
复制代码


评分

参与人数 1金币 +2 收起 理由
Zack + 2 原创技术贴

查看全部评分

您需要登录后才可以回帖 登录 | 注册/Sign up

本版积分规则

手机版|cgmsv引擎论坛

GMT+8, 2024-11-21 21:51 , Processed in 0.202658 second(s), 12 queries , Gzip On, MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表