C++ - L# Downloader


SUBMITTED BY: Guest

DATE: Dec. 6, 2014, 4:08 p.m.

FORMAT: C++

SIZE: 5.4 kB

HITS: 831

  1. //To be compiled using VS 2013
  2. #pragma warning(disable: 4996)
  3. #include <iostream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <windows.h>
  7. #include <fstream>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include <curl/curl.h>
  11. #undef max
  12. using namespace std;
  13. size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  14. size_t written = fwrite(ptr, size, nmemb, stream);
  15. return written;
  16. }
  17. int checkVersion()
  18. {
  19. const char* appdataPathc = getenv("appdata");
  20. string appdataPathz = appdataPathc;
  21. string appdataPath = appdataPathz + "\\LSharpDownloader\\version.txt";
  22. string appdataPathRemote = appdataPathz + "\\LSharpDownloader\\versiontmp.txt";
  23. string clientVersions;
  24. string remoteVersions;
  25. fstream versionFile;
  26. fstream remoteVersionFile;
  27. ofstream remoteVersionFileC(appdataPathRemote);
  28. remoteVersionFileC.close();
  29. int clientVersion;
  30. int remoteVersion;
  31. versionFile.open(appdataPath, ios::in | ios::out);
  32. if (versionFile.good())
  33. {
  34. getline(versionFile, clientVersions);
  35. clientVersion = atoi(clientVersions.c_str());
  36. versionFile.close();
  37. CURL *curl;
  38. FILE *fp;
  39. CURLcode res;
  40. char *url = "http://hehehemlsharp.cba.pl/version.txt";
  41. const char *outfilename = appdataPathRemote.c_str();
  42. curl = curl_easy_init();
  43. if (curl) {
  44. fp = fopen(outfilename, "wb");
  45. curl_easy_setopt(curl, CURLOPT_URL, url);
  46. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  47. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  48. res = curl_easy_perform(curl);
  49. curl_easy_cleanup(curl);
  50. fclose(fp);
  51. }
  52. remoteVersionFile.open(appdataPathRemote, ios::in | ios::out);
  53. if (remoteVersionFile.good())
  54. {
  55. getline(remoteVersionFile, remoteVersions);
  56. remoteVersion = atoi(remoteVersions.c_str());
  57. remoteVersionFile.close();
  58. if (remoteVersion > clientVersion)
  59. {
  60. remove(appdataPathRemote.c_str());
  61. return 1;
  62. }
  63. else
  64. {
  65. remove(appdataPathRemote.c_str());
  66. return 0;
  67. }
  68. }
  69. else
  70. {
  71. MessageBox(NULL, L"Błąd podczas pobierania wersji z pliku na serwerze: version.txt!", NULL, MB_OK | MB_ICONSTOP);
  72. abort();
  73. }
  74. }
  75. else
  76. {
  77. return 2;
  78. }
  79. }
  80. void readmeInit()
  81. {
  82. fstream readmeFileCreate;
  83. const char* appdataPathc = getenv("appdata");
  84. string appdataPathz = appdataPathc;
  85. string appdataPath = appdataPathz + "\\LSharpDownloader\\readme.txt";
  86. const char* readmeFileValue = "Ten plik zostal wygenerowany podczas uruchomienia L# Downloadera.\nProsze nie modyfikowac plikow znajdujacych sie wewnatrz folderu LSharpDownloader.";
  87. readmeFileCreate.open(appdataPath, ios::in | ios::out | ios::trunc);
  88. if (readmeFileCreate.good())
  89. {
  90. readmeFileCreate.write(readmeFileValue, 147);
  91. }
  92. else
  93. {
  94. MessageBox(NULL, L"Błąd podczas tworzenia pliku readme.txt!", NULL, MB_OK | MB_ICONSTOP);
  95. abort();
  96. }
  97. }
  98. int main()
  99. {
  100. system("if not exist %appdata%\\LSharpDownloader mkdir %appdata%\\LSharpDownloader");
  101. readmeInit();
  102. const char* appdataPathc = getenv("appdata");
  103. string appdataPathz = appdataPathc;
  104. string appdataPath = appdataPathz + "\\LSharpDownloader\\leaguesharp.rar";
  105. string appdataPath2 = appdataPathz + "\\LSharpDownloader\\version.txt";
  106. int version = checkVersion();
  107. if (version == 1)
  108. {
  109. cout << "L# Downloader. Pobieranie najnowszej wersji..." << endl;
  110. CURL *curl;
  111. FILE *fp;
  112. CURLcode res;
  113. char *url = "http://hehehemlsharp.cba.pl/leaguesharp.rar";
  114. const char *outfilename = appdataPath.c_str();
  115. curl = curl_easy_init();
  116. if (curl) {
  117. fp = fopen(outfilename, "wb");
  118. curl_easy_setopt(curl, CURLOPT_URL, url);
  119. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  120. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  121. res = curl_easy_perform(curl);
  122. curl_easy_cleanup(curl);
  123. fclose(fp);
  124. }
  125. CURL *curl2;
  126. FILE *fp2;
  127. CURLcode res2;
  128. char *url2 = "http://hehehemlsharp.cba.pl/version.txt";
  129. const char *outfilename2 = appdataPath2.c_str();
  130. curl2 = curl_easy_init();
  131. if (curl2) {
  132. fp2 = fopen(outfilename2, "wb");
  133. curl_easy_setopt(curl2, CURLOPT_URL, url2);
  134. curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, write_data);
  135. curl_easy_setopt(curl2, CURLOPT_WRITEDATA, fp2);
  136. res2 = curl_easy_perform(curl2);
  137. curl_easy_cleanup(curl2);
  138. fclose(fp2);
  139. }
  140. cout << "Pobrano najnowsza wersje. Nacisnij dowolny klawisz, by zamknac program.\nL# Downloader, napisany przez mistake010 dla HeheheM'a." << endl;
  141. system("explorer %appdata%\\LSharpDownloader");
  142. return 0;
  143. }
  144. else if (version == 0)
  145. {
  146. cout << "Nie ma obecnie nowszej wersji na serwerze. Nacisnij dowolny klawisz, by zamknac program.\nL# Downloader, napisany przez mistake010 dla HeheheM'a.";
  147. }
  148. else if (version == 2)
  149. {
  150. ofstream versionFileCreate(appdataPath2);
  151. versionFileCreate.close();
  152. system("cls");
  153. main();
  154. }
  155. else
  156. {
  157. cout << "Nieznany blad." << endl;
  158. }
  159. _getch();
  160. return 0;
  161. }

comments powered by Disqus