helpers.js 11 KB

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