Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeon8 committed Mar 3, 2023
0 parents commit 742f0aa
Show file tree
Hide file tree
Showing 33 changed files with 1,986 additions and 0 deletions.
455 changes: 455 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions HOI4Launcher.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HOI4Launcher", "HOI4Launcher\HOI4Launcher.csproj", "{0B1DE9A1-FCD7-4DDD-AAF7-6BA1D733BE11}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0B1DE9A1-FCD7-4DDD-AAF7-6BA1D733BE11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B1DE9A1-FCD7-4DDD-AAF7-6BA1D733BE11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B1DE9A1-FCD7-4DDD-AAF7-6BA1D733BE11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B1DE9A1-FCD7-4DDD-AAF7-6BA1D733BE11}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
15 changes: 15 additions & 0 deletions HOI4Launcher/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HOI4Launcher"
xmlns:theme="using:Avalonia.Themes.Default"
x:Class="HOI4Launcher.App">
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<theme:DefaultTheme/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseLight.xaml"/>
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Default.xaml"/>
</Application.Styles>
</Application>
30 changes: 30 additions & 0 deletions HOI4Launcher/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using Avalonia.Themes.Default;
using HOI4Launcher.ViewModels;
using HOI4Launcher.Views;

namespace HOI4Launcher;

public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
ExpressionObserver.DataValidators.RemoveAll(x => x is DataAnnotationsValidationPlugin);
desktop.MainWindow = new MainWindow();
}
base.OnFrameworkInitializationCompleted();
}
}
Binary file added HOI4Launcher/Assets/avalonia-logo.ico
Binary file not shown.
Binary file added HOI4Launcher/Assets/noimage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added HOI4Launcher/Assets/refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions HOI4Launcher/HOI4Launcher.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ApplicationIcon>Assets/avalonia-logo.ico</ApplicationIcon>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<!-- <Folder Include="Models\" /> -->
<AvaloniaResource Include="Assets\**" />
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<TrimmerRootDescriptor Include="Roots.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.18" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.18" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.18" />
<PackageReference Condition="'$(Configuration)' == 'Release'" Include="Avalonia.Controls.DataGrid" Version="0.10.18" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="MessageBox.Avalonia" Version="2.1.0" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.5.1" />
</ItemGroup>
</Project>
29 changes: 29 additions & 0 deletions HOI4Launcher/Models/DLC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections;
using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using HOI4Launcher.Utility;

namespace HOI4Launcher.Models;

public class DLC
{
public DLC(string name, string category, string fileName)
{
Name = name;
Category = category;
FileName = fileName;
}

public string Name { get; }

public string Category { get; }

public string FileName { get; }

public bool Enabled { get; set; }

public Bitmap Picture { get; set; } = PictureUtillity.LoadStandardPicture();

}
42 changes: 42 additions & 0 deletions HOI4Launcher/Models/DLCFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using HOI4Launcher.Services;

namespace HOI4Launcher.Models;

public class DLCFile
{
public DLCFile(IEnumerable<string> enabledMods, IEnumerable<string> disabledDLCs)
{
EnabledMods = enabledMods;
DisabledDLCs = disabledDLCs;
}

[JsonPropertyName("enabled_mods")]
public IEnumerable<string> EnabledMods { get; set; }

[JsonPropertyName("disabled_dlcs")]
public IEnumerable<string> DisabledDLCs { get; set; }

private static readonly string DlcLoadFilePath = ModsService.GameDataPath + "dlc_load.json";

public static DLCFile? Read()
{
if (File.Exists(DlcLoadFilePath))
{
var json = File.ReadAllText(DlcLoadFilePath);
var file = JsonSerializer.Deserialize<DLCFile>(json)!;
return file;
}
return null;
}

public Task Save()
{
var json = JsonSerializer.Serialize(this);
return File.WriteAllTextAsync(Path.Join(ModsService.GameDataPath + "dlc_load.json"), json);
}
}
27 changes: 27 additions & 0 deletions HOI4Launcher/Models/Mod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using HOI4Launcher.Utility;

namespace HOI4Launcher.Models;

public class Mod
{
public Mod(string name, string fileName)
{
Name = name;
FileName = fileName;
}

public string Name { get; set; }

public string FileName { get; set; }
public bool Enabled { get; set; }
public Bitmap Picture { get; set; } = PictureUtillity.LoadStandardPicture();
public IEnumerable<string>? Tags { get; set; }

}
32 changes: 32 additions & 0 deletions HOI4Launcher/Models/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.IO;
using System.Text.Json;

namespace HOI4Launcher.Models;

public class Settings
{
public string GameDirectoryPath { get; set; }
public string Language { get; set; } = "Eng";

public Settings(string gameDirectoryPath)
{
GameDirectoryPath = gameDirectoryPath;
}

public static Settings? Read()
{
if (File.Exists("settings.json"))
{
var settings = JsonSerializer.Deserialize<Settings>(File.ReadAllText("settings.json"));
if(settings is not null)
return settings;
}
return null;
}

public void Save()
{
var json = JsonSerializer.Serialize(this);
File.WriteAllText("settings.json", json);
}
}
20 changes: 20 additions & 0 deletions HOI4Launcher/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Avalonia;
using System;

namespace HOI4Launcher;

class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace();
}
5 changes: 5 additions & 0 deletions HOI4Launcher/Roots.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<linker>
<!-- Can be removed if CompiledBinding and no reflection are used -->
<assembly fullname="HOI4Launcher" preserve="All" />
<assembly fullname="Avalonia.Themes.Default" preserve="All" />
</linker>
47 changes: 47 additions & 0 deletions HOI4Launcher/Services/DLCService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Avalonia.Media.Imaging;
using HOI4Launcher.Models;
using HOI4Launcher.Utility;

namespace HOI4Launcher.Services;

public partial class DLCService
{
private readonly string _gameDirectory;
private readonly DLCFile _file;

public DLCService(string gameDirectory, DLCFile file)
{
_gameDirectory = gameDirectory;
_file = file;
}

public IEnumerable<DLC> LoadDLCs()
{
var dlcs = new List<DLC>();
var directory = new DirectoryInfo(Path.Join(_gameDirectory,"/dlc/"));
foreach (FileInfo file in directory.GetFiles("*.dlc", SearchOption.AllDirectories))
{
var text = File.ReadAllText(file.FullName);

string name = text.ParseValue("name");
string category = text.ParseValue("category").FormatCategory();
string fileName = GetRelativePath(file.FullName);

var dlc = new DLC(name, category, fileName);
dlc.Enabled = !_file.DisabledDLCs.Contains(fileName);

dlc.Picture = PictureUtillity.LoadPicture(file.Directory!.FullName) ?? dlc.Picture;
dlcs.Add(dlc);
}
return dlcs;
}

private static string GetRelativePath(string name)
{
var index = name.IndexOf("dlc");
return name[index..].Replace('\\', '/');
}
}
72 changes: 72 additions & 0 deletions HOI4Launcher/Services/ModsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Avalonia.Media.Imaging;
using HOI4Launcher.Models;
using HOI4Launcher.Utility;

namespace HOI4Launcher.Services;

public class ModsService
{
public readonly static string GameDataPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"/Paradox Interactive/Hearts of Iron IV/";
public readonly static string ModsPath = Path.Join(GameDataPath,"mod/");
private readonly DLCFile _file;

public ModsService(DLCFile file)
{
_file = file;
}

public IEnumerable<Mod> LoadMods()
{
var mods = new List<Mod>();
var directory = new DirectoryInfo(ModsPath);
foreach (var file in directory.GetFiles("*.mod"))
{
var text = File.ReadAllText(file.FullName);
var name = text.ParseValue("name");
var fileName = "mod/" + file.Name;
var mod = new Mod(name, fileName);
mod.Enabled = _file.EnabledMods.Contains(mod.FileName);
mods.Add(mod);

if(text.Contains("tags"))
mod.Tags = GetTags(text);

if(text.Contains("path"))
{
var path = text.ParseValue("path");
if (!Directory.Exists(path))
path = Path.Join(GameDataPath, path);

mod.Picture = PictureUtillity.LoadPicture(path) ?? mod.Picture;
}
}
return mods;
}

private static IEnumerable<string> GetTags(string text)
{
List<string> tags = new();
int startIndex = text.IndexOf("\ntags");
if(startIndex == -1)
startIndex = text.IndexOf("tags");
int openBracket = text.IndexOf('{', startIndex);
int closeBracket = text.IndexOf('}', openBracket);

int openQuote = text.IndexOf('"', openBracket)+1;
int closeQuote = text.IndexOf('"', openQuote);
while(openQuote < closeBracket)
{
tags.Add(text[openQuote..closeQuote]);
openQuote = text.IndexOf('"', closeQuote+1)+1;
closeQuote = text.IndexOf('"', openQuote);
}
return tags;
}



}
Loading

0 comments on commit 742f0aa

Please sign in to comment.