helpers.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. var networking = require("./networking");
  2. var logging = require("./logging");
  3. var renders = require("./renders");
  4. var config = require("./config");
  5. var cache = require("./cache");
  6. var skins = require("./skins");
  7. var path = require("path");
  8. var fs = require("fs");
  9. // 0098cb60-fa8e-427c-b299-793cbd302c9a
  10. var valid_user_id = /^([0-9a-f-A-F-]{32,36}|[a-zA-Z0-9_]{1,16})$/; // uuid|username
  11. var hash_pattern = /[0-9a-f]+$/;
  12. // gets the hash from the textures.minecraft.net +url+
  13. function get_hash(url) {
  14. return hash_pattern.exec(url)[0].toLowerCase();
  15. }
  16. // gets the skin for +userId+ with +profile+
  17. // uses +cache_details+ to determine if the skin needs to be downloaded or can be taken from cache
  18. // face and face+helm images are extracted and stored to files
  19. // callback: error, skin hash
  20. function store_skin(rid, userId, profile, cache_details, callback) {
  21. networking.get_skin_url(rid, userId, profile, function(err, url) {
  22. if (!err && url) {
  23. var skin_hash = get_hash(url);
  24. if (cache_details && cache_details.skin === skin_hash) {
  25. cache.update_timestamp(rid, userId, skin_hash, false, function(cache_err) {
  26. callback(cache_err, skin_hash);
  27. });
  28. } else {
  29. logging.log(rid, "new skin hash:", skin_hash);
  30. var facepath = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
  31. var helmpath = path.join(__dirname, "..", config.helms_dir, skin_hash + ".png");
  32. fs.exists(facepath, function(exists) {
  33. if (exists) {
  34. logging.log(rid, "skin already exists, not downloading");
  35. callback(null, skin_hash);
  36. } else {
  37. networking.get_from(rid, url, function(img, response, err1) {
  38. if (err1 || !img) {
  39. callback(err1, null);
  40. } else {
  41. skins.extract_face(img, facepath, function(err2) {
  42. if (err2) {
  43. logging.error(rid, err2.stack);
  44. callback(err2, null);
  45. } else {
  46. logging.debug(rid, "face extracted");
  47. skins.extract_helm(rid, facepath, img, helmpath, function(err3) {
  48. logging.debug(rid, "helm extracted");
  49. logging.debug(rid, helmpath);
  50. callback(err3, skin_hash);
  51. });
  52. }
  53. });
  54. }
  55. });
  56. }
  57. });
  58. }
  59. } else {
  60. callback(err, null);
  61. }
  62. });
  63. }
  64. // gets the cape for +userId+ with +profile+
  65. // uses +cache_details+ to determine if the cape needs to be downloaded or can be taken from cache
  66. // the cape - if downloaded - is stored to file
  67. // callback: error, cape hash
  68. function store_cape(rid, userId, profile, cache_details, callback) {
  69. networking.get_cape_url(rid, userId, profile, function(err, url) {
  70. if (!err && url) {
  71. var cape_hash = get_hash(url);
  72. if (cache_details && cache_details.cape === cape_hash) {
  73. cache.update_timestamp(rid, userId, cape_hash, false, function(cache_err) {
  74. callback(cache_err, cape_hash);
  75. });
  76. } else {
  77. logging.log(rid, "new cape hash:", cape_hash);
  78. var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
  79. fs.exists(capepath, function(exists) {
  80. if (exists) {
  81. logging.log(rid, "cape already exists, not downloading");
  82. callback(null, cape_hash);
  83. } else {
  84. networking.get_from(rid, url, function(img, response, net_err) {
  85. if (net_err || !img) {
  86. logging.error(rid, net_err.stack);
  87. callback(net_err, null);
  88. } else {
  89. skins.save_image(img, capepath, function(skin_err) {
  90. logging.debug(rid, "cape saved");
  91. callback(skin_err, cape_hash);
  92. });
  93. }
  94. });
  95. }
  96. });
  97. }
  98. } else {
  99. callback(err, null);
  100. }
  101. });
  102. }
  103. // used by store_images to queue simultaneous requests for identical userId
  104. // the first request has to be completed until all others are continued
  105. var currently_running = [];
  106. // calls back all queued requests that match userId and type
  107. function callback_for(userId, type, err, hash) {
  108. var req_count = 0;
  109. for (var i = 0; i < currently_running.length; i++) {
  110. var current = currently_running[i];
  111. if (current.userid === userId && current.type === type) {
  112. req_count++;
  113. if (req_count !== 1) {
  114. // otherwise this would show up on single/first requests, too
  115. logging.debug(current.rid, "queued", type + " request continued");
  116. }
  117. currently_running.splice(i, 1); // remove from array
  118. current.callback(err, hash);
  119. i--;
  120. }
  121. }
  122. if (req_count > 1) {
  123. logging.debug(req_count + " simultaneous requests for", userId);
  124. }
  125. }
  126. // returns true if any object in +arr+ has a +property+ that matches +value+
  127. //
  128. // deep_property_check([{foo: "bar"}, {foo: "baz"}], "foo", "baz");
  129. //
  130. function deep_property_check(arr, property, value) {
  131. for (var i = 0; i < arr.length; i++) {
  132. if (arr[i][property] === value) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. // downloads the images for +userId+ while checking the cache
  139. // status based on +cache_details+. +type+ specifies which
  140. // image type should be called back on
  141. // callback: error, image hash
  142. function store_images(rid, userId, cache_details, type, callback) {
  143. var is_uuid = userId.length > 16;
  144. var new_hash = {
  145. rid: rid,
  146. userid: userId,
  147. type: type,
  148. callback: callback
  149. };
  150. if (!deep_property_check(currently_running, "userid", userId)) {
  151. currently_running.push(new_hash);
  152. networking.get_profile(rid, (is_uuid ? userId : null), function(err, profile) {
  153. if (err || (is_uuid && !profile)) {
  154. // error or uuid without profile
  155. if (!err && !profile) {
  156. // no error, but uuid without profile
  157. cache.save_hash(rid, userId, null, null, function(cache_err) {
  158. // we have no profile, so we have neither skin nor cape
  159. callback_for(userId, "skin", cache_err, null);
  160. callback_for(userId, "cape", cache_err, null);
  161. });
  162. } else {
  163. // an error occured, not caching. we can try in 60 seconds
  164. callback_for(userId, type, err, null);
  165. }
  166. } else {
  167. // no error and we have a profile (if it's a uuid)
  168. store_skin(rid, userId, profile, cache_details, function(store_err, skin_hash) {
  169. if (store_err && !skin_hash) {
  170. // an error occured, not caching. we can try in 60 seconds
  171. callback_for(userId, "skin", store_err, null);
  172. } else {
  173. cache.save_hash(rid, userId, skin_hash, null, function(cache_err) {
  174. callback_for(userId, "skin", (store_err || cache_err), skin_hash);
  175. });
  176. }
  177. });
  178. store_cape(rid, userId, profile, cache_details, function(store_err, cape_hash) {
  179. if (store_err && !cape_hash) {
  180. // an error occured, not caching. we can try in 60 seconds
  181. callback_for(userId, "cape", (store_err), cape_hash);
  182. } else {
  183. cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) {
  184. callback_for(userId, "cape", (store_err || cache_err), cape_hash);
  185. });
  186. }
  187. });
  188. }
  189. });
  190. } else {
  191. logging.log(rid, "ID already being processed, adding to queue");
  192. currently_running.push(new_hash);
  193. }
  194. }
  195. var exp = {};
  196. // returns true if the +userId+ is a valid userId or username
  197. // the userId may be not exist, however
  198. exp.id_valid = function(userId) {
  199. return valid_user_id.test(userId);
  200. };
  201. // decides whether to get a +type+ image for +userId+ from disk or to download it
  202. // callback: error, status, hash
  203. // the status gives information about how the image was received
  204. // -1: "error"
  205. // 0: "none" - cached as null
  206. // 1: "cached" - found on disk
  207. // 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
  208. // 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
  209. exp.get_image_hash = function(rid, userId, type, callback) {
  210. cache.get_details(userId, function(err, cache_details) {
  211. var cached_hash = null;
  212. if (cache_details !== null) {
  213. cached_hash = type === "skin" ? cache_details.skin : cache_details.cape;
  214. }
  215. if (err) {
  216. callback(err, -1, null);
  217. } else {
  218. if (cache_details && cache_details[type] !== undefined && cache_details.time + config.local_cache_time * 1000 >= new Date().getTime()) {
  219. // use cached image
  220. logging.log(rid, "userId cached & recently updated");
  221. callback(null, (cached_hash ? 1 : 0), cached_hash);
  222. } else {
  223. // download image
  224. if (cache_details) {
  225. logging.log(rid, "userId cached, but too old");
  226. } else {
  227. logging.log(rid, "userId not cached");
  228. }
  229. store_images(rid, userId, cache_details, type, function(store_err, new_hash) {
  230. if (store_err) {
  231. // we might have a cached hash although an error occured
  232. // (e.g. Mojang servers not reachable, using outdated hash)
  233. cache.update_timestamp(rid, userId, cached_hash, true, function(err2) {
  234. callback(err2 || store_err, -1, cache_details && cached_hash);
  235. });
  236. } else {
  237. var status = cache_details && (cached_hash === new_hash) ? 3 : 2;
  238. logging.debug(rid, "cached hash:", (cache_details && cached_hash));
  239. logging.log(rid, "new hash:", new_hash);
  240. callback(null, status, new_hash);
  241. }
  242. });
  243. }
  244. }
  245. });
  246. };
  247. // handles requests for +userId+ avatars with +size+
  248. // callback: error, status, image buffer, skin hash
  249. // image is the user's face+helm when helm is true, or the face otherwise
  250. // for status, see get_image_hash
  251. exp.get_avatar = function(rid, userId, helm, size, callback) {
  252. exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
  253. if (skin_hash) {
  254. var facepath = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
  255. var helmpath = path.join(__dirname, "..", config.helms_dir, skin_hash + ".png");
  256. var filepath = facepath;
  257. fs.exists(helmpath, function(exists) {
  258. if (helm && exists) {
  259. filepath = helmpath;
  260. }
  261. skins.resize_img(filepath, size, function(img_err, image) {
  262. if (img_err) {
  263. callback(img_err, -1, null, skin_hash);
  264. } else {
  265. callback(err, (err ? -1 : status), image, skin_hash);
  266. }
  267. });
  268. });
  269. } else {
  270. // hash is null when userId has no skin
  271. callback(err, status, null, null);
  272. }
  273. });
  274. };
  275. // handles requests for +userId+ skins
  276. // callback: error, skin hash, image buffer
  277. exp.get_skin = function(rid, userId, callback) {
  278. exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
  279. // FIXME: err is not used / handled
  280. var skinpath = path.join(__dirname, "..", config.skins_dir, skin_hash + ".png");
  281. fs.exists(skinpath, function(exists) {
  282. if (exists) {
  283. logging.log(rid, "skin already exists, not downloading");
  284. skins.open_skin(rid, skinpath, function(skin_err, img) {
  285. callback(skin_err, skin_hash, img);
  286. });
  287. } else {
  288. networking.save_texture(rid, skin_hash, skinpath, function(net_err, response, img) {
  289. callback(net_err, skin_hash, img);
  290. });
  291. }
  292. });
  293. });
  294. };
  295. // helper method used for file names
  296. // possible returned names based on +helm+ and +body+ are:
  297. // body, bodyhelm, head, headhelm
  298. function get_type(helm, body) {
  299. var text = body ? "body" : "head";
  300. return helm ? text + "helm" : text;
  301. }
  302. // handles creations of 3D renders
  303. // callback: error, skin hash, image buffer
  304. exp.get_render = function(rid, userId, scale, helm, body, callback) {
  305. exp.get_skin(rid, userId, function(err, skin_hash, img) {
  306. if (!skin_hash) {
  307. callback(err, -1, skin_hash, null);
  308. return;
  309. }
  310. var renderpath = path.join(__dirname, "..", config.renders_dir, [skin_hash, "-", scale, "-", get_type(helm, body)].join("-") + ".png");
  311. fs.exists(renderpath, function(exists) {
  312. if (exists) {
  313. renders.open_render(rid, renderpath, function(render_err, rendered_img) {
  314. callback(render_err, 1, skin_hash, rendered_img);
  315. });
  316. return;
  317. } else {
  318. if (!img) {
  319. callback(err, 0, skin_hash, null);
  320. return;
  321. }
  322. renders.draw_model(rid, img, scale, helm, body, function(draw_err, drawn_img) {
  323. if (draw_err) {
  324. callback(draw_err, -1, skin_hash, null);
  325. } else if (!drawn_img) {
  326. callback(null, 0, skin_hash, null);
  327. } else {
  328. fs.writeFile(renderpath, drawn_img, "binary", function(fs_err) {
  329. if (fs_err) {
  330. logging.error(rid, fs_err.stack);
  331. }
  332. callback(null, 2, skin_hash, img);
  333. });
  334. }
  335. });
  336. }
  337. });
  338. });
  339. };
  340. // handles requests for +userId+ capes
  341. // callback: error, cape hash, image buffer
  342. exp.get_cape = function(rid, userId, callback) {
  343. exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
  344. if (!cape_hash) {
  345. callback(err, null, null);
  346. return;
  347. }
  348. var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
  349. fs.exists(capepath, function(exists) {
  350. if (exists) {
  351. logging.log(rid, "cape already exists, not downloading");
  352. skins.open_skin(rid, capepath, function(skin_err, img) {
  353. callback(skin_err, cape_hash, img);
  354. });
  355. } else {
  356. networking.save_texture(rid, cape_hash, capepath, function(net_err, response, img) {
  357. if (response && response.statusCode === 404) {
  358. callback(net_err, cape_hash, null);
  359. } else {
  360. callback(net_err, cape_hash, img);
  361. }
  362. });
  363. }
  364. });
  365. });
  366. };
  367. module.exports = exp;