Skip to content

Commit

Permalink
Bugfix, core refactor, code cleanup
Browse files Browse the repository at this point in the history
-Fixed a bug where the launcher could not handle new dual folder structure the manager is using.
Moved several classes to their own folders.
Renamed form for better readability.
  • Loading branch information
rjtwins committed Jul 27, 2021
1 parent 5a577d0 commit 6e4c79a
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 176 deletions.
155 changes: 155 additions & 0 deletions Logic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using Newtonsoft.Json;
using Octokit;
using System;
using System.IO;
using System.Net;

namespace MW5LOMLauncherV2
{
public class Logic
{
public static Logic logic;
ProgramData ProgramData = new ProgramData();
string ProgramDataPath = "";
string LatestVersion = "";
public bool ExeExists = false;
bool GithubUnreachable = false;

public Logic(OutputForm form1)
{
Logic.logic = this;
this.ProgramDataPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
@"\MW5LoadOrderManager";

bool update = false;

try
{
var client = new GitHubClient(new ProductHeaderValue("MW5LoadOrderManagerUpdater"));
this.LatestVersion = client.Repository.Release.GetLatest("rjtwins", "MW5-Mod-Manager").Result.TagName;
}
catch (Exception e)
{
form1.listBox1.Items.Add("Github unreachable for update trying to launch from existing executable...");
Console.WriteLine("Github unreachable for update trying to launch from existing executable...");
GithubUnreachable = true;

Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}

//Is this our first time running?
if (LoadGenProgramData())
{
form1.listBox1.Items.Add("First time startup or corrupted files detected, staring update/restore...");
Console.WriteLine("First time startup or corrupted files detected, staring update/restore...");

//we are running a "fresh" version with no stored program data or a corrupt program data file.
update = true;
}
else if(!GithubUnreachable)
{
form1.listBox1.Items.Add("Checking for updates...");
form1.listBox1.Items.Add(String.Format("The latest release is tagged at {0} we are running {1}",
LatestVersion,
this.ProgramData.version));

Console.WriteLine("Checking for updates...");
Console.WriteLine(
"The latest release is tagged at {0} we are running {1}",
LatestVersion,
this.ProgramData.version);

if (this.ProgramData.version < float.Parse(LatestVersion))
{
form1.listBox1.Items.Add("A new version is available, starting update...");
Console.WriteLine("A new version is available, starting update...");
update = true;
}
}

if (update && !GithubUnreachable)
{
try
{
using (var webClient = new WebClient())
{
string url = String.Format("https://github.com/rjtwins/MW5-Mod-Manager/releases/download/{0}/MW5.Mod.Manager.exe", this.LatestVersion);
webClient.DownloadFile(url, this.ProgramDataPath + @"\MW5 Mod Manager.exe");
}
this.ProgramData.version = float.Parse(this.LatestVersion);

//Update program data
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
using (StreamWriter sw = new StreamWriter(ProgramDataPath + @"\ProgramData.json"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, this.ProgramData);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
form1.listBox1.Items.Add("Done, starting MW5 Load Order Manager");
Console.WriteLine("Done, starting MW5 Mod Loader Manager");

form1.timer1.Enabled = true;
form1.timer1.Start();
}

internal static void StartMainProgram()
{
//Start the main program
System.Diagnostics.Process.Start(Logic.logic.ProgramDataPath + @"\MW5 Mod Manager.exe");
}

public bool LoadGenProgramData()
{
//Load install dir from previous session:
if (!File.Exists(ProgramDataPath + @"\ProgramData.json"))
{
System.IO.Directory.CreateDirectory(ProgramDataPath);
System.IO.File.Create(ProgramDataPath + @"\ProgramData.json").Close();
this.ProgramData.vendor = "EPIC";
this.ProgramData.installdir = new string[2] {"",""};
this.ProgramData.version = 0f;
Console.WriteLine(ProgramDataPath + @"\ProgramData.json was not found.");
return true;
}
try
{
string json = File.ReadAllText(ProgramDataPath + @"\ProgramData.json");
this.ProgramData = JsonConvert.DeserializeObject<ProgramData>(json);
}
catch (Exception e)
{
Console.WriteLine(ProgramDataPath + @"\ProgramData.json unreadable.");
//Console.WriteLine(e.Message);
//Console.WriteLine(e.StackTrace);
return true;
}
if (this.ProgramData.version <= 0f)
{
Console.WriteLine("Version outside of possible range.");
return true;
}


if (File.Exists(ProgramDataPath + @"\MW5 Mod Manager.exe"))
{
ExeExists = true;
}
//ProgramData.json is fine but the exe file is missing? Update to get it.
else
{
Console.WriteLine(ProgramDataPath + @"\MW5 Mod Manager.exe not found.");
return true;
}
return false;
}
}
}
57 changes: 49 additions & 8 deletions MW5LOMLauncherV2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
Expand All @@ -22,10 +23,11 @@
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<AutorunEnabled>true</AutorunEnabled>
<ApplicationRevision>1</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand All @@ -43,13 +45,46 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>false</RunCodeAnalysis>
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
<Prefer32Bit>true</Prefer32Bit>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>exeIcon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>EA77E3833AE0941F5D372A3C9F6384745177644B</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>
</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>false</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<SignManifests>false</SignManifests>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>MW5LauncherKey.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup>
<DelaySign>false</DelaySign>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup>
<TargetZone>Internet</TargetZone>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -64,16 +99,18 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<Compile Include="Logic.cs" />
<Compile Include="OutputForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
<Compile Include="OutputForm.Designer.cs">
<DependentUpon>OutputForm.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="ProgramData.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
<EmbeddedResource Include="OutputForm.resx">
<DependentUpon>OutputForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand All @@ -85,6 +122,10 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="MW5LauncherKey.pfx" />
<None Include="MW5LOMLauncherV2_1_TemporaryKey.pfx" />
<None Include="MW5LOMLauncherV2_TemporaryKey.pfx" />
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
3 changes: 2 additions & 1 deletion Form1.Designer.cs → OutputForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Form1.cs → OutputForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace MW5LOMLauncherV2
{
public partial class Form1 : Form
public partial class OutputForm : Form
{
Logic logic;
public Form1()
private Logic logic;
public OutputForm()
{
InitializeComponent();
this.logic = new Logic(this);
Expand Down Expand Up @@ -45,5 +45,10 @@ public void timer1_Tick(object sender, EventArgs e)
}
Environment.Exit(0);
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}
File renamed without changes.
Loading

0 comments on commit 6e4c79a

Please sign in to comment.