add support for slim renders, fixes #125, adjust tests

This commit is contained in:
jomo
2015-12-13 14:08:59 +01:00
parent 7eed1fa09b
commit 3c21a59c94
7 changed files with 162 additions and 121 deletions

View File

@@ -67,15 +67,14 @@ exp.get_redis = function() {
// updates the redis instance's server_info object
// callback: error, info object
exp.info = function(callback) {
redis.info(function (err, res) {
redis.info(function(err, res) {
// parse the info command and store it in redis.server_info
// this code block was taken from mranney/node_redis#on_info_cmd
// http://git.io/LBUNbg
var lines = res.toString().split("\r\n");
var obj = {};
lines.forEach(function (line) {
lines.forEach(function(line) {
var parts = line.split(":");
if (parts[1]) {
obj[parts[0]] = parts[1];
@@ -109,31 +108,36 @@ exp.update_timestamp = function(rid, userId, hash, temp, callback) {
update_file_date(rid, hash);
};
// create the key +userId+, store +skin_hash+, +cape_hash+ and time
// if either +skin_hash+ or +cape_hash+ are undefined, they will not be stored
// this feature can be used to write both cape and skin at separate times
// create the key +userId+, store +skin_hash+, +cape_hash+, +slim+ and current time
// if +skin_hash+ or +cape_hash+ are undefined, they aren't stored
// this is useful to store cape and skin at separate times, without overwriting the other
// +slim+ can be true (alex) or false (steve)
// +callback+ contans error
exp.save_hash = function(rid, userId, skin_hash, cape_hash, callback) {
exp.save_hash = function(rid, userId, skin_hash, cape_hash, slim, callback) {
logging.debug(rid, "caching skin:" + skin_hash + " cape:" + cape_hash);
var time = Date.now();
// store shorter null byte instead of "null"
skin_hash = (skin_hash === null ? "" : skin_hash);
cape_hash = (cape_hash === null ? "" : cape_hash);
// store shorter null value instead of "null" string
skin_hash = skin_hash === null ? "" : skin_hash;
cape_hash = cape_hash === null ? "" : cape_hash;
// store userId in lower case if not null
userId = userId && userId.toLowerCase();
if (skin_hash === undefined) {
redis.hmset(userId, "c", cape_hash, "t", time, function(err) {
callback(err);
});
} else if (cape_hash === undefined) {
redis.hmset(userId, "s", skin_hash, "t", time, function(err) {
callback(err);
});
} else {
redis.hmset(userId, "s", skin_hash, "c", cape_hash, "t", time, function(err) {
callback(err);
});
var args = [];
if (cape_hash !== undefined) {
args.push("c", cape_hash);
}
if (skin_hash !== undefined) {
args.push("s", skin_hash);
}
if (slim !== undefined) {
args.push("a", Number(!!slim));
}
args.push("t", Date.now());
redis.hmset(userId, args, function(err) {
callback(err);
});
};
// removes the hash for +userId+ from the cache
@@ -155,6 +159,7 @@ exp.get_details = function(userId, callback) {
details = {
skin: data.s === "" ? null : data.s,
cape: data.c === "" ? null : data.c,
slim: data.a === 1,
time: Number(data.t)
};
}

View File

@@ -21,12 +21,12 @@ function get_hash(url) {
// face and face+helm images are extracted and stored to files
// callback: error, skin hash
function store_skin(rid, userId, profile, cache_details, callback) {
networking.get_skin_url(rid, userId, profile, function(err, url) {
networking.get_skin_info(rid, userId, profile, function(err, url, slim) {
if (!err && url) {
var skin_hash = get_hash(url);
if (cache_details && cache_details.skin === skin_hash) {
cache.update_timestamp(rid, userId, skin_hash, false, function(cache_err) {
callback(cache_err, skin_hash);
callback(cache_err, skin_hash, slim);
});
} else {
logging.debug(rid, "new skin hash:", skin_hash);
@@ -36,27 +36,27 @@ function store_skin(rid, userId, profile, cache_details, callback) {
fs.exists(facepath, function(exists) {
if (exists) {
logging.debug(rid, "skin already exists, not downloading");
callback(null, skin_hash);
callback(null, skin_hash, slim);
} else {
networking.get_from(rid, url, function(img, response, err1) {
if (err1 || !img) {
callback(err1, null);
callback(err1, null, slim);
} else {
skins.save_image(img, skinpath, function(skin_err) {
if (skin_err) {
logging.error(rid, skin_err);
callback(skin_err, null);
callback(skin_err, null, slim);
} else {
skins.extract_face(img, facepath, function(err2) {
if (err2) {
logging.error(rid, err2.stack);
callback(err2, null);
callback(err2, null, slim);
} else {
logging.debug(rid, "face extracted");
skins.extract_helm(rid, facepath, img, helmpath, function(err3) {
logging.debug(rid, "helm extracted");
logging.debug(rid, helmpath);
callback(err3, skin_hash);
callback(err3, skin_hash, slim);
});
}
});
@@ -129,7 +129,7 @@ function push_request(userId, type, fun) {
}
// calls back all queued requests that match userId and type
function resume(userId, type, err, hash) {
function resume(userId, type, err, hash, slim) {
var callbacks = requests[type][userId];
if (callbacks) {
if (callbacks.length > 1) {
@@ -138,7 +138,7 @@ function resume(userId, type, err, hash) {
for (var i = 0; i < callbacks.length; i++) {
// continue the request
callbacks[i](err, hash);
callbacks[i](err, hash, slim);
// remove from array
callbacks.splice(i, 1);
i--;
@@ -167,34 +167,34 @@ function store_images(rid, userId, cache_details, type, callback) {
// error or uuid without profile
if (!err && !profile) {
// no error, but uuid without profile
cache.save_hash(rid, userId, null, null, function(cache_err) {
cache.save_hash(rid, userId, null, null, undefined, function(cache_err) {
// we have no profile, so we have neither skin nor cape
resume(userId, "skin", cache_err, null);
resume(userId, "cape", cache_err, null);
resume(userId, "skin", cache_err, null, false);
resume(userId, "cape", cache_err, null, false);
});
} else {
// an error occured, not caching. we can try in 60 seconds
resume(userId, type, err, null);
resume(userId, type, err, null, false);
}
} else {
// no error and we have a profile (if it's a uuid)
store_skin(rid, userId, profile, cache_details, function(store_err, skin_hash) {
store_skin(rid, userId, profile, cache_details, function(store_err, skin_hash, slim) {
if (store_err && !skin_hash) {
// an error occured, not caching. we can try in 60 seconds
resume(userId, "skin", store_err, null);
resume(userId, "skin", store_err, null, slim);
} else {
cache.save_hash(rid, userId, skin_hash, undefined, function(cache_err) {
resume(userId, "skin", (store_err || cache_err), skin_hash);
cache.save_hash(rid, userId, skin_hash, undefined, slim, function(cache_err) {
resume(userId, "skin", (store_err || cache_err), skin_hash, slim);
});
}
});
store_cape(rid, userId, profile, cache_details, function(store_err, cape_hash) {
if (store_err && !cape_hash) {
// an error occured, not caching. we can try in 60 seconds
resume(userId, "cape", (store_err), cape_hash);
resume(userId, "cape", (store_err), cape_hash, false);
} else {
cache.save_hash(rid, userId, undefined, cape_hash, function(cache_err) {
resume(userId, "cape", (store_err || cache_err), cape_hash);
cache.save_hash(rid, userId, undefined, cape_hash, undefined, function(cache_err) {
resume(userId, "cape", (store_err || cache_err), cape_hash, false);
});
}
});
@@ -212,7 +212,7 @@ exp.id_valid = function(userId) {
};
// decides whether to get a +type+ image for +userId+ from disk or to download it
// callback: error, status, hash
// callback: error, status, hash, slim
// for status, see response.js
exp.get_image_hash = function(rid, userId, type, callback) {
cache.get_details(userId, function(err, cache_details) {
@@ -221,12 +221,12 @@ exp.get_image_hash = function(rid, userId, type, callback) {
cached_hash = type === "skin" ? cache_details.skin : cache_details.cape;
}
if (err) {
callback(err, -1, null);
callback(err, -1, null, false);
} else {
if (cache_details && cache_details[type] !== undefined && cache_details.time + config.caching.local * 1000 >= Date.now()) {
// use cached image
logging.debug(rid, "userId cached & recently updated");
callback(null, (cached_hash ? 1 : 0), cached_hash);
callback(null, (cached_hash ? 1 : 0), cached_hash, cache_details.slim);
} else {
// download image
if (cache_details) {
@@ -234,18 +234,18 @@ exp.get_image_hash = function(rid, userId, type, callback) {
} else {
logging.debug(rid, "userId not cached");
}
store_images(rid, userId, cache_details, type, function(store_err, new_hash) {
store_images(rid, userId, cache_details, type, function(store_err, new_hash, slim) {
if (store_err) {
// we might have a cached hash although an error occured
// (e.g. Mojang servers not reachable, using outdated hash)
cache.update_timestamp(rid, userId, cached_hash, true, function(err2) {
callback(err2 || store_err, -1, cache_details && cached_hash);
callback(err2 || store_err, -1, cache_details && cached_hash, slim);
});
} else {
var status = cache_details && (cached_hash === new_hash) ? 3 : 2;
logging.debug(rid, "cached hash:", (cache_details && cached_hash));
logging.debug(rid, "new hash:", new_hash);
callback(null, status, new_hash);
callback(null, status, new_hash, slim);
}
});
}
@@ -259,7 +259,7 @@ exp.get_image_hash = function(rid, userId, type, callback) {
// image is the user's face+helm when helm is true, or the face otherwise
// for status, see get_image_hash
exp.get_avatar = function(rid, userId, helm, size, callback) {
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash, slim) {
if (skin_hash) {
var facepath = path.join(__dirname, "..", config.directories.faces, skin_hash + ".png");
var helmpath = path.join(__dirname, "..", config.directories.helms, skin_hash + ".png");
@@ -286,23 +286,23 @@ exp.get_avatar = function(rid, userId, helm, size, callback) {
// handles requests for +userId+ skins
// callback: error, skin hash, status, image buffer
exp.get_skin = function(rid, userId, callback) {
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash) {
exp.get_image_hash(rid, userId, "skin", function(err, status, skin_hash, slim) {
if (skin_hash) {
var skinpath = path.join(__dirname, "..", config.directories.skins, skin_hash + ".png");
fs.exists(skinpath, function(exists) {
if (exists) {
logging.debug(rid, "skin already exists, not downloading");
skins.open_skin(rid, skinpath, function(skin_err, img) {
callback(skin_err || err, skin_hash, status, img);
callback(skin_err || err, skin_hash, status, img, slim);
});
} else {
networking.save_texture(rid, skin_hash, skinpath, function(net_err, response, img) {
callback(net_err || err, skin_hash, status, img);
callback(net_err || err, skin_hash, status, img, slim);
});
}
});
} else {
callback(err, null, status, null);
callback(err, null, status, null, slim);
}
});
};
@@ -318,12 +318,12 @@ function get_type(helm, body) {
// handles creations of 3D renders
// callback: error, skin hash, image buffer
exp.get_render = function(rid, userId, scale, helm, body, callback) {
exp.get_skin(rid, userId, function(err, skin_hash, status, img) {
exp.get_skin(rid, userId, function(err, skin_hash, status, img, slim) {
if (!skin_hash) {
callback(err, status, skin_hash, null);
return;
}
var renderpath = path.join(__dirname, "..", config.directories.renders, [skin_hash, scale, get_type(helm, body)].join("-") + ".png");
var renderpath = path.join(__dirname, "..", config.directories.renders, [skin_hash, scale, get_type(helm, body), slim ? "s" : "t"].join("-") + ".png");
fs.exists(renderpath, function(exists) {
if (exists) {
renders.open_render(rid, renderpath, function(render_err, rendered_img) {
@@ -335,7 +335,7 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
callback(err, 0, skin_hash, null);
return;
}
renders.draw_model(rid, img, scale, helm, body, function(draw_err, drawn_img) {
renders.draw_model(rid, img, scale, helm, body, slim, function(draw_err, drawn_img) {
if (draw_err) {
callback(draw_err, -1, skin_hash, null);
} else if (!drawn_img) {
@@ -357,7 +357,7 @@ exp.get_render = function(rid, userId, scale, helm, body, callback) {
// handles requests for +userId+ capes
// callback: error, cape hash, status, image buffer
exp.get_cape = function(rid, userId, callback) {
exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash) {
exp.get_image_hash(rid, userId, "cape", function(err, status, cape_hash, slim) {
if (!cape_hash) {
callback(err, null, status, null);
return;

View File

@@ -3,6 +3,7 @@ var logging = require("./logging");
var request = require("request");
var config = require("../config");
var skins = require("./skins");
require("./object-patch");
var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
@@ -22,40 +23,46 @@ function extract_url(profile, type) {
if (prop.name === "textures") {
var json = new Buffer(prop.value, "base64").toString();
var props = JSON.parse(json);
url = props && props.textures && props.textures[type] && props.textures[type].url || null;
url = Object.get(props, "textures." + type + ".url") || null;
}
});
}
return url;
}
// helper method that calls `get_username_url` or `get_uuid_url` based on the +usedId+
// extracts the +type+ [SKIN|CAPE] URL
// from the nested & encoded +profile+ object
// returns the if the model is "slim"
function extract_model(profile) {
var slim = null;
if (profile && profile.properties) {
profile.properties.forEach(function(prop) {
if (prop.name === "textures") {
var json = new Buffer(prop.value, "base64").toString();
var props = JSON.parse(json);
slim = Object.get(props, "textures.SKIN.metadata.model");
}
});
}
return slim === "slim";
}
// helper method that calls `get_username_url` or `get_uuid_info` based on the +usedId+
// +userId+ is used for usernames, while +profile+ is used for UUIDs
function get_url(rid, userId, profile, type, callback) {
// callback: error, url, slim
function get_info(rid, userId, profile, type, callback) {
if (userId.length <= 16) {
// username
exp.get_username_url(rid, userId, type, function(err, url) {
callback(err, url || null);
callback(err, url || null, false);
});
} else {
exp.get_uuid_url(profile, type, function(url) {
callback(null, url || null);
exp.get_uuid_info(profile, type, function(url, slim) {
callback(null, url || null, slim);
});
}
}
// exracts the skin URL of a +profile+ object
// returns null when no URL found (user has no skin)
exp.extract_skin_url = function(profile) {
return extract_url(profile, "SKIN");
};
// exracts the cape URL of a +profile+ object
// returns null when no URL found (user has no cape)
exp.extract_cape_url = function(profile) {
return extract_url(profile, "CAPE");
};
// performs a GET request to the +url+
// +options+ object includes these options:
// encoding (string), default is to return a buffer
@@ -112,6 +119,7 @@ exp.get_from = function(rid, url, callback) {
// the skin url is taken from the HTTP redirect
// type reference is above
exp.get_username_url = function(rid, name, type, callback) {
type = Number(type === "CAPE");
exp.get_from(rid, mojang_urls[type] + name + ".png", function(body, response, err) {
if (!err) {
if (response) {
@@ -126,15 +134,24 @@ exp.get_username_url = function(rid, name, type, callback) {
};
// gets the URL for a skin/cape from the profile
// +type+ specifies which to retrieve
exp.get_uuid_url = function(profile, type, callback) {
var url = null;
if (type === 0) {
url = exp.extract_skin_url(profile);
} else if (type === 1) {
url = exp.extract_cape_url(profile);
// +type+ "SKIN" or "CAPE", specifies which to retrieve
// callback: url, slim
exp.get_uuid_info = function(profile, type, callback) {
var properties = Object.get(profile, "properties") || [];
properties.forEach(function(prop) {
if (prop.name === "textures") {
var json = new Buffer(prop.value, "base64").toString();
profile = JSON.parse(json);
}
});
var url = Object.get(profile, "textures." + type + ".url");
var slim;
if (type === "SKIN") {
slim = Object.get(profile, "textures.SKIN.metadata.model");
}
callback(url || null);
callback(url || null, !!slim);
};
// make a request to sessionserver for +uuid+
@@ -149,20 +166,17 @@ exp.get_profile = function(rid, uuid, callback) {
}
};
// get the skin URL for +userId+
// get the skin URL and type for +userId+
// +profile+ is used if +userId+ is a uuid
exp.get_skin_url = function(rid, userId, profile, callback) {
get_url(rid, userId, profile, 0, function(err, url) {
callback(err, url);
});
// callback: error, url, slim
exp.get_skin_info = function(rid, userId, profile, callback) {
get_info(rid, userId, profile, "SKIN", callback);
};
// get the cape URL for +userId+
// +profile+ is used if +userId+ is a uuid
exp.get_cape_url = function(rid, userId, profile, callback) {
get_url(rid, userId, profile, 1, function(err, url) {
callback(err, url);
});
get_info(rid, userId, profile, "CAPE", callback);
};
// download the +tex_hash+ image from the texture server

22
lib/object-patch.js Normal file
View File

@@ -0,0 +1,22 @@
// Adds Object.get function
// +pathstr+ is a string of dot-separated nested properties on +ojb+
// returns undefined if any of the properties do not exist
// returns the value of the last property otherwise
//
// Object.get({"foo": {"bar": 123}}, "foo.bar"); // 123
// Object.get({"foo": {"bar": 123}}, "bar.foo"); // undefined
Object.get = function(obj, pathstr) {
var path = pathstr.split(".");
var result = obj;
for (var i = 0; i < path.length; i++) {
var key = path[i];
if (!result || !result.hasOwnProperty(key)) {
return undefined;
} else {
result = result[key];
}
}
return result;
};

View File

@@ -78,7 +78,7 @@ function flip(src) {
var skew_a = 26 / 45; // 0.57777777
var skew_b = skew_a * 2; // 1.15555555
exp.draw_model = function(rid, img, scale, overlay, is_body, callback) {
exp.draw_model = function(rid, img, scale, overlay, is_body, slim, callback) {
var canvas = new Canvas();
canvas.width = scale * 20;
canvas.height = scale * (is_body ? 45.1 : 18.5);
@@ -88,7 +88,7 @@ exp.draw_model = function(rid, img, scale, overlay, is_body, callback) {
skin.onload = function() {
var old_skin = skin.height === 32;
var arm_width = 4;
var arm_width = slim ? 3 : 4;
/* eslint-disable no-multi-spaces */
var head_top = resize(removeTransparency(getPart(skin, 8, 0, 8, 8, 1)), scale);

View File

@@ -36,7 +36,7 @@ function handle_default(rid, scale, helm, body, img_status, userId, size, def, r
// handle steve and alex
fs.readFile(path.join(__dirname, "..", "public", "images", def + "_skin.png"), function(fs_err, buf) {
// we render the default skins, but not custom images
renders.draw_model(rid, buf, scale, helm, body, function(render_err, def_img) {
renders.draw_model(rid, buf, scale, helm, body, def === "alex", function(render_err, def_img) {
callback({
status: img_status,
body: def_img,