Skip to content

Commit

Permalink
func: added save string base64 unicode encoding
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
uyen18827 committed Sep 17, 2021
1 parent 8903ab8 commit 20f9de1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/core/script/saveScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getCurrentParagraphName, updateParagraph } from "../paragraphs/paragrap
import { getPlayer, setPlayer, showNameDiv } from "../player/playerInfo";
import { loadPronounsRadioBtn, showPronouns } from "../player/pronouns";
import DOMPurify from 'DOMPurify';
import { b64DecodeUnicode, b64EncodeUnicode } from "../../tools/system";

/**
* Create a new save and stringify it.
Expand Down Expand Up @@ -86,7 +87,7 @@ export function exportStorageSave(saveSlot: string) {
Copy and keep the code bellow to load later`);
let saveOutput = document.querySelector(`#saveOutput`);
(<HTMLInputElement>saveOutput).value = ``; //clear old save
(<HTMLInputElement>saveOutput).value = `${btoa(retrievedSave!)}`; //encode to Base64
(<HTMLInputElement>saveOutput).value = `${b64EncodeUnicode(retrievedSave!)}`; //encode to Base64
(<HTMLInputElement>saveOutput).select();
}

Expand All @@ -101,7 +102,7 @@ export function exportSave() {
Copy and keep the code bellow to load later`);
let saveOutput = document.querySelector(`#saveOutput`);
(<HTMLInputElement>saveOutput).value = ``; //clear old save
(<HTMLInputElement>saveOutput).value += `${btoa(save())}`; //encode to Base64
(<HTMLInputElement>saveOutput).value += `${b64EncodeUnicode(save())}`; //encode to Base64
(<HTMLInputElement>saveOutput).select();
}

Expand All @@ -111,7 +112,7 @@ export function exportSave() {
*/
export function loadSaveCode() {
let loadCode = (<HTMLInputElement>document.querySelector("#saveOutput")).value;
loadCode = atob(loadCode);
loadCode = b64DecodeUnicode(loadCode);
console.log(loadCode);
let retrievedSave = JSON.parse(loadCode);
console.log(retrievedSave)
Expand Down
21 changes: 21 additions & 0 deletions src/tools/system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Encode unicode string to base64
* @param str string to be encoded
* @returns encoded string
*/
export function b64EncodeUnicode(str: string) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode(parseInt(p1, 16))
}))
}

/**
* Decode unicode base64 string
* @param str base64 string to be decoded
* @returns decoded string
*/
export function b64DecodeUnicode(str: string) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
}).join(''))
}

0 comments on commit 20f9de1

Please sign in to comment.