Patcher.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /******************************************************************************
  2. Use 'Patcher' for patching files on local device,
  3. according to server files uploaded using "Uploader" tool.
  4. /******************************************************************************/
  5. const_mem_addr struct Patcher // class for automatic downloading file updates from a http server !! must be stored in constant memory address !!
  6. {
  7. struct LocalFile // use this class to describe an element stored on local device
  8. {
  9. enum TYPE : Byte
  10. {
  11. PAK_FILE , // PakFile ( file inside Pak archive)
  12. SYSTEM_FILE, // system file (normal file on your drive )
  13. SYSTEM_DIR , // system directory (normal folder on your drive )
  14. };
  15. TYPE type ; // specifies how the file is stored on the local device
  16. Str full_name ; // file full name (path+name) relative to your data storage folder (don't include the drive here, instead, use path relative to the folder which was used for uploading your files)
  17. Long file_size ; // file size in bytes (leave 0 for folders)
  18. UInt xxHash64_32 ; // file hash (this is optional, it can be left at 0 which will disable checking for hash differences, however when enabled, it can provide more reliable detecting differences between file versions)
  19. DateTime modify_time_utc; // file modification time in UTC time zone
  20. LocalFile& set(C Str &full_name, C FileInfo &fi); // set from 'full_name' and 'FileInfo'
  21. LocalFile() {type=PAK_FILE; file_size=0; xxHash64_32=0; modify_time_utc.zero();}
  22. };
  23. struct Downloaded
  24. {
  25. Bool success ; // file downloaded successfully, if this is equal to false it means that the file was not found or it was different than expected
  26. FSTD_TYPE type ; // type of the element, can be FSTD_FILE, FSTD_DIR or FSTD_LINK
  27. Int index ; // file index in 'Patcher.index' Pak
  28. UInt xxHash64_32 ; // file data hash (can be 0 if not calculated)
  29. DateTime modify_time_utc; // file modification time in UTC time zone
  30. Str full_name ; // full name of downloaded file
  31. File data ; // file data
  32. Downloaded() {success=false; index=-1; xxHash64_32=0; type=FSTD_NONE; modify_time_utc.zero();}
  33. #if EE_PRIVATE
  34. void create (C Pak &pak, Int index, Download &download, Cipher *cipher);
  35. void createEmpty(C Pak &pak, Int index);
  36. void createFail (C Pak &pak, Int index);
  37. #endif
  38. };
  39. struct InstallerInfo
  40. {
  41. Int size ; // file size
  42. UInt xxHash64_32 ; // file data hash
  43. DateTime modify_time_utc; // file modification time in UTC time zone
  44. #if EE_PRIVATE
  45. void zero() {size=0; xxHash64_32=0; modify_time_utc.zero();}
  46. #endif
  47. InstallerInfo();
  48. };
  49. // local file list
  50. static void SetList(MemPtr<LocalFile> local_files, C Str &dir ); // automatically create 'local_files' list from 'dir' folder on your local device (this will process all system files and folders inside 'dir' and list them in 'local_files' container)
  51. static void SetList(MemPtr<LocalFile> local_files, C PakSet &paks); // automatically create 'local_files' list from 'paks' set of paks (this will process all pak files inside 'paks' and list them in 'local_files' container)
  52. // manage
  53. Patcher& create(C Str &http_dir, C Str &upload_name, Cipher *cipher=null); // create the patcher according to provided internet address, 'http_dir'=case-sensitive http address of the directory where files were uploaded (for example: "http://www.domain.com/download"), 'upload_name'=case-sensitive name of the upload (for example: "GameName"), 'cipher'=cipher keys used during upload, these parameters must match the ones you have provided in the Uploader tool
  54. // get
  55. // installer information
  56. Patcher& downloadInstallerInfo (); // initialize downloading of the installer information, downloading will begin on secondary thread
  57. DWNL_STATE installerInfoState(); // get state of installer information download, once it's equal to DWNL_DONE you can access it
  58. C InstallerInfo* installerInfo (); // get access to installer information (this will return a valid pointer only if 'installerInfoState' is equal to DWNL_DONE), null on fail
  59. // installer
  60. Patcher& downloadInstaller (); // initialize downloading of the installer, downloading will begin on secondary thread (this automatically initiates downloading of installer information if it wasn't previously downloaded)
  61. DWNL_STATE installerState (); // get state of installer download, once it's equal to DWNL_DONE you can access it
  62. Int installerProgress(); // get number of downloaded bytes for the installer
  63. C Mems<Byte>* installer (); // get access to raw bytes of installer .exe file which you can write on disk, quit current installer and run the updated version (this will return a valid pointer only if 'installerState' is equal to DWNL_DONE), null on fail
  64. // index
  65. Patcher& downloadIndex (); // initialize downloading of the index (list of all current files on the server), downloading will begin on secondary thread
  66. DWNL_STATE indexState(); // get state of index download, once it's equal to DWNL_DONE you can access its information
  67. C Pak* index (); // get access to the index of files on the server (this will return a valid pointer only if 'indexState' is equal to DWNL_DONE), null on fail
  68. // file
  69. Patcher& downloadFile(Int i ); // initialize downloading of i-th file in index from server, downloading will begin on secondary thread, this method should be called after downloading the index
  70. Bool getNextDownload(Downloaded &downloaded); // get next downloaded file, this method checks if there is a file fully downloaded, if there is one, it sets information about it into 'downloaded' parameter and returns true, if there is no file downloaded at this moment, then false is returned and given parameter is unmodified
  71. // stats
  72. Long progress ()C; // get number of bytes that have been already downloaded (this includes only files, this does not include installer info, installer, index and overhead)
  73. Long filesSize()C; // get number of bytes of all files requested to be downloaded (this includes files that have been downloaded and those that still need to be downloaded)
  74. Int filesLeft()C; // get number of files that are still waiting to be downloaded
  75. // compare
  76. Bool compare(C MemPtr<LocalFile> &local_files, MemPtr<Int> local_remove, MemPtr<Int> server_download); // compare list of files from server (index) with list of files on local device (local_files), 'local_remove'=list of indexes from 'local_files' that need to be removed (those files are not present in the server, therefore we should remove them), 'server_download'=list of indexes from "index->file" that need to be downloaded from the server (those files aren't present in the local device or are of different version, therefore we should download latest version and replace existing one if any), this method relies on 'index', so you should call it only after downloading the index using 'downloadIndex' method with 'indexState' returning DWNL_DONE, false on fail ('index' not yet available)
  77. static void Compare(C MemPtr<LocalFile> &src_files, C MemPtr<LocalFile> &dest_files, MemPtr<Int> dest_remove, MemPtr<Int> src_copy); // compare list of files from source ('src_files') with list of files from destination ('dest_files'), 'dest_remove'=list of indexes from 'dest_files' that need to be removed (those files are not present in the source, therefore we should remove them), 'src_copy'=list of indexes from 'src_files' that need to be copied from the source to the destination (those files aren't present in destination or are of different version, therefore we should copy latest version from the source and replace existing one in the destination, if any)
  78. Patcher& del(); // delete manually
  79. ~Patcher() {del();}
  80. Patcher();
  81. #if EE_PRIVATE
  82. Bool is () {return _http.is();}
  83. void zero ();
  84. void update();
  85. #endif
  86. private:
  87. struct FileDownload : Download
  88. {
  89. Int index;
  90. };
  91. Str _http, _name;
  92. Cipher *_cipher;
  93. Bool _pak_available, _inst_info_available, _inst_available;
  94. Download _pak_download , _inst_info_download , _inst_download ;
  95. FileDownload _file_download[6];
  96. Int _files_left;
  97. Long _bytes_downloaded;
  98. Pak _pak;
  99. InstallerInfo _inst_info;
  100. Mems<Byte> _inst;
  101. Memc<Int > _to_download;
  102. Memc<Downloaded> _downloaded;
  103. SyncLock _lock;
  104. Thread _thread;
  105. };
  106. /******************************************************************************/