Skip to content

Commit

Permalink
Fix crash when Install locations contains invalid folders
Browse files Browse the repository at this point in the history
  • Loading branch information
floh22 committed Jul 9, 2021
1 parent 4895d00 commit 12b3a19
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 97 deletions.
196 changes: 101 additions & 95 deletions LeagueBroadcast/Ingame/Data/Provider/LiveEventsDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@ public LiveEventsDataProvider()
{
//Check default path for league install. Prompt user if it cannot be found
Log.Info("Checking League install for LiveEventsAPI");
bool found = false;
ConfigController.Component.App.LeagueInstall.ForEach(InstallLocation =>
{
found = CheckGameConfigLocation(InstallLocation) || found;
});

if (!found)
if (!ConfigController.Component.App.LeagueInstall.Any(InstallLocation => CheckGameConfigLocation(InstallLocation)))
{

//Prompt user for league install location
Expand Down Expand Up @@ -67,116 +61,128 @@ public void Connect()

private bool CheckGameConfigLocation(string configLocation)
{
//Check for config file in given folder
List<string> files = Directory.GetFiles(configLocation).Select(f => Path.GetFileName(f)).ToList();

//Check for config folder in given folder
List<string> folders = Directory.GetDirectories(configLocation).Select(f => f = f.Replace(configLocation, "").Remove(0, 1)).ToList();

//Determine which to use depending on location of game.cfg
string LeagueFolder = folders.Contains("Config") && configLocation.EndsWith("League of Legends")
? Path.Combine(configLocation, "Config")
: files.Contains("game.cfg")
? configLocation
: Path.Join(Path.Join(Path.Join(configLocation, "Riot Games"), "League of Legends"), "Config");

if (Directory.Exists(LeagueFolder))
//Make sure the folder exists
if(!Directory.Exists(configLocation))
return false;
try
{
Log.Info("Found League install location");
string cfgContent = "";
try
{
cfgContent = File.ReadAllText(Path.Join(LeagueFolder, "game.cfg"));
}
catch
{
Log.Warn("Could not find config file in config folder");
return false;
}
//Check for Live Events
if (!(cfgContent.Contains("[LiveEvents]") && cfgContent.Contains("Enable=1")))
{
Log.Info("Could not find LiveEvents in game config. Appending to end");
var writer = File.AppendText(Path.Join(LeagueFolder, "game.cfg"));
writer.Write($"\n\n{EnableAPIString}");
writer.Close();
Log.Info("Updated Game Config");
}
else
{
Log.Info("LiveEvents API found in Game config");
}
//Check for config file in given folder
List<string> files = Directory.GetFiles(configLocation).Select(f => Path.GetFileName(f)).ToList();

//Check for Replay API. This shouldnt be here but i'll shoehorn it in since it works
List<string> lines = cfgContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
int generalLoc = lines.FindIndex(0, l => l == "[General]");
//Check for config folder in given folder
List<string> folders = Directory.GetDirectories(configLocation).Select(f => f = f.Replace(configLocation, "").Remove(0, 1)).ToList();

//found general config
if(generalLoc != -1)
{
int ReplayAPILineLoc = lines.FindIndex(l => l.StartsWith("EnableReplayApi"));
bool Overwrite = false;
//Determine which to use depending on location of game.cfg
string LeagueFolder = folders.Contains("Config") && configLocation.EndsWith("League of Legends")
? Path.Combine(configLocation, "Config")
: files.Contains("game.cfg")
? configLocation
: Path.Join(Path.Join(Path.Join(configLocation, "Riot Games"), "League of Legends"), "Config");

//Replay API Line does not exist
if (ReplayAPILineLoc == -1)
if (Directory.Exists(LeagueFolder))
{
Log.Info("Found League install location");
string cfgContent = "";
try
{
Log.Verbose("Could not find Replay API in config");
lines.Insert(generalLoc + 2, EnableReplayAPIString);
Overwrite = true;
cfgContent = File.ReadAllText(Path.Join(LeagueFolder, "game.cfg"));
}
//Replay API disabled
else if(lines[ReplayAPILineLoc].Contains("0"))
catch
{
Log.Info("Replay API has been manually disabled. Reenabling");
lines[ReplayAPILineLoc] = EnableReplayAPIString;
Overwrite = true;
Log.Warn("Could not find config file in config folder");
return false;
}

if(Overwrite)
//Check for Live Events
if (!(cfgContent.Contains("[LiveEvents]") && cfgContent.Contains("Enable=1")))
{
Log.Info("Enabling Replay API in Game config");
File.WriteAllLines(Path.Join(LeagueFolder, "game.cfg"), lines);
Log.Verbose("Replay API enabled");
} else
Log.Info("Could not find LiveEvents in game config. Appending to end");
var writer = File.AppendText(Path.Join(LeagueFolder, "game.cfg"));
writer.Write($"\n\n{EnableAPIString}");
writer.Close();
Log.Info("Updated Game Config");
}
else
{
//Replay API Enabled
Log.Info("Replay API found in Game config");
Log.Info("LiveEvents API found in Game config");
}
} else
{
Log.Warn("Could not parse game config. Replay API may not be enabled!");
}

//Check for Replay API. This shouldnt be here but i'll shoehorn it in since it works
List<string> lines = cfgContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
int generalLoc = lines.FindIndex(0, l => l == "[General]");

try
{
Log.Info("Verifying LiveEvents list");
var liveEventsCfg = Path.Join(LeagueFolder, "LiveEvents.ini");
var events = File.ReadAllLines(liveEventsCfg);
//found general config
if (generalLoc != -1)
{
int ReplayAPILineLoc = lines.FindIndex(l => l.StartsWith("EnableReplayApi"));
bool Overwrite = false;

//Replay API Line does not exist
if (ReplayAPILineLoc == -1)
{
Log.Verbose("Could not find Replay API in config");
lines.Insert(generalLoc + 2, EnableReplayAPIString);
Overwrite = true;
}
//Replay API disabled
else if (lines[ReplayAPILineLoc].Contains("0"))
{
Log.Info("Replay API has been manually disabled. Reenabling");
lines[ReplayAPILineLoc] = EnableReplayAPIString;
Overwrite = true;
}

if (Overwrite)
{
Log.Info("Enabling Replay API in Game config");
File.WriteAllLines(Path.Join(LeagueFolder, "game.cfg"), lines);
Log.Verbose("Replay API enabled");
}
else
{
//Replay API Enabled
Log.Info("Replay API found in Game config");
}
}
else
{
Log.Warn("Could not parse game config. Replay API may not be enabled!");
}

if (!events.Contains("OnMinionKill"))

try
{
File.AppendText(liveEventsCfg).Write("\nOnMinionKill");
Log.Info("Adding OnMinionKill to LiveEvents.ini");
Log.Info("Verifying LiveEvents list");
var liveEventsCfg = Path.Join(LeagueFolder, "LiveEvents.ini");
var events = File.ReadAllLines(liveEventsCfg);

if (!events.Contains("OnMinionKill"))
{
File.AppendText(liveEventsCfg).Write("\nOnMinionKill");
Log.Info("Adding OnMinionKill to LiveEvents.ini");
}
if (!events.Contains("OnNeutralMinionKill"))
{
File.AppendText(liveEventsCfg).Write("\nOnNeutralMinionKill");
Log.Info("Adding OnNeutralMinionKill to LiveEvents.ini");
}
return true;
}
if (!events.Contains("OnNeutralMinionKill"))
catch (FileNotFoundException)
{
File.AppendText(liveEventsCfg).Write("\nOnNeutralMinionKill");
Log.Info("Adding OnNeutralMinionKill to LiveEvents.ini");

Log.Info("LiveEvents.ini not found. Generating now");
File.WriteAllLines(Path.Join(LeagueFolder, "LiveEvents.ini"), new string[] { "OnMinionKill", "OnNeutralMinionKill" });
Log.Info("LiveEvents.ini created. Added only nescesary events!");
return true;
}
return true;
}
catch (FileNotFoundException)
else
{

Log.Info("LiveEvents.ini not found. Generating now");
File.WriteAllLines(Path.Join(LeagueFolder, "LiveEvents.ini"), new string[] { "OnMinionKill", "OnNeutralMinionKill" });
Log.Info("LiveEvents.ini created. Added only nescesary events!");
return true;
return false;
}
}
else
} catch (Exception e)
{
Log.Warn($"Could not read config in folder {configLocation}: {e}");
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions LeagueBroadcast/LeagueBroadcast.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<RepositoryUrl>https://github.com/floh22/LeagueBroadcast</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageIcon>BE_icon.png</PackageIcon>
<AssemblyVersion>1.4.0.0</AssemblyVersion>
<FileVersion>1.4.0.21189</FileVersion>
<AssemblyVersion>1.4.2.0</AssemblyVersion>
<FileVersion>1.4.2.21190</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 12b3a19

Please sign in to comment.