helpers.js 14 KB

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