helpers.js 12 KB

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