Skip to content

Commit

Permalink
Merge pull request #26 from xADDBx/AutoBinzRT
Browse files Browse the repository at this point in the history
Make compatible with some game version
  • Loading branch information
factubsio authored Jul 6, 2024
2 parents 1249e0e + e1930b0 commit 9a9cec5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 52 deletions.
13 changes: 12 additions & 1 deletion BlueprintExplorer/BlueprintDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ public void ExtractFromGame(ConnectionProgress progress, string wrathPath, strin
.FirstOrDefault();

var typeIdGuid = typeIdType.GetField("GuidString");
if (typeIdGuid == null) {
typeIdGuid = typeIdType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).First(f => f.Name.Contains("GuidString", StringComparison.InvariantCultureIgnoreCase));
}

foreach (var type in types)
{
Expand Down Expand Up @@ -591,7 +594,15 @@ public bool TryConnect(ConnectionProgress progress, string forceFileName = null)
header.Major = headerIn.ReadInt32();
header.Minor = headerIn.ReadInt32();
header.Patch = headerIn.ReadInt32();
header.Suffix = headerIn.ReadChar();
try
{
header.Suffix = headerIn.ReadString();
} // binz files created in the old BubblePrints version will only have char here.
catch (EndOfStreamException) {
headerIn.BaseStream.Position = 0;
for (int i = 0; i < 3; i++) headerIn.ReadInt32();
header.Suffix = headerIn.ReadChar().ToString();
}
header.Bubble = 0;
count = headerIn.ReadInt32();

Expand Down
34 changes: 27 additions & 7 deletions BlueprintExplorer/BubblePrints.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -62,13 +62,13 @@ public static void Install()
}
}

private static Regex ExtractVersionPattern = new(@"(?<major>\d)\.(?<minor>\d)\.(?<patch>\d+)(?<suffix>[a-zA-Z]?)");
private static Regex ExtractVersionPattern = new(@"(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?<suffix>\.\d+|[a-zA-Z]*)");

internal static GameVersion GetGameVersion(string wrathPath)
{
if (Game_Data == "Kingmaker_Data")
{
return new(2, 1, 4, 'a', 0);
return new(2, 1, 4, "a", 0);
}

var versionPath = Path.Combine(wrathPath, Game_Data, "StreamingAssets", "Version.info");
Expand Down Expand Up @@ -99,13 +99,18 @@ internal static GameVersion GetGameVersion(string wrathPath)
var major = int.Parse(r.Groups["major"].Value);
var minor = int.Parse(r.Groups["minor"].Value);
var patch = int.Parse(r.Groups["patch"].Value);
char.TryParse(r.Groups["suffix"].Value, out char suffix);
var suffix = r.Groups["suffix"].Value;
return new(major, minor, patch, suffix, 0);
}

internal static void SetWrathPath(bool forceSelect)
{
var path = Settings.WrathPath;
var path = BubblePrints.Game_Data switch {
"Wrath_Data" => Settings.WrathPath,
"Kingmaker_Data" => Settings.KMPath,
"WH40KRT_Data" => Settings.RTPath,
_ => throw new NotSupportedException()
};
if (forceSelect)
{
path = null;
Expand Down Expand Up @@ -147,7 +152,12 @@ static bool ContainsDirectory(string path, string directoryName) =>
}
if (!errored)
{
Settings.WrathPath = path;
switch (BubblePrints.Game_Data) {
case "Wrath_Data": Settings.WrathPath = path; break;
case "Kingmaker_Data": Settings.KMPath = path; break;
case "WH40KRT_Data": Settings.RTPath = path; break;
default: throw new NotSupportedException();
};
SaveSettings();
}
}
Expand All @@ -169,7 +179,7 @@ static bool ContainsDirectory(string path, string directoryName) =>

public static string CurrentGame => Game_Data switch
{
"Kingmaker_Data" => "Kingmaker",
"Kingmaker_Data" => "KM",
"Wrath_Data" => "Wrath",
"WH40KRT_Data" => "RT",
_ => throw new NotSupportedException(),
Expand Down Expand Up @@ -257,6 +267,16 @@ public SettingsProxy(SettingsProxy settings)
[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
public string WrathPath { get; set; }

[Description("Full path to your Kingmaker folder (i.e. the folder containing Kingmaker.exe")]
[DisplayName("Kingmaker Install Folder")]
[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
public string KMPath { get; set; }

[Description("Full path to your Rogue Trader folder (i.e. the folder containing WH40KRT.exe")]
[DisplayName("Rogue Trader Install Folder")]
[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
public string RTPath { get; set; }

[Description("If set, clicking on a blueprint in the search results will automatically open it in the external editor")]
[DisplayName("Always Open Externally")]
public bool AlwaysOpenInEditor { get; set; }
Expand Down
13 changes: 9 additions & 4 deletions BlueprintExplorer/GameVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public partial class BlueprintDB
{
public struct GameVersion : IComparable<GameVersion> {
public int Major, Minor, Patch;
public char Suffix;
public string Suffix;
public int Bubble;

public GameVersion(int major, int minor, int patch, char suffix, int bubble)
public GameVersion(int major, int minor, int patch, string suffix, int bubble)
{
Major = major;
Minor = minor;
Expand Down Expand Up @@ -39,8 +39,13 @@ public int CompareTo(GameVersion other)
public override int GetHashCode() => HashCode.Combine(Major, Minor, Patch, Suffix, Bubble);


public override string ToString() => $"{Major}.{Minor}.{Patch}{(Suffix == default ? "" : Suffix.ToString())}_{Bubble}";

public override string ToString() {
if (int.TryParse(Suffix, out var _)) {
return $"{Major}.{Minor}.{Patch}.{Suffix}_{Bubble}";
} else {
return $"{Major}.{Minor}.{Patch}{(Suffix == default ? "" : Suffix)}_{Bubble}";
}
}
}
}
}
91 changes: 51 additions & 40 deletions BlueprintExplorer/SplashScreenChooserJobbie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,33 @@ namespace BlueprintExplorer
public partial class SplashScreenChooserJobbie : Form
{
private readonly BindingList<Binz> Available = new();
private List<Binz> PreSort;
private readonly Dictionary<BinzVersion, Binz> ByVersion = new();

public SplashScreenChooserJobbie()
{
InitializeComponent();
loadAnim = new();

PreSort = new();
try
{
if (!Directory.Exists(CacheDir))
Directory.CreateDirectory(CacheDir);

Console.WriteLine("setting available = from web");
using var web = new WebClient();

Check warning on line 40 in BlueprintExplorer/SplashScreenChooserJobbie.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)
ParseWebJson(web, "https://raw.githubusercontent.com/factubsio/BubblePrintsData/main/versions.json", "Wrath");
ParseWebJson(web, "https://raw.githubusercontent.com/factubsio/BubblePrintsData/main/versions_RT.json", "RT");
ParseWebJson(web, "https://raw.githubusercontent.com/factubsio/BubblePrintsData/main/versions_KM.json", "KM");


var raw = web.DownloadString("https://raw.githubusercontent.com/factubsio/BubblePrintsData/main/versions.json");
var versions = JsonSerializer.Deserialize<JsonElement>(raw);

foreach (var version in versions.EnumerateArray())
{
GameVersion gv = new()
{
Major = version[0].GetInt32(),
Minor = version[1].GetInt32(),
Patch = version[2].GetInt32(),
Suffix = version[3].GetString()[0],
Bubble = version[4].GetInt32(),
};
Binz binz = new()
{
Version = new()
{
Version = gv,
Game = "Wrath",
},
Source = "bubbles",
};
Available.Insert(0, binz);
ByVersion.Add(binz.Version, binz);
}
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}


foreach (var file in Directory.EnumerateFiles(BubblePrints.DataPath, "*.binz"))
{
BinzVersion v = VersionFromFile(file);
Expand All @@ -87,11 +67,16 @@ public SplashScreenChooserJobbie()
Version = v,
Source = "local",
};
Available.Insert(0, binz);
PreSort.Insert(0, binz);
ByVersion.Add(v, binz);
}
}

PreSort.Sort((a, b) => {
var tmp = b.Version.Game.CompareTo(a.Version.Game);
if (tmp == 0) return a.Version.Version.CompareTo(b.Version.Version);
else return tmp;
});
PreSort.ForEach(i => Available.Insert(0, i));

versions.SelectionChanged += OnSelectedRowChanged;
versions.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
Expand All @@ -114,6 +99,29 @@ public SplashScreenChooserJobbie()
}
}
}
public void ParseWebJson(WebClient web, string url, string game) {
var raw = web.DownloadString(url);
var versions = JsonSerializer.Deserialize<JsonElement>(raw);

foreach (var version in versions.EnumerateArray()) {
GameVersion gv = new() {
Major = version[0].GetInt32(),
Minor = version[1].GetInt32(),
Patch = version[2].GetInt32(),
Suffix = version[3].GetString(),
Bubble = version[4].GetInt32(),
};
Binz binz = new() {
Version = new() {
Version = gv,
Game = game,
},
Source = "bubbles",
};
PreSort.Insert(0, binz);
ByVersion.Add(binz.Version, binz);
}
}

private void OnSelectedRowChanged(object sender, EventArgs e)
{
Expand All @@ -125,21 +133,21 @@ private void OnSelectedRowChanged(object sender, EventArgs e)
delete.Enabled = selected.Local;
}

private static Regex extractVersion = new(@"blueprints_raw_(\d+).(\d+)\.(\d+)(.)_(\d).binz");
private static Regex extractVersionKM = new(@"blueprints_raw_km_(\d+).(\d+)\.(\d+)(.)_(\d).binz");
private static Regex extractVersionRT = new(@"blueprints_raw_RT_(\d+).(\d+)\.(\d+)(.)_(\d).binz");
private static Regex extractVersion = new(@"blueprints_raw_(\d+).(\d+)\.(\d+)(.*)_(\d).binz");
private static Regex extractVersionKM = new(@"blueprints_raw_KM_(\d+).(\d+)\.(\d+)(.)_(\d).binz");
private static Regex extractVersionRT = new(@"blueprints_raw_RT_(\d+).(\d+)\.(\d+)(\.\d+|.*)_(\d).binz");

private static BinzVersion VersionFromFile(string file)
{
Match match;
string game = "Wrath";
string fileName = Path.GetFileName(file);
if (fileName.StartsWith("blueprints_raw_km"))
if (fileName.StartsWith("blueprints_raw_KM", StringComparison.InvariantCultureIgnoreCase))
{
match = extractVersionKM.Match(file);
game = "Kingmaker";
game = "KM";
}
else if (fileName.StartsWith("blueprints_raw_RT"))
else if (fileName.StartsWith("blueprints_raw_RT", StringComparison.InvariantCultureIgnoreCase))
{
match = extractVersionRT.Match(file);
game = "RT";
Expand All @@ -154,7 +162,7 @@ private static BinzVersion VersionFromFile(string file)
Major = int.Parse(match.Groups[1].Value),
Minor = int.Parse(match.Groups[2].Value),
Patch = int.Parse(match.Groups[3].Value),
Suffix = match.Groups[4].Value[0],
Suffix = match.Groups[4].Value,
Bubble = int.Parse(match.Groups[5].Value),
};
return new()
Expand Down Expand Up @@ -219,25 +227,27 @@ private async void DoLoadSelected(object sender, EventArgs e)
BubblePrints.Game_Data = toLoad.Version.Game switch
{
"Wrath" => "Wrath_Data",
"Kingmaker" => "Kingmaker_Data",
"KM" => "Kingmaker_Data",
"RT" => "WH40KRT_Data",
_ => throw new NotSupportedException(),
};

if (!toLoad.Local)
{
if (toLoad.Version.Game != "Wrath")
if (toLoad.Version.Game != "KM" && toLoad.Version.Game != "Wrath" && toLoad.Version.Game != "RT")
{
throw new Exception("Can only auto-download wrath binz");
throw new Exception("Can only auto-download km, wrath and rt binz");
}

loadAnim.Image = Resources.downloading;
loadAnim.ShowProgressBar = true;
loadAnim.Caption = "Downloading";
const string host = "https://github.com/factubsio/BubblePrintsData/releases/download";
string filename = BlueprintDB.FileNameFor(toLoad.Version.Version, toLoad.Version.Game);
var latestVersionUrl = new Uri($"{host}/{toLoad.Version.Version}/{filename}");

Uri latestVersionUrl = null;
if (toLoad.Version.Game == "Wrath") latestVersionUrl = new Uri($"{host}/{toLoad.Version.Version}/{filename}");
else if (toLoad.Version.Game == "RT") latestVersionUrl = new Uri($"{host}/RT_{toLoad.Version.Version}/{filename}");
else if (toLoad.Version.Game == "KM") latestVersionUrl = new Uri($"{host}/KM_{toLoad.Version.Version}/{filename}");
var client = new WebClient();

Check warning on line 251 in BlueprintExplorer/SplashScreenChooserJobbie.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

string tmp = Path.Combine(CacheDir, "binz_download.tmp");
Expand All @@ -249,6 +259,7 @@ private async void DoLoadSelected(object sender, EventArgs e)
client.DownloadProgressChanged += (sender, e) => loadAnim.SetPercentSafe(e.ProgressPercentage);
var download = client.DownloadFileTaskAsync(latestVersionUrl, tmp);
await download;
if (File.Exists(toLoad.Path)) File.Delete(toLoad.Path);
File.Move(tmp, toLoad.Path);
}

Expand Down

0 comments on commit 9a9cec5

Please sign in to comment.