Discord integration for Gamecraft
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Core.cs 143KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace Discord
  5. {
  6. public enum Result
  7. {
  8. Ok = 0,
  9. ServiceUnavailable = 1,
  10. InvalidVersion = 2,
  11. LockFailed = 3,
  12. InternalError = 4,
  13. InvalidPayload = 5,
  14. InvalidCommand = 6,
  15. InvalidPermissions = 7,
  16. NotFetched = 8,
  17. NotFound = 9,
  18. Conflict = 10,
  19. InvalidSecret = 11,
  20. InvalidJoinSecret = 12,
  21. NoEligibleActivity = 13,
  22. InvalidInvite = 14,
  23. NotAuthenticated = 15,
  24. InvalidAccessToken = 16,
  25. ApplicationMismatch = 17,
  26. InvalidDataUrl = 18,
  27. InvalidBase64 = 19,
  28. NotFiltered = 20,
  29. LobbyFull = 21,
  30. InvalidLobbySecret = 22,
  31. InvalidFilename = 23,
  32. InvalidFileSize = 24,
  33. InvalidEntitlement = 25,
  34. NotInstalled = 26,
  35. NotRunning = 27,
  36. InsufficientBuffer = 28,
  37. PurchaseCanceled = 29,
  38. InvalidGuild = 30,
  39. InvalidEvent = 31,
  40. InvalidChannel = 32,
  41. InvalidOrigin = 33,
  42. RateLimited = 34,
  43. OAuth2Error = 35,
  44. SelectChannelTimeout = 36,
  45. GetGuildTimeout = 37,
  46. SelectVoiceForceRequired = 38,
  47. CaptureShortcutAlreadyListening = 39,
  48. UnauthorizedForAchievement = 40,
  49. InvalidGiftCode = 41,
  50. PurchaseError = 42,
  51. TransactionAborted = 43,
  52. }
  53. public enum CreateFlags
  54. {
  55. Default = 0,
  56. NoRequireDiscord = 1,
  57. }
  58. public enum LogLevel
  59. {
  60. Error = 1,
  61. Warn,
  62. Info,
  63. Debug,
  64. }
  65. public enum UserFlag
  66. {
  67. Partner = 2,
  68. HypeSquadEvents = 4,
  69. HypeSquadHouse1 = 64,
  70. HypeSquadHouse2 = 128,
  71. HypeSquadHouse3 = 256,
  72. }
  73. public enum PremiumType
  74. {
  75. None = 0,
  76. Tier1 = 1,
  77. Tier2 = 2,
  78. }
  79. public enum ImageType
  80. {
  81. User,
  82. }
  83. public enum ActivityType
  84. {
  85. Playing,
  86. Streaming,
  87. Listening,
  88. Watching,
  89. }
  90. public enum ActivityActionType
  91. {
  92. Join = 1,
  93. Spectate,
  94. }
  95. public enum ActivityJoinRequestReply
  96. {
  97. No,
  98. Yes,
  99. Ignore,
  100. }
  101. public enum Status
  102. {
  103. Offline = 0,
  104. Online = 1,
  105. Idle = 2,
  106. DoNotDisturb = 3,
  107. }
  108. public enum RelationshipType
  109. {
  110. None,
  111. Friend,
  112. Blocked,
  113. PendingIncoming,
  114. PendingOutgoing,
  115. Implicit,
  116. }
  117. public enum LobbyType
  118. {
  119. Private = 1,
  120. Public,
  121. }
  122. public enum LobbySearchComparison
  123. {
  124. LessThanOrEqual = -2,
  125. LessThan,
  126. Equal,
  127. GreaterThan,
  128. GreaterThanOrEqual,
  129. NotEqual,
  130. }
  131. public enum LobbySearchCast
  132. {
  133. String = 1,
  134. Number,
  135. }
  136. public enum LobbySearchDistance
  137. {
  138. Local,
  139. Default,
  140. Extended,
  141. Global,
  142. }
  143. public enum EntitlementType
  144. {
  145. Purchase = 1,
  146. PremiumSubscription,
  147. DeveloperGift,
  148. TestModePurchase,
  149. FreePurchase,
  150. UserGift,
  151. PremiumPurchase,
  152. }
  153. public enum SkuType
  154. {
  155. Application = 1,
  156. DLC,
  157. Consumable,
  158. Bundle,
  159. }
  160. public enum InputModeType
  161. {
  162. VoiceActivity = 0,
  163. PushToTalk,
  164. }
  165. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  166. public partial struct User
  167. {
  168. public Int64 Id;
  169. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  170. public string Username;
  171. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
  172. public string Discriminator;
  173. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  174. public string Avatar;
  175. public bool Bot;
  176. }
  177. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  178. public partial struct OAuth2Token
  179. {
  180. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  181. public string AccessToken;
  182. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
  183. public string Scopes;
  184. public Int64 Expires;
  185. }
  186. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  187. public partial struct ImageHandle
  188. {
  189. public ImageType Type;
  190. public Int64 Id;
  191. public UInt32 Size;
  192. }
  193. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  194. public partial struct ImageDimensions
  195. {
  196. public UInt32 Width;
  197. public UInt32 Height;
  198. }
  199. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  200. public partial struct ActivityTimestamps
  201. {
  202. public Int64 Start;
  203. public Int64 End;
  204. }
  205. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  206. public partial struct ActivityAssets
  207. {
  208. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  209. public string LargeImage;
  210. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  211. public string LargeText;
  212. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  213. public string SmallImage;
  214. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  215. public string SmallText;
  216. }
  217. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  218. public partial struct PartySize
  219. {
  220. public Int32 CurrentSize;
  221. public Int32 MaxSize;
  222. }
  223. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  224. public partial struct ActivityParty
  225. {
  226. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  227. public string Id;
  228. public PartySize Size;
  229. }
  230. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  231. public partial struct ActivitySecrets
  232. {
  233. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  234. public string Match;
  235. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  236. public string Join;
  237. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  238. public string Spectate;
  239. }
  240. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  241. public partial struct Activity
  242. {
  243. public ActivityType Type;
  244. public Int64 ApplicationId;
  245. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  246. public string Name;
  247. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  248. public string State;
  249. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  250. public string Details;
  251. public ActivityTimestamps Timestamps;
  252. public ActivityAssets Assets;
  253. public ActivityParty Party;
  254. public ActivitySecrets Secrets;
  255. public bool Instance;
  256. }
  257. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  258. public partial struct Presence
  259. {
  260. public Status Status;
  261. public Activity Activity;
  262. }
  263. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  264. public partial struct Relationship
  265. {
  266. public RelationshipType Type;
  267. public User User;
  268. public Presence Presence;
  269. }
  270. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  271. public partial struct Lobby
  272. {
  273. public Int64 Id;
  274. public LobbyType Type;
  275. public Int64 OwnerId;
  276. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  277. public string Secret;
  278. public UInt32 Capacity;
  279. public bool Locked;
  280. }
  281. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  282. public partial struct FileStat
  283. {
  284. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  285. public string Filename;
  286. public UInt64 Size;
  287. public UInt64 LastModified;
  288. }
  289. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  290. public partial struct Entitlement
  291. {
  292. public Int64 Id;
  293. public EntitlementType Type;
  294. public Int64 SkuId;
  295. }
  296. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  297. public partial struct SkuPrice
  298. {
  299. public UInt32 Amount;
  300. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
  301. public string Currency;
  302. }
  303. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  304. public partial struct Sku
  305. {
  306. public Int64 Id;
  307. public SkuType Type;
  308. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  309. public string Name;
  310. public SkuPrice Price;
  311. }
  312. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  313. public partial struct InputMode
  314. {
  315. public InputModeType Type;
  316. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  317. public string Shortcut;
  318. }
  319. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  320. public partial struct UserAchievement
  321. {
  322. public Int64 UserId;
  323. public Int64 AchievementId;
  324. public byte PercentComplete;
  325. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
  326. public string UnlockedAt;
  327. }
  328. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  329. public partial struct LobbyTransaction
  330. {
  331. [StructLayout(LayoutKind.Sequential)]
  332. internal partial struct FFIMethods
  333. {
  334. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  335. internal delegate Result SetTypeMethod(IntPtr methodsPtr, LobbyType type);
  336. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  337. internal delegate Result SetOwnerMethod(IntPtr methodsPtr, Int64 ownerId);
  338. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  339. internal delegate Result SetCapacityMethod(IntPtr methodsPtr, UInt32 capacity);
  340. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  341. internal delegate Result SetMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, [MarshalAs(UnmanagedType.LPStr)]string value);
  342. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  343. internal delegate Result DeleteMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key);
  344. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  345. internal delegate Result SetLockedMethod(IntPtr methodsPtr, bool locked);
  346. internal SetTypeMethod SetType;
  347. internal SetOwnerMethod SetOwner;
  348. internal SetCapacityMethod SetCapacity;
  349. internal SetMetadataMethod SetMetadata;
  350. internal DeleteMetadataMethod DeleteMetadata;
  351. internal SetLockedMethod SetLocked;
  352. }
  353. internal IntPtr MethodsPtr;
  354. internal Object MethodsStructure;
  355. private FFIMethods Methods
  356. {
  357. get
  358. {
  359. if (MethodsStructure == null)
  360. {
  361. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  362. }
  363. return (FFIMethods)MethodsStructure;
  364. }
  365. }
  366. public void SetType(LobbyType type)
  367. {
  368. if (MethodsPtr != IntPtr.Zero)
  369. {
  370. var res = Methods.SetType(MethodsPtr, type);
  371. if (res != Result.Ok)
  372. {
  373. throw new ResultException(res);
  374. }
  375. }
  376. }
  377. public void SetOwner(Int64 ownerId)
  378. {
  379. if (MethodsPtr != IntPtr.Zero)
  380. {
  381. var res = Methods.SetOwner(MethodsPtr, ownerId);
  382. if (res != Result.Ok)
  383. {
  384. throw new ResultException(res);
  385. }
  386. }
  387. }
  388. public void SetCapacity(UInt32 capacity)
  389. {
  390. if (MethodsPtr != IntPtr.Zero)
  391. {
  392. var res = Methods.SetCapacity(MethodsPtr, capacity);
  393. if (res != Result.Ok)
  394. {
  395. throw new ResultException(res);
  396. }
  397. }
  398. }
  399. public void SetMetadata(string key, string value)
  400. {
  401. if (MethodsPtr != IntPtr.Zero)
  402. {
  403. var res = Methods.SetMetadata(MethodsPtr, key, value);
  404. if (res != Result.Ok)
  405. {
  406. throw new ResultException(res);
  407. }
  408. }
  409. }
  410. public void DeleteMetadata(string key)
  411. {
  412. if (MethodsPtr != IntPtr.Zero)
  413. {
  414. var res = Methods.DeleteMetadata(MethodsPtr, key);
  415. if (res != Result.Ok)
  416. {
  417. throw new ResultException(res);
  418. }
  419. }
  420. }
  421. public void SetLocked(bool locked)
  422. {
  423. if (MethodsPtr != IntPtr.Zero)
  424. {
  425. var res = Methods.SetLocked(MethodsPtr, locked);
  426. if (res != Result.Ok)
  427. {
  428. throw new ResultException(res);
  429. }
  430. }
  431. }
  432. }
  433. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  434. public partial struct LobbyMemberTransaction
  435. {
  436. [StructLayout(LayoutKind.Sequential)]
  437. internal partial struct FFIMethods
  438. {
  439. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  440. internal delegate Result SetMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, [MarshalAs(UnmanagedType.LPStr)]string value);
  441. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  442. internal delegate Result DeleteMetadataMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key);
  443. internal SetMetadataMethod SetMetadata;
  444. internal DeleteMetadataMethod DeleteMetadata;
  445. }
  446. internal IntPtr MethodsPtr;
  447. internal Object MethodsStructure;
  448. private FFIMethods Methods
  449. {
  450. get
  451. {
  452. if (MethodsStructure == null)
  453. {
  454. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  455. }
  456. return (FFIMethods)MethodsStructure;
  457. }
  458. }
  459. public void SetMetadata(string key, string value)
  460. {
  461. if (MethodsPtr != IntPtr.Zero)
  462. {
  463. var res = Methods.SetMetadata(MethodsPtr, key, value);
  464. if (res != Result.Ok)
  465. {
  466. throw new ResultException(res);
  467. }
  468. }
  469. }
  470. public void DeleteMetadata(string key)
  471. {
  472. if (MethodsPtr != IntPtr.Zero)
  473. {
  474. var res = Methods.DeleteMetadata(MethodsPtr, key);
  475. if (res != Result.Ok)
  476. {
  477. throw new ResultException(res);
  478. }
  479. }
  480. }
  481. }
  482. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  483. public partial struct LobbySearchQuery
  484. {
  485. [StructLayout(LayoutKind.Sequential)]
  486. internal partial struct FFIMethods
  487. {
  488. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  489. internal delegate Result FilterMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, LobbySearchComparison comparison, LobbySearchCast cast, [MarshalAs(UnmanagedType.LPStr)]string value);
  490. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  491. internal delegate Result SortMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string key, LobbySearchCast cast, [MarshalAs(UnmanagedType.LPStr)]string value);
  492. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  493. internal delegate Result LimitMethod(IntPtr methodsPtr, UInt32 limit);
  494. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  495. internal delegate Result DistanceMethod(IntPtr methodsPtr, LobbySearchDistance distance);
  496. internal FilterMethod Filter;
  497. internal SortMethod Sort;
  498. internal LimitMethod Limit;
  499. internal DistanceMethod Distance;
  500. }
  501. internal IntPtr MethodsPtr;
  502. internal Object MethodsStructure;
  503. private FFIMethods Methods
  504. {
  505. get
  506. {
  507. if (MethodsStructure == null)
  508. {
  509. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  510. }
  511. return (FFIMethods)MethodsStructure;
  512. }
  513. }
  514. public void Filter(string key, LobbySearchComparison comparison, LobbySearchCast cast, string value)
  515. {
  516. if (MethodsPtr != IntPtr.Zero)
  517. {
  518. var res = Methods.Filter(MethodsPtr, key, comparison, cast, value);
  519. if (res != Result.Ok)
  520. {
  521. throw new ResultException(res);
  522. }
  523. }
  524. }
  525. public void Sort(string key, LobbySearchCast cast, string value)
  526. {
  527. if (MethodsPtr != IntPtr.Zero)
  528. {
  529. var res = Methods.Sort(MethodsPtr, key, cast, value);
  530. if (res != Result.Ok)
  531. {
  532. throw new ResultException(res);
  533. }
  534. }
  535. }
  536. public void Limit(UInt32 limit)
  537. {
  538. if (MethodsPtr != IntPtr.Zero)
  539. {
  540. var res = Methods.Limit(MethodsPtr, limit);
  541. if (res != Result.Ok)
  542. {
  543. throw new ResultException(res);
  544. }
  545. }
  546. }
  547. public void Distance(LobbySearchDistance distance)
  548. {
  549. if (MethodsPtr != IntPtr.Zero)
  550. {
  551. var res = Methods.Distance(MethodsPtr, distance);
  552. if (res != Result.Ok)
  553. {
  554. throw new ResultException(res);
  555. }
  556. }
  557. }
  558. }
  559. public partial class ResultException : Exception
  560. {
  561. public readonly Result Result;
  562. public ResultException(Result result) : base(result.ToString())
  563. {
  564. }
  565. }
  566. public partial class Discord : IDisposable
  567. {
  568. [StructLayout(LayoutKind.Sequential)]
  569. internal partial struct FFIEvents
  570. {
  571. }
  572. [StructLayout(LayoutKind.Sequential)]
  573. internal partial struct FFIMethods
  574. {
  575. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  576. internal delegate void DestroyHandler(IntPtr MethodsPtr);
  577. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  578. internal delegate Result RunCallbacksMethod(IntPtr methodsPtr);
  579. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  580. internal delegate void SetLogHookCallback(IntPtr ptr, LogLevel level, [MarshalAs(UnmanagedType.LPStr)]string message);
  581. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  582. internal delegate void SetLogHookMethod(IntPtr methodsPtr, LogLevel minLevel, IntPtr callbackData, SetLogHookCallback callback);
  583. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  584. internal delegate IntPtr GetApplicationManagerMethod(IntPtr discordPtr);
  585. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  586. internal delegate IntPtr GetUserManagerMethod(IntPtr discordPtr);
  587. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  588. internal delegate IntPtr GetImageManagerMethod(IntPtr discordPtr);
  589. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  590. internal delegate IntPtr GetActivityManagerMethod(IntPtr discordPtr);
  591. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  592. internal delegate IntPtr GetRelationshipManagerMethod(IntPtr discordPtr);
  593. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  594. internal delegate IntPtr GetLobbyManagerMethod(IntPtr discordPtr);
  595. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  596. internal delegate IntPtr GetNetworkManagerMethod(IntPtr discordPtr);
  597. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  598. internal delegate IntPtr GetOverlayManagerMethod(IntPtr discordPtr);
  599. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  600. internal delegate IntPtr GetStorageManagerMethod(IntPtr discordPtr);
  601. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  602. internal delegate IntPtr GetStoreManagerMethod(IntPtr discordPtr);
  603. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  604. internal delegate IntPtr GetVoiceManagerMethod(IntPtr discordPtr);
  605. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  606. internal delegate IntPtr GetAchievementManagerMethod(IntPtr discordPtr);
  607. internal DestroyHandler Destroy;
  608. internal RunCallbacksMethod RunCallbacks;
  609. internal SetLogHookMethod SetLogHook;
  610. internal GetApplicationManagerMethod GetApplicationManager;
  611. internal GetUserManagerMethod GetUserManager;
  612. internal GetImageManagerMethod GetImageManager;
  613. internal GetActivityManagerMethod GetActivityManager;
  614. internal GetRelationshipManagerMethod GetRelationshipManager;
  615. internal GetLobbyManagerMethod GetLobbyManager;
  616. internal GetNetworkManagerMethod GetNetworkManager;
  617. internal GetOverlayManagerMethod GetOverlayManager;
  618. internal GetStorageManagerMethod GetStorageManager;
  619. internal GetStoreManagerMethod GetStoreManager;
  620. internal GetVoiceManagerMethod GetVoiceManager;
  621. internal GetAchievementManagerMethod GetAchievementManager;
  622. }
  623. [StructLayout(LayoutKind.Sequential)]
  624. internal partial struct FFICreateParams
  625. {
  626. internal Int64 ClientId;
  627. internal UInt64 Flags;
  628. internal IntPtr Events;
  629. internal IntPtr EventData;
  630. internal IntPtr ApplicationEvents;
  631. internal UInt32 ApplicationVersion;
  632. internal IntPtr UserEvents;
  633. internal UInt32 UserVersion;
  634. internal IntPtr ImageEvents;
  635. internal UInt32 ImageVersion;
  636. internal IntPtr ActivityEvents;
  637. internal UInt32 ActivityVersion;
  638. internal IntPtr RelationshipEvents;
  639. internal UInt32 RelationshipVersion;
  640. internal IntPtr LobbyEvents;
  641. internal UInt32 LobbyVersion;
  642. internal IntPtr NetworkEvents;
  643. internal UInt32 NetworkVersion;
  644. internal IntPtr OverlayEvents;
  645. internal UInt32 OverlayVersion;
  646. internal IntPtr StorageEvents;
  647. internal UInt32 StorageVersion;
  648. internal IntPtr StoreEvents;
  649. internal UInt32 StoreVersion;
  650. internal IntPtr VoiceEvents;
  651. internal UInt32 VoiceVersion;
  652. internal IntPtr AchievementEvents;
  653. internal UInt32 AchievementVersion;
  654. }
  655. [DllImport(Constants.DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
  656. private static extern Result DiscordCreate(UInt32 version, ref FFICreateParams createParams, out IntPtr manager);
  657. public delegate void SetLogHookHandler(LogLevel level, string message);
  658. private GCHandle SelfHandle;
  659. private IntPtr EventsPtr;
  660. private FFIEvents Events;
  661. private IntPtr ApplicationEventsPtr;
  662. private ApplicationManager.FFIEvents ApplicationEvents;
  663. internal ApplicationManager ApplicationManagerInstance;
  664. private IntPtr UserEventsPtr;
  665. private UserManager.FFIEvents UserEvents;
  666. internal UserManager UserManagerInstance;
  667. private IntPtr ImageEventsPtr;
  668. private ImageManager.FFIEvents ImageEvents;
  669. internal ImageManager ImageManagerInstance;
  670. private IntPtr ActivityEventsPtr;
  671. private ActivityManager.FFIEvents ActivityEvents;
  672. internal ActivityManager ActivityManagerInstance;
  673. private IntPtr RelationshipEventsPtr;
  674. private RelationshipManager.FFIEvents RelationshipEvents;
  675. internal RelationshipManager RelationshipManagerInstance;
  676. private IntPtr LobbyEventsPtr;
  677. private LobbyManager.FFIEvents LobbyEvents;
  678. internal LobbyManager LobbyManagerInstance;
  679. private IntPtr NetworkEventsPtr;
  680. private NetworkManager.FFIEvents NetworkEvents;
  681. internal NetworkManager NetworkManagerInstance;
  682. private IntPtr OverlayEventsPtr;
  683. private OverlayManager.FFIEvents OverlayEvents;
  684. internal OverlayManager OverlayManagerInstance;
  685. private IntPtr StorageEventsPtr;
  686. private StorageManager.FFIEvents StorageEvents;
  687. internal StorageManager StorageManagerInstance;
  688. private IntPtr StoreEventsPtr;
  689. private StoreManager.FFIEvents StoreEvents;
  690. internal StoreManager StoreManagerInstance;
  691. private IntPtr VoiceEventsPtr;
  692. private VoiceManager.FFIEvents VoiceEvents;
  693. internal VoiceManager VoiceManagerInstance;
  694. private IntPtr AchievementEventsPtr;
  695. private AchievementManager.FFIEvents AchievementEvents;
  696. internal AchievementManager AchievementManagerInstance;
  697. private IntPtr MethodsPtr;
  698. private Object MethodsStructure;
  699. private FFIMethods Methods
  700. {
  701. get
  702. {
  703. if (MethodsStructure == null)
  704. {
  705. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  706. }
  707. return (FFIMethods)MethodsStructure;
  708. }
  709. }
  710. private GCHandle? setLogHook;
  711. public Discord(Int64 clientId, UInt64 flags)
  712. {
  713. FFICreateParams createParams;
  714. createParams.ClientId = clientId;
  715. createParams.Flags = flags;
  716. Events = new FFIEvents();
  717. EventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Events));
  718. createParams.Events = EventsPtr;
  719. SelfHandle = GCHandle.Alloc(this);
  720. createParams.EventData = GCHandle.ToIntPtr(SelfHandle);
  721. ApplicationEvents = new ApplicationManager.FFIEvents();
  722. ApplicationEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ApplicationEvents));
  723. createParams.ApplicationEvents = ApplicationEventsPtr;
  724. createParams.ApplicationVersion = 1;
  725. UserEvents = new UserManager.FFIEvents();
  726. UserEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(UserEvents));
  727. createParams.UserEvents = UserEventsPtr;
  728. createParams.UserVersion = 1;
  729. ImageEvents = new ImageManager.FFIEvents();
  730. ImageEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ImageEvents));
  731. createParams.ImageEvents = ImageEventsPtr;
  732. createParams.ImageVersion = 1;
  733. ActivityEvents = new ActivityManager.FFIEvents();
  734. ActivityEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ActivityEvents));
  735. createParams.ActivityEvents = ActivityEventsPtr;
  736. createParams.ActivityVersion = 1;
  737. RelationshipEvents = new RelationshipManager.FFIEvents();
  738. RelationshipEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(RelationshipEvents));
  739. createParams.RelationshipEvents = RelationshipEventsPtr;
  740. createParams.RelationshipVersion = 1;
  741. LobbyEvents = new LobbyManager.FFIEvents();
  742. LobbyEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(LobbyEvents));
  743. createParams.LobbyEvents = LobbyEventsPtr;
  744. createParams.LobbyVersion = 1;
  745. NetworkEvents = new NetworkManager.FFIEvents();
  746. NetworkEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(NetworkEvents));
  747. createParams.NetworkEvents = NetworkEventsPtr;
  748. createParams.NetworkVersion = 1;
  749. OverlayEvents = new OverlayManager.FFIEvents();
  750. OverlayEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(OverlayEvents));
  751. createParams.OverlayEvents = OverlayEventsPtr;
  752. createParams.OverlayVersion = 1;
  753. StorageEvents = new StorageManager.FFIEvents();
  754. StorageEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(StorageEvents));
  755. createParams.StorageEvents = StorageEventsPtr;
  756. createParams.StorageVersion = 1;
  757. StoreEvents = new StoreManager.FFIEvents();
  758. StoreEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(StoreEvents));
  759. createParams.StoreEvents = StoreEventsPtr;
  760. createParams.StoreVersion = 1;
  761. VoiceEvents = new VoiceManager.FFIEvents();
  762. VoiceEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(VoiceEvents));
  763. createParams.VoiceEvents = VoiceEventsPtr;
  764. createParams.VoiceVersion = 1;
  765. AchievementEvents = new AchievementManager.FFIEvents();
  766. AchievementEventsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(AchievementEvents));
  767. createParams.AchievementEvents = AchievementEventsPtr;
  768. createParams.AchievementVersion = 1;
  769. InitEvents(EventsPtr, ref Events);
  770. var result = DiscordCreate(2, ref createParams, out MethodsPtr);
  771. if (result != Result.Ok)
  772. {
  773. Dispose();
  774. throw new ResultException(result);
  775. }
  776. }
  777. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  778. {
  779. Marshal.StructureToPtr(events, eventsPtr, false);
  780. }
  781. public void Dispose()
  782. {
  783. if (MethodsPtr != IntPtr.Zero)
  784. {
  785. Methods.Destroy(MethodsPtr);
  786. }
  787. SelfHandle.Free();
  788. Marshal.FreeHGlobal(EventsPtr);
  789. Marshal.FreeHGlobal(ApplicationEventsPtr);
  790. Marshal.FreeHGlobal(UserEventsPtr);
  791. Marshal.FreeHGlobal(ImageEventsPtr);
  792. Marshal.FreeHGlobal(ActivityEventsPtr);
  793. Marshal.FreeHGlobal(RelationshipEventsPtr);
  794. Marshal.FreeHGlobal(LobbyEventsPtr);
  795. Marshal.FreeHGlobal(NetworkEventsPtr);
  796. Marshal.FreeHGlobal(OverlayEventsPtr);
  797. Marshal.FreeHGlobal(StorageEventsPtr);
  798. Marshal.FreeHGlobal(StoreEventsPtr);
  799. Marshal.FreeHGlobal(VoiceEventsPtr);
  800. Marshal.FreeHGlobal(AchievementEventsPtr);
  801. if (setLogHook.HasValue) {
  802. setLogHook.Value.Free();
  803. }
  804. }
  805. public void RunCallbacks()
  806. {
  807. var res = Methods.RunCallbacks(MethodsPtr);
  808. if (res != Result.Ok)
  809. {
  810. throw new ResultException(res);
  811. }
  812. }
  813. [MonoPInvokeCallback]
  814. private static void SetLogHookCallbackImpl(IntPtr ptr, LogLevel level, string message)
  815. {
  816. GCHandle h = GCHandle.FromIntPtr(ptr);
  817. SetLogHookHandler callback = (SetLogHookHandler)h.Target;
  818. callback(level, message);
  819. }
  820. public void SetLogHook(LogLevel minLevel, SetLogHookHandler callback)
  821. {
  822. if (setLogHook.HasValue) {
  823. setLogHook.Value.Free();
  824. }
  825. setLogHook = GCHandle.Alloc(callback);
  826. Methods.SetLogHook(MethodsPtr, minLevel, GCHandle.ToIntPtr(setLogHook.Value), SetLogHookCallbackImpl);
  827. }
  828. public ApplicationManager GetApplicationManager()
  829. {
  830. if (ApplicationManagerInstance == null) {
  831. ApplicationManagerInstance = new ApplicationManager(
  832. Methods.GetApplicationManager(MethodsPtr),
  833. ApplicationEventsPtr,
  834. ref ApplicationEvents
  835. );
  836. }
  837. return ApplicationManagerInstance;
  838. }
  839. public UserManager GetUserManager()
  840. {
  841. if (UserManagerInstance == null) {
  842. UserManagerInstance = new UserManager(
  843. Methods.GetUserManager(MethodsPtr),
  844. UserEventsPtr,
  845. ref UserEvents
  846. );
  847. }
  848. return UserManagerInstance;
  849. }
  850. public ImageManager GetImageManager()
  851. {
  852. if (ImageManagerInstance == null) {
  853. ImageManagerInstance = new ImageManager(
  854. Methods.GetImageManager(MethodsPtr),
  855. ImageEventsPtr,
  856. ref ImageEvents
  857. );
  858. }
  859. return ImageManagerInstance;
  860. }
  861. public ActivityManager GetActivityManager()
  862. {
  863. if (ActivityManagerInstance == null) {
  864. ActivityManagerInstance = new ActivityManager(
  865. Methods.GetActivityManager(MethodsPtr),
  866. ActivityEventsPtr,
  867. ref ActivityEvents
  868. );
  869. }
  870. return ActivityManagerInstance;
  871. }
  872. public RelationshipManager GetRelationshipManager()
  873. {
  874. if (RelationshipManagerInstance == null) {
  875. RelationshipManagerInstance = new RelationshipManager(
  876. Methods.GetRelationshipManager(MethodsPtr),
  877. RelationshipEventsPtr,
  878. ref RelationshipEvents
  879. );
  880. }
  881. return RelationshipManagerInstance;
  882. }
  883. public LobbyManager GetLobbyManager()
  884. {
  885. if (LobbyManagerInstance == null) {
  886. LobbyManagerInstance = new LobbyManager(
  887. Methods.GetLobbyManager(MethodsPtr),
  888. LobbyEventsPtr,
  889. ref LobbyEvents
  890. );
  891. }
  892. return LobbyManagerInstance;
  893. }
  894. public NetworkManager GetNetworkManager()
  895. {
  896. if (NetworkManagerInstance == null) {
  897. NetworkManagerInstance = new NetworkManager(
  898. Methods.GetNetworkManager(MethodsPtr),
  899. NetworkEventsPtr,
  900. ref NetworkEvents
  901. );
  902. }
  903. return NetworkManagerInstance;
  904. }
  905. public OverlayManager GetOverlayManager()
  906. {
  907. if (OverlayManagerInstance == null) {
  908. OverlayManagerInstance = new OverlayManager(
  909. Methods.GetOverlayManager(MethodsPtr),
  910. OverlayEventsPtr,
  911. ref OverlayEvents
  912. );
  913. }
  914. return OverlayManagerInstance;
  915. }
  916. public StorageManager GetStorageManager()
  917. {
  918. if (StorageManagerInstance == null) {
  919. StorageManagerInstance = new StorageManager(
  920. Methods.GetStorageManager(MethodsPtr),
  921. StorageEventsPtr,
  922. ref StorageEvents
  923. );
  924. }
  925. return StorageManagerInstance;
  926. }
  927. public StoreManager GetStoreManager()
  928. {
  929. if (StoreManagerInstance == null) {
  930. StoreManagerInstance = new StoreManager(
  931. Methods.GetStoreManager(MethodsPtr),
  932. StoreEventsPtr,
  933. ref StoreEvents
  934. );
  935. }
  936. return StoreManagerInstance;
  937. }
  938. public VoiceManager GetVoiceManager()
  939. {
  940. if (VoiceManagerInstance == null) {
  941. VoiceManagerInstance = new VoiceManager(
  942. Methods.GetVoiceManager(MethodsPtr),
  943. VoiceEventsPtr,
  944. ref VoiceEvents
  945. );
  946. }
  947. return VoiceManagerInstance;
  948. }
  949. public AchievementManager GetAchievementManager()
  950. {
  951. if (AchievementManagerInstance == null) {
  952. AchievementManagerInstance = new AchievementManager(
  953. Methods.GetAchievementManager(MethodsPtr),
  954. AchievementEventsPtr,
  955. ref AchievementEvents
  956. );
  957. }
  958. return AchievementManagerInstance;
  959. }
  960. }
  961. internal partial class MonoPInvokeCallbackAttribute : Attribute
  962. {
  963. }
  964. public partial class ApplicationManager
  965. {
  966. [StructLayout(LayoutKind.Sequential)]
  967. internal partial struct FFIEvents
  968. {
  969. }
  970. [StructLayout(LayoutKind.Sequential)]
  971. internal partial struct FFIMethods
  972. {
  973. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  974. internal delegate void ValidateOrExitCallback(IntPtr ptr, Result result);
  975. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  976. internal delegate void ValidateOrExitMethod(IntPtr methodsPtr, IntPtr callbackData, ValidateOrExitCallback callback);
  977. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  978. internal delegate void GetCurrentLocaleMethod(IntPtr methodsPtr, StringBuilder locale);
  979. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  980. internal delegate void GetCurrentBranchMethod(IntPtr methodsPtr, StringBuilder branch);
  981. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  982. internal delegate void GetOAuth2TokenCallback(IntPtr ptr, Result result, ref OAuth2Token oauth2Token);
  983. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  984. internal delegate void GetOAuth2TokenMethod(IntPtr methodsPtr, IntPtr callbackData, GetOAuth2TokenCallback callback);
  985. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  986. internal delegate void GetTicketCallback(IntPtr ptr, Result result, [MarshalAs(UnmanagedType.LPStr)]ref string data);
  987. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  988. internal delegate void GetTicketMethod(IntPtr methodsPtr, IntPtr callbackData, GetTicketCallback callback);
  989. internal ValidateOrExitMethod ValidateOrExit;
  990. internal GetCurrentLocaleMethod GetCurrentLocale;
  991. internal GetCurrentBranchMethod GetCurrentBranch;
  992. internal GetOAuth2TokenMethod GetOAuth2Token;
  993. internal GetTicketMethod GetTicket;
  994. }
  995. public delegate void ValidateOrExitHandler(Result result);
  996. public delegate void GetOAuth2TokenHandler(Result result, ref OAuth2Token oauth2Token);
  997. public delegate void GetTicketHandler(Result result, ref string data);
  998. private IntPtr MethodsPtr;
  999. private Object MethodsStructure;
  1000. private FFIMethods Methods
  1001. {
  1002. get
  1003. {
  1004. if (MethodsStructure == null)
  1005. {
  1006. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  1007. }
  1008. return (FFIMethods)MethodsStructure;
  1009. }
  1010. }
  1011. internal ApplicationManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  1012. {
  1013. if (eventsPtr == IntPtr.Zero) {
  1014. throw new ResultException(Result.InternalError);
  1015. }
  1016. InitEvents(eventsPtr, ref events);
  1017. MethodsPtr = ptr;
  1018. if (MethodsPtr == IntPtr.Zero) {
  1019. throw new ResultException(Result.InternalError);
  1020. }
  1021. }
  1022. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  1023. {
  1024. Marshal.StructureToPtr(events, eventsPtr, false);
  1025. }
  1026. [MonoPInvokeCallback]
  1027. private static void ValidateOrExitCallbackImpl(IntPtr ptr, Result result)
  1028. {
  1029. GCHandle h = GCHandle.FromIntPtr(ptr);
  1030. ValidateOrExitHandler callback = (ValidateOrExitHandler)h.Target;
  1031. h.Free();
  1032. callback(result);
  1033. }
  1034. public void ValidateOrExit(ValidateOrExitHandler callback)
  1035. {
  1036. GCHandle wrapped = GCHandle.Alloc(callback);
  1037. Methods.ValidateOrExit(MethodsPtr, GCHandle.ToIntPtr(wrapped), ValidateOrExitCallbackImpl);
  1038. }
  1039. public string GetCurrentLocale()
  1040. {
  1041. var ret = new StringBuilder(128);
  1042. Methods.GetCurrentLocale(MethodsPtr, ret);
  1043. return ret.ToString();
  1044. }
  1045. public string GetCurrentBranch()
  1046. {
  1047. var ret = new StringBuilder(4096);
  1048. Methods.GetCurrentBranch(MethodsPtr, ret);
  1049. return ret.ToString();
  1050. }
  1051. [MonoPInvokeCallback]
  1052. private static void GetOAuth2TokenCallbackImpl(IntPtr ptr, Result result, ref OAuth2Token oauth2Token)
  1053. {
  1054. GCHandle h = GCHandle.FromIntPtr(ptr);
  1055. GetOAuth2TokenHandler callback = (GetOAuth2TokenHandler)h.Target;
  1056. h.Free();
  1057. callback(result, ref oauth2Token);
  1058. }
  1059. public void GetOAuth2Token(GetOAuth2TokenHandler callback)
  1060. {
  1061. GCHandle wrapped = GCHandle.Alloc(callback);
  1062. Methods.GetOAuth2Token(MethodsPtr, GCHandle.ToIntPtr(wrapped), GetOAuth2TokenCallbackImpl);
  1063. }
  1064. [MonoPInvokeCallback]
  1065. private static void GetTicketCallbackImpl(IntPtr ptr, Result result, ref string data)
  1066. {
  1067. GCHandle h = GCHandle.FromIntPtr(ptr);
  1068. GetTicketHandler callback = (GetTicketHandler)h.Target;
  1069. h.Free();
  1070. callback(result, ref data);
  1071. }
  1072. public void GetTicket(GetTicketHandler callback)
  1073. {
  1074. GCHandle wrapped = GCHandle.Alloc(callback);
  1075. Methods.GetTicket(MethodsPtr, GCHandle.ToIntPtr(wrapped), GetTicketCallbackImpl);
  1076. }
  1077. }
  1078. public partial class UserManager
  1079. {
  1080. [StructLayout(LayoutKind.Sequential)]
  1081. internal partial struct FFIEvents
  1082. {
  1083. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1084. internal delegate void CurrentUserUpdateHandler(IntPtr ptr);
  1085. internal CurrentUserUpdateHandler OnCurrentUserUpdate;
  1086. }
  1087. [StructLayout(LayoutKind.Sequential)]
  1088. internal partial struct FFIMethods
  1089. {
  1090. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1091. internal delegate Result GetCurrentUserMethod(IntPtr methodsPtr, ref User currentUser);
  1092. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1093. internal delegate void GetUserCallback(IntPtr ptr, Result result, ref User user);
  1094. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1095. internal delegate void GetUserMethod(IntPtr methodsPtr, Int64 userId, IntPtr callbackData, GetUserCallback callback);
  1096. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1097. internal delegate Result GetCurrentUserPremiumTypeMethod(IntPtr methodsPtr, ref PremiumType premiumType);
  1098. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1099. internal delegate Result CurrentUserHasFlagMethod(IntPtr methodsPtr, UserFlag flag, ref bool hasFlag);
  1100. internal GetCurrentUserMethod GetCurrentUser;
  1101. internal GetUserMethod GetUser;
  1102. internal GetCurrentUserPremiumTypeMethod GetCurrentUserPremiumType;
  1103. internal CurrentUserHasFlagMethod CurrentUserHasFlag;
  1104. }
  1105. public delegate void GetUserHandler(Result result, ref User user);
  1106. public delegate void CurrentUserUpdateHandler();
  1107. private IntPtr MethodsPtr;
  1108. private Object MethodsStructure;
  1109. private FFIMethods Methods
  1110. {
  1111. get
  1112. {
  1113. if (MethodsStructure == null)
  1114. {
  1115. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  1116. }
  1117. return (FFIMethods)MethodsStructure;
  1118. }
  1119. }
  1120. public event CurrentUserUpdateHandler OnCurrentUserUpdate;
  1121. internal UserManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  1122. {
  1123. if (eventsPtr == IntPtr.Zero) {
  1124. throw new ResultException(Result.InternalError);
  1125. }
  1126. InitEvents(eventsPtr, ref events);
  1127. MethodsPtr = ptr;
  1128. if (MethodsPtr == IntPtr.Zero) {
  1129. throw new ResultException(Result.InternalError);
  1130. }
  1131. }
  1132. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  1133. {
  1134. events.OnCurrentUserUpdate = OnCurrentUserUpdateImpl;
  1135. Marshal.StructureToPtr(events, eventsPtr, false);
  1136. }
  1137. public User GetCurrentUser()
  1138. {
  1139. var ret = new User();
  1140. var res = Methods.GetCurrentUser(MethodsPtr, ref ret);
  1141. if (res != Result.Ok)
  1142. {
  1143. throw new ResultException(res);
  1144. }
  1145. return ret;
  1146. }
  1147. [MonoPInvokeCallback]
  1148. private static void GetUserCallbackImpl(IntPtr ptr, Result result, ref User user)
  1149. {
  1150. GCHandle h = GCHandle.FromIntPtr(ptr);
  1151. GetUserHandler callback = (GetUserHandler)h.Target;
  1152. h.Free();
  1153. callback(result, ref user);
  1154. }
  1155. public void GetUser(Int64 userId, GetUserHandler callback)
  1156. {
  1157. GCHandle wrapped = GCHandle.Alloc(callback);
  1158. Methods.GetUser(MethodsPtr, userId, GCHandle.ToIntPtr(wrapped), GetUserCallbackImpl);
  1159. }
  1160. public PremiumType GetCurrentUserPremiumType()
  1161. {
  1162. var ret = new PremiumType();
  1163. var res = Methods.GetCurrentUserPremiumType(MethodsPtr, ref ret);
  1164. if (res != Result.Ok)
  1165. {
  1166. throw new ResultException(res);
  1167. }
  1168. return ret;
  1169. }
  1170. public bool CurrentUserHasFlag(UserFlag flag)
  1171. {
  1172. var ret = new bool();
  1173. var res = Methods.CurrentUserHasFlag(MethodsPtr, flag, ref ret);
  1174. if (res != Result.Ok)
  1175. {
  1176. throw new ResultException(res);
  1177. }
  1178. return ret;
  1179. }
  1180. [MonoPInvokeCallback]
  1181. private static void OnCurrentUserUpdateImpl(IntPtr ptr)
  1182. {
  1183. GCHandle h = GCHandle.FromIntPtr(ptr);
  1184. Discord d = (Discord)h.Target;
  1185. if (d.UserManagerInstance.OnCurrentUserUpdate != null)
  1186. {
  1187. d.UserManagerInstance.OnCurrentUserUpdate.Invoke();
  1188. }
  1189. }
  1190. }
  1191. public partial class ImageManager
  1192. {
  1193. [StructLayout(LayoutKind.Sequential)]
  1194. internal partial struct FFIEvents
  1195. {
  1196. }
  1197. [StructLayout(LayoutKind.Sequential)]
  1198. internal partial struct FFIMethods
  1199. {
  1200. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1201. internal delegate void FetchCallback(IntPtr ptr, Result result, ImageHandle handleResult);
  1202. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1203. internal delegate void FetchMethod(IntPtr methodsPtr, ImageHandle handle, bool refresh, IntPtr callbackData, FetchCallback callback);
  1204. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1205. internal delegate Result GetDimensionsMethod(IntPtr methodsPtr, ImageHandle handle, ref ImageDimensions dimensions);
  1206. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1207. internal delegate Result GetDataMethod(IntPtr methodsPtr, ImageHandle handle, byte[] data, Int32 dataLen);
  1208. internal FetchMethod Fetch;
  1209. internal GetDimensionsMethod GetDimensions;
  1210. internal GetDataMethod GetData;
  1211. }
  1212. public delegate void FetchHandler(Result result, ImageHandle handleResult);
  1213. private IntPtr MethodsPtr;
  1214. private Object MethodsStructure;
  1215. private FFIMethods Methods
  1216. {
  1217. get
  1218. {
  1219. if (MethodsStructure == null)
  1220. {
  1221. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  1222. }
  1223. return (FFIMethods)MethodsStructure;
  1224. }
  1225. }
  1226. internal ImageManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  1227. {
  1228. if (eventsPtr == IntPtr.Zero) {
  1229. throw new ResultException(Result.InternalError);
  1230. }
  1231. InitEvents(eventsPtr, ref events);
  1232. MethodsPtr = ptr;
  1233. if (MethodsPtr == IntPtr.Zero) {
  1234. throw new ResultException(Result.InternalError);
  1235. }
  1236. }
  1237. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  1238. {
  1239. Marshal.StructureToPtr(events, eventsPtr, false);
  1240. }
  1241. [MonoPInvokeCallback]
  1242. private static void FetchCallbackImpl(IntPtr ptr, Result result, ImageHandle handleResult)
  1243. {
  1244. GCHandle h = GCHandle.FromIntPtr(ptr);
  1245. FetchHandler callback = (FetchHandler)h.Target;
  1246. h.Free();
  1247. callback(result, handleResult);
  1248. }
  1249. public void Fetch(ImageHandle handle, bool refresh, FetchHandler callback)
  1250. {
  1251. GCHandle wrapped = GCHandle.Alloc(callback);
  1252. Methods.Fetch(MethodsPtr, handle, refresh, GCHandle.ToIntPtr(wrapped), FetchCallbackImpl);
  1253. }
  1254. public ImageDimensions GetDimensions(ImageHandle handle)
  1255. {
  1256. var ret = new ImageDimensions();
  1257. var res = Methods.GetDimensions(MethodsPtr, handle, ref ret);
  1258. if (res != Result.Ok)
  1259. {
  1260. throw new ResultException(res);
  1261. }
  1262. return ret;
  1263. }
  1264. public void GetData(ImageHandle handle, byte[] data)
  1265. {
  1266. var res = Methods.GetData(MethodsPtr, handle, data, data.Length);
  1267. if (res != Result.Ok)
  1268. {
  1269. throw new ResultException(res);
  1270. }
  1271. }
  1272. }
  1273. public partial class ActivityManager
  1274. {
  1275. [StructLayout(LayoutKind.Sequential)]
  1276. internal partial struct FFIEvents
  1277. {
  1278. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1279. internal delegate void ActivityJoinHandler(IntPtr ptr, [MarshalAs(UnmanagedType.LPStr)]string secret);
  1280. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1281. internal delegate void ActivitySpectateHandler(IntPtr ptr, [MarshalAs(UnmanagedType.LPStr)]string secret);
  1282. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1283. internal delegate void ActivityJoinRequestHandler(IntPtr ptr, ref User user);
  1284. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1285. internal delegate void ActivityInviteHandler(IntPtr ptr, ActivityActionType type, ref User user, ref Activity activity);
  1286. internal ActivityJoinHandler OnActivityJoin;
  1287. internal ActivitySpectateHandler OnActivitySpectate;
  1288. internal ActivityJoinRequestHandler OnActivityJoinRequest;
  1289. internal ActivityInviteHandler OnActivityInvite;
  1290. }
  1291. [StructLayout(LayoutKind.Sequential)]
  1292. internal partial struct FFIMethods
  1293. {
  1294. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1295. internal delegate Result RegisterCommandMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string command);
  1296. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1297. internal delegate Result RegisterSteamMethod(IntPtr methodsPtr, UInt32 steamId);
  1298. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1299. internal delegate void UpdateActivityCallback(IntPtr ptr, Result result);
  1300. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1301. internal delegate void UpdateActivityMethod(IntPtr methodsPtr, ref Activity activity, IntPtr callbackData, UpdateActivityCallback callback);
  1302. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1303. internal delegate void ClearActivityCallback(IntPtr ptr, Result result);
  1304. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1305. internal delegate void ClearActivityMethod(IntPtr methodsPtr, IntPtr callbackData, ClearActivityCallback callback);
  1306. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1307. internal delegate void SendRequestReplyCallback(IntPtr ptr, Result result);
  1308. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1309. internal delegate void SendRequestReplyMethod(IntPtr methodsPtr, Int64 userId, ActivityJoinRequestReply reply, IntPtr callbackData, SendRequestReplyCallback callback);
  1310. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1311. internal delegate void SendInviteCallback(IntPtr ptr, Result result);
  1312. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1313. internal delegate void SendInviteMethod(IntPtr methodsPtr, Int64 userId, ActivityActionType type, [MarshalAs(UnmanagedType.LPStr)]string content, IntPtr callbackData, SendInviteCallback callback);
  1314. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1315. internal delegate void AcceptInviteCallback(IntPtr ptr, Result result);
  1316. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1317. internal delegate void AcceptInviteMethod(IntPtr methodsPtr, Int64 userId, IntPtr callbackData, AcceptInviteCallback callback);
  1318. internal RegisterCommandMethod RegisterCommand;
  1319. internal RegisterSteamMethod RegisterSteam;
  1320. internal UpdateActivityMethod UpdateActivity;
  1321. internal ClearActivityMethod ClearActivity;
  1322. internal SendRequestReplyMethod SendRequestReply;
  1323. internal SendInviteMethod SendInvite;
  1324. internal AcceptInviteMethod AcceptInvite;
  1325. }
  1326. public delegate void UpdateActivityHandler(Result result);
  1327. public delegate void ClearActivityHandler(Result result);
  1328. public delegate void SendRequestReplyHandler(Result result);
  1329. public delegate void SendInviteHandler(Result result);
  1330. public delegate void AcceptInviteHandler(Result result);
  1331. public delegate void ActivityJoinHandler(string secret);
  1332. public delegate void ActivitySpectateHandler(string secret);
  1333. public delegate void ActivityJoinRequestHandler(ref User user);
  1334. public delegate void ActivityInviteHandler(ActivityActionType type, ref User user, ref Activity activity);
  1335. private IntPtr MethodsPtr;
  1336. private Object MethodsStructure;
  1337. private FFIMethods Methods
  1338. {
  1339. get
  1340. {
  1341. if (MethodsStructure == null)
  1342. {
  1343. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  1344. }
  1345. return (FFIMethods)MethodsStructure;
  1346. }
  1347. }
  1348. public event ActivityJoinHandler OnActivityJoin;
  1349. public event ActivitySpectateHandler OnActivitySpectate;
  1350. public event ActivityJoinRequestHandler OnActivityJoinRequest;
  1351. public event ActivityInviteHandler OnActivityInvite;
  1352. internal ActivityManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  1353. {
  1354. if (eventsPtr == IntPtr.Zero) {
  1355. throw new ResultException(Result.InternalError);
  1356. }
  1357. InitEvents(eventsPtr, ref events);
  1358. MethodsPtr = ptr;
  1359. if (MethodsPtr == IntPtr.Zero) {
  1360. throw new ResultException(Result.InternalError);
  1361. }
  1362. }
  1363. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  1364. {
  1365. events.OnActivityJoin = OnActivityJoinImpl;
  1366. events.OnActivitySpectate = OnActivitySpectateImpl;
  1367. events.OnActivityJoinRequest = OnActivityJoinRequestImpl;
  1368. events.OnActivityInvite = OnActivityInviteImpl;
  1369. Marshal.StructureToPtr(events, eventsPtr, false);
  1370. }
  1371. public void RegisterCommand(string command)
  1372. {
  1373. var res = Methods.RegisterCommand(MethodsPtr, command);
  1374. if (res != Result.Ok)
  1375. {
  1376. throw new ResultException(res);
  1377. }
  1378. }
  1379. public void RegisterSteam(UInt32 steamId)
  1380. {
  1381. var res = Methods.RegisterSteam(MethodsPtr, steamId);
  1382. if (res != Result.Ok)
  1383. {
  1384. throw new ResultException(res);
  1385. }
  1386. }
  1387. [MonoPInvokeCallback]
  1388. private static void UpdateActivityCallbackImpl(IntPtr ptr, Result result)
  1389. {
  1390. GCHandle h = GCHandle.FromIntPtr(ptr);
  1391. UpdateActivityHandler callback = (UpdateActivityHandler)h.Target;
  1392. h.Free();
  1393. callback(result);
  1394. }
  1395. public void UpdateActivity(Activity activity, UpdateActivityHandler callback)
  1396. {
  1397. GCHandle wrapped = GCHandle.Alloc(callback);
  1398. Methods.UpdateActivity(MethodsPtr, ref activity, GCHandle.ToIntPtr(wrapped), UpdateActivityCallbackImpl);
  1399. }
  1400. [MonoPInvokeCallback]
  1401. private static void ClearActivityCallbackImpl(IntPtr ptr, Result result)
  1402. {
  1403. GCHandle h = GCHandle.FromIntPtr(ptr);
  1404. ClearActivityHandler callback = (ClearActivityHandler)h.Target;
  1405. h.Free();
  1406. callback(result);
  1407. }
  1408. public void ClearActivity(ClearActivityHandler callback)
  1409. {
  1410. GCHandle wrapped = GCHandle.Alloc(callback);
  1411. Methods.ClearActivity(MethodsPtr, GCHandle.ToIntPtr(wrapped), ClearActivityCallbackImpl);
  1412. }
  1413. [MonoPInvokeCallback]
  1414. private static void SendRequestReplyCallbackImpl(IntPtr ptr, Result result)
  1415. {
  1416. GCHandle h = GCHandle.FromIntPtr(ptr);
  1417. SendRequestReplyHandler callback = (SendRequestReplyHandler)h.Target;
  1418. h.Free();
  1419. callback(result);
  1420. }
  1421. public void SendRequestReply(Int64 userId, ActivityJoinRequestReply reply, SendRequestReplyHandler callback)
  1422. {
  1423. GCHandle wrapped = GCHandle.Alloc(callback);
  1424. Methods.SendRequestReply(MethodsPtr, userId, reply, GCHandle.ToIntPtr(wrapped), SendRequestReplyCallbackImpl);
  1425. }
  1426. [MonoPInvokeCallback]
  1427. private static void SendInviteCallbackImpl(IntPtr ptr, Result result)
  1428. {
  1429. GCHandle h = GCHandle.FromIntPtr(ptr);
  1430. SendInviteHandler callback = (SendInviteHandler)h.Target;
  1431. h.Free();
  1432. callback(result);
  1433. }
  1434. public void SendInvite(Int64 userId, ActivityActionType type, string content, SendInviteHandler callback)
  1435. {
  1436. GCHandle wrapped = GCHandle.Alloc(callback);
  1437. Methods.SendInvite(MethodsPtr, userId, type, content, GCHandle.ToIntPtr(wrapped), SendInviteCallbackImpl);
  1438. }
  1439. [MonoPInvokeCallback]
  1440. private static void AcceptInviteCallbackImpl(IntPtr ptr, Result result)
  1441. {
  1442. GCHandle h = GCHandle.FromIntPtr(ptr);
  1443. AcceptInviteHandler callback = (AcceptInviteHandler)h.Target;
  1444. h.Free();
  1445. callback(result);
  1446. }
  1447. public void AcceptInvite(Int64 userId, AcceptInviteHandler callback)
  1448. {
  1449. GCHandle wrapped = GCHandle.Alloc(callback);
  1450. Methods.AcceptInvite(MethodsPtr, userId, GCHandle.ToIntPtr(wrapped), AcceptInviteCallbackImpl);
  1451. }
  1452. [MonoPInvokeCallback]
  1453. private static void OnActivityJoinImpl(IntPtr ptr, string secret)
  1454. {
  1455. GCHandle h = GCHandle.FromIntPtr(ptr);
  1456. Discord d = (Discord)h.Target;
  1457. if (d.ActivityManagerInstance.OnActivityJoin != null)
  1458. {
  1459. d.ActivityManagerInstance.OnActivityJoin.Invoke(secret);
  1460. }
  1461. }
  1462. [MonoPInvokeCallback]
  1463. private static void OnActivitySpectateImpl(IntPtr ptr, string secret)
  1464. {
  1465. GCHandle h = GCHandle.FromIntPtr(ptr);
  1466. Discord d = (Discord)h.Target;
  1467. if (d.ActivityManagerInstance.OnActivitySpectate != null)
  1468. {
  1469. d.ActivityManagerInstance.OnActivitySpectate.Invoke(secret);
  1470. }
  1471. }
  1472. [MonoPInvokeCallback]
  1473. private static void OnActivityJoinRequestImpl(IntPtr ptr, ref User user)
  1474. {
  1475. GCHandle h = GCHandle.FromIntPtr(ptr);
  1476. Discord d = (Discord)h.Target;
  1477. if (d.ActivityManagerInstance.OnActivityJoinRequest != null)
  1478. {
  1479. d.ActivityManagerInstance.OnActivityJoinRequest.Invoke(ref user);
  1480. }
  1481. }
  1482. [MonoPInvokeCallback]
  1483. private static void OnActivityInviteImpl(IntPtr ptr, ActivityActionType type, ref User user, ref Activity activity)
  1484. {
  1485. GCHandle h = GCHandle.FromIntPtr(ptr);
  1486. Discord d = (Discord)h.Target;
  1487. if (d.ActivityManagerInstance.OnActivityInvite != null)
  1488. {
  1489. d.ActivityManagerInstance.OnActivityInvite.Invoke(type, ref user, ref activity);
  1490. }
  1491. }
  1492. }
  1493. public partial class RelationshipManager
  1494. {
  1495. [StructLayout(LayoutKind.Sequential)]
  1496. internal partial struct FFIEvents
  1497. {
  1498. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1499. internal delegate void RefreshHandler(IntPtr ptr);
  1500. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1501. internal delegate void RelationshipUpdateHandler(IntPtr ptr, ref Relationship relationship);
  1502. internal RefreshHandler OnRefresh;
  1503. internal RelationshipUpdateHandler OnRelationshipUpdate;
  1504. }
  1505. [StructLayout(LayoutKind.Sequential)]
  1506. internal partial struct FFIMethods
  1507. {
  1508. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1509. internal delegate bool FilterCallback(IntPtr ptr, ref Relationship relationship);
  1510. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1511. internal delegate void FilterMethod(IntPtr methodsPtr, IntPtr callbackData, FilterCallback callback);
  1512. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1513. internal delegate Result CountMethod(IntPtr methodsPtr, ref Int32 count);
  1514. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1515. internal delegate Result GetMethod(IntPtr methodsPtr, Int64 userId, ref Relationship relationship);
  1516. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1517. internal delegate Result GetAtMethod(IntPtr methodsPtr, UInt32 index, ref Relationship relationship);
  1518. internal FilterMethod Filter;
  1519. internal CountMethod Count;
  1520. internal GetMethod Get;
  1521. internal GetAtMethod GetAt;
  1522. }
  1523. public delegate bool FilterHandler(ref Relationship relationship);
  1524. public delegate void RefreshHandler();
  1525. public delegate void RelationshipUpdateHandler(ref Relationship relationship);
  1526. private IntPtr MethodsPtr;
  1527. private Object MethodsStructure;
  1528. private FFIMethods Methods
  1529. {
  1530. get
  1531. {
  1532. if (MethodsStructure == null)
  1533. {
  1534. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  1535. }
  1536. return (FFIMethods)MethodsStructure;
  1537. }
  1538. }
  1539. public event RefreshHandler OnRefresh;
  1540. public event RelationshipUpdateHandler OnRelationshipUpdate;
  1541. internal RelationshipManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  1542. {
  1543. if (eventsPtr == IntPtr.Zero) {
  1544. throw new ResultException(Result.InternalError);
  1545. }
  1546. InitEvents(eventsPtr, ref events);
  1547. MethodsPtr = ptr;
  1548. if (MethodsPtr == IntPtr.Zero) {
  1549. throw new ResultException(Result.InternalError);
  1550. }
  1551. }
  1552. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  1553. {
  1554. events.OnRefresh = OnRefreshImpl;
  1555. events.OnRelationshipUpdate = OnRelationshipUpdateImpl;
  1556. Marshal.StructureToPtr(events, eventsPtr, false);
  1557. }
  1558. [MonoPInvokeCallback]
  1559. private static bool FilterCallbackImpl(IntPtr ptr, ref Relationship relationship)
  1560. {
  1561. GCHandle h = GCHandle.FromIntPtr(ptr);
  1562. FilterHandler callback = (FilterHandler)h.Target;
  1563. return callback(ref relationship);
  1564. }
  1565. public void Filter(FilterHandler callback)
  1566. {
  1567. GCHandle wrapped = GCHandle.Alloc(callback);
  1568. Methods.Filter(MethodsPtr, GCHandle.ToIntPtr(wrapped), FilterCallbackImpl);
  1569. wrapped.Free();
  1570. }
  1571. public Int32 Count()
  1572. {
  1573. var ret = new Int32();
  1574. var res = Methods.Count(MethodsPtr, ref ret);
  1575. if (res != Result.Ok)
  1576. {
  1577. throw new ResultException(res);
  1578. }
  1579. return ret;
  1580. }
  1581. public Relationship Get(Int64 userId)
  1582. {
  1583. var ret = new Relationship();
  1584. var res = Methods.Get(MethodsPtr, userId, ref ret);
  1585. if (res != Result.Ok)
  1586. {
  1587. throw new ResultException(res);
  1588. }
  1589. return ret;
  1590. }
  1591. public Relationship GetAt(UInt32 index)
  1592. {
  1593. var ret = new Relationship();
  1594. var res = Methods.GetAt(MethodsPtr, index, ref ret);
  1595. if (res != Result.Ok)
  1596. {
  1597. throw new ResultException(res);
  1598. }
  1599. return ret;
  1600. }
  1601. [MonoPInvokeCallback]
  1602. private static void OnRefreshImpl(IntPtr ptr)
  1603. {
  1604. GCHandle h = GCHandle.FromIntPtr(ptr);
  1605. Discord d = (Discord)h.Target;
  1606. if (d.RelationshipManagerInstance.OnRefresh != null)
  1607. {
  1608. d.RelationshipManagerInstance.OnRefresh.Invoke();
  1609. }
  1610. }
  1611. [MonoPInvokeCallback]
  1612. private static void OnRelationshipUpdateImpl(IntPtr ptr, ref Relationship relationship)
  1613. {
  1614. GCHandle h = GCHandle.FromIntPtr(ptr);
  1615. Discord d = (Discord)h.Target;
  1616. if (d.RelationshipManagerInstance.OnRelationshipUpdate != null)
  1617. {
  1618. d.RelationshipManagerInstance.OnRelationshipUpdate.Invoke(ref relationship);
  1619. }
  1620. }
  1621. }
  1622. public partial class LobbyManager
  1623. {
  1624. [StructLayout(LayoutKind.Sequential)]
  1625. internal partial struct FFIEvents
  1626. {
  1627. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1628. internal delegate void LobbyUpdateHandler(IntPtr ptr, Int64 lobbyId);
  1629. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1630. internal delegate void LobbyDeleteHandler(IntPtr ptr, Int64 lobbyId, UInt32 reason);
  1631. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1632. internal delegate void MemberConnectHandler(IntPtr ptr, Int64 lobbyId, Int64 userId);
  1633. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1634. internal delegate void MemberUpdateHandler(IntPtr ptr, Int64 lobbyId, Int64 userId);
  1635. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1636. internal delegate void MemberDisconnectHandler(IntPtr ptr, Int64 lobbyId, Int64 userId);
  1637. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1638. internal delegate void LobbyMessageHandler(IntPtr ptr, Int64 lobbyId, Int64 userId, IntPtr dataPtr, Int32 dataLen);
  1639. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1640. internal delegate void SpeakingHandler(IntPtr ptr, Int64 lobbyId, Int64 userId, bool speaking);
  1641. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1642. internal delegate void NetworkMessageHandler(IntPtr ptr, Int64 lobbyId, Int64 userId, byte channelId, IntPtr dataPtr, Int32 dataLen);
  1643. internal LobbyUpdateHandler OnLobbyUpdate;
  1644. internal LobbyDeleteHandler OnLobbyDelete;
  1645. internal MemberConnectHandler OnMemberConnect;
  1646. internal MemberUpdateHandler OnMemberUpdate;
  1647. internal MemberDisconnectHandler OnMemberDisconnect;
  1648. internal LobbyMessageHandler OnLobbyMessage;
  1649. internal SpeakingHandler OnSpeaking;
  1650. internal NetworkMessageHandler OnNetworkMessage;
  1651. }
  1652. [StructLayout(LayoutKind.Sequential)]
  1653. internal partial struct FFIMethods
  1654. {
  1655. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1656. internal delegate Result GetLobbyCreateTransactionMethod(IntPtr methodsPtr, ref IntPtr transaction);
  1657. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1658. internal delegate Result GetLobbyUpdateTransactionMethod(IntPtr methodsPtr, Int64 lobbyId, ref IntPtr transaction);
  1659. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1660. internal delegate Result GetMemberUpdateTransactionMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, ref IntPtr transaction);
  1661. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1662. internal delegate void CreateLobbyCallback(IntPtr ptr, Result result, ref Lobby lobby);
  1663. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1664. internal delegate void CreateLobbyMethod(IntPtr methodsPtr, IntPtr transaction, IntPtr callbackData, CreateLobbyCallback callback);
  1665. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1666. internal delegate void UpdateLobbyCallback(IntPtr ptr, Result result);
  1667. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1668. internal delegate void UpdateLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr transaction, IntPtr callbackData, UpdateLobbyCallback callback);
  1669. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1670. internal delegate void DeleteLobbyCallback(IntPtr ptr, Result result);
  1671. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1672. internal delegate void DeleteLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, DeleteLobbyCallback callback);
  1673. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1674. internal delegate void ConnectLobbyCallback(IntPtr ptr, Result result, ref Lobby lobby);
  1675. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1676. internal delegate void ConnectLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, [MarshalAs(UnmanagedType.LPStr)]string secret, IntPtr callbackData, ConnectLobbyCallback callback);
  1677. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1678. internal delegate void ConnectLobbyWithActivitySecretCallback(IntPtr ptr, Result result, ref Lobby lobby);
  1679. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1680. internal delegate void ConnectLobbyWithActivitySecretMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string activitySecret, IntPtr callbackData, ConnectLobbyWithActivitySecretCallback callback);
  1681. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1682. internal delegate void DisconnectLobbyCallback(IntPtr ptr, Result result);
  1683. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1684. internal delegate void DisconnectLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, DisconnectLobbyCallback callback);
  1685. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1686. internal delegate Result GetLobbyMethod(IntPtr methodsPtr, Int64 lobbyId, ref Lobby lobby);
  1687. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1688. internal delegate Result GetLobbyActivitySecretMethod(IntPtr methodsPtr, Int64 lobbyId, StringBuilder secret);
  1689. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1690. internal delegate Result GetLobbyMetadataValueMethod(IntPtr methodsPtr, Int64 lobbyId, [MarshalAs(UnmanagedType.LPStr)]string key, StringBuilder value);
  1691. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1692. internal delegate Result GetLobbyMetadataKeyMethod(IntPtr methodsPtr, Int64 lobbyId, Int32 index, StringBuilder key);
  1693. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1694. internal delegate Result LobbyMetadataCountMethod(IntPtr methodsPtr, Int64 lobbyId, ref Int32 count);
  1695. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1696. internal delegate Result MemberCountMethod(IntPtr methodsPtr, Int64 lobbyId, ref Int32 count);
  1697. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1698. internal delegate Result GetMemberUserIdMethod(IntPtr methodsPtr, Int64 lobbyId, Int32 index, ref Int64 userId);
  1699. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1700. internal delegate Result GetMemberUserMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, ref User user);
  1701. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1702. internal delegate Result GetMemberMetadataValueMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, [MarshalAs(UnmanagedType.LPStr)]string key, StringBuilder value);
  1703. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1704. internal delegate Result GetMemberMetadataKeyMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, Int32 index, StringBuilder key);
  1705. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1706. internal delegate Result MemberMetadataCountMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, ref Int32 count);
  1707. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1708. internal delegate void UpdateMemberCallback(IntPtr ptr, Result result);
  1709. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1710. internal delegate void UpdateMemberMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, IntPtr transaction, IntPtr callbackData, UpdateMemberCallback callback);
  1711. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1712. internal delegate void SendLobbyMessageCallback(IntPtr ptr, Result result);
  1713. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1714. internal delegate void SendLobbyMessageMethod(IntPtr methodsPtr, Int64 lobbyId, byte[] data, Int32 dataLen, IntPtr callbackData, SendLobbyMessageCallback callback);
  1715. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1716. internal delegate Result GetSearchQueryMethod(IntPtr methodsPtr, ref IntPtr query);
  1717. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1718. internal delegate void SearchCallback(IntPtr ptr, Result result);
  1719. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1720. internal delegate void SearchMethod(IntPtr methodsPtr, IntPtr query, IntPtr callbackData, SearchCallback callback);
  1721. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1722. internal delegate void LobbyCountMethod(IntPtr methodsPtr, ref Int32 count);
  1723. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1724. internal delegate Result GetLobbyIdMethod(IntPtr methodsPtr, Int32 index, ref Int64 lobbyId);
  1725. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1726. internal delegate void ConnectVoiceCallback(IntPtr ptr, Result result);
  1727. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1728. internal delegate void ConnectVoiceMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, ConnectVoiceCallback callback);
  1729. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1730. internal delegate void DisconnectVoiceCallback(IntPtr ptr, Result result);
  1731. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1732. internal delegate void DisconnectVoiceMethod(IntPtr methodsPtr, Int64 lobbyId, IntPtr callbackData, DisconnectVoiceCallback callback);
  1733. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1734. internal delegate Result ConnectNetworkMethod(IntPtr methodsPtr, Int64 lobbyId);
  1735. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1736. internal delegate Result DisconnectNetworkMethod(IntPtr methodsPtr, Int64 lobbyId);
  1737. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1738. internal delegate Result FlushNetworkMethod(IntPtr methodsPtr);
  1739. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1740. internal delegate Result OpenNetworkChannelMethod(IntPtr methodsPtr, Int64 lobbyId, byte channelId, bool reliable);
  1741. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  1742. internal delegate Result SendNetworkMessageMethod(IntPtr methodsPtr, Int64 lobbyId, Int64 userId, byte channelId, byte[] data, Int32 dataLen);
  1743. internal GetLobbyCreateTransactionMethod GetLobbyCreateTransaction;
  1744. internal GetLobbyUpdateTransactionMethod GetLobbyUpdateTransaction;
  1745. internal GetMemberUpdateTransactionMethod GetMemberUpdateTransaction;
  1746. internal CreateLobbyMethod CreateLobby;
  1747. internal UpdateLobbyMethod UpdateLobby;
  1748. internal DeleteLobbyMethod DeleteLobby;
  1749. internal ConnectLobbyMethod ConnectLobby;
  1750. internal ConnectLobbyWithActivitySecretMethod ConnectLobbyWithActivitySecret;
  1751. internal DisconnectLobbyMethod DisconnectLobby;
  1752. internal GetLobbyMethod GetLobby;
  1753. internal GetLobbyActivitySecretMethod GetLobbyActivitySecret;
  1754. internal GetLobbyMetadataValueMethod GetLobbyMetadataValue;
  1755. internal GetLobbyMetadataKeyMethod GetLobbyMetadataKey;
  1756. internal LobbyMetadataCountMethod LobbyMetadataCount;
  1757. internal MemberCountMethod MemberCount;
  1758. internal GetMemberUserIdMethod GetMemberUserId;
  1759. internal GetMemberUserMethod GetMemberUser;
  1760. internal GetMemberMetadataValueMethod GetMemberMetadataValue;
  1761. internal GetMemberMetadataKeyMethod GetMemberMetadataKey;
  1762. internal MemberMetadataCountMethod MemberMetadataCount;
  1763. internal UpdateMemberMethod UpdateMember;
  1764. internal SendLobbyMessageMethod SendLobbyMessage;
  1765. internal GetSearchQueryMethod GetSearchQuery;
  1766. internal SearchMethod Search;
  1767. internal LobbyCountMethod LobbyCount;
  1768. internal GetLobbyIdMethod GetLobbyId;
  1769. internal ConnectVoiceMethod ConnectVoice;
  1770. internal DisconnectVoiceMethod DisconnectVoice;
  1771. internal ConnectNetworkMethod ConnectNetwork;
  1772. internal DisconnectNetworkMethod DisconnectNetwork;
  1773. internal FlushNetworkMethod FlushNetwork;
  1774. internal OpenNetworkChannelMethod OpenNetworkChannel;
  1775. internal SendNetworkMessageMethod SendNetworkMessage;
  1776. }
  1777. public delegate void CreateLobbyHandler(Result result, ref Lobby lobby);
  1778. public delegate void UpdateLobbyHandler(Result result);
  1779. public delegate void DeleteLobbyHandler(Result result);
  1780. public delegate void ConnectLobbyHandler(Result result, ref Lobby lobby);
  1781. public delegate void ConnectLobbyWithActivitySecretHandler(Result result, ref Lobby lobby);
  1782. public delegate void DisconnectLobbyHandler(Result result);
  1783. public delegate void UpdateMemberHandler(Result result);
  1784. public delegate void SendLobbyMessageHandler(Result result);
  1785. public delegate void SearchHandler(Result result);
  1786. public delegate void ConnectVoiceHandler(Result result);
  1787. public delegate void DisconnectVoiceHandler(Result result);
  1788. public delegate void LobbyUpdateHandler(Int64 lobbyId);
  1789. public delegate void LobbyDeleteHandler(Int64 lobbyId, UInt32 reason);
  1790. public delegate void MemberConnectHandler(Int64 lobbyId, Int64 userId);
  1791. public delegate void MemberUpdateHandler(Int64 lobbyId, Int64 userId);
  1792. public delegate void MemberDisconnectHandler(Int64 lobbyId, Int64 userId);
  1793. public delegate void LobbyMessageHandler(Int64 lobbyId, Int64 userId, byte[] data);
  1794. public delegate void SpeakingHandler(Int64 lobbyId, Int64 userId, bool speaking);
  1795. public delegate void NetworkMessageHandler(Int64 lobbyId, Int64 userId, byte channelId, byte[] data);
  1796. private IntPtr MethodsPtr;
  1797. private Object MethodsStructure;
  1798. private FFIMethods Methods
  1799. {
  1800. get
  1801. {
  1802. if (MethodsStructure == null)
  1803. {
  1804. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  1805. }
  1806. return (FFIMethods)MethodsStructure;
  1807. }
  1808. }
  1809. public event LobbyUpdateHandler OnLobbyUpdate;
  1810. public event LobbyDeleteHandler OnLobbyDelete;
  1811. public event MemberConnectHandler OnMemberConnect;
  1812. public event MemberUpdateHandler OnMemberUpdate;
  1813. public event MemberDisconnectHandler OnMemberDisconnect;
  1814. public event LobbyMessageHandler OnLobbyMessage;
  1815. public event SpeakingHandler OnSpeaking;
  1816. public event NetworkMessageHandler OnNetworkMessage;
  1817. internal LobbyManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  1818. {
  1819. if (eventsPtr == IntPtr.Zero) {
  1820. throw new ResultException(Result.InternalError);
  1821. }
  1822. InitEvents(eventsPtr, ref events);
  1823. MethodsPtr = ptr;
  1824. if (MethodsPtr == IntPtr.Zero) {
  1825. throw new ResultException(Result.InternalError);
  1826. }
  1827. }
  1828. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  1829. {
  1830. events.OnLobbyUpdate = OnLobbyUpdateImpl;
  1831. events.OnLobbyDelete = OnLobbyDeleteImpl;
  1832. events.OnMemberConnect = OnMemberConnectImpl;
  1833. events.OnMemberUpdate = OnMemberUpdateImpl;
  1834. events.OnMemberDisconnect = OnMemberDisconnectImpl;
  1835. events.OnLobbyMessage = OnLobbyMessageImpl;
  1836. events.OnSpeaking = OnSpeakingImpl;
  1837. events.OnNetworkMessage = OnNetworkMessageImpl;
  1838. Marshal.StructureToPtr(events, eventsPtr, false);
  1839. }
  1840. public LobbyTransaction GetLobbyCreateTransaction()
  1841. {
  1842. var ret = new LobbyTransaction();
  1843. var res = Methods.GetLobbyCreateTransaction(MethodsPtr, ref ret.MethodsPtr);
  1844. if (res != Result.Ok)
  1845. {
  1846. throw new ResultException(res);
  1847. }
  1848. return ret;
  1849. }
  1850. public LobbyTransaction GetLobbyUpdateTransaction(Int64 lobbyId)
  1851. {
  1852. var ret = new LobbyTransaction();
  1853. var res = Methods.GetLobbyUpdateTransaction(MethodsPtr, lobbyId, ref ret.MethodsPtr);
  1854. if (res != Result.Ok)
  1855. {
  1856. throw new ResultException(res);
  1857. }
  1858. return ret;
  1859. }
  1860. public LobbyMemberTransaction GetMemberUpdateTransaction(Int64 lobbyId, Int64 userId)
  1861. {
  1862. var ret = new LobbyMemberTransaction();
  1863. var res = Methods.GetMemberUpdateTransaction(MethodsPtr, lobbyId, userId, ref ret.MethodsPtr);
  1864. if (res != Result.Ok)
  1865. {
  1866. throw new ResultException(res);
  1867. }
  1868. return ret;
  1869. }
  1870. [MonoPInvokeCallback]
  1871. private static void CreateLobbyCallbackImpl(IntPtr ptr, Result result, ref Lobby lobby)
  1872. {
  1873. GCHandle h = GCHandle.FromIntPtr(ptr);
  1874. CreateLobbyHandler callback = (CreateLobbyHandler)h.Target;
  1875. h.Free();
  1876. callback(result, ref lobby);
  1877. }
  1878. public void CreateLobby(LobbyTransaction transaction, CreateLobbyHandler callback)
  1879. {
  1880. GCHandle wrapped = GCHandle.Alloc(callback);
  1881. Methods.CreateLobby(MethodsPtr, transaction.MethodsPtr, GCHandle.ToIntPtr(wrapped), CreateLobbyCallbackImpl);
  1882. transaction.MethodsPtr = IntPtr.Zero;
  1883. }
  1884. [MonoPInvokeCallback]
  1885. private static void UpdateLobbyCallbackImpl(IntPtr ptr, Result result)
  1886. {
  1887. GCHandle h = GCHandle.FromIntPtr(ptr);
  1888. UpdateLobbyHandler callback = (UpdateLobbyHandler)h.Target;
  1889. h.Free();
  1890. callback(result);
  1891. }
  1892. public void UpdateLobby(Int64 lobbyId, LobbyTransaction transaction, UpdateLobbyHandler callback)
  1893. {
  1894. GCHandle wrapped = GCHandle.Alloc(callback);
  1895. Methods.UpdateLobby(MethodsPtr, lobbyId, transaction.MethodsPtr, GCHandle.ToIntPtr(wrapped), UpdateLobbyCallbackImpl);
  1896. transaction.MethodsPtr = IntPtr.Zero;
  1897. }
  1898. [MonoPInvokeCallback]
  1899. private static void DeleteLobbyCallbackImpl(IntPtr ptr, Result result)
  1900. {
  1901. GCHandle h = GCHandle.FromIntPtr(ptr);
  1902. DeleteLobbyHandler callback = (DeleteLobbyHandler)h.Target;
  1903. h.Free();
  1904. callback(result);
  1905. }
  1906. public void DeleteLobby(Int64 lobbyId, DeleteLobbyHandler callback)
  1907. {
  1908. GCHandle wrapped = GCHandle.Alloc(callback);
  1909. Methods.DeleteLobby(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), DeleteLobbyCallbackImpl);
  1910. }
  1911. [MonoPInvokeCallback]
  1912. private static void ConnectLobbyCallbackImpl(IntPtr ptr, Result result, ref Lobby lobby)
  1913. {
  1914. GCHandle h = GCHandle.FromIntPtr(ptr);
  1915. ConnectLobbyHandler callback = (ConnectLobbyHandler)h.Target;
  1916. h.Free();
  1917. callback(result, ref lobby);
  1918. }
  1919. public void ConnectLobby(Int64 lobbyId, string secret, ConnectLobbyHandler callback)
  1920. {
  1921. GCHandle wrapped = GCHandle.Alloc(callback);
  1922. Methods.ConnectLobby(MethodsPtr, lobbyId, secret, GCHandle.ToIntPtr(wrapped), ConnectLobbyCallbackImpl);
  1923. }
  1924. [MonoPInvokeCallback]
  1925. private static void ConnectLobbyWithActivitySecretCallbackImpl(IntPtr ptr, Result result, ref Lobby lobby)
  1926. {
  1927. GCHandle h = GCHandle.FromIntPtr(ptr);
  1928. ConnectLobbyWithActivitySecretHandler callback = (ConnectLobbyWithActivitySecretHandler)h.Target;
  1929. h.Free();
  1930. callback(result, ref lobby);
  1931. }
  1932. public void ConnectLobbyWithActivitySecret(string activitySecret, ConnectLobbyWithActivitySecretHandler callback)
  1933. {
  1934. GCHandle wrapped = GCHandle.Alloc(callback);
  1935. Methods.ConnectLobbyWithActivitySecret(MethodsPtr, activitySecret, GCHandle.ToIntPtr(wrapped), ConnectLobbyWithActivitySecretCallbackImpl);
  1936. }
  1937. [MonoPInvokeCallback]
  1938. private static void DisconnectLobbyCallbackImpl(IntPtr ptr, Result result)
  1939. {
  1940. GCHandle h = GCHandle.FromIntPtr(ptr);
  1941. DisconnectLobbyHandler callback = (DisconnectLobbyHandler)h.Target;
  1942. h.Free();
  1943. callback(result);
  1944. }
  1945. public void DisconnectLobby(Int64 lobbyId, DisconnectLobbyHandler callback)
  1946. {
  1947. GCHandle wrapped = GCHandle.Alloc(callback);
  1948. Methods.DisconnectLobby(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), DisconnectLobbyCallbackImpl);
  1949. }
  1950. public Lobby GetLobby(Int64 lobbyId)
  1951. {
  1952. var ret = new Lobby();
  1953. var res = Methods.GetLobby(MethodsPtr, lobbyId, ref ret);
  1954. if (res != Result.Ok)
  1955. {
  1956. throw new ResultException(res);
  1957. }
  1958. return ret;
  1959. }
  1960. public string GetLobbyActivitySecret(Int64 lobbyId)
  1961. {
  1962. var ret = new StringBuilder(128);
  1963. var res = Methods.GetLobbyActivitySecret(MethodsPtr, lobbyId, ret);
  1964. if (res != Result.Ok)
  1965. {
  1966. throw new ResultException(res);
  1967. }
  1968. return ret.ToString();
  1969. }
  1970. public string GetLobbyMetadataValue(Int64 lobbyId, string key)
  1971. {
  1972. var ret = new StringBuilder(4096);
  1973. var res = Methods.GetLobbyMetadataValue(MethodsPtr, lobbyId, key, ret);
  1974. if (res != Result.Ok)
  1975. {
  1976. throw new ResultException(res);
  1977. }
  1978. return ret.ToString();
  1979. }
  1980. public string GetLobbyMetadataKey(Int64 lobbyId, Int32 index)
  1981. {
  1982. var ret = new StringBuilder(256);
  1983. var res = Methods.GetLobbyMetadataKey(MethodsPtr, lobbyId, index, ret);
  1984. if (res != Result.Ok)
  1985. {
  1986. throw new ResultException(res);
  1987. }
  1988. return ret.ToString();
  1989. }
  1990. public Int32 LobbyMetadataCount(Int64 lobbyId)
  1991. {
  1992. var ret = new Int32();
  1993. var res = Methods.LobbyMetadataCount(MethodsPtr, lobbyId, ref ret);
  1994. if (res != Result.Ok)
  1995. {
  1996. throw new ResultException(res);
  1997. }
  1998. return ret;
  1999. }
  2000. public Int32 MemberCount(Int64 lobbyId)
  2001. {
  2002. var ret = new Int32();
  2003. var res = Methods.MemberCount(MethodsPtr, lobbyId, ref ret);
  2004. if (res != Result.Ok)
  2005. {
  2006. throw new ResultException(res);
  2007. }
  2008. return ret;
  2009. }
  2010. public Int64 GetMemberUserId(Int64 lobbyId, Int32 index)
  2011. {
  2012. var ret = new Int64();
  2013. var res = Methods.GetMemberUserId(MethodsPtr, lobbyId, index, ref ret);
  2014. if (res != Result.Ok)
  2015. {
  2016. throw new ResultException(res);
  2017. }
  2018. return ret;
  2019. }
  2020. public User GetMemberUser(Int64 lobbyId, Int64 userId)
  2021. {
  2022. var ret = new User();
  2023. var res = Methods.GetMemberUser(MethodsPtr, lobbyId, userId, ref ret);
  2024. if (res != Result.Ok)
  2025. {
  2026. throw new ResultException(res);
  2027. }
  2028. return ret;
  2029. }
  2030. public string GetMemberMetadataValue(Int64 lobbyId, Int64 userId, string key)
  2031. {
  2032. var ret = new StringBuilder(4096);
  2033. var res = Methods.GetMemberMetadataValue(MethodsPtr, lobbyId, userId, key, ret);
  2034. if (res != Result.Ok)
  2035. {
  2036. throw new ResultException(res);
  2037. }
  2038. return ret.ToString();
  2039. }
  2040. public string GetMemberMetadataKey(Int64 lobbyId, Int64 userId, Int32 index)
  2041. {
  2042. var ret = new StringBuilder(256);
  2043. var res = Methods.GetMemberMetadataKey(MethodsPtr, lobbyId, userId, index, ret);
  2044. if (res != Result.Ok)
  2045. {
  2046. throw new ResultException(res);
  2047. }
  2048. return ret.ToString();
  2049. }
  2050. public Int32 MemberMetadataCount(Int64 lobbyId, Int64 userId)
  2051. {
  2052. var ret = new Int32();
  2053. var res = Methods.MemberMetadataCount(MethodsPtr, lobbyId, userId, ref ret);
  2054. if (res != Result.Ok)
  2055. {
  2056. throw new ResultException(res);
  2057. }
  2058. return ret;
  2059. }
  2060. [MonoPInvokeCallback]
  2061. private static void UpdateMemberCallbackImpl(IntPtr ptr, Result result)
  2062. {
  2063. GCHandle h = GCHandle.FromIntPtr(ptr);
  2064. UpdateMemberHandler callback = (UpdateMemberHandler)h.Target;
  2065. h.Free();
  2066. callback(result);
  2067. }
  2068. public void UpdateMember(Int64 lobbyId, Int64 userId, LobbyMemberTransaction transaction, UpdateMemberHandler callback)
  2069. {
  2070. GCHandle wrapped = GCHandle.Alloc(callback);
  2071. Methods.UpdateMember(MethodsPtr, lobbyId, userId, transaction.MethodsPtr, GCHandle.ToIntPtr(wrapped), UpdateMemberCallbackImpl);
  2072. transaction.MethodsPtr = IntPtr.Zero;
  2073. }
  2074. [MonoPInvokeCallback]
  2075. private static void SendLobbyMessageCallbackImpl(IntPtr ptr, Result result)
  2076. {
  2077. GCHandle h = GCHandle.FromIntPtr(ptr);
  2078. SendLobbyMessageHandler callback = (SendLobbyMessageHandler)h.Target;
  2079. h.Free();
  2080. callback(result);
  2081. }
  2082. public void SendLobbyMessage(Int64 lobbyId, byte[] data, SendLobbyMessageHandler callback)
  2083. {
  2084. GCHandle wrapped = GCHandle.Alloc(callback);
  2085. Methods.SendLobbyMessage(MethodsPtr, lobbyId, data, data.Length, GCHandle.ToIntPtr(wrapped), SendLobbyMessageCallbackImpl);
  2086. }
  2087. public LobbySearchQuery GetSearchQuery()
  2088. {
  2089. var ret = new LobbySearchQuery();
  2090. var res = Methods.GetSearchQuery(MethodsPtr, ref ret.MethodsPtr);
  2091. if (res != Result.Ok)
  2092. {
  2093. throw new ResultException(res);
  2094. }
  2095. return ret;
  2096. }
  2097. [MonoPInvokeCallback]
  2098. private static void SearchCallbackImpl(IntPtr ptr, Result result)
  2099. {
  2100. GCHandle h = GCHandle.FromIntPtr(ptr);
  2101. SearchHandler callback = (SearchHandler)h.Target;
  2102. h.Free();
  2103. callback(result);
  2104. }
  2105. public void Search(LobbySearchQuery query, SearchHandler callback)
  2106. {
  2107. GCHandle wrapped = GCHandle.Alloc(callback);
  2108. Methods.Search(MethodsPtr, query.MethodsPtr, GCHandle.ToIntPtr(wrapped), SearchCallbackImpl);
  2109. query.MethodsPtr = IntPtr.Zero;
  2110. }
  2111. public Int32 LobbyCount()
  2112. {
  2113. var ret = new Int32();
  2114. Methods.LobbyCount(MethodsPtr, ref ret);
  2115. return ret;
  2116. }
  2117. public Int64 GetLobbyId(Int32 index)
  2118. {
  2119. var ret = new Int64();
  2120. var res = Methods.GetLobbyId(MethodsPtr, index, ref ret);
  2121. if (res != Result.Ok)
  2122. {
  2123. throw new ResultException(res);
  2124. }
  2125. return ret;
  2126. }
  2127. [MonoPInvokeCallback]
  2128. private static void ConnectVoiceCallbackImpl(IntPtr ptr, Result result)
  2129. {
  2130. GCHandle h = GCHandle.FromIntPtr(ptr);
  2131. ConnectVoiceHandler callback = (ConnectVoiceHandler)h.Target;
  2132. h.Free();
  2133. callback(result);
  2134. }
  2135. public void ConnectVoice(Int64 lobbyId, ConnectVoiceHandler callback)
  2136. {
  2137. GCHandle wrapped = GCHandle.Alloc(callback);
  2138. Methods.ConnectVoice(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), ConnectVoiceCallbackImpl);
  2139. }
  2140. [MonoPInvokeCallback]
  2141. private static void DisconnectVoiceCallbackImpl(IntPtr ptr, Result result)
  2142. {
  2143. GCHandle h = GCHandle.FromIntPtr(ptr);
  2144. DisconnectVoiceHandler callback = (DisconnectVoiceHandler)h.Target;
  2145. h.Free();
  2146. callback(result);
  2147. }
  2148. public void DisconnectVoice(Int64 lobbyId, DisconnectVoiceHandler callback)
  2149. {
  2150. GCHandle wrapped = GCHandle.Alloc(callback);
  2151. Methods.DisconnectVoice(MethodsPtr, lobbyId, GCHandle.ToIntPtr(wrapped), DisconnectVoiceCallbackImpl);
  2152. }
  2153. public void ConnectNetwork(Int64 lobbyId)
  2154. {
  2155. var res = Methods.ConnectNetwork(MethodsPtr, lobbyId);
  2156. if (res != Result.Ok)
  2157. {
  2158. throw new ResultException(res);
  2159. }
  2160. }
  2161. public void DisconnectNetwork(Int64 lobbyId)
  2162. {
  2163. var res = Methods.DisconnectNetwork(MethodsPtr, lobbyId);
  2164. if (res != Result.Ok)
  2165. {
  2166. throw new ResultException(res);
  2167. }
  2168. }
  2169. public void FlushNetwork()
  2170. {
  2171. var res = Methods.FlushNetwork(MethodsPtr);
  2172. if (res != Result.Ok)
  2173. {
  2174. throw new ResultException(res);
  2175. }
  2176. }
  2177. public void OpenNetworkChannel(Int64 lobbyId, byte channelId, bool reliable)
  2178. {
  2179. var res = Methods.OpenNetworkChannel(MethodsPtr, lobbyId, channelId, reliable);
  2180. if (res != Result.Ok)
  2181. {
  2182. throw new ResultException(res);
  2183. }
  2184. }
  2185. public void SendNetworkMessage(Int64 lobbyId, Int64 userId, byte channelId, byte[] data)
  2186. {
  2187. var res = Methods.SendNetworkMessage(MethodsPtr, lobbyId, userId, channelId, data, data.Length);
  2188. if (res != Result.Ok)
  2189. {
  2190. throw new ResultException(res);
  2191. }
  2192. }
  2193. [MonoPInvokeCallback]
  2194. private static void OnLobbyUpdateImpl(IntPtr ptr, Int64 lobbyId)
  2195. {
  2196. GCHandle h = GCHandle.FromIntPtr(ptr);
  2197. Discord d = (Discord)h.Target;
  2198. if (d.LobbyManagerInstance.OnLobbyUpdate != null)
  2199. {
  2200. d.LobbyManagerInstance.OnLobbyUpdate.Invoke(lobbyId);
  2201. }
  2202. }
  2203. [MonoPInvokeCallback]
  2204. private static void OnLobbyDeleteImpl(IntPtr ptr, Int64 lobbyId, UInt32 reason)
  2205. {
  2206. GCHandle h = GCHandle.FromIntPtr(ptr);
  2207. Discord d = (Discord)h.Target;
  2208. if (d.LobbyManagerInstance.OnLobbyDelete != null)
  2209. {
  2210. d.LobbyManagerInstance.OnLobbyDelete.Invoke(lobbyId, reason);
  2211. }
  2212. }
  2213. [MonoPInvokeCallback]
  2214. private static void OnMemberConnectImpl(IntPtr ptr, Int64 lobbyId, Int64 userId)
  2215. {
  2216. GCHandle h = GCHandle.FromIntPtr(ptr);
  2217. Discord d = (Discord)h.Target;
  2218. if (d.LobbyManagerInstance.OnMemberConnect != null)
  2219. {
  2220. d.LobbyManagerInstance.OnMemberConnect.Invoke(lobbyId, userId);
  2221. }
  2222. }
  2223. [MonoPInvokeCallback]
  2224. private static void OnMemberUpdateImpl(IntPtr ptr, Int64 lobbyId, Int64 userId)
  2225. {
  2226. GCHandle h = GCHandle.FromIntPtr(ptr);
  2227. Discord d = (Discord)h.Target;
  2228. if (d.LobbyManagerInstance.OnMemberUpdate != null)
  2229. {
  2230. d.LobbyManagerInstance.OnMemberUpdate.Invoke(lobbyId, userId);
  2231. }
  2232. }
  2233. [MonoPInvokeCallback]
  2234. private static void OnMemberDisconnectImpl(IntPtr ptr, Int64 lobbyId, Int64 userId)
  2235. {
  2236. GCHandle h = GCHandle.FromIntPtr(ptr);
  2237. Discord d = (Discord)h.Target;
  2238. if (d.LobbyManagerInstance.OnMemberDisconnect != null)
  2239. {
  2240. d.LobbyManagerInstance.OnMemberDisconnect.Invoke(lobbyId, userId);
  2241. }
  2242. }
  2243. [MonoPInvokeCallback]
  2244. private static void OnLobbyMessageImpl(IntPtr ptr, Int64 lobbyId, Int64 userId, IntPtr dataPtr, Int32 dataLen)
  2245. {
  2246. GCHandle h = GCHandle.FromIntPtr(ptr);
  2247. Discord d = (Discord)h.Target;
  2248. if (d.LobbyManagerInstance.OnLobbyMessage != null)
  2249. {
  2250. byte[] data = new byte[dataLen];
  2251. Marshal.Copy(dataPtr, data, 0, (int)dataLen);
  2252. d.LobbyManagerInstance.OnLobbyMessage.Invoke(lobbyId, userId, data);
  2253. }
  2254. }
  2255. [MonoPInvokeCallback]
  2256. private static void OnSpeakingImpl(IntPtr ptr, Int64 lobbyId, Int64 userId, bool speaking)
  2257. {
  2258. GCHandle h = GCHandle.FromIntPtr(ptr);
  2259. Discord d = (Discord)h.Target;
  2260. if (d.LobbyManagerInstance.OnSpeaking != null)
  2261. {
  2262. d.LobbyManagerInstance.OnSpeaking.Invoke(lobbyId, userId, speaking);
  2263. }
  2264. }
  2265. [MonoPInvokeCallback]
  2266. private static void OnNetworkMessageImpl(IntPtr ptr, Int64 lobbyId, Int64 userId, byte channelId, IntPtr dataPtr, Int32 dataLen)
  2267. {
  2268. GCHandle h = GCHandle.FromIntPtr(ptr);
  2269. Discord d = (Discord)h.Target;
  2270. if (d.LobbyManagerInstance.OnNetworkMessage != null)
  2271. {
  2272. byte[] data = new byte[dataLen];
  2273. Marshal.Copy(dataPtr, data, 0, (int)dataLen);
  2274. d.LobbyManagerInstance.OnNetworkMessage.Invoke(lobbyId, userId, channelId, data);
  2275. }
  2276. }
  2277. }
  2278. public partial class NetworkManager
  2279. {
  2280. [StructLayout(LayoutKind.Sequential)]
  2281. internal partial struct FFIEvents
  2282. {
  2283. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2284. internal delegate void MessageHandler(IntPtr ptr, UInt64 peerId, byte channelId, IntPtr dataPtr, Int32 dataLen);
  2285. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2286. internal delegate void RouteUpdateHandler(IntPtr ptr, [MarshalAs(UnmanagedType.LPStr)]string routeData);
  2287. internal MessageHandler OnMessage;
  2288. internal RouteUpdateHandler OnRouteUpdate;
  2289. }
  2290. [StructLayout(LayoutKind.Sequential)]
  2291. internal partial struct FFIMethods
  2292. {
  2293. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2294. internal delegate void GetPeerIdMethod(IntPtr methodsPtr, ref UInt64 peerId);
  2295. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2296. internal delegate Result FlushMethod(IntPtr methodsPtr);
  2297. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2298. internal delegate Result OpenPeerMethod(IntPtr methodsPtr, UInt64 peerId, [MarshalAs(UnmanagedType.LPStr)]string routeData);
  2299. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2300. internal delegate Result UpdatePeerMethod(IntPtr methodsPtr, UInt64 peerId, [MarshalAs(UnmanagedType.LPStr)]string routeData);
  2301. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2302. internal delegate Result ClosePeerMethod(IntPtr methodsPtr, UInt64 peerId);
  2303. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2304. internal delegate Result OpenChannelMethod(IntPtr methodsPtr, UInt64 peerId, byte channelId, bool reliable);
  2305. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2306. internal delegate Result CloseChannelMethod(IntPtr methodsPtr, UInt64 peerId, byte channelId);
  2307. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2308. internal delegate Result SendMessageMethod(IntPtr methodsPtr, UInt64 peerId, byte channelId, byte[] data, Int32 dataLen);
  2309. internal GetPeerIdMethod GetPeerId;
  2310. internal FlushMethod Flush;
  2311. internal OpenPeerMethod OpenPeer;
  2312. internal UpdatePeerMethod UpdatePeer;
  2313. internal ClosePeerMethod ClosePeer;
  2314. internal OpenChannelMethod OpenChannel;
  2315. internal CloseChannelMethod CloseChannel;
  2316. internal SendMessageMethod SendMessage;
  2317. }
  2318. public delegate void MessageHandler(UInt64 peerId, byte channelId, byte[] data);
  2319. public delegate void RouteUpdateHandler(string routeData);
  2320. private IntPtr MethodsPtr;
  2321. private Object MethodsStructure;
  2322. private FFIMethods Methods
  2323. {
  2324. get
  2325. {
  2326. if (MethodsStructure == null)
  2327. {
  2328. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  2329. }
  2330. return (FFIMethods)MethodsStructure;
  2331. }
  2332. }
  2333. public event MessageHandler OnMessage;
  2334. public event RouteUpdateHandler OnRouteUpdate;
  2335. internal NetworkManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  2336. {
  2337. if (eventsPtr == IntPtr.Zero) {
  2338. throw new ResultException(Result.InternalError);
  2339. }
  2340. InitEvents(eventsPtr, ref events);
  2341. MethodsPtr = ptr;
  2342. if (MethodsPtr == IntPtr.Zero) {
  2343. throw new ResultException(Result.InternalError);
  2344. }
  2345. }
  2346. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  2347. {
  2348. events.OnMessage = OnMessageImpl;
  2349. events.OnRouteUpdate = OnRouteUpdateImpl;
  2350. Marshal.StructureToPtr(events, eventsPtr, false);
  2351. }
  2352. /// <summary>
  2353. /// Get the local peer ID for this process.
  2354. /// </summary>
  2355. public UInt64 GetPeerId()
  2356. {
  2357. var ret = new UInt64();
  2358. Methods.GetPeerId(MethodsPtr, ref ret);
  2359. return ret;
  2360. }
  2361. /// <summary>
  2362. /// Send pending network messages.
  2363. /// </summary>
  2364. public void Flush()
  2365. {
  2366. var res = Methods.Flush(MethodsPtr);
  2367. if (res != Result.Ok)
  2368. {
  2369. throw new ResultException(res);
  2370. }
  2371. }
  2372. /// <summary>
  2373. /// Open a connection to a remote peer.
  2374. /// </summary>
  2375. public void OpenPeer(UInt64 peerId, string routeData)
  2376. {
  2377. var res = Methods.OpenPeer(MethodsPtr, peerId, routeData);
  2378. if (res != Result.Ok)
  2379. {
  2380. throw new ResultException(res);
  2381. }
  2382. }
  2383. /// <summary>
  2384. /// Update the route data for a connected peer.
  2385. /// </summary>
  2386. public void UpdatePeer(UInt64 peerId, string routeData)
  2387. {
  2388. var res = Methods.UpdatePeer(MethodsPtr, peerId, routeData);
  2389. if (res != Result.Ok)
  2390. {
  2391. throw new ResultException(res);
  2392. }
  2393. }
  2394. /// <summary>
  2395. /// Close the connection to a remote peer.
  2396. /// </summary>
  2397. public void ClosePeer(UInt64 peerId)
  2398. {
  2399. var res = Methods.ClosePeer(MethodsPtr, peerId);
  2400. if (res != Result.Ok)
  2401. {
  2402. throw new ResultException(res);
  2403. }
  2404. }
  2405. /// <summary>
  2406. /// Open a message channel to a connected peer.
  2407. /// </summary>
  2408. public void OpenChannel(UInt64 peerId, byte channelId, bool reliable)
  2409. {
  2410. var res = Methods.OpenChannel(MethodsPtr, peerId, channelId, reliable);
  2411. if (res != Result.Ok)
  2412. {
  2413. throw new ResultException(res);
  2414. }
  2415. }
  2416. /// <summary>
  2417. /// Close a message channel to a connected peer.
  2418. /// </summary>
  2419. public void CloseChannel(UInt64 peerId, byte channelId)
  2420. {
  2421. var res = Methods.CloseChannel(MethodsPtr, peerId, channelId);
  2422. if (res != Result.Ok)
  2423. {
  2424. throw new ResultException(res);
  2425. }
  2426. }
  2427. /// <summary>
  2428. /// Send a message to a connected peer over an opened message channel.
  2429. /// </summary>
  2430. public void SendMessage(UInt64 peerId, byte channelId, byte[] data)
  2431. {
  2432. var res = Methods.SendMessage(MethodsPtr, peerId, channelId, data, data.Length);
  2433. if (res != Result.Ok)
  2434. {
  2435. throw new ResultException(res);
  2436. }
  2437. }
  2438. [MonoPInvokeCallback]
  2439. private static void OnMessageImpl(IntPtr ptr, UInt64 peerId, byte channelId, IntPtr dataPtr, Int32 dataLen)
  2440. {
  2441. GCHandle h = GCHandle.FromIntPtr(ptr);
  2442. Discord d = (Discord)h.Target;
  2443. if (d.NetworkManagerInstance.OnMessage != null)
  2444. {
  2445. byte[] data = new byte[dataLen];
  2446. Marshal.Copy(dataPtr, data, 0, (int)dataLen);
  2447. d.NetworkManagerInstance.OnMessage.Invoke(peerId, channelId, data);
  2448. }
  2449. }
  2450. [MonoPInvokeCallback]
  2451. private static void OnRouteUpdateImpl(IntPtr ptr, string routeData)
  2452. {
  2453. GCHandle h = GCHandle.FromIntPtr(ptr);
  2454. Discord d = (Discord)h.Target;
  2455. if (d.NetworkManagerInstance.OnRouteUpdate != null)
  2456. {
  2457. d.NetworkManagerInstance.OnRouteUpdate.Invoke(routeData);
  2458. }
  2459. }
  2460. }
  2461. public partial class OverlayManager
  2462. {
  2463. [StructLayout(LayoutKind.Sequential)]
  2464. internal partial struct FFIEvents
  2465. {
  2466. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2467. internal delegate void ToggleHandler(IntPtr ptr, bool locked);
  2468. internal ToggleHandler OnToggle;
  2469. }
  2470. [StructLayout(LayoutKind.Sequential)]
  2471. internal partial struct FFIMethods
  2472. {
  2473. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2474. internal delegate void IsEnabledMethod(IntPtr methodsPtr, ref bool enabled);
  2475. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2476. internal delegate void IsLockedMethod(IntPtr methodsPtr, ref bool locked);
  2477. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2478. internal delegate void SetLockedCallback(IntPtr ptr, Result result);
  2479. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2480. internal delegate void SetLockedMethod(IntPtr methodsPtr, bool locked, IntPtr callbackData, SetLockedCallback callback);
  2481. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2482. internal delegate void OpenActivityInviteCallback(IntPtr ptr, Result result);
  2483. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2484. internal delegate void OpenActivityInviteMethod(IntPtr methodsPtr, ActivityActionType type, IntPtr callbackData, OpenActivityInviteCallback callback);
  2485. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2486. internal delegate void OpenGuildInviteCallback(IntPtr ptr, Result result);
  2487. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2488. internal delegate void OpenGuildInviteMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string code, IntPtr callbackData, OpenGuildInviteCallback callback);
  2489. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2490. internal delegate void OpenVoiceSettingsCallback(IntPtr ptr, Result result);
  2491. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2492. internal delegate void OpenVoiceSettingsMethod(IntPtr methodsPtr, IntPtr callbackData, OpenVoiceSettingsCallback callback);
  2493. internal IsEnabledMethod IsEnabled;
  2494. internal IsLockedMethod IsLocked;
  2495. internal SetLockedMethod SetLocked;
  2496. internal OpenActivityInviteMethod OpenActivityInvite;
  2497. internal OpenGuildInviteMethod OpenGuildInvite;
  2498. internal OpenVoiceSettingsMethod OpenVoiceSettings;
  2499. }
  2500. public delegate void SetLockedHandler(Result result);
  2501. public delegate void OpenActivityInviteHandler(Result result);
  2502. public delegate void OpenGuildInviteHandler(Result result);
  2503. public delegate void OpenVoiceSettingsHandler(Result result);
  2504. public delegate void ToggleHandler(bool locked);
  2505. private IntPtr MethodsPtr;
  2506. private Object MethodsStructure;
  2507. private FFIMethods Methods
  2508. {
  2509. get
  2510. {
  2511. if (MethodsStructure == null)
  2512. {
  2513. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  2514. }
  2515. return (FFIMethods)MethodsStructure;
  2516. }
  2517. }
  2518. public event ToggleHandler OnToggle;
  2519. internal OverlayManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  2520. {
  2521. if (eventsPtr == IntPtr.Zero) {
  2522. throw new ResultException(Result.InternalError);
  2523. }
  2524. InitEvents(eventsPtr, ref events);
  2525. MethodsPtr = ptr;
  2526. if (MethodsPtr == IntPtr.Zero) {
  2527. throw new ResultException(Result.InternalError);
  2528. }
  2529. }
  2530. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  2531. {
  2532. events.OnToggle = OnToggleImpl;
  2533. Marshal.StructureToPtr(events, eventsPtr, false);
  2534. }
  2535. public bool IsEnabled()
  2536. {
  2537. var ret = new bool();
  2538. Methods.IsEnabled(MethodsPtr, ref ret);
  2539. return ret;
  2540. }
  2541. public bool IsLocked()
  2542. {
  2543. var ret = new bool();
  2544. Methods.IsLocked(MethodsPtr, ref ret);
  2545. return ret;
  2546. }
  2547. [MonoPInvokeCallback]
  2548. private static void SetLockedCallbackImpl(IntPtr ptr, Result result)
  2549. {
  2550. GCHandle h = GCHandle.FromIntPtr(ptr);
  2551. SetLockedHandler callback = (SetLockedHandler)h.Target;
  2552. h.Free();
  2553. callback(result);
  2554. }
  2555. public void SetLocked(bool locked, SetLockedHandler callback)
  2556. {
  2557. GCHandle wrapped = GCHandle.Alloc(callback);
  2558. Methods.SetLocked(MethodsPtr, locked, GCHandle.ToIntPtr(wrapped), SetLockedCallbackImpl);
  2559. }
  2560. [MonoPInvokeCallback]
  2561. private static void OpenActivityInviteCallbackImpl(IntPtr ptr, Result result)
  2562. {
  2563. GCHandle h = GCHandle.FromIntPtr(ptr);
  2564. OpenActivityInviteHandler callback = (OpenActivityInviteHandler)h.Target;
  2565. h.Free();
  2566. callback(result);
  2567. }
  2568. public void OpenActivityInvite(ActivityActionType type, OpenActivityInviteHandler callback)
  2569. {
  2570. GCHandle wrapped = GCHandle.Alloc(callback);
  2571. Methods.OpenActivityInvite(MethodsPtr, type, GCHandle.ToIntPtr(wrapped), OpenActivityInviteCallbackImpl);
  2572. }
  2573. [MonoPInvokeCallback]
  2574. private static void OpenGuildInviteCallbackImpl(IntPtr ptr, Result result)
  2575. {
  2576. GCHandle h = GCHandle.FromIntPtr(ptr);
  2577. OpenGuildInviteHandler callback = (OpenGuildInviteHandler)h.Target;
  2578. h.Free();
  2579. callback(result);
  2580. }
  2581. public void OpenGuildInvite(string code, OpenGuildInviteHandler callback)
  2582. {
  2583. GCHandle wrapped = GCHandle.Alloc(callback);
  2584. Methods.OpenGuildInvite(MethodsPtr, code, GCHandle.ToIntPtr(wrapped), OpenGuildInviteCallbackImpl);
  2585. }
  2586. [MonoPInvokeCallback]
  2587. private static void OpenVoiceSettingsCallbackImpl(IntPtr ptr, Result result)
  2588. {
  2589. GCHandle h = GCHandle.FromIntPtr(ptr);
  2590. OpenVoiceSettingsHandler callback = (OpenVoiceSettingsHandler)h.Target;
  2591. h.Free();
  2592. callback(result);
  2593. }
  2594. public void OpenVoiceSettings(OpenVoiceSettingsHandler callback)
  2595. {
  2596. GCHandle wrapped = GCHandle.Alloc(callback);
  2597. Methods.OpenVoiceSettings(MethodsPtr, GCHandle.ToIntPtr(wrapped), OpenVoiceSettingsCallbackImpl);
  2598. }
  2599. [MonoPInvokeCallback]
  2600. private static void OnToggleImpl(IntPtr ptr, bool locked)
  2601. {
  2602. GCHandle h = GCHandle.FromIntPtr(ptr);
  2603. Discord d = (Discord)h.Target;
  2604. if (d.OverlayManagerInstance.OnToggle != null)
  2605. {
  2606. d.OverlayManagerInstance.OnToggle.Invoke(locked);
  2607. }
  2608. }
  2609. }
  2610. public partial class StorageManager
  2611. {
  2612. [StructLayout(LayoutKind.Sequential)]
  2613. internal partial struct FFIEvents
  2614. {
  2615. }
  2616. [StructLayout(LayoutKind.Sequential)]
  2617. internal partial struct FFIMethods
  2618. {
  2619. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2620. internal delegate Result ReadMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, byte[] data, Int32 dataLen, ref UInt32 read);
  2621. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2622. internal delegate void ReadAsyncCallback(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen);
  2623. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2624. internal delegate void ReadAsyncMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, IntPtr callbackData, ReadAsyncCallback callback);
  2625. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2626. internal delegate void ReadAsyncPartialCallback(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen);
  2627. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2628. internal delegate void ReadAsyncPartialMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, UInt64 offset, UInt64 length, IntPtr callbackData, ReadAsyncPartialCallback callback);
  2629. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2630. internal delegate Result WriteMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, byte[] data, Int32 dataLen);
  2631. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2632. internal delegate void WriteAsyncCallback(IntPtr ptr, Result result);
  2633. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2634. internal delegate void WriteAsyncMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, byte[] data, Int32 dataLen, IntPtr callbackData, WriteAsyncCallback callback);
  2635. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2636. internal delegate Result DeleteMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name);
  2637. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2638. internal delegate Result ExistsMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, ref bool exists);
  2639. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2640. internal delegate void CountMethod(IntPtr methodsPtr, ref Int32 count);
  2641. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2642. internal delegate Result StatMethod(IntPtr methodsPtr, [MarshalAs(UnmanagedType.LPStr)]string name, ref FileStat stat);
  2643. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2644. internal delegate Result StatAtMethod(IntPtr methodsPtr, Int32 index, ref FileStat stat);
  2645. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2646. internal delegate Result GetPathMethod(IntPtr methodsPtr, StringBuilder path);
  2647. internal ReadMethod Read;
  2648. internal ReadAsyncMethod ReadAsync;
  2649. internal ReadAsyncPartialMethod ReadAsyncPartial;
  2650. internal WriteMethod Write;
  2651. internal WriteAsyncMethod WriteAsync;
  2652. internal DeleteMethod Delete;
  2653. internal ExistsMethod Exists;
  2654. internal CountMethod Count;
  2655. internal StatMethod Stat;
  2656. internal StatAtMethod StatAt;
  2657. internal GetPathMethod GetPath;
  2658. }
  2659. public delegate void ReadAsyncHandler(Result result, byte[] data);
  2660. public delegate void ReadAsyncPartialHandler(Result result, byte[] data);
  2661. public delegate void WriteAsyncHandler(Result result);
  2662. private IntPtr MethodsPtr;
  2663. private Object MethodsStructure;
  2664. private FFIMethods Methods
  2665. {
  2666. get
  2667. {
  2668. if (MethodsStructure == null)
  2669. {
  2670. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  2671. }
  2672. return (FFIMethods)MethodsStructure;
  2673. }
  2674. }
  2675. internal StorageManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  2676. {
  2677. if (eventsPtr == IntPtr.Zero) {
  2678. throw new ResultException(Result.InternalError);
  2679. }
  2680. InitEvents(eventsPtr, ref events);
  2681. MethodsPtr = ptr;
  2682. if (MethodsPtr == IntPtr.Zero) {
  2683. throw new ResultException(Result.InternalError);
  2684. }
  2685. }
  2686. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  2687. {
  2688. Marshal.StructureToPtr(events, eventsPtr, false);
  2689. }
  2690. public UInt32 Read(string name, byte[] data)
  2691. {
  2692. var ret = new UInt32();
  2693. var res = Methods.Read(MethodsPtr, name, data, data.Length, ref ret);
  2694. if (res != Result.Ok)
  2695. {
  2696. throw new ResultException(res);
  2697. }
  2698. return ret;
  2699. }
  2700. [MonoPInvokeCallback]
  2701. private static void ReadAsyncCallbackImpl(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen)
  2702. {
  2703. GCHandle h = GCHandle.FromIntPtr(ptr);
  2704. ReadAsyncHandler callback = (ReadAsyncHandler)h.Target;
  2705. h.Free();
  2706. byte[] data = new byte[dataLen];
  2707. Marshal.Copy(dataPtr, data, 0, (int)dataLen);
  2708. callback(result, data);
  2709. }
  2710. public void ReadAsync(string name, ReadAsyncHandler callback)
  2711. {
  2712. GCHandle wrapped = GCHandle.Alloc(callback);
  2713. Methods.ReadAsync(MethodsPtr, name, GCHandle.ToIntPtr(wrapped), ReadAsyncCallbackImpl);
  2714. }
  2715. [MonoPInvokeCallback]
  2716. private static void ReadAsyncPartialCallbackImpl(IntPtr ptr, Result result, IntPtr dataPtr, Int32 dataLen)
  2717. {
  2718. GCHandle h = GCHandle.FromIntPtr(ptr);
  2719. ReadAsyncPartialHandler callback = (ReadAsyncPartialHandler)h.Target;
  2720. h.Free();
  2721. byte[] data = new byte[dataLen];
  2722. Marshal.Copy(dataPtr, data, 0, (int)dataLen);
  2723. callback(result, data);
  2724. }
  2725. public void ReadAsyncPartial(string name, UInt64 offset, UInt64 length, ReadAsyncPartialHandler callback)
  2726. {
  2727. GCHandle wrapped = GCHandle.Alloc(callback);
  2728. Methods.ReadAsyncPartial(MethodsPtr, name, offset, length, GCHandle.ToIntPtr(wrapped), ReadAsyncPartialCallbackImpl);
  2729. }
  2730. public void Write(string name, byte[] data)
  2731. {
  2732. var res = Methods.Write(MethodsPtr, name, data, data.Length);
  2733. if (res != Result.Ok)
  2734. {
  2735. throw new ResultException(res);
  2736. }
  2737. }
  2738. [MonoPInvokeCallback]
  2739. private static void WriteAsyncCallbackImpl(IntPtr ptr, Result result)
  2740. {
  2741. GCHandle h = GCHandle.FromIntPtr(ptr);
  2742. WriteAsyncHandler callback = (WriteAsyncHandler)h.Target;
  2743. h.Free();
  2744. callback(result);
  2745. }
  2746. public void WriteAsync(string name, byte[] data, WriteAsyncHandler callback)
  2747. {
  2748. GCHandle wrapped = GCHandle.Alloc(callback);
  2749. Methods.WriteAsync(MethodsPtr, name, data, data.Length, GCHandle.ToIntPtr(wrapped), WriteAsyncCallbackImpl);
  2750. }
  2751. public void Delete(string name)
  2752. {
  2753. var res = Methods.Delete(MethodsPtr, name);
  2754. if (res != Result.Ok)
  2755. {
  2756. throw new ResultException(res);
  2757. }
  2758. }
  2759. public bool Exists(string name)
  2760. {
  2761. var ret = new bool();
  2762. var res = Methods.Exists(MethodsPtr, name, ref ret);
  2763. if (res != Result.Ok)
  2764. {
  2765. throw new ResultException(res);
  2766. }
  2767. return ret;
  2768. }
  2769. public Int32 Count()
  2770. {
  2771. var ret = new Int32();
  2772. Methods.Count(MethodsPtr, ref ret);
  2773. return ret;
  2774. }
  2775. public FileStat Stat(string name)
  2776. {
  2777. var ret = new FileStat();
  2778. var res = Methods.Stat(MethodsPtr, name, ref ret);
  2779. if (res != Result.Ok)
  2780. {
  2781. throw new ResultException(res);
  2782. }
  2783. return ret;
  2784. }
  2785. public FileStat StatAt(Int32 index)
  2786. {
  2787. var ret = new FileStat();
  2788. var res = Methods.StatAt(MethodsPtr, index, ref ret);
  2789. if (res != Result.Ok)
  2790. {
  2791. throw new ResultException(res);
  2792. }
  2793. return ret;
  2794. }
  2795. public string GetPath()
  2796. {
  2797. var ret = new StringBuilder(4096);
  2798. var res = Methods.GetPath(MethodsPtr, ret);
  2799. if (res != Result.Ok)
  2800. {
  2801. throw new ResultException(res);
  2802. }
  2803. return ret.ToString();
  2804. }
  2805. }
  2806. public partial class StoreManager
  2807. {
  2808. [StructLayout(LayoutKind.Sequential)]
  2809. internal partial struct FFIEvents
  2810. {
  2811. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2812. internal delegate void EntitlementCreateHandler(IntPtr ptr, ref Entitlement entitlement);
  2813. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2814. internal delegate void EntitlementDeleteHandler(IntPtr ptr, ref Entitlement entitlement);
  2815. internal EntitlementCreateHandler OnEntitlementCreate;
  2816. internal EntitlementDeleteHandler OnEntitlementDelete;
  2817. }
  2818. [StructLayout(LayoutKind.Sequential)]
  2819. internal partial struct FFIMethods
  2820. {
  2821. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2822. internal delegate void FetchSkusCallback(IntPtr ptr, Result result);
  2823. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2824. internal delegate void FetchSkusMethod(IntPtr methodsPtr, IntPtr callbackData, FetchSkusCallback callback);
  2825. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2826. internal delegate void CountSkusMethod(IntPtr methodsPtr, ref Int32 count);
  2827. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2828. internal delegate Result GetSkuMethod(IntPtr methodsPtr, Int64 skuId, ref Sku sku);
  2829. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2830. internal delegate Result GetSkuAtMethod(IntPtr methodsPtr, Int32 index, ref Sku sku);
  2831. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2832. internal delegate void FetchEntitlementsCallback(IntPtr ptr, Result result);
  2833. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2834. internal delegate void FetchEntitlementsMethod(IntPtr methodsPtr, IntPtr callbackData, FetchEntitlementsCallback callback);
  2835. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2836. internal delegate void CountEntitlementsMethod(IntPtr methodsPtr, ref Int32 count);
  2837. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2838. internal delegate Result GetEntitlementMethod(IntPtr methodsPtr, Int64 entitlementId, ref Entitlement entitlement);
  2839. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2840. internal delegate Result GetEntitlementAtMethod(IntPtr methodsPtr, Int32 index, ref Entitlement entitlement);
  2841. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2842. internal delegate Result HasSkuEntitlementMethod(IntPtr methodsPtr, Int64 skuId, ref bool hasEntitlement);
  2843. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2844. internal delegate void StartPurchaseCallback(IntPtr ptr, Result result);
  2845. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  2846. internal delegate void StartPurchaseMethod(IntPtr methodsPtr, Int64 skuId, IntPtr callbackData, StartPurchaseCallback callback);
  2847. internal FetchSkusMethod FetchSkus;
  2848. internal CountSkusMethod CountSkus;
  2849. internal GetSkuMethod GetSku;
  2850. internal GetSkuAtMethod GetSkuAt;
  2851. internal FetchEntitlementsMethod FetchEntitlements;
  2852. internal CountEntitlementsMethod CountEntitlements;
  2853. internal GetEntitlementMethod GetEntitlement;
  2854. internal GetEntitlementAtMethod GetEntitlementAt;
  2855. internal HasSkuEntitlementMethod HasSkuEntitlement;
  2856. internal StartPurchaseMethod StartPurchase;
  2857. }
  2858. public delegate void FetchSkusHandler(Result result);
  2859. public delegate void FetchEntitlementsHandler(Result result);
  2860. public delegate void StartPurchaseHandler(Result result);
  2861. public delegate void EntitlementCreateHandler(ref Entitlement entitlement);
  2862. public delegate void EntitlementDeleteHandler(ref Entitlement entitlement);
  2863. private IntPtr MethodsPtr;
  2864. private Object MethodsStructure;
  2865. private FFIMethods Methods
  2866. {
  2867. get
  2868. {
  2869. if (MethodsStructure == null)
  2870. {
  2871. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  2872. }
  2873. return (FFIMethods)MethodsStructure;
  2874. }
  2875. }
  2876. public event EntitlementCreateHandler OnEntitlementCreate;
  2877. public event EntitlementDeleteHandler OnEntitlementDelete;
  2878. internal StoreManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  2879. {
  2880. if (eventsPtr == IntPtr.Zero) {
  2881. throw new ResultException(Result.InternalError);
  2882. }
  2883. InitEvents(eventsPtr, ref events);
  2884. MethodsPtr = ptr;
  2885. if (MethodsPtr == IntPtr.Zero) {
  2886. throw new ResultException(Result.InternalError);
  2887. }
  2888. }
  2889. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  2890. {
  2891. events.OnEntitlementCreate = OnEntitlementCreateImpl;
  2892. events.OnEntitlementDelete = OnEntitlementDeleteImpl;
  2893. Marshal.StructureToPtr(events, eventsPtr, false);
  2894. }
  2895. [MonoPInvokeCallback]
  2896. private static void FetchSkusCallbackImpl(IntPtr ptr, Result result)
  2897. {
  2898. GCHandle h = GCHandle.FromIntPtr(ptr);
  2899. FetchSkusHandler callback = (FetchSkusHandler)h.Target;
  2900. h.Free();
  2901. callback(result);
  2902. }
  2903. public void FetchSkus(FetchSkusHandler callback)
  2904. {
  2905. GCHandle wrapped = GCHandle.Alloc(callback);
  2906. Methods.FetchSkus(MethodsPtr, GCHandle.ToIntPtr(wrapped), FetchSkusCallbackImpl);
  2907. }
  2908. public Int32 CountSkus()
  2909. {
  2910. var ret = new Int32();
  2911. Methods.CountSkus(MethodsPtr, ref ret);
  2912. return ret;
  2913. }
  2914. public Sku GetSku(Int64 skuId)
  2915. {
  2916. var ret = new Sku();
  2917. var res = Methods.GetSku(MethodsPtr, skuId, ref ret);
  2918. if (res != Result.Ok)
  2919. {
  2920. throw new ResultException(res);
  2921. }
  2922. return ret;
  2923. }
  2924. public Sku GetSkuAt(Int32 index)
  2925. {
  2926. var ret = new Sku();
  2927. var res = Methods.GetSkuAt(MethodsPtr, index, ref ret);
  2928. if (res != Result.Ok)
  2929. {
  2930. throw new ResultException(res);
  2931. }
  2932. return ret;
  2933. }
  2934. [MonoPInvokeCallback]
  2935. private static void FetchEntitlementsCallbackImpl(IntPtr ptr, Result result)
  2936. {
  2937. GCHandle h = GCHandle.FromIntPtr(ptr);
  2938. FetchEntitlementsHandler callback = (FetchEntitlementsHandler)h.Target;
  2939. h.Free();
  2940. callback(result);
  2941. }
  2942. public void FetchEntitlements(FetchEntitlementsHandler callback)
  2943. {
  2944. GCHandle wrapped = GCHandle.Alloc(callback);
  2945. Methods.FetchEntitlements(MethodsPtr, GCHandle.ToIntPtr(wrapped), FetchEntitlementsCallbackImpl);
  2946. }
  2947. public Int32 CountEntitlements()
  2948. {
  2949. var ret = new Int32();
  2950. Methods.CountEntitlements(MethodsPtr, ref ret);
  2951. return ret;
  2952. }
  2953. public Entitlement GetEntitlement(Int64 entitlementId)
  2954. {
  2955. var ret = new Entitlement();
  2956. var res = Methods.GetEntitlement(MethodsPtr, entitlementId, ref ret);
  2957. if (res != Result.Ok)
  2958. {
  2959. throw new ResultException(res);
  2960. }
  2961. return ret;
  2962. }
  2963. public Entitlement GetEntitlementAt(Int32 index)
  2964. {
  2965. var ret = new Entitlement();
  2966. var res = Methods.GetEntitlementAt(MethodsPtr, index, ref ret);
  2967. if (res != Result.Ok)
  2968. {
  2969. throw new ResultException(res);
  2970. }
  2971. return ret;
  2972. }
  2973. public bool HasSkuEntitlement(Int64 skuId)
  2974. {
  2975. var ret = new bool();
  2976. var res = Methods.HasSkuEntitlement(MethodsPtr, skuId, ref ret);
  2977. if (res != Result.Ok)
  2978. {
  2979. throw new ResultException(res);
  2980. }
  2981. return ret;
  2982. }
  2983. [MonoPInvokeCallback]
  2984. private static void StartPurchaseCallbackImpl(IntPtr ptr, Result result)
  2985. {
  2986. GCHandle h = GCHandle.FromIntPtr(ptr);
  2987. StartPurchaseHandler callback = (StartPurchaseHandler)h.Target;
  2988. h.Free();
  2989. callback(result);
  2990. }
  2991. public void StartPurchase(Int64 skuId, StartPurchaseHandler callback)
  2992. {
  2993. GCHandle wrapped = GCHandle.Alloc(callback);
  2994. Methods.StartPurchase(MethodsPtr, skuId, GCHandle.ToIntPtr(wrapped), StartPurchaseCallbackImpl);
  2995. }
  2996. [MonoPInvokeCallback]
  2997. private static void OnEntitlementCreateImpl(IntPtr ptr, ref Entitlement entitlement)
  2998. {
  2999. GCHandle h = GCHandle.FromIntPtr(ptr);
  3000. Discord d = (Discord)h.Target;
  3001. if (d.StoreManagerInstance.OnEntitlementCreate != null)
  3002. {
  3003. d.StoreManagerInstance.OnEntitlementCreate.Invoke(ref entitlement);
  3004. }
  3005. }
  3006. [MonoPInvokeCallback]
  3007. private static void OnEntitlementDeleteImpl(IntPtr ptr, ref Entitlement entitlement)
  3008. {
  3009. GCHandle h = GCHandle.FromIntPtr(ptr);
  3010. Discord d = (Discord)h.Target;
  3011. if (d.StoreManagerInstance.OnEntitlementDelete != null)
  3012. {
  3013. d.StoreManagerInstance.OnEntitlementDelete.Invoke(ref entitlement);
  3014. }
  3015. }
  3016. }
  3017. public partial class VoiceManager
  3018. {
  3019. [StructLayout(LayoutKind.Sequential)]
  3020. internal partial struct FFIEvents
  3021. {
  3022. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3023. internal delegate void SettingsUpdateHandler(IntPtr ptr);
  3024. internal SettingsUpdateHandler OnSettingsUpdate;
  3025. }
  3026. [StructLayout(LayoutKind.Sequential)]
  3027. internal partial struct FFIMethods
  3028. {
  3029. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3030. internal delegate Result GetInputModeMethod(IntPtr methodsPtr, ref InputMode inputMode);
  3031. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3032. internal delegate void SetInputModeCallback(IntPtr ptr, Result result);
  3033. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3034. internal delegate void SetInputModeMethod(IntPtr methodsPtr, InputMode inputMode, IntPtr callbackData, SetInputModeCallback callback);
  3035. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3036. internal delegate Result IsSelfMuteMethod(IntPtr methodsPtr, ref bool mute);
  3037. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3038. internal delegate Result SetSelfMuteMethod(IntPtr methodsPtr, bool mute);
  3039. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3040. internal delegate Result IsSelfDeafMethod(IntPtr methodsPtr, ref bool deaf);
  3041. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3042. internal delegate Result SetSelfDeafMethod(IntPtr methodsPtr, bool deaf);
  3043. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3044. internal delegate Result IsLocalMuteMethod(IntPtr methodsPtr, Int64 userId, ref bool mute);
  3045. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3046. internal delegate Result SetLocalMuteMethod(IntPtr methodsPtr, Int64 userId, bool mute);
  3047. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3048. internal delegate Result GetLocalVolumeMethod(IntPtr methodsPtr, Int64 userId, ref byte volume);
  3049. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3050. internal delegate Result SetLocalVolumeMethod(IntPtr methodsPtr, Int64 userId, byte volume);
  3051. internal GetInputModeMethod GetInputMode;
  3052. internal SetInputModeMethod SetInputMode;
  3053. internal IsSelfMuteMethod IsSelfMute;
  3054. internal SetSelfMuteMethod SetSelfMute;
  3055. internal IsSelfDeafMethod IsSelfDeaf;
  3056. internal SetSelfDeafMethod SetSelfDeaf;
  3057. internal IsLocalMuteMethod IsLocalMute;
  3058. internal SetLocalMuteMethod SetLocalMute;
  3059. internal GetLocalVolumeMethod GetLocalVolume;
  3060. internal SetLocalVolumeMethod SetLocalVolume;
  3061. }
  3062. public delegate void SetInputModeHandler(Result result);
  3063. public delegate void SettingsUpdateHandler();
  3064. private IntPtr MethodsPtr;
  3065. private Object MethodsStructure;
  3066. private FFIMethods Methods
  3067. {
  3068. get
  3069. {
  3070. if (MethodsStructure == null)
  3071. {
  3072. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  3073. }
  3074. return (FFIMethods)MethodsStructure;
  3075. }
  3076. }
  3077. public event SettingsUpdateHandler OnSettingsUpdate;
  3078. internal VoiceManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  3079. {
  3080. if (eventsPtr == IntPtr.Zero) {
  3081. throw new ResultException(Result.InternalError);
  3082. }
  3083. InitEvents(eventsPtr, ref events);
  3084. MethodsPtr = ptr;
  3085. if (MethodsPtr == IntPtr.Zero) {
  3086. throw new ResultException(Result.InternalError);
  3087. }
  3088. }
  3089. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  3090. {
  3091. events.OnSettingsUpdate = OnSettingsUpdateImpl;
  3092. Marshal.StructureToPtr(events, eventsPtr, false);
  3093. }
  3094. public InputMode GetInputMode()
  3095. {
  3096. var ret = new InputMode();
  3097. var res = Methods.GetInputMode(MethodsPtr, ref ret);
  3098. if (res != Result.Ok)
  3099. {
  3100. throw new ResultException(res);
  3101. }
  3102. return ret;
  3103. }
  3104. [MonoPInvokeCallback]
  3105. private static void SetInputModeCallbackImpl(IntPtr ptr, Result result)
  3106. {
  3107. GCHandle h = GCHandle.FromIntPtr(ptr);
  3108. SetInputModeHandler callback = (SetInputModeHandler)h.Target;
  3109. h.Free();
  3110. callback(result);
  3111. }
  3112. public void SetInputMode(InputMode inputMode, SetInputModeHandler callback)
  3113. {
  3114. GCHandle wrapped = GCHandle.Alloc(callback);
  3115. Methods.SetInputMode(MethodsPtr, inputMode, GCHandle.ToIntPtr(wrapped), SetInputModeCallbackImpl);
  3116. }
  3117. public bool IsSelfMute()
  3118. {
  3119. var ret = new bool();
  3120. var res = Methods.IsSelfMute(MethodsPtr, ref ret);
  3121. if (res != Result.Ok)
  3122. {
  3123. throw new ResultException(res);
  3124. }
  3125. return ret;
  3126. }
  3127. public void SetSelfMute(bool mute)
  3128. {
  3129. var res = Methods.SetSelfMute(MethodsPtr, mute);
  3130. if (res != Result.Ok)
  3131. {
  3132. throw new ResultException(res);
  3133. }
  3134. }
  3135. public bool IsSelfDeaf()
  3136. {
  3137. var ret = new bool();
  3138. var res = Methods.IsSelfDeaf(MethodsPtr, ref ret);
  3139. if (res != Result.Ok)
  3140. {
  3141. throw new ResultException(res);
  3142. }
  3143. return ret;
  3144. }
  3145. public void SetSelfDeaf(bool deaf)
  3146. {
  3147. var res = Methods.SetSelfDeaf(MethodsPtr, deaf);
  3148. if (res != Result.Ok)
  3149. {
  3150. throw new ResultException(res);
  3151. }
  3152. }
  3153. public bool IsLocalMute(Int64 userId)
  3154. {
  3155. var ret = new bool();
  3156. var res = Methods.IsLocalMute(MethodsPtr, userId, ref ret);
  3157. if (res != Result.Ok)
  3158. {
  3159. throw new ResultException(res);
  3160. }
  3161. return ret;
  3162. }
  3163. public void SetLocalMute(Int64 userId, bool mute)
  3164. {
  3165. var res = Methods.SetLocalMute(MethodsPtr, userId, mute);
  3166. if (res != Result.Ok)
  3167. {
  3168. throw new ResultException(res);
  3169. }
  3170. }
  3171. public byte GetLocalVolume(Int64 userId)
  3172. {
  3173. var ret = new byte();
  3174. var res = Methods.GetLocalVolume(MethodsPtr, userId, ref ret);
  3175. if (res != Result.Ok)
  3176. {
  3177. throw new ResultException(res);
  3178. }
  3179. return ret;
  3180. }
  3181. public void SetLocalVolume(Int64 userId, byte volume)
  3182. {
  3183. var res = Methods.SetLocalVolume(MethodsPtr, userId, volume);
  3184. if (res != Result.Ok)
  3185. {
  3186. throw new ResultException(res);
  3187. }
  3188. }
  3189. [MonoPInvokeCallback]
  3190. private static void OnSettingsUpdateImpl(IntPtr ptr)
  3191. {
  3192. GCHandle h = GCHandle.FromIntPtr(ptr);
  3193. Discord d = (Discord)h.Target;
  3194. if (d.VoiceManagerInstance.OnSettingsUpdate != null)
  3195. {
  3196. d.VoiceManagerInstance.OnSettingsUpdate.Invoke();
  3197. }
  3198. }
  3199. }
  3200. public partial class AchievementManager
  3201. {
  3202. [StructLayout(LayoutKind.Sequential)]
  3203. internal partial struct FFIEvents
  3204. {
  3205. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3206. internal delegate void UserAchievementUpdateHandler(IntPtr ptr, ref UserAchievement userAchievement);
  3207. internal UserAchievementUpdateHandler OnUserAchievementUpdate;
  3208. }
  3209. [StructLayout(LayoutKind.Sequential)]
  3210. internal partial struct FFIMethods
  3211. {
  3212. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3213. internal delegate void SetUserAchievementCallback(IntPtr ptr, Result result);
  3214. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3215. internal delegate void SetUserAchievementMethod(IntPtr methodsPtr, Int64 achievementId, byte percentComplete, IntPtr callbackData, SetUserAchievementCallback callback);
  3216. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3217. internal delegate void FetchUserAchievementsCallback(IntPtr ptr, Result result);
  3218. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3219. internal delegate void FetchUserAchievementsMethod(IntPtr methodsPtr, IntPtr callbackData, FetchUserAchievementsCallback callback);
  3220. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3221. internal delegate void CountUserAchievementsMethod(IntPtr methodsPtr, ref Int32 count);
  3222. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3223. internal delegate Result GetUserAchievementMethod(IntPtr methodsPtr, Int64 userAchievementId, ref UserAchievement userAchievement);
  3224. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  3225. internal delegate Result GetUserAchievementAtMethod(IntPtr methodsPtr, Int32 index, ref UserAchievement userAchievement);
  3226. internal SetUserAchievementMethod SetUserAchievement;
  3227. internal FetchUserAchievementsMethod FetchUserAchievements;
  3228. internal CountUserAchievementsMethod CountUserAchievements;
  3229. internal GetUserAchievementMethod GetUserAchievement;
  3230. internal GetUserAchievementAtMethod GetUserAchievementAt;
  3231. }
  3232. public delegate void SetUserAchievementHandler(Result result);
  3233. public delegate void FetchUserAchievementsHandler(Result result);
  3234. public delegate void UserAchievementUpdateHandler(ref UserAchievement userAchievement);
  3235. private IntPtr MethodsPtr;
  3236. private Object MethodsStructure;
  3237. private FFIMethods Methods
  3238. {
  3239. get
  3240. {
  3241. if (MethodsStructure == null)
  3242. {
  3243. MethodsStructure = Marshal.PtrToStructure(MethodsPtr, typeof(FFIMethods));
  3244. }
  3245. return (FFIMethods)MethodsStructure;
  3246. }
  3247. }
  3248. public event UserAchievementUpdateHandler OnUserAchievementUpdate;
  3249. internal AchievementManager(IntPtr ptr, IntPtr eventsPtr, ref FFIEvents events)
  3250. {
  3251. if (eventsPtr == IntPtr.Zero) {
  3252. throw new ResultException(Result.InternalError);
  3253. }
  3254. InitEvents(eventsPtr, ref events);
  3255. MethodsPtr = ptr;
  3256. if (MethodsPtr == IntPtr.Zero) {
  3257. throw new ResultException(Result.InternalError);
  3258. }
  3259. }
  3260. private void InitEvents(IntPtr eventsPtr, ref FFIEvents events)
  3261. {
  3262. events.OnUserAchievementUpdate = OnUserAchievementUpdateImpl;
  3263. Marshal.StructureToPtr(events, eventsPtr, false);
  3264. }
  3265. [MonoPInvokeCallback]
  3266. private static void SetUserAchievementCallbackImpl(IntPtr ptr, Result result)
  3267. {
  3268. GCHandle h = GCHandle.FromIntPtr(ptr);
  3269. SetUserAchievementHandler callback = (SetUserAchievementHandler)h.Target;
  3270. h.Free();
  3271. callback(result);
  3272. }
  3273. public void SetUserAchievement(Int64 achievementId, byte percentComplete, SetUserAchievementHandler callback)
  3274. {
  3275. GCHandle wrapped = GCHandle.Alloc(callback);
  3276. Methods.SetUserAchievement(MethodsPtr, achievementId, percentComplete, GCHandle.ToIntPtr(wrapped), SetUserAchievementCallbackImpl);
  3277. }
  3278. [MonoPInvokeCallback]
  3279. private static void FetchUserAchievementsCallbackImpl(IntPtr ptr, Result result)
  3280. {
  3281. GCHandle h = GCHandle.FromIntPtr(ptr);
  3282. FetchUserAchievementsHandler callback = (FetchUserAchievementsHandler)h.Target;
  3283. h.Free();
  3284. callback(result);
  3285. }
  3286. public void FetchUserAchievements(FetchUserAchievementsHandler callback)
  3287. {
  3288. GCHandle wrapped = GCHandle.Alloc(callback);
  3289. Methods.FetchUserAchievements(MethodsPtr, GCHandle.ToIntPtr(wrapped), FetchUserAchievementsCallbackImpl);
  3290. }
  3291. public Int32 CountUserAchievements()
  3292. {
  3293. var ret = new Int32();
  3294. Methods.CountUserAchievements(MethodsPtr, ref ret);
  3295. return ret;
  3296. }
  3297. public UserAchievement GetUserAchievement(Int64 userAchievementId)
  3298. {
  3299. var ret = new UserAchievement();
  3300. var res = Methods.GetUserAchievement(MethodsPtr, userAchievementId, ref ret);
  3301. if (res != Result.Ok)
  3302. {
  3303. throw new ResultException(res);
  3304. }
  3305. return ret;
  3306. }
  3307. public UserAchievement GetUserAchievementAt(Int32 index)
  3308. {
  3309. var ret = new UserAchievement();
  3310. var res = Methods.GetUserAchievementAt(MethodsPtr, index, ref ret);
  3311. if (res != Result.Ok)
  3312. {
  3313. throw new ResultException(res);
  3314. }
  3315. return ret;
  3316. }
  3317. [MonoPInvokeCallback]
  3318. private static void OnUserAchievementUpdateImpl(IntPtr ptr, ref UserAchievement userAchievement)
  3319. {
  3320. GCHandle h = GCHandle.FromIntPtr(ptr);
  3321. Discord d = (Discord)h.Target;
  3322. if (d.AchievementManagerInstance.OnUserAchievementUpdate != null)
  3323. {
  3324. d.AchievementManagerInstance.OnUserAchievementUpdate.Invoke(ref userAchievement);
  3325. }
  3326. }
  3327. }
  3328. }