y_version.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*----------------------------------------------------------------------------*-
  2. =====================
  3. YSI - Version Check
  4. =====================
  5. Description:
  6. Checks online to see if there is a newer version of YSI available.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the SA:MP script information include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2008
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk
  23. Thanks:
  24. Peter, Cam - Support.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN.
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 0.2
  43. Changelog:
  44. 26/10/10:
  45. Added changelog capabilities.
  46. 22/10/10:
  47. First version.
  48. Functions:
  49. Public:
  50. -
  51. Core:
  52. -
  53. Stock:
  54. -
  55. Static:
  56. -
  57. Inline:
  58. -
  59. API:
  60. -
  61. Hooks:
  62. OnGameModeInit
  63. Callbacks:
  64. -
  65. Definitions:
  66. -
  67. Enums:
  68. -
  69. Macros:
  70. -
  71. Tags:
  72. -
  73. Variables:
  74. Global:
  75. -
  76. Static:
  77. -
  78. Commands:
  79. -
  80. Compile options:
  81. -
  82. Operators:
  83. -
  84. Natives:
  85. -
  86. -*----------------------------------------------------------------------------*/
  87. #include <a_samp>
  88. #include <a_http>
  89. #define YSI_VERSION_RESPO 2
  90. #define YSI_VERSION_MAJOR 1
  91. #define YSI_VERSION_MINOR 03
  92. #define YSI_VERSION_BUILD 0011
  93. #define YSI_VERSION #YSI_VERSION_MAJOR "." #YSI_VERSION_MINOR "." #YSI_VERSION_BUILD
  94. forward YVers_Callback(index, code, data[]);
  95. /*----------------------------------------------------------------------------*-
  96. Hook:
  97. OnScriptInit
  98. Notes:
  99. Constructor. Checks to see if there is a new version available. This code
  100. can not use ANY of the rest of YSI as it needs to be included by everything
  101. first.
  102. -*----------------------------------------------------------------------------*/
  103. #if defined FILTERSCRIPT
  104. public OnFilterScriptInit()
  105. #else
  106. public OnGameModeInit()
  107. #endif
  108. {
  109. print(" ");
  110. print(" ======================================= ");
  111. print(" | | ");
  112. print(" | YSI version " YSI_VERSION " | ");
  113. print(" | By Alex \"Y_Less\" Cole | ");
  114. print(" | | ");
  115. print(" | Checking the latest YSI version.. | ");
  116. print(" | | ");
  117. print(" ======================================= ");
  118. print(" ");
  119. // Call my server to check the current public YSI version. v is the current
  120. // version and c is the version of data response which this code can parse.
  121. // Note that the response data SHOULD be backward compatible, but may not
  122. // always be - hence the accept parameter. This will never send any data
  123. // except the current version for targeted replies (e.g. to ignore minor
  124. // updates which aren't critical). It MAY in the future send what libraries
  125. // are in use so that it only tells you to upgrade if the libraries you are
  126. // using have changed, but that will take more work and I'm not going to do
  127. // that for now (I'm not entirely sure exactly how to do it (though I have
  128. // an idea - note to self: chain callbacks from repeated inclusions of this
  129. // file in the same way as ALS then call them BEFORE sending the HTTP)).
  130. // Note that due to the way the internet works the server will know the IP
  131. // of the server which sent the request, but the ENTIRE current contents of
  132. // the remote page are (note: I won't update this comment every time the
  133. // version updates, but that's the gist of it):
  134. //
  135. // 1
  136. // 1.01.0000
  137. //
  138. // This remote script has now been updated to include changelog information.
  139. HTTP(0, HTTP_GET, "ysi-version.y-less.com/index.php?c=" #YSI_VERSION_RESPO "&v=" YSI_VERSION, "", "YVers_Callback");
  140. CallLocalFunction("YVers_OnScriptInit", "");
  141. return 1;
  142. }
  143. #if defined FILTERSCRIPT
  144. #if defined _ALS_OnFilterScriptInit
  145. #undef OnFilterScriptInit
  146. #else
  147. #define _ALS_OnFilterScriptInit
  148. #endif
  149. #define OnFilterScriptInit YVers_OnScriptInit
  150. #else
  151. #if defined _ALS_OnGameModeInit
  152. #undef OnGameModeInit
  153. #else
  154. #define _ALS_OnGameModeInit
  155. #endif
  156. #define OnGameModeInit YVers_OnScriptInit
  157. #endif
  158. forward YVers_OnScriptInit();
  159. /*----------------------------------------------------------------------------*-
  160. Function:
  161. YVers_Callback
  162. Params:
  163. index - Not used.
  164. code - Response code from the server.
  165. data[] - HTTP data sent from the server.
  166. Return:
  167. -
  168. Notes:
  169. This is called when my server responds to the HTTP request sent above (or
  170. when it doesn't). This prints information on the current and any future
  171. versions of YSI. Note that it only does a strcmp to determine if the
  172. version is newer - people can't have versions newer than the latest, only
  173. older or equal (unless they play with the version numbers, but then that's
  174. their own fault).
  175. -*----------------------------------------------------------------------------*/
  176. public YVers_Callback(index, code, data[])
  177. {
  178. print(" ");
  179. if (code == 200)
  180. {
  181. // Got the remote page.
  182. // The first line is the version data version. This should be forward
  183. // compatible, so new data is always added to the end of the file.
  184. // Skip the first line - contains the response version.
  185. new
  186. pos = strfind(data, "\n") + 1;
  187. if (strcmp(data[pos], YSI_VERSION, false, 9))
  188. {
  189. //data[pos + 9] = '\0';
  190. print(" ========================================== ");
  191. print(" | | ");
  192. printf(" | A new version (v%.9s) of YSI is | ", data[pos]);
  193. print(" | available from: | ");
  194. print(" | | ");
  195. print(" | www.y-less.com/YSI/YSI_1.0.zip | ");
  196. //printf("data[0]: %c", data[0]);
  197. if (data[0] == '2')
  198. {
  199. print(" | | ");
  200. print(" | Changelog: | ");
  201. // Print the changelog.
  202. new
  203. last = pos + 13;
  204. for ( ; ; )
  205. {
  206. pos = strfind(data[last], "\n", false);
  207. //printf("%d %d %s", last, pos, data[last]);
  208. if (pos == -1)
  209. {
  210. // To break out in the middle of a loop.
  211. break;
  212. }
  213. pos += last;
  214. data[pos - 1] = '\0';
  215. printf(" | %38s | ", data[last]);
  216. last = pos + 1;
  217. }
  218. }
  219. print(" | | ");
  220. print(" ========================================== ");
  221. }
  222. else
  223. {
  224. print(" ====================================== ");
  225. print(" | | ");
  226. print(" | Congratulations! You are running | ");
  227. print(" | the latest version of YSI! | ");
  228. print(" | | ");
  229. print(" ====================================== ");
  230. }
  231. }
  232. else
  233. {
  234. // Didn't get the remote page.
  235. print(" ======================================= ");
  236. print(" | | ");
  237. print(" | Error: Could not connect to YSI | ");
  238. printf(" | update server (response was %03d). | ", code);
  239. print(" | | ");
  240. print(" ======================================= ");
  241. }
  242. print(" ");
  243. }