Skip to content

Commit

Permalink
Generate descriptor.mod and startup arguments
Browse files Browse the repository at this point in the history
I finally added a way to generate descriptor.mod files into Documents\Paradox Interactive\Hearts of Iron IV\mod and also added start up arguments, both of these options are in Settings tab. Please make sure you have the mod folder created in documents because otherwise the program will crash and burn and it's to late for me to fix it I just want to sleep. Keep in mind that these features are barely tested so please if you encounter any problems just open up an issue.

- Added ability to update mods by creating descriptor.mod files (Requested by: #4 )
- Added startup arguments (Requseted by: #5 )
  • Loading branch information
Xferno2 committed Oct 27, 2021
1 parent e07c648 commit caf331b
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 77 deletions.
19 changes: 19 additions & 0 deletions Hoi4 Launcher/App.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Hoi4_Launcher.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
Expand All @@ -9,6 +14,20 @@
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<userSettings>
<Hoi4_Launcher.Properties.Settings>
<setting name="generateDescriptor" serializeAs="String">
<value>False</value>
</setting>
<setting name="startArguments" serializeAs="String">
<value />
</setting>
</Hoi4_Launcher.Properties.Settings>
</userSettings>
</configuration>
2 changes: 1 addition & 1 deletion Hoi4 Launcher/Form1.Designer.cs

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

46 changes: 36 additions & 10 deletions Hoi4 Launcher/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Timer = System.Timers.Timer;
using Hoi4_Launcher.Utility;
using System.Drawing.Imaging;
using Hoi4_Launcher.Parser;

namespace Hoi4_Launcher
{
Expand All @@ -30,6 +31,7 @@ public partial class Form1 : MetroSet_UI.Forms.MetroSetForm
private string args;

Timer updateUI = new Timer(100);
modParser modParser;

static launchSettings data = new launchSettings();

Expand All @@ -40,14 +42,32 @@ public partial class Form1 : MetroSet_UI.Forms.MetroSetForm

public Form1(string[] args)
{
foreach (var arg in args)
var defaultArgsLaunch = args.ToList();
var settingsArgsLaunch = Properties.Settings.Default.startArguments.Split(' ').ToList();
var launchArguments = defaultArgsLaunch.Union(settingsArgsLaunch).ToList();
foreach (var arg in launchArguments)
{
this.args += arg + " ";
}
InitializeComponent();
var steamLink = new SteamLink { Dock = DockStyle.Fill, TopLevel = false };
panel2.Controls.Add(steamLink) ;
steamLink.Show();
if (Properties.Settings.Default.generateDescriptor)
{
modParser = new modParser(AppContext.BaseDirectory);
if (modParser.isPathValid)
{
foreach (var mod in modParser.mods)
{
var id = mod.Split('\\').Last();
modParser.createDescriptorFile(mod, Hoi4_Mods, id);
}
}
else {
Logger("Application encontered an error: " + modParser.exception);
}
}
}

protected override CreateParams CreateParams
Expand Down Expand Up @@ -83,19 +103,19 @@ private void Form1_Load_1(object sender, EventArgs e)

//Release Candidate disable settings
//Remove this when Finished
//Settings settingsForm = new Settings();
//settingsForm.TopLevel = false;
//settingsForm.AutoScroll = true;
//settingsForm.Dock = DockStyle.Fill;
//panel1.Controls.Add(settingsForm);
//settingsForm.Show();
Settings settingsForm = new Settings();
settingsForm.TopLevel = false;
settingsForm.AutoScroll = true;
settingsForm.Dock = DockStyle.Fill;
panel1.Controls.Add(settingsForm);
settingsForm.Show();


//panel1.AutoScroll = true;
panel1.AutoScroll = true;


categoriesBox.DropDownStyle = ComboBoxStyle.DropDownList;
Logger("Application arguments: " + (String.IsNullOrEmpty(args) ? "null" : args));
Logger("Application arguments: " + (String.IsNullOrWhiteSpace(args) ? "null" : args));
this.DoubleBuffered = true;
Util.enableDoubleBuff(tabControl1);
Util.enableDoubleBuff(tabPage1);
Expand Down Expand Up @@ -144,7 +164,7 @@ public launchSettings load_items()
}

public List<newModInfo> load_mods_info() {
string[] stringSeparators = new string[] { "\n\t" };
string[] stringSeparators = new string[] { "\n\t", "\n\r", Environment.NewLine };
List<newModInfo> mods = new List<newModInfo>();
DirectoryInfo d = new DirectoryInfo(Hoi4_Mods);
FileInfo[] Files = d.GetFiles("*.mod");
Expand Down Expand Up @@ -276,6 +296,7 @@ private void UserControl11_Click(object sender, EventArgs e)
config.enabled_mods = enabled_mods;
config.disabled_dlcs = disabled_dlc;
SerializeConfig(config);
Properties.Settings.Default.Save();
Process.Start(@"hoi4.exe", args);
Application.Exit();
}
Expand Down Expand Up @@ -543,6 +564,11 @@ private void panel1_Paint(object sender, PaintEventArgs e)
{

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();
}
}

}
Expand Down
19 changes: 19 additions & 0 deletions Hoi4 Launcher/Hoi4 Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,30 @@
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="SharpCompress, Version=0.30.0.0, Culture=neutral, PublicKeyToken=afb0a02973931d96, processorArchitecture=MSIL">
<HintPath>..\packages\SharpCompress.0.30.0\lib\net461\SharpCompress.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Design" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
123 changes: 122 additions & 1 deletion Hoi4 Launcher/Parser/modParser.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,133 @@
using System;
using Hoi4_Launcher.Utility;
using SharpCompress.Readers;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hoi4_Launcher.Parser
{
class modParser
{
public string modsFolder;
public string[] mods;
public bool isPathValid = false;
public Exception exception;
public modParser(string thisPath)
{
this.modsFolder = workshop(thisPath);
try
{
mods = Directory.GetDirectories(modsFolder);
this.isPathValid = true;
}
catch (Exception ex) {
string message = "I couldn't find the path to the mods directory." +
Environment.NewLine+
"Perhaps this is not a steam install?" +
Environment.NewLine +
"Either way descriptor.mod is disabled. If you think this is an issue, open one on github."+
Environment.NewLine +
"More info in Log tab!";
string title = "Something intresting happened";
MessageBox.Show(message, title);
Properties.Settings.Default.generateDescriptor = false;
Properties.Settings.Default.Save();
this.exception = ex;
}
}
public string workshop(string path) => path.Split(new string[] { "steamapps" }, StringSplitOptions.None)[0] + "steamapps\\workshop\\content\\394360";

public bool createDescriptorFile(string path, string savePath, string id)
{
DirectoryInfo dInfo = new DirectoryInfo(path);
FileInfo zipFile;
try
{
zipFile = dInfo.GetFilesByExtensions(new string[] { ".rar", ".zip", ".7z" }).First();
}
catch (Exception ex) {
zipFile = null;
}
// We are going to check if descriptor.mod exists
if (File.Exists(dInfo.FullName + "\\descriptor.mod"))
{
if (!File.Exists(savePath + "\\ugc_" + id + ".mod")){
var descriptorFile = editDescriptor(File.ReadAllText(dInfo.FullName + "\\descriptor.mod"), dInfo.FullName);
System.IO.File.WriteAllLines(savePath + "\\ugc_" + id + ".mod", descriptorFile);
}
return true;
}
else if (zipFile != null)
{
if (!File.Exists(savePath + "\\ugc_" + id + ".mod"))
{
try
{
//using (ZipArchive archive = ZipFile.OpenRead(zipFile.FullName))
//{
// ZipArchiveEntry descriptor = archive.GetEntry("descriptor.mod");
// descriptor.ExtractToFile(@"descriptor.mod");
// var textStream = descriptor.Open();
// StreamReader reader = new StreamReader(textStream);
// var descriptorFile = editDescriptor(reader.ReadToEnd(), zipFile.FullName);
// System.IO.File.WriteAllLines(savePath + "\\ugc_" + id + ".mod", descriptorFile);
//}

using (Stream stream = File.OpenRead(zipFile.FullName))
using (var reader = ReaderFactory.Open(stream))
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory && reader.Entry.Key == "descriptor.mod")
{
reader.WriteEntryToFile(@"tempDescriptor.mod");
var descriptorFile = editDescriptor(File.ReadAllText(@"tempDescriptor.mod"), zipFile.FullName);
System.IO.File.WriteAllLines(savePath + "\\ugc_" + id + ".mod", descriptorFile);
break;
}
}
}
}
catch (Exception ex) { }
}
if (File.Exists(@"tempDescriptor.mod"))
{
File.Delete(@"tempDescriptor.mod");
}
return true;
}
else
{
return false;
}
}

// this rlly needs a rewrite
private string[] editDescriptor(string descriptor, string path)
{
bool doesItContainPath = false;
List<string> descriptorFile = new List<string>();
foreach (var line in descriptor.Split(new string[] { Environment.NewLine, "\n\t", "\n\r", "\n"}, StringSplitOptions.RemoveEmptyEntries))
{
var currentLine = line;
if (line.Split('=').First().ToLower() == "path")
{
currentLine = "path=" + path;
doesItContainPath = true;

}
descriptorFile.Add(currentLine);
}
if (!doesItContainPath)
{
descriptorFile.Add("path=" + path);
}
return descriptorFile.ToArray();
}
}
}
46 changes: 33 additions & 13 deletions Hoi4 Launcher/Properties/Settings.Designer.cs

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

17 changes: 11 additions & 6 deletions Hoi4 Launcher/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
</SettingsFile>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Hoi4_Launcher.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="generateDescriptor" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="startArguments" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
Loading

0 comments on commit caf331b

Please sign in to comment.