@adlad/plugin-viverse 0.3.0 → 0.5.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 +18 -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 r=null;const l=new Map,c="adlad_viverse_";return{name:"viverse",async initialize(l){if(a)throw new Error("Viverse plugin is being initialized more than once");if(a=!0,await l.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]();r=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(r)await s[o.setPlayerData](e,i,r);else try{localStorage.setItem(c+e,i)}catch{l.set(e,i)}},async getStorageItem(e){let t;if(r)
|
|
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 r=null;const l=new Map,c="adlad_viverse_";return{name:"viverse",async initialize(l){if(a)throw new Error("Viverse plugin is being initialized more than once");if(a=!0,await l.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]();r=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(r)await s[o.setPlayerData](e,{s:i},r);else{await new Promise((e=>setTimeout(e,1e3)));try{localStorage.setItem(c+e,i)}catch{l.set(e,i)}}},async getStorageItem(e){let t;if(r){const i=await s[o.getPlayerData](e,r);t="object"==typeof i&&null!=i?i.s:null}else{await new Promise((e=>setTimeout(e,1e3)));try{t=localStorage.getItem(c+e)}catch{t=l.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,8 +92,15 @@ 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 {
|
|
101
|
+
// Calls to the viverse sdk normally do not resolve instantly, which might cause bugs in the application.
|
|
102
|
+
// Let's simulate this behavior so it's easier to reproduce these issues in local development.
|
|
103
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
97
104
|
try {
|
|
98
105
|
localStorage.setItem(localStoragePrefix + key, stringifiedValue);
|
|
99
106
|
} catch {
|
|
@@ -109,8 +116,17 @@ export function viversePlugin({
|
|
|
109
116
|
async getStorageItem(key) {
|
|
110
117
|
let stringifiedValue;
|
|
111
118
|
if (accessToken) {
|
|
112
|
-
|
|
119
|
+
const objectifiedValue =
|
|
120
|
+
/** @type {{s: string}} */ (await cloudSaveClient[props.getPlayerData](key, accessToken));
|
|
121
|
+
if (typeof objectifiedValue == "object" && objectifiedValue != null) {
|
|
122
|
+
stringifiedValue = objectifiedValue.s;
|
|
123
|
+
} else {
|
|
124
|
+
stringifiedValue = null;
|
|
125
|
+
}
|
|
113
126
|
} else {
|
|
127
|
+
// Calls to the viverse sdk normally do not resolve instantly, which might cause bugs in the application.
|
|
128
|
+
// Let's simulate this behavior so it's easier to reproduce these issues in local development.
|
|
129
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
114
130
|
try {
|
|
115
131
|
stringifiedValue = localStorage.getItem(localStoragePrefix + key);
|
|
116
132
|
} catch {
|