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