@adlad/plugin-viverse 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adlad-plugin-viverse.js +1 -1
- package/package.json +1 -1
- package/src/mod.js +12 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
function viversePlugin({appId:e,domain:t="account.htcvive.com",cookieDomain:i}){let a=!1;if(!e)throw new Error("No app id was provided");const o={viverse:"viverse",client:"client",clientId:"clientId",domain:"domain",cookieDomain:"cookieDomain",checkAuth:"checkAuth",loginWithWorlds:"loginWithWorlds",storage:"storage",newCloudSaveClient:"newCloudSaveClient",setPlayerData:"setPlayerData",getPlayerData:"getPlayerData"};let n,s;o>0&&console.log(o);let
|
|
1
|
+
function viversePlugin({appId:e,domain:t="account.htcvive.com",cookieDomain:i}){let a=!1;if(!e)throw new Error("No app id was provided");const o={viverse:"viverse",client:"client",clientId:"clientId",domain:"domain",cookieDomain:"cookieDomain",checkAuth:"checkAuth",loginWithWorlds:"loginWithWorlds",storage:"storage",newCloudSaveClient:"newCloudSaveClient",setPlayerData:"setPlayerData",getPlayerData:"getPlayerData"};let n,s;o>0&&console.log(o);let l=null;const r=new Map,c="adlad_viverse_";return{name:"viverse",async initialize(r){if(a)throw new Error("Viverse plugin is being initialized more than once");if(a=!0,await r.loadScriptTag("https://www.viverse.com/static-assets/viverse-sdk/index.umd.cjs"),n=new globalThis[o.viverse][o.client]({[o.clientId]:e,[o.domain]:t,[o.cookieDomain]:i}),window.parent==window)throw new Error("The Viverse SDK is not supported unless the game is embedded on the viverse.com.");const c=await n[o.checkAuth]();l=c?.access_token||null;const d=new globalThis[o.viverse][o.storage];s=await d[o.newCloudSaveClient](e)},customRequests:{async setStorageItem(e,t){const i=JSON.stringify(t);if(l)await s[o.setPlayerData](e,{s:i},l);else try{localStorage.setItem(c+e,i)}catch{r.set(e,i)}},async getStorageItem(e){let t;if(l){const i=await s[o.getPlayerData](e,l);t="object"==typeof i&&null!=i?i.s:null}else try{t=localStorage.getItem(c+e)}catch{t=r.get(e)}return t&&"string"==typeof t?JSON.parse(t):null},showAuthPrompt(){n[o.loginWithWorlds]()}}}}export{viversePlugin};
|
package/package.json
CHANGED
package/src/mod.js
CHANGED
|
@@ -92,7 +92,11 @@ export function viversePlugin({
|
|
|
92
92
|
// This does mean the SDK will probably stringify it a second time, but oh well.
|
|
93
93
|
const stringifiedValue = JSON.stringify(value);
|
|
94
94
|
if (accessToken) {
|
|
95
|
-
|
|
95
|
+
// If you give the viverse sdk a json string as input, the sdk will try to parse it.
|
|
96
|
+
// This is rather unexpected however, because if a develoepr calls `setStorageItem("foo", "42")`,
|
|
97
|
+
// they expect to get exactly `"42"` back when calling `getStorageItem("foo")`.
|
|
98
|
+
// So in order to prevent the sdk from parsing our strings, we'll just provide an object.
|
|
99
|
+
await cloudSaveClient[props.setPlayerData](key, { s: stringifiedValue }, accessToken);
|
|
96
100
|
} else {
|
|
97
101
|
try {
|
|
98
102
|
localStorage.setItem(localStoragePrefix + key, stringifiedValue);
|
|
@@ -109,7 +113,13 @@ export function viversePlugin({
|
|
|
109
113
|
async getStorageItem(key) {
|
|
110
114
|
let stringifiedValue;
|
|
111
115
|
if (accessToken) {
|
|
112
|
-
|
|
116
|
+
const objectifiedValue =
|
|
117
|
+
/** @type {{s: string}} */ (await cloudSaveClient[props.getPlayerData](key, accessToken));
|
|
118
|
+
if (typeof objectifiedValue == "object" && objectifiedValue != null) {
|
|
119
|
+
stringifiedValue = objectifiedValue.s;
|
|
120
|
+
} else {
|
|
121
|
+
stringifiedValue = null;
|
|
122
|
+
}
|
|
113
123
|
} else {
|
|
114
124
|
try {
|
|
115
125
|
stringifiedValue = localStorage.getItem(localStoragePrefix + key);
|