Skip to content

Commit

Permalink
Make sure StreamWriter is disposed in time (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
SadPencil authored Feb 26, 2024
1 parent 29d44c7 commit e1ad582
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 47 deletions.
52 changes: 27 additions & 25 deletions DXMainClient/DXGUI/Generic/CampaignSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,43 +270,45 @@ private void LaunchMission(Mission mission)
bool copyMapsToSpawnmapINI = ClientConfiguration.Instance.CopyMissionsToSpawnmapINI;

Logger.Log("About to write spawn.ini.");
using var spawnStreamWriter = new StreamWriter(SafePath.CombineFilePath(ProgramConstants.GamePath, "spawn.ini"));
spawnStreamWriter.WriteLine("; Generated by DTA Client");
spawnStreamWriter.WriteLine("[Settings]");
if (copyMapsToSpawnmapINI)
spawnStreamWriter.WriteLine("Scenario=spawnmap.ini");
else
spawnStreamWriter.WriteLine("Scenario=" + mission.Scenario);
using (var spawnStreamWriter = new StreamWriter(SafePath.CombineFilePath(ProgramConstants.GamePath, "spawn.ini")))
{
spawnStreamWriter.WriteLine("; Generated by DTA Client");
spawnStreamWriter.WriteLine("[Settings]");
if (copyMapsToSpawnmapINI)
spawnStreamWriter.WriteLine("Scenario=spawnmap.ini");
else
spawnStreamWriter.WriteLine("Scenario=" + mission.Scenario);

// No one wants to play missions on Fastest, so we'll change it to Faster
if (UserINISettings.Instance.GameSpeed == 0)
UserINISettings.Instance.GameSpeed.Value = 1;
// No one wants to play missions on Fastest, so we'll change it to Faster
if (UserINISettings.Instance.GameSpeed == 0)
UserINISettings.Instance.GameSpeed.Value = 1;

spawnStreamWriter.WriteLine("CampaignID=" + mission.Index);
spawnStreamWriter.WriteLine("GameSpeed=" + UserINISettings.Instance.GameSpeed);
spawnStreamWriter.WriteLine("CampaignID=" + mission.Index);
spawnStreamWriter.WriteLine("GameSpeed=" + UserINISettings.Instance.GameSpeed);
#if YR || ARES
spawnStreamWriter.WriteLine("Ra2Mode=" + !mission.RequiredAddon);
spawnStreamWriter.WriteLine("Ra2Mode=" + !mission.RequiredAddon);
#else
spawnStreamWriter.WriteLine("Firestorm=" + mission.RequiredAddon);
#endif
spawnStreamWriter.WriteLine("CustomLoadScreen=" + LoadingScreenController.GetLoadScreenName(mission.Side.ToString()));
spawnStreamWriter.WriteLine("IsSinglePlayer=Yes");
spawnStreamWriter.WriteLine("SidebarHack=" + ClientConfiguration.Instance.SidebarHack);
spawnStreamWriter.WriteLine("Side=" + mission.Side);
spawnStreamWriter.WriteLine("BuildOffAlly=" + mission.BuildOffAlly);
spawnStreamWriter.WriteLine("CustomLoadScreen=" + LoadingScreenController.GetLoadScreenName(mission.Side.ToString()));
spawnStreamWriter.WriteLine("IsSinglePlayer=Yes");
spawnStreamWriter.WriteLine("SidebarHack=" + ClientConfiguration.Instance.SidebarHack);
spawnStreamWriter.WriteLine("Side=" + mission.Side);
spawnStreamWriter.WriteLine("BuildOffAlly=" + mission.BuildOffAlly);

UserINISettings.Instance.Difficulty.Value = trbDifficultySelector.Value;
UserINISettings.Instance.Difficulty.Value = trbDifficultySelector.Value;

spawnStreamWriter.WriteLine("DifficultyModeHuman=" + (mission.PlayerAlwaysOnNormalDifficulty ? "1" : trbDifficultySelector.Value.ToString()));
spawnStreamWriter.WriteLine("DifficultyModeComputer=" + GetComputerDifficulty());
spawnStreamWriter.WriteLine("DifficultyModeHuman=" + (mission.PlayerAlwaysOnNormalDifficulty ? "1" : trbDifficultySelector.Value.ToString()));
spawnStreamWriter.WriteLine("DifficultyModeComputer=" + GetComputerDifficulty());

spawnStreamWriter.WriteLine();
spawnStreamWriter.WriteLine();
spawnStreamWriter.WriteLine();
}

var difficultyIni = new IniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, DifficultyIniPaths[trbDifficultySelector.Value]));
string difficultyName = DifficultyNames[trbDifficultySelector.Value];

spawnStreamWriter.WriteLine();
spawnStreamWriter.WriteLine();
spawnStreamWriter.WriteLine();

if (copyMapsToSpawnmapINI)
{
var mapIni = new IniFile(SafePath.CombineFilePath(ProgramConstants.GamePath, mission.Scenario));
Expand Down
36 changes: 20 additions & 16 deletions DXMainClient/DXGUI/Generic/GameLoadingWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,32 @@ private void BtnLaunch_LeftClick(object sender, EventArgs e)
if (spawnerSettingsFile.Exists)
spawnerSettingsFile.Delete();

using StreamWriter spawnStreamWriter = new StreamWriter(spawnerSettingsFile.FullName);
spawnStreamWriter.WriteLine("; generated by DTA Client");
spawnStreamWriter.WriteLine("[Settings]");
spawnStreamWriter.WriteLine("Scenario=spawnmap.ini");
spawnStreamWriter.WriteLine("SaveGameName=" + sg.FileName);
spawnStreamWriter.WriteLine("LoadSaveGame=Yes");
spawnStreamWriter.WriteLine("SidebarHack=" + ClientConfiguration.Instance.SidebarHack);
spawnStreamWriter.WriteLine("CustomLoadScreen=" + LoadingScreenController.GetLoadScreenName("g"));
spawnStreamWriter.WriteLine("Firestorm=No");
spawnStreamWriter.WriteLine("GameSpeed=" + UserINISettings.Instance.GameSpeed);
spawnStreamWriter.WriteLine();
using (var spawnStreamWriter = new StreamWriter(spawnerSettingsFile.FullName))
{
spawnStreamWriter.WriteLine("; generated by DTA Client");
spawnStreamWriter.WriteLine("[Settings]");
spawnStreamWriter.WriteLine("Scenario=spawnmap.ini");
spawnStreamWriter.WriteLine("SaveGameName=" + sg.FileName);
spawnStreamWriter.WriteLine("LoadSaveGame=Yes");
spawnStreamWriter.WriteLine("SidebarHack=" + ClientConfiguration.Instance.SidebarHack);
spawnStreamWriter.WriteLine("CustomLoadScreen=" + LoadingScreenController.GetLoadScreenName("g"));
spawnStreamWriter.WriteLine("Firestorm=No");
spawnStreamWriter.WriteLine("GameSpeed=" + UserINISettings.Instance.GameSpeed);
spawnStreamWriter.WriteLine();
}

FileInfo spawnMapIniFile = SafePath.GetFile(ProgramConstants.GamePath, "spawnmap.ini");

if (spawnMapIniFile.Exists)
spawnMapIniFile.Delete();

using StreamWriter spawnMapStreamWriter = new StreamWriter(spawnMapIniFile.FullName);
spawnMapStreamWriter.WriteLine("[Map]");
spawnMapStreamWriter.WriteLine("Size=0,0,50,50");
spawnMapStreamWriter.WriteLine("LocalSize=0,0,50,50");
spawnMapStreamWriter.WriteLine();
using (var spawnMapStreamWriter = new StreamWriter(spawnMapIniFile.FullName))
{
spawnMapStreamWriter.WriteLine("[Map]");
spawnMapStreamWriter.WriteLine("Size=0,0,50,50");
spawnMapStreamWriter.WriteLine("LocalSize=0,0,50,50");
spawnMapStreamWriter.WriteLine();
}

discordHandler.UpdatePresence(sg.GUIName, true);

Expand Down
13 changes: 7 additions & 6 deletions DXMainClient/DXGUI/Multiplayer/GameLoadingLobbyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,14 @@ protected void LoadGame()
spawnIni.WriteIniFile();

FileInfo spawnMapFileInfo = SafePath.GetFile(ProgramConstants.GamePath, "spawnmap.ini");

spawnMapFileInfo.Delete();
using StreamWriter spawnMapStreamWriter = new StreamWriter(spawnMapFileInfo.FullName);
spawnMapStreamWriter.WriteLine("[Map]");
spawnMapStreamWriter.WriteLine("Size=0,0,50,50");
spawnMapStreamWriter.WriteLine("LocalSize=0,0,50,50");
spawnMapStreamWriter.WriteLine();
using (var spawnMapStreamWriter = new StreamWriter(spawnMapFileInfo.FullName))
{
spawnMapStreamWriter.WriteLine("[Map]");
spawnMapStreamWriter.WriteLine("Size=0,0,50,50");
spawnMapStreamWriter.WriteLine("LocalSize=0,0,50,50");
spawnMapStreamWriter.WriteLine();
}

gameLoadTime = DateTime.Now;

Expand Down

0 comments on commit e1ad582

Please sign in to comment.