helpers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. var networking = require("./networking");
  2. var logging = require("./logging");
  3. var config = require("./config");
  4. var cache = require("./cache");
  5. var skins = require("./skins");
  6. var renders = require("./renders");
  7. var fs = require("fs");
  8. // 0098cb60-fa8e-427c-b299-793cbd302c9a
  9. var valid_uuid = /^([0-9a-f-A-F-]{32,36}|[a-zA-Z0-9_]{1,16})$/; // uuid|username
  10. var hash_pattern = /[0-9a-f]+$/;
  11. // gets the hash from the textures.minecraft.net +url+
  12. function get_hash(url) {
  13. return hash_pattern.exec(url)[0].toLowerCase();
  14. }
  15. function store_skin(uuid, profile, details, callback) {
  16. networking.get_skin_url(uuid, profile, function(url) {
  17. if (url) {
  18. var hash = get_hash(url);
  19. if (details && details.skin === hash) {
  20. cache.update_timestamp(uuid, hash);
  21. callback(null, hash);
  22. } else {
  23. logging.log(uuid + " new skin hash: " + hash);
  24. var facepath = __dirname + "/../" + config.faces_dir + hash + ".png";
  25. var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png"
  26. fs.exists(facepath, function(exists) {
  27. if (exists) {
  28. logging.log(uuid + " skin already exists, not downloading");
  29. callback(null, hash);
  30. } else {
  31. networking.get_from(url, function(img, response, err) {
  32. if (err || !img) {
  33. callback(err, null);
  34. } else {
  35. skins.extract_face(img, facepath, function(err) {
  36. if (err) {
  37. logging.error(err);
  38. callback(err, null);
  39. } else {
  40. logging.log(uuid + " face extracted");
  41. skins.extract_helm(facepath, img, helmpath, function(err) {
  42. logging.log(uuid + " helm extracted");
  43. logging.debug(helmpath);
  44. callback(err, hash);
  45. });
  46. }
  47. });
  48. }
  49. });
  50. }
  51. });
  52. }
  53. } else {
  54. callback(null, null);
  55. }
  56. });
  57. }
  58. function store_cape(uuid, profile, details, callback) {
  59. networking.get_cape_url(uuid, profile, function(url) {
  60. if (url) {
  61. var hash = get_hash(url);
  62. if (details && details.cape === hash) {
  63. cache.update_timestamp(uuid, hash);
  64. callback(null, hash);
  65. } else {
  66. logging.log(uuid + " new cape hash: " + hash);
  67. var capepath = __dirname + "/../" + config.capes_dir + hash + ".png";
  68. fs.exists(capepath, function(exists) {
  69. if (exists) {
  70. logging.log(uuid + " cape already exists, not downloading");
  71. callback(null, hash);
  72. } else {
  73. networking.get_from(url, function(img, response, err) {
  74. if (err || !img) {
  75. logging.error(err);
  76. callback(err, null);
  77. } else {
  78. skins.save_image(img, capepath, function(err) {
  79. logging.log(uuid + " cape saved");
  80. callback(err, hash);
  81. });
  82. }
  83. });
  84. }
  85. });
  86. }
  87. } else {
  88. callback(null, null);
  89. }
  90. });
  91. }
  92. function remove_from_array(arr, item) {
  93. var i;
  94. while((i = arr.indexOf(item)) !== -1) {
  95. arr.splice(i, 1);
  96. }
  97. }
  98. // downloads the images for +uuid+ while checking the cache
  99. // status based on +details+. +whichhash+ specifies which
  100. // image is more important, and should be called back on
  101. // +callback+ contains the error buffer and image hash
  102. var currently_running = [];
  103. function callback_for(uuid, which, err, cape_hash, skin_hash) {
  104. for (var i = 0; i < currently_running.length; i++) {
  105. if (currently_running[i] && currently_running[i].uuid === uuid && (currently_running[i].which === which || which === null)) {
  106. var will_call = currently_running[i];
  107. will_call.callback(err, will_call.which === 'skin' ? skin_hash : cape_hash);
  108. //remove_from_array(currently_running, i);
  109. delete(currently_running[i]);
  110. }
  111. }
  112. }
  113. function array_has_hash(arr, property, value) {
  114. for (var i = 0; i < arr.length; i++) {
  115. if (arr[i] && arr[i][property] === value) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. function store_images(uuid, details, whichhash, callback) {
  122. var isUUID = uuid.length > 16;
  123. var new_hash = { 'uuid': uuid, 'which': whichhash, 'callback': callback };
  124. if (!array_has_hash(currently_running, 'uuid', uuid)) {
  125. currently_running.push(new_hash);
  126. networking.get_profile((isUUID ? uuid : null), function(err, profile) {
  127. if (err || (isUUID && !profile)) {
  128. callback_for(uuid, err, null, null);
  129. } else {
  130. store_skin(uuid, profile, details, function(err, skin_hash) {
  131. cache.save_hash(uuid, skin_hash, null);
  132. callback_for(uuid, 'skin', err, null, skin_hash);
  133. store_cape(uuid, profile, details, function(err, cape_hash) {
  134. cache.save_hash(uuid, skin_hash, cape_hash);
  135. callback_for(uuid, 'cape', err, cape_hash, skin_hash);
  136. });
  137. });
  138. }
  139. });
  140. } else {
  141. currently_running.push(new_hash);
  142. }
  143. }
  144. var exp = {};
  145. // returns true if the +uuid+ is a valid uuid or username
  146. // the uuid may be not exist, however
  147. exp.uuid_valid = function(uuid) {
  148. return valid_uuid.test(uuid);
  149. };
  150. // decides whether to get an image from disk or to download it
  151. // callback contains error, status, hash
  152. // the status gives information about how the image was received
  153. // -1: "error"
  154. // 0: "none" - cached as null
  155. // 1: "cached" - found on disk
  156. // 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
  157. // 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
  158. exp.get_image_hash = function(uuid, raw_type, callback) {
  159. cache.get_details(uuid, function(err, details) {
  160. var type = (details !== null ? (raw_type === "skin" ? details.skin : details.cape) : null);
  161. if (err) {
  162. callback(err, -1, null);
  163. } else {
  164. if (details && details.time + config.local_cache_time * 1000 >= new Date().getTime()) {logging.log(uuid + " uuid cached & recently updated");
  165. callback(null, (type ? 1 : 0), type);
  166. } else {
  167. if (details) {
  168. logging.log(uuid + " uuid cached, but too old");
  169. } else {
  170. logging.log(uuid + " uuid not cached");
  171. }
  172. store_images(uuid, details, raw_type, function(err, hash) {
  173. if (err) {
  174. callback(err, -1, details && type);
  175. } else {
  176. var status = details && (type === hash) ? 3 : 2;
  177. logging.debug(uuid + " old hash: " + (details && type));
  178. logging.log(uuid + " hash: " + hash);
  179. callback(null, status, hash);
  180. }
  181. });
  182. }
  183. }
  184. });
  185. };
  186. // handles requests for +uuid+ avatars with +size+
  187. // callback contains error, status, image buffer, hash
  188. // image is the user's face+helm when helm is true, or the face otherwise
  189. // for status, see get_image_hash
  190. exp.get_avatar = function(uuid, helm, size, callback) {
  191. logging.log("request: " + uuid);
  192. exp.get_image_hash(uuid, "skin", function(err, status, hash) {
  193. if (hash) {
  194. var facepath = __dirname + "/../" + config.faces_dir + hash + ".png";
  195. var helmpath = __dirname + "/../" + config.helms_dir + hash + ".png";
  196. var filepath = facepath;
  197. fs.exists(helmpath, function (exists) {
  198. if (helm && exists) {
  199. filepath = helmpath;
  200. }
  201. skins.resize_img(filepath, size, function(img_err, result) {
  202. if (img_err) {
  203. callback(img_err, -1, null, hash);
  204. } else {
  205. // we might have a hash although an error occured
  206. // (e.g. Mojang servers not reachable, using outdated hash)
  207. callback(err, (err ? -1 : status), result, hash);
  208. }
  209. });
  210. });
  211. } else {
  212. // hash is null when uuid has no skin
  213. callback(err, status, null, null);
  214. }
  215. });
  216. };
  217. // handles requests for +uuid+ skins
  218. // callback contains error, hash, image buffer
  219. exp.get_skin = function(uuid, callback) {
  220. logging.log(uuid + " skin request");
  221. exp.get_image_hash(uuid, 'skin', function(err, status, hash) {
  222. var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png";
  223. fs.exists(skinpath, function (exists) {
  224. if (exists) {
  225. logging.log(uuid + " skin already exists, not downloading");
  226. skins.open_skin(uuid, skinpath, function(err, img) {
  227. callback(err, hash, img);
  228. });
  229. } else {
  230. networking.save_skin(uuid, hash, skinpath, function(err, img) {
  231. callback(err, hash, img);
  232. });
  233. }
  234. });
  235. });
  236. };
  237. function get_type(helm, body) {
  238. var text = body ? "body" : "head";
  239. return helm ? text + "helm" : text;
  240. }
  241. // handles creations of skin renders
  242. // callback contanis error, hash, image buffer
  243. exp.get_render = function(uuid, scale, helm, body, callback) {
  244. exp.get_skin(uuid, function(err, hash, img) {
  245. if (!hash) {
  246. callback(err, -1, hash, null);
  247. return;
  248. }
  249. var renderpath = __dirname + "/../" + config.renders_dir + hash + "-" + scale + "-" + get_type(helm, body) + ".png";
  250. fs.exists(renderpath, function(exists) {
  251. if (exists) {
  252. renders.open_render(uuid, renderpath, function(err, img) {
  253. callback(err, 1, hash, img);
  254. });
  255. return;
  256. } else {
  257. if (!img) {
  258. callback(err, 0, hash, null);
  259. return;
  260. }
  261. renders.draw_model(uuid, img, scale, helm, body, function(err, img) {
  262. if (err) {
  263. callback(err, -1, hash, null);
  264. } else if (!img) {
  265. callback(null, 0, hash, null);
  266. } else {
  267. fs.writeFile(renderpath, img, "binary", function(err) {
  268. if (err) {
  269. logging.log(err);
  270. }
  271. callback(null, 2, hash, img);
  272. });
  273. }
  274. });
  275. }
  276. });
  277. });
  278. };
  279. // handles requests for +uuid+ skins
  280. // callback contains error, hash, image buffer
  281. exp.get_skin = function(uuid, callback) {
  282. logging.log(uuid + " skin request");
  283. exp.get_image_hash(uuid, "skin", function(err, status, hash) {
  284. var skinpath = __dirname + "/../" + config.skins_dir + hash + ".png";
  285. fs.exists(skinpath, function(exists) {
  286. if (exists) {
  287. logging.log("skin already exists, not downloading");
  288. skins.open_skin(skinpath, function(err, img) {
  289. callback(err, hash, img);
  290. });
  291. } else {
  292. networking.save_texture(uuid, hash, skinpath, function(err, response, img) {
  293. callback(err, hash, img);
  294. });
  295. }
  296. });
  297. });
  298. };
  299. // handles requests for +uuid+ capes
  300. // callback contains error, hash, image buffer
  301. exp.get_cape = function(uuid, callback) {
  302. logging.log(uuid + " cape request");
  303. exp.get_image_hash(uuid, "cape", function(err, status, hash) {
  304. if (!hash) {
  305. callback(err, null, null);
  306. return;
  307. }
  308. var capepath = __dirname + "/../" + config.capes_path + hash + ".png";
  309. fs.exists(capepath, function(exists) {
  310. if (exists) {
  311. logging.log("cape already exists, not downloading");
  312. skins.open_skin(capepath, function(err, img) {
  313. callback(err, hash, img);
  314. });
  315. } else {
  316. networking.save_texture(uuid, hash, capepath, function(err, response, img) {
  317. if (response && response.statusCode === 404) {
  318. callback(err, hash, null);
  319. } else {
  320. callback(err, hash, img);
  321. }
  322. });
  323. }
  324. });
  325. });
  326. };
  327. module.exports = exp;