Exemplo n.º 1
0
        static async Task Run()
        {
            Console.WriteLine("Presence Started");
            string str = String.Empty;

            do
            {
                if (ready)
                {
                    Console.WriteLine("Enter Next State:");
                    str      = Console.ReadLine();
                    RP.State = str;
                    await Discord.UpdatePresenceAsync(RP);
                }
                else
                {
                    Console.Write(".");
                    DiscordEventHandlers handlers = new DiscordEventHandlers {
                    };
                    handlers.ready        = Ready;
                    handlers.disconnected = Disconnected;
                    await Discord.InitializeAsync("418842770057461762", handlers, 1, "");
                }
                await Task.Delay(100);
            } while (str.ToLower() != "c");
            await Discord.ShutdownAsync();
        }
Exemplo n.º 2
0
        public RichPresence()
        {
            if (Environment.Is64BitProcess)
            {
                LibName = "discord-rpc-64.dll";
            }
            // Embedding the native lib makes deployment easier. TODO possibly embed multiple platforms for a cross-platform release?
            // https://stackoverflow.com/questions/4651803/loading-a-library-dynamically-in-linux-or-osx
            // https://stackoverflow.com/questions/9954548/sigsegv-when-p-invoking-dlopen
            // RuntimeInformation.IsOSPlatform(OSPlatform.Windows)

            _tempPath = Path.Combine(Path.GetTempPath(), LibName);
            if (File.Exists(_tempPath))
            {
                File.Delete(_tempPath);
            }

            using (var fs = File.Create(_tempPath))
                using (var s = typeof(RichPresence).Assembly.GetManifestResourceStream($"DiscordRpc.{LibName}.gz"))
                    using (var gz = new GZipStream(s, CompressionMode.Decompress))
                    {
                        gz.CopyTo(fs);
                    }

            _hDiscordLib = NativeMethods.LoadLibrary(_tempPath);

            if (_hDiscordLib == IntPtr.Zero)
            {
                throw new TypeLoadException($"Failed to load Discord RPC lib from {LibName}: {NativeMethods.GetLastError()}\nTemp Path: {_tempPath}");
            }

            // Marshal methods from native lib
            _initialize     = WrapNativeFunction <InitializeDel>(_hDiscordLib, RpcMethods.Initialize);
            _shutdown       = WrapNativeFunction <ShutdownDel>(_hDiscordLib, RpcMethods.Shutdown);
            _runCallbacks   = WrapNativeFunction <RunCallbacksDel>(_hDiscordLib, RpcMethods.RunCallbacks);
            _updatePresence = WrapNativeFunction <UpdatePresenceDel>(_hDiscordLib, RpcMethods.UpdatePresence);
            _clearPresence  = WrapNativeFunction <ClearPresenceDel>(_hDiscordLib, RpcMethods.ClearPresence);
            _respond        = WrapNativeFunction <RespondDel>(_hDiscordLib, RpcMethods.Respond);

            // Marshal methods to native lib
            _eventHandlers = new DiscordEventHandlers
            {
                Ready        = OnReady,
                Disconnected = OnDisconnected,
                Errored      = OnErrored,
                JoinGame     = OnJoinGame,
                SpectateGame = OnSpectateGame,
                JoinRequest  = OnJoinRequest
            };
        }
 public static extern void Initialize(string applicationId, ref DiscordEventHandlers handlers, bool autoRegister, string optionalSteamId);