@adlad/plugin-viverse 0.2.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.
@@ -1 +1 @@
1
- function viversePlugin({appId:e,domain:t="account.htcvive.com",cookieDomain:a}){let i=!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(i)throw new Error("Viverse plugin is being initialized more than once");i=!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]:a});const c=await n[o.checkAuth]();l=c?.access_token||null;const v=new globalThis[o.viverse][o.storage];s=await v[o.newCloudSaveClient](e)},customRequests:{async setStorageItem(e,t){const a=JSON.stringify(t);if(l)await s[o.setPlayerData](e,a,l);else try{localStorage.setItem(c+e,a)}catch{r.set(e,a)}},async getStorageItem(e){let t;if(l)t=await s[o.getPlayerData](e,l);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};
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adlad/plugin-viverse",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "AdLad plugin for the Viverse sdk.",
5
5
  "main": "mod.js",
6
6
  "types": "dist/adlad-plugin-viverse.d.ts",
package/src/mod.js CHANGED
@@ -68,6 +68,13 @@ export function viversePlugin({
68
68
  [props.cookieDomain]: cookieDomain,
69
69
  });
70
70
 
71
+ // If we're not running in an iframe, the client.checkAuth() promise stays pending forever.
72
+ // But since it is pretty common to run games in local development without an iframe, we'll also make this check here and throw an error.
73
+ // That way at least some of the api calls stay working.
74
+ if (window.parent == window) {
75
+ throw new Error("The Viverse SDK is not supported unless the game is embedded on the viverse.com.");
76
+ }
77
+
71
78
  const authResult = await client[props.checkAuth]();
72
79
  accessToken = authResult?.access_token || null;
73
80
 
@@ -85,7 +92,11 @@ export function viversePlugin({
85
92
  // This does mean the SDK will probably stringify it a second time, but oh well.
86
93
  const stringifiedValue = JSON.stringify(value);
87
94
  if (accessToken) {
88
- await cloudSaveClient[props.setPlayerData](key, stringifiedValue, accessToken);
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);
89
100
  } else {
90
101
  try {
91
102
  localStorage.setItem(localStoragePrefix + key, stringifiedValue);
@@ -102,7 +113,13 @@ export function viversePlugin({
102
113
  async getStorageItem(key) {
103
114
  let stringifiedValue;
104
115
  if (accessToken) {
105
- stringifiedValue = await cloudSaveClient[props.getPlayerData](key, accessToken);
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
+ }
106
123
  } else {
107
124
  try {
108
125
  stringifiedValue = localStorage.getItem(localStoragePrefix + key);