Subversion Repositories spacemission

Rev

Rev 74 | Rev 80 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit Global;
  2.  
  3. interface
  4.  
  5. const
  6.   ProgramVersion = '1.2.1';
  7.   LevEditRasterW = 48;
  8.   LevEditRasterH = 32;
  9.   MaxPossibleEnemyLives = 999;
  10.   MaxPossibleLevels = 999;
  11.   RegistrySettingsKey = 'SOFTWARE\ViaThinkSoft\SpaceMission\Settings'; // do not localize
  12.   MusicSettingKey = 'Music'; // do not localize
  13.   SoundSettingKey = 'Sound'; // do not localize
  14.   SpeedSettingKey = 'Speed'; // do not localize
  15.   DefaultLevelLength = 1200;
  16.   StartLives = 6;
  17.   conleicht =  650 div 60; // 10
  18.   conmittel = 1000 div 60; // 16
  19.   conschwer = 1350 div 60; // 22
  20.   conmaster = 2000 div 60; // 33
  21.   DEFAULT_ANIMSPEED = 15/1000;
  22.   ADDITIONAL_ENEMIES_PER_LEVEL = 75; // Zufalls-Level
  23.   BossWidth = 4;
  24.   BossHeight = 2;
  25.   SpaceMissionExe = 'SpaceMission.exe'; // do not localize
  26.   LevEditExe = 'LevEdit.exe'; // do not localize
  27.  
  28. type
  29.   // DirectX\Music.dxm
  30.   TSpaceMissionMusicTrack = (
  31.     smmNone,
  32.     smmBoss,   // dxmusic.Midis[0]
  33.     smmGame,   // dxmusic.Midis[1]
  34.     smmScene,  // dxmusic.Midis[2]
  35.     smmTitle   // dxmusic.Midis[3]
  36.   );
  37.  
  38.   // DirectX\Graphics.dxg
  39.   TSpaceMissionGraphicSprite = (
  40.     smgNone,
  41.     smgEnemyDisk,         // ImageList.Items.Item[0]
  42.     smgEnemyAttacker,     // ImageList.Items.Item[1]
  43.     smgEnemyBoss,         // ImageList.Items.Item[2]
  44.     smgBounce,            // ImageList.Items.Item[3]
  45.     smgMachine,           // ImageList.Items.Item[4]
  46.     smgEnemyAttacker2,    // ImageList.Items.Item[5]
  47.     smgEnemyAttacker3,    // ImageList.Items.Item[6]
  48.     smgEnemyMeteor,       // ImageList.Items.Item[7]
  49.     smgBounce2,           // ImageList.Items.Item[8]
  50.     smgEnemyDisk2,        // ImageList.Items.Item[9]
  51.     smgLogo,              // ImageList.Items.Item[10]
  52.     smgExplosion,         // ImageList.Items.Item[11]
  53.     smgBackgroundPlanet1, // ImageList.Items.Item[12]
  54.     smgMatrix,            // ImageList.Items.Item[13]
  55.     smgStar1,             // ImageList.Items.Item[14]
  56.     smgStar2,             // ImageList.Items.Item[15]
  57.     smgStar3,             // ImageList.Items.Item[16]
  58.     smgBackgroundBlue,    // ImageList.Items.Item[17]
  59.     smgBackgroundRed,     // ImageList.Items.Item[18]
  60.     smgBackgroundYellow,  // ImageList.Items.Item[19]
  61.     smgHintergrundRot,    // ImageList.Items.Item[20]
  62.     smgItemMedikit        // ImageList.Items.Item[21]
  63.   );
  64.  
  65.   // DirectX\Sound.dxw
  66.   TSpaceMissionSound = (
  67.     smsNone,
  68.     smsSceneMov,      // WaveList.Items.Item[0]
  69.     smsExplosion,     // WaveList.Items.Item[1]
  70.     smsHit,           // WaveList.Items.Item[2]
  71.     smsShoot,         // WaveList.Items.Item[3]
  72.     smsItemCollected  // WaveList.Items.Item[4]
  73.   );
  74.  
  75. function OwnDirectory: string;
  76.  
  77. const
  78.   FOLDERID_SavedGames: TGuid = '{4C5C32FF-BB9D-43b0-B5B4-2D72E54EAAA4}'; // do not localize
  79.  
  80. // Useful functions
  81. function GetKnownFolderPath(const rfid: TGUID): string;
  82. function KillTask(ExeFileName: string): Integer;
  83. procedure CheckForUpdates(ViaThinkSoftProjectName: string; AVersion: string);
  84.  
  85. implementation
  86.  
  87. uses
  88.   Windows, SysUtils, Dialogs, ActiveX, ShlObj, TlHelp32, wininet, Forms, ShellAPI,
  89.   System.UITypes;
  90.  
  91. function GetKnownFolderPath(const rfid: TGUID): string;
  92. var
  93.   OutPath: PWideChar;
  94. begin
  95.   // https://www.delphipraxis.net/135471-unit-zur-verwendung-von-shgetknownfolderpath.html
  96.   if ShGetKnownFolderPath(rfid, 0, 0, OutPath) {>= 0} = S_OK then
  97.   begin
  98.     Result := OutPath;
  99.     // From MSDN
  100.     // ppszPath [out]
  101.     // Type: PWSTR*
  102.     // When this method returns, contains the address of a pointer to a null-terminated Unicode string that specifies the path of the known folder
  103.     // The calling process is responsible for freeing this resource once it is no longer needed by calling CoTaskMemFree.
  104.     // The returned path does not include a trailing backslash. For example, "C:\Users" is returned rather than "C:\Users\".
  105.     CoTaskMemFree(OutPath);
  106.   end
  107.   else
  108.   begin
  109.     Result := '';
  110.   end;
  111. end;
  112.  
  113. // https://stackoverflow.com/questions/43774320/how-to-kill-a-process-by-name
  114. function KillTask(ExeFileName: string): Integer;
  115. const
  116.   PROCESS_TERMINATE = $0001;
  117. var
  118.   ContinueLoop: BOOL;
  119.   FSnapshotHandle: THandle;
  120.   FProcessEntry32: TProcessEntry32;
  121. begin
  122.   Result := 0;
  123.   FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  124.   FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  125.   ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  126.  
  127.   while Integer(ContinueLoop) <> 0 do
  128.   begin
  129.     if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
  130.       UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
  131.       UpperCase(ExeFileName))) then
  132.       Result := Integer(TerminateProcess(
  133.                         OpenProcess(PROCESS_TERMINATE,
  134.                                     BOOL(0),
  135.                                     FProcessEntry32.th32ProcessID),
  136.                                     0));
  137.      ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  138.   end;
  139.   CloseHandle(FSnapshotHandle);
  140. end;
  141.  
  142. function OwnDirectory: string;
  143. begin
  144.   result := extractfilepath(paramstr(0));
  145. end;
  146.  
  147. // https://www.delphipraxis.net/post43515.html , fixed , works for Delphi 12 Athens
  148. function GetHTML(AUrl: string): RawByteString;
  149. var
  150.   databuffer : array[0..4095] of ansichar; // SIC! ansichar!
  151.   ResStr : ansistring; // SIC! ansistring
  152.   hSession, hfile: hInternet;
  153.   dwindex,dwcodelen,dwread,dwNumber: cardinal;
  154.   dwcode : array[1..20] of char;
  155.   res    : pchar;
  156.   Str    : pansichar; // SIC! pansichar
  157. begin
  158.   ResStr:='';
  159.   if (system.pos('http://',lowercase(AUrl))=0) and // do not localize
  160.      (system.pos('https://',lowercase(AUrl))=0) then // do not localize
  161.      AUrl:='http://'+AUrl; // do not localize
  162.  
  163.   // Hinzugefügt
  164.   if Assigned(Application) then Application.ProcessMessages;
  165.  
  166.   hSession:=InternetOpen('InetURL:/1.0', // do not localize
  167.                          INTERNET_OPEN_TYPE_PRECONFIG,
  168.                          nil,
  169.                          nil,
  170.                          0);
  171.   if assigned(hsession) then
  172.   begin
  173.     // Hinzugefügt
  174.     if Assigned(Application) then application.ProcessMessages;
  175.  
  176.     hfile:=InternetOpenUrl(
  177.            hsession,
  178.            pchar(AUrl),
  179.            nil,
  180.            0,
  181.            INTERNET_FLAG_RELOAD,
  182.            0);
  183.     dwIndex  := 0;
  184.     dwCodeLen := 10;
  185.  
  186.     // Hinzugefügt
  187.     if Assigned(Application) then application.ProcessMessages;
  188.  
  189.     HttpQueryInfo(hfile,
  190.                   HTTP_QUERY_STATUS_CODE,
  191.                   @dwcode,
  192.                   dwcodeLen,
  193.                   dwIndex);
  194.     res := pchar(@dwcode);
  195.     dwNumber := sizeof(databuffer)-1;
  196.     if (res ='200') or (res = '302') then // do not localize
  197.     begin
  198.       while (InternetReadfile(hfile,
  199.                               @databuffer,
  200.                               dwNumber,
  201.                               DwRead)) do
  202.       begin
  203.  
  204.         // Hinzugefügt
  205.         if Assigned(Application) then application.ProcessMessages;
  206.  
  207.         if dwRead =0 then
  208.           break;
  209.         databuffer[dwread]:=#0;
  210.         Str := pansichar(@databuffer);
  211.         resStr := resStr + Str;
  212.       end;
  213.     end
  214.     else
  215.       ResStr := 'Status:'+AnsiString(res); // do not localize
  216.     if assigned(hfile) then
  217.       InternetCloseHandle(hfile);
  218.   end;
  219.  
  220.   // Hinzugefügt
  221.   if Assigned(Application) then application.ProcessMessages;
  222.  
  223.   InternetCloseHandle(hsession);
  224.   Result := resStr;
  225. end;
  226.  
  227. procedure CheckForUpdates(ViaThinkSoftProjectName: string; AVersion: string);
  228. resourcestring
  229.   SDownloadError = 'Ein Fehler ist aufgetreten. Wahrscheinlich ist keine Internetverbindung aufgebaut, oder der der ViaThinkSoft-Server vorübergehend offline.';
  230.   SNewProgramVersionAvailable = 'Eine neue Programmversion ist vorhanden. Möchten Sie diese jetzt herunterladen?';
  231.   SNoUpdateAvailable = 'Es ist keine neue Programmversion vorhanden.';
  232. var
  233.   cont: RawByteString;
  234. begin
  235.   cont := GetHTML('https://www.viathinksoft.de/update/?id='+ViaThinkSoftProjectName); // do not localize
  236.   if copy(cont, 0, 7) = 'Status:' then
  237.   begin
  238.     MessageDlg(SDownloadError, mtError, [mbOk], 0);
  239.   end
  240.   else
  241.   begin
  242.     if string(cont) <> AVersion then
  243.     begin
  244.       if MessageDlg(SNewProgramVersionAvailable, mtConfirmation, mbYesNoCancel, 0) = mrYes then
  245.         shellexecute(application.handle, 'open', pchar('https://www.viathinksoft.de/update/?id=@'+ViaThinkSoftProjectName), '', '', sw_normal); // do not localize
  246.     end
  247.     else
  248.     begin
  249.       MessageDlg(SNoUpdateAvailable, mtInformation, [mbOk], 0);
  250.     end;
  251.   end;
  252. end;
  253.  
  254. end.
  255.