UNPKG

rune-sdk

Version:

Build a multiplayer game played by millions! Your game runs inside the Rune app with 10 million installs across [iOS](https://apps.apple.com/app/rune-games-and-voice-chat/id1450358364) and [Android](https://play.google.com/store/apps/details?id=ai.rune.ti

10 lines (8 loc) 6.05 kB
/* Copyright (c) 2024 Rune AI Inc. All rights reserved. This code is proprietary to Rune AI Inc. The code may be used solely for accessing the Service provided by Rune AI Inc. following the Rune AI Inc. Terms of Service ("Terms") accessible at rune.ai/eula. You may not use this code for any use or purpose other than as expressly permitted by the Terms. Restrictions set forth in the Terms include, but is not limited to, that you may not copy, adapt, modify, prepare derivative works based upon, distribute, license, sell, transfer, publicly display, publicly perform, transmit, stream, broadcast, attempt to discover any source code, reverse engineer, decompile, dissemble, or otherwise exploit the code as a whole or any portion of the code. */ "use strict";var e=require("rune-msgpack"),t=require("base-64"),r=require("compatto");function o(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var n=o(e),i=o(t),s=function(){function e(){}return e.prototype.encodeClientToGame=function(e){return JSON.stringify(e)},e.prototype.encodeGameToClient=function(e){return JSON.stringify(e)},e.prototype.decodeClientToGame=function(e){if("string"==typeof e)try{return JSON.parse(e)}catch(e){return}},e.prototype.decodeGameToClient=function(e){if("string"==typeof e)try{return JSON.parse(e)}catch(e){return}},e}(),c=function(e){for(var t=[],r=0,o=e.length;r<o;r++)t.push(String.fromCharCode(e[r]));return t.join("")},a=function(e){return i.encode(e)},d=function(e){return e.charCodeAt(0)},u=function(e){return Uint8Array.from(e,d)},p=function(e){return i.decode(e)};function f(e){var t=n.encode(e,{maxDepth:25});return"RUNE_MSG_V2;"+a(c(t))}var m,v=new(function(){function e(){this.lastDecodedValue="",this.lastDecodedMessage=void 0}return e.prototype.isClientMessage=function(e){if(!this.isRuneMsg(e))return!1;var t=this.decode(e);return t.runeGameEvent||t.runeGameCommand},e.prototype.decodeGameToClient=function(e){if(this.isRuneMsg(e))return this.decode(e).runeGameEvent},e.prototype.decodeClientToGame=function(e){if(this.isRuneMsg(e))return this.decode(e).runeGameCommand},e.prototype.decodeGameToServer=function(e){if(this.isRuneMsg(e))return this.decode(e).gameToServer},e.prototype.decodeServerToGame=function(e){if(this.isRuneMsg(e))return this.decode(e).serverToGame},e.prototype.isRuneMsg=function(e){return"string"==typeof e&&e.startsWith("RUNE_MSG_V2;")},e.prototype.decode=function(e){return e===this.lastDecodedValue||(this.lastDecodedValue=e,this.lastDecodedMessage=function(e){var t=u(p(e.slice("RUNE_MSG_V2;".length)));return n.decode(t)}(e)),this.lastDecodedMessage},e.prototype.encodeClientToGame=function(e){return f({runeGameCommand:e})},e.prototype.encodeGameToClient=function(e){return f({runeGameEvent:e})},e.prototype.encodeGameToServer=function(e){return f({gameToServer:e})},e.prototype.encodeServerToGame=function(e){return f({serverToGame:e})},e}());function l(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var o,n,i=r.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(o=i.next()).done;)s.push(o.value)}catch(e){n={error:e}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return s}!function(e){e.GameToServer="G",e.ServerToGame="S"}(m||(m={}));function h(e){var t=l(e.split(";"),2),r=t[0],o=t[1];if(r&&o){var n=l(r.split(","),4),i=n[0],s=n[1],c=n[2],a=n[3];if(!i||!s||!a)throw new Error("Missing fields: "+i+","+s+","+a+" - "+r+" - "+e);var d=Number.parseInt(i),u=Number.parseInt(s);if(Number.isNaN(d))throw new Error("Invalid version number provided in game message: ".concat(d));if(Number.isNaN(u))throw new Error("Invalid game ID provided in game message: ".concat(u));if(!Object.values(m).includes(a))throw new Error("Invalid message type provided in game message: ".concat(a));return{version:d,gameId:u,sessionId:0===c.length?void 0:c,type:a}}}var y=function(){function e(e,t,o,n,i){this.compatto=r.compatto({dictionary:[]}),this.compatto=r.compatto({dictionary:o}),this.gameId=e,this.expectedSessionId=t,this.logger=n,this.sdkProtocolVersion=i}return e.prototype.encodeServerToGame=function(e,t){return void 0===t&&(t=!1),this.encodeHeader(m.ServerToGame)+";"+this.encodeBody(e,t)},e.prototype.encodeGameToServer=function(e,t){return void 0===t&&(t=!1),this.encodeHeader(m.GameToServer)+";"+this.encodeBody(e,t)},e.prototype.encodeBody=function(e,t){var r=n.encode(e,{maxDepth:25});return this.sdkProtocolVersion>=4&&this.compatto&&t?"_"+a(c(this.compatto.compress(c(r)))):a(c(r))},e.prototype.decodeGameToServer=function(e,t){if("string"==typeof e){var r=h(e);if(r&&r.type===m.GameToServer)return this.decodeBody(r,e,t)}},e.prototype.decodeServerToGame=function(e,t){if("string"==typeof e){var r=h(e);if(r&&r.type===m.ServerToGame)return this.decodeBody(r,e,t)}},e.prototype.extractBody=function(e){var t=l(e.split(";"),2);return t[0],t[1]},e.prototype.decodeBody=function(e,t,r){var o,i=this.extractBody(t),s=i.startsWith("_");if(s){if(e.sessionId!==this.expectedSessionId)return void this.logger.onSessionMismatch(null!==(o=e.sessionId)&&void 0!==o?o:null,this.expectedSessionId,r);i=i.substring(1)}var c=u(p(i));return s&&(c=u(this.compatto.decompress(c))),n.decode(c)},e.prototype.encodeHeader=function(e){var t;return this.sdkProtocolVersion+","+this.gameId+","+(null!==(t=this.expectedSessionId)&&void 0!==t?t:"")+","+e},e}();exports.CLIENT_PROTOCOL_VERSION_LATEST=3,exports.CLIENT_PROTOCOL_VERSION_RUNE_MSG=2,exports.CLIENT_PROTOCOL_VERSION_SIMPLE_JSON=3,exports.SDK_PROTOCOL_VERSION_LATEST=4,exports.SDK_PROTOCOL_VERSION_NEW_HEADER=3,exports.SDK_PROTOCOL_VERSION_RUNE_MSG=2,exports.SDK_PROTOCOL_VERSION_WITH_COMPRESSION=4,exports.createClientMessageCodecForVersion=function(e){return e>2?new s:v},exports.createSdkMessageCodecForVersion=function(e){var t=e.sdkProtocolVersion,r=e.gameId,o=e.sessionId,n=e.dictionary,i=e.logger;return t>2?new y(r,o,n,i,t):v};