helpers.js 13 KB

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