diff --git a/GlobalSuppressions.cs b/GlobalSuppressions.cs new file mode 100644 index 0000000..a5fe501 --- /dev/null +++ b/GlobalSuppressions.cs @@ -0,0 +1,8 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage( "Interoperability", "CA1416:Validate platform compatibility", Justification = "", Scope = "member", Target = "~M:CSGODiscordRP.ApplicationSetup.GetCSGODir~System.String" )] diff --git a/README.md b/README.md index 782e1c8..48ebdc5 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ A Rich Presence application for showing custom status of your active Counter-Str - [CS:GO Discord Rich Presence](#csgo-discord-rich-presence) - [How It Works](#how-it-works) - [Install](#install) + - [Troubleshooting](#troubleshooting) - [Credits](#credits) - [Licensing](#licensing) @@ -14,10 +15,12 @@ This application uses the [Counter-Strike: Global Offensive Game State Integrati ## Install Go to the [latest release](https://github.com/Retr0-01/CSGO-Discord-RP/releases/latest), download the ``CSGO-Discord-RP.zip`` file from the Assets section and then extract it to the location of your choosing. Now you should have the exe and a file called ``gamestate_integration_discord-rp.cfg``. -Cut the cfg file and paste it inside the "cfg" folder of your CS:GO installation. -For example ``C:\Program Files (x86)\Steam\steamapps\common\Counter-Strike Global Offensive\csgo\cfg`` is where I will have to put it. -Once you have done that just run the exe file and you should be good to go! +All you have to do now is run the exe and you should be good to go! +## Troubleshooting +**[SETUP] CS:GO installation path not found! Aborting...** +> If for whatever reason the application couldn't find your game installation path or write the config file needed, you need to manually copy the ``gamestate_integration_discord-rp.cfg`` file and paste it *inside* the ``cfg`` folder of your CS:GO installation. +> Example path: `C:\Program Files (x86)\Steam\steamapps\common\Counter-Strike Global Offensive\csgo\cfg` ## Credits Huge thanks to the following devs/teams for creating some of the packages/tools used. - [Newtonsoft](https://www.newtonsoft.com/json) for Json.NET diff --git a/src/Classes/ApplicationSetup.cs b/src/Classes/ApplicationSetup.cs new file mode 100644 index 0000000..86a4558 --- /dev/null +++ b/src/Classes/ApplicationSetup.cs @@ -0,0 +1,97 @@ +using Microsoft.Win32; +using System.Text.RegularExpressions; + +namespace CSGODiscordRP; + +public static class ApplicationSetup +{ + public static readonly string configFileName = "gamestate_integration_discord-rp.cfg"; + + public static void Configure() + { + string gamePath = GetCSGODir(); + if ( gamePath == null ) + Console.WriteLine( "[SETUP] CS:GO installation path not found! Aborting..." ); + else + { + Console.WriteLine( $"[SETUP] Found CS:GO installation path...\n|--- {gamePath}" ); + + string configFile = Path.Combine( gamePath, "cfg", configFileName ); + // Delete the file if it already exists and make a clean one. + if ( File.Exists( configFile ) ) + { + File.Delete( configFile ); + Console.WriteLine( "[SETUP] Deleted existing config file." ); + } + + Console.WriteLine( $"[SETUP] Writing \"{configFileName}\" config file..." ); + using ( StreamWriter sw = File.CreateText( configFile ) ) + { + sw.WriteLine( "\"CSGO-Discord-RP\"" ); + sw.WriteLine( "{" ); + sw.WriteLine( " \"uri\" \"http://localhost:3000\"" ); + sw.WriteLine( " \"timeout\" \"5.0\"" ); + sw.WriteLine( " \"buffer\" \"1\"" ); + sw.WriteLine( " \"throttle\" \"5\"" ); + sw.WriteLine( " \"heartbeat\" \"15\"" ); + sw.WriteLine( " \"data\"" ); + sw.WriteLine( " {" ); + sw.WriteLine( " \"provider\" \"1\"" ); + sw.WriteLine( " \"map\" \"1\"" ); + sw.WriteLine( " \"round\" \"1\"" ); + sw.WriteLine( " \"player_id\" \"1\"" ); + sw.WriteLine( " \"player_match_stats\" \"1\"" ); + sw.WriteLine( " \"player_state\" \"1\"" ); + sw.WriteLine( " }" ); + sw.WriteLine( "}" ); + } + Console.WriteLine( "[SETUP] Done!" ); + } + } + + /// + /// Returns the location of the CS:GO installation, or null if it's unable to find it. + /// + /// + private static string GetCSGODir() + { + // Credit to moritzuehling for this code snippet + // https://gist.github.com/moritzuehling/7f1c512871e193c0222f + + string steamPath = (string)Registry.GetValue( "HKEY_CURRENT_USER\\Software\\Valve\\Steam", "SteamPath", "" ); + string pathsFile = Path.Combine( steamPath, "steamapps", "libraryfolders.vdf" ); + + if ( !File.Exists( pathsFile ) ) + return null; + + List libraries = new() + { + Path.Combine( steamPath ) + }; + + var pathVDF = File.ReadAllLines( pathsFile ); + + Regex pathRegex = new( @"\""(([^\""]*):\\([^\""]*))\""" ); + foreach ( var line in pathVDF ) + { + if ( pathRegex.IsMatch( line ) ) + { + string match = pathRegex.Matches( line )[0].Groups[1].Value; + + // De-Escape vdf. + libraries.Add( match.Replace( "\\\\", "\\" ) ); + } + } + + foreach ( var library in libraries ) + { + string csgoPath = Path.Combine( library, "steamapps\\common\\Counter-Strike Global Offensive\\csgo" ); + if ( Directory.Exists( csgoPath ) ) + { + return csgoPath; + } + } + + return null; + } +} diff --git a/src/Program.cs b/src/Program.cs index 9b7f005..90f867b 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -10,6 +10,7 @@ public static void Main( string[] args ) Console.WriteLine( "Source Code: https://github.com/Retr0-01/CSGO-Discord-RP" ); Console.WriteLine(); + ApplicationSetup.Configure(); DiscordManager.Initialize(); HttpServer.Start(); }