helpers.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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, undefined, 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. // for status, see response.js
  204. exp.get_image_hash = function(rid, userId, type, callback) {
  205. cache.get_details(userId, function(err, cache_details) {
  206. var cached_hash = null;
  207. if (cache_details !== null) {
  208. cached_hash = type === "skin" ? cache_details.skin : cache_details.cape;
  209. }
  210. if (err) {
  211. callback(err, -1, null);
  212. } else {
  213. if (cache_details && cache_details[type] !== undefined && cache_details.time + config.local_cache_time * 1000 >= Date.now()) {
  214. // use cached image
  215. logging.log(rid, "userId cached & recently updated");
  216. callback(null, (cached_hash ? 1 : 0), cached_hash);
  217. } else {
  218. // download image
  219. if (cache_details) {
  220. logging.log(rid, "userId cached, but too old");
  221. } else {
  222. logging.log(rid, "userId not cached");
  223. }
  224. store_images(rid, userId, cache_details, type, function(store_err, new_hash) {
  225. if (store_err) {
  226. // we might have a cached hash although an error occured
  227. // (e.g. Mojang servers not reachable, using outdated hash)
  228. cache.update_timestamp(rid, userId, cached_hash, true, function(err2) {
  229. callback(err2 || store_err, -1, cache_details && cached_hash);
  230. });
  231. } else {
  232. var status = cache_details && (cached_hash === new_hash) ? 3 : 2;
  233. logging.debug(rid, "cached hash:", (cache_details && cached_hash));
  234. logging.log(rid, "new hash:", new_hash);
  235. callback(null, status, new_hash);
  236. }
  237. });
  238. }
  239. }
  240. });
  241. };
  242. // handles requests for +userId+ avatars with +size+
  243. // callback: error, status, image buffer, skin hash
  244. // image is the user's face+helm when helm is true, or the face otherwise
  245. // for status, see get_image_hash
  246. exp.get_avatar = function(rid, userId, helm, size, callback) {
  247. exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
  248. if (skin_hash) {
  249. var facepath = path.join(__dirname, "..", config.faces_dir, skin_hash + ".png");
  250. var helmpath = path.join(__dirname, "..", config.helms_dir, skin_hash + ".png");
  251. var filepath = facepath;
  252. fs.exists(helmpath, function(exists) {
  253. if (helm && exists) {
  254. filepath = helmpath;
  255. }
  256. skins.resize_img(filepath, size, function(img_err, image) {
  257. if (img_err) {
  258. callback(img_err, -1, null, skin_hash);
  259. } else {
  260. callback(err, (err ? -1 : status), image, skin_hash);
  261. }
  262. });
  263. });
  264. } else {
  265. // hash is null when userId has no skin
  266. callback(err, status, null, null);
  267. }
  268. });
  269. };
  270. // handles requests for +userId+ skins
  271. // callback: error, skin hash, status, image buffer
  272. exp.get_skin = function(rid, userId, callback) {
  273. exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
  274. if (skin_hash) {
  275. var skinpath = path.join(__dirname, "..", config.skins_dir, skin_hash + ".png");
  276. fs.exists(skinpath, function(exists) {
  277. if (exists) {
  278. logging.log(rid, "skin already exists, not downloading");
  279. skins.open_skin(rid, skinpath, function(skin_err, img) {
  280. callback(skin_err || err, skin_hash, status, img);
  281. });
  282. } else {
  283. networking.save_texture(rid, skin_hash, skinpath, function(net_err, response, img) {
  284. callback(net_err || err, skin_hash, status, img);
  285. });
  286. }
  287. });
  288. } else {
  289. callback(err, null, status, null);
  290. }
  291. });
  292. };
  293. // helper method used for file names
  294. // possible returned names based on +helm+ and +body+ are:
  295. // body, bodyhelm, head, headhelm
  296. function get_type(helm, body) {
  297. var text = body ? "body" : "head";
  298. return helm ? text + "helm" : text;
  299. }
  300. // handles creations of 3D renders
  301. // callback: error, skin hash, image buffer
  302. exp.get_render = function(rid, userId, scale, helm, body, callback) {
  303. exp.get_skin(rid, userId, function(err, skin_hash, status, img) {
  304. if (!skin_hash) {
  305. callback(err, status, skin_hash, null);
  306. return;
  307. }
  308. var renderpath = path.join(__dirname, "..", config.renders_dir, [skin_hash, scale, get_type(helm, body)].join("-") + ".png");
  309. fs.exists(renderpath, function(exists) {
  310. if (exists) {
  311. renders.open_render(rid, renderpath, function(render_err, rendered_img) {
  312. callback(render_err, 1, skin_hash, rendered_img);
  313. });
  314. return;
  315. } else {
  316. if (!img) {
  317. callback(err, 0, skin_hash, null);
  318. return;
  319. }
  320. renders.draw_model(rid, img, scale, helm, body, function(draw_err, drawn_img) {
  321. if (draw_err) {
  322. callback(draw_err, -1, skin_hash, null);
  323. } else if (!drawn_img) {
  324. callback(null, 0, skin_hash, null);
  325. } else {
  326. fs.writeFile(renderpath, drawn_img, "binary", function(fs_err) {
  327. if (fs_err) {
  328. logging.error(rid, fs_err.stack);
  329. }
  330. callback(null, 2, skin_hash, drawn_img);
  331. });
  332. }
  333. });
  334. }
  335. });
  336. });
  337. };
  338. // handles requests for +userId+ capes
  339. // callback: error, cape hash, status, image buffer
  340. exp.get_cape = function(rid, userId, callback) {
  341. exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
  342. if (!cape_hash) {
  343. callback(err, null, null, null);
  344. return;
  345. }
  346. var capepath = path.join(__dirname, "..", config.capes_dir, cape_hash + ".png");
  347. fs.exists(capepath, function(exists) {
  348. if (exists) {
  349. logging.log(rid, "cape already exists, not downloading");
  350. skins.open_skin(rid, capepath, function(skin_err, img) {
  351. callback(skin_err || err, cape_hash, status, img);
  352. });
  353. } else {
  354. networking.save_texture(rid, cape_hash, capepath, function(net_err, response, img) {
  355. if (response && response.statusCode === 404) {
  356. callback(net_err, cape_hash, status, null);
  357. } else {
  358. callback(net_err, cape_hash, status, img);
  359. }
  360. });
  361. }
  362. });
  363. });
  364. };
  365. module.exports = exp;