Skip to content

Commit

Permalink
Handle non-ascii names
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPrimate committed Jun 10, 2021
1 parent a84c08c commit 7aa20eb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# [3.1.5]

* File names are now converted to ascii to deal with Foundry unicode filename issues. It will revert to hash of character name if a suitable string cannot be generated.

# [3.1.3/4]

* Add a button to load token image from Token Variants module.
Expand Down
2 changes: 1 addition & 1 deletion src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class Tokenizer extends FormApplication {

async _getFilename(suffix = "Avatar") {
const isWildCard = () => this.actor.data.token.randomImg;
const actorName = Utils.makeSlug(this.actor.name);
const actorName = await Utils.makeSlug(this.actor);
const imageFormat = game.settings.get("vtta-tokenizer", "image-save-type");

if (suffix === "Token" && isWildCard()) {
Expand Down
47 changes: 45 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ export default class Utils {
return firstPart + secondPart;
}

static U2A(str) {
let reserved = '';
const code = str.match(/&#(d+);/g);

if (code === null) {
return str;
}

for (var i = 0; i < code.length; i++) {
reserved += String.fromCharCode(code[i].replace(/[&#;]/g, ''));
}

return reserved;
}

static getElementPosition(obj) {
var curleft = 0,
curtop = 0;
Expand Down Expand Up @@ -108,15 +123,43 @@ export default class Utils {
return result.path;
}

static makeSlug(s) {
static getHash(str, algo = "SHA-256") {
let strBuf = new TextEncoder('utf-8').encode(str);
return crypto.subtle.digest(algo, strBuf)
.then(hash => {
// window.hash = hash;
// here hash is an arrayBuffer,
// so we'll connvert it to its hex version
let result = '';
const view = new DataView(hash);
for (let i = 0; i < hash.byteLength; i += 4) {
result += ('00000000' + view.getUint32(i).toString(16)).slice(-8);
}
return result;
});
}

static async makeSlug(actor) {
const toReplace = "а,б,в,г,д,е,ё,ж,з,и,й,к,л,м,н,о,п,р,с,т,у,ф,х,ц,ч,ш,щ,ъ,ы,ь,э,ю,я".split(",");
const replacers = "a,b,v,g,d,e,yo,zh,z,i,y,k,l,m,n,o,p,r,s,t,u,f,kh,c,ch,sh,sch,_,y,_,e,yu,ya".split(",");
const replaceDict = Object.fromEntries(toReplace.map((_, i) => [toReplace[i], replacers[i]]));
return s.toLowerCase()
const unicodeString = actor.name.toLowerCase()
.split("")
.map(x => replaceDict.hasOwnProperty(x) ? replaceDict[x] : x)
.join("")
.replace(/[^\w.]/gi, "_")
.replace(/__+/g, "_")
let asciiString = Utils.U2A(unicodeString);
return new Promise((resolve) => {
if (asciiString.length < 2) {
// asciiString = actor.id;
Utils.getHash(actor.name).then((hash) => {
console.debug("Tokenizer is having to use a hashed file name.");
resolve(hash);
});
} else {
resolve(asciiString);
}
});
}
}

0 comments on commit 7aa20eb

Please sign in to comment.