Skip to content

Commit

Permalink
fix update and relative config path
Browse files Browse the repository at this point in the history
closes #15 and closes #14
  • Loading branch information
ardittristan committed Jul 7, 2023
1 parent 32ba545 commit 920cb40
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 15 deletions.
6 changes: 5 additions & 1 deletion ExtractAllTexturesFromHS/ExtractAllTexturesFromHS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ThisAssembly.Project" Version="1.2.14">
<PackageReference Include="ThisAssembly.Project" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -30,4 +30,8 @@
<ProjectReference Include="..\TextureExtractor\TextureExtractor.csproj" />
</ItemGroup>

<Target Name="CleanOutput" AfterTargets="Clean">
<RemoveDir Directories="obj\hs" />
</Target>

</Project>
9 changes: 8 additions & 1 deletion ExtractAllTexturesFromHS/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@

string extractDir = Path.Combine(ThisAssembly.Project.ProjectPath, @"obj\hs");

IEnumerable<string> assetBundles = Directory.EnumerateFiles(assetBundleDir, "*.unity3d");
string searchPattern = "*.unity3d";

if (args.Length > 0 && args[0].Length > 0)
searchPattern = args[0];

IEnumerable<string> assetBundles = Directory.EnumerateFiles(assetBundleDir, searchPattern);

Extractor extractor = new(extractDir, "1");

Console.WriteLine(extractor.FindBundle(assetBundleDir, "initial_base_global-*-texture-*.unity3d", "class_druid-icon", true));

Task[] tasks = assetBundles.Select(bundle => extractor.ExtractAsync(bundle, true)).ToArray();

Task whenAll = Task.WhenAll(tasks);
Expand Down
15 changes: 15 additions & 0 deletions ExtractAllTexturesFromHS/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"profiles": {
"Extract[initial_base_global]TexturesFromHS": {
"commandName": "Project",
"commandLineArgs": "\"initial_base_global-*-texture-*.unity3d\""
},
"ExtractAllFromHS": {
"commandName": "Project"
},
"ExtractAllTexturesFromHS": {
"commandName": "Project",
"commandLineArgs": "\"*-texture-*.unity3d\""
}
}
}
2 changes: 1 addition & 1 deletion HearthMirrorGenerator/HearthMirrorGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ICSharpCode.Decompiler" Version="8.0.0.7313-preview4" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="ICSharpCode.Decompiler" Version="8.0.0.7345" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="[3.11.0,4.0.0-1)" PrivateAssets="all" />
</ItemGroup>

Expand Down
34 changes: 29 additions & 5 deletions QuestOverlayPlugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Hearthstone_Deck_Tracker.Utility.Logging;
using Hearthstone_Deck_Tracker.Windows;
using HearthWatcher.EventArgs;
using HSReflection;
using HSReflection.Enums;
using MahApps.Metro.Controls;
using QuestOverlayPlugin.Controls;
Expand All @@ -29,7 +30,7 @@ namespace QuestOverlayPlugin;
// ReSharper disable once ClassNeverInstantiated.Global
public class Plugin : IPlugin, Updater.IUpdater
{
public const string QUEST_ICONS_LOC = "initial_base_global-07ca451d-texture-0";
public static string QUEST_ICONS_LOC { private set; get; } = string.Empty;

internal Settings Settings = null!;
private Flyout _settingsFlyout = null!;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class Plugin : IPlugin, Updater.IUpdater
private RecurringTask? _questListWindowRT;

private static double ScreenRatio => (4.0 / 3.0) / (Core.OverlayWindow.Width / Core.OverlayWindow.Height);

private static double QuestsButtonOffset
{
get
Expand All @@ -81,6 +83,7 @@ private static double QuestsButtonOffset
return Core.OverlayWindow.Height * 0.05 + 74;
}
}

private static double BattlegroundsQuestsButtonOffset
{
get
Expand All @@ -105,6 +108,14 @@ public void OnLoad()

new Updater(this).CheckUpdate();

Extractor = new Extractor(
Path.Combine(Config.Instance.ConfigDir, "Plugins", "HearthstoneQuestOverlay", "TextureExtractor"),
Core.Game.MetaData.HearthstoneBuild.ToString());

QUEST_ICONS_LOC = Path.GetFileNameWithoutExtension(Extractor.FindBundle(
Path.Combine(Config.Instance.HearthstoneDirectory, @"Data\Win"),
"initial_base_global-*-texture-*.unity3d", "class_druid-icon"));

_questListButton = new QuestListButton(QuestListVM);
_questListButtonBehavior = new OverlayElementBehavior(_questListButton)
{
Expand Down Expand Up @@ -163,23 +174,30 @@ public void OnLoad()
GameEvents.OnModeChanged.Add(OnGameStart);
GameEvents.OnModeChanged.Add(UpdateQuestWindow);
Watchers.ExperienceWatcher.NewExperienceHandler += UpdateEventHandler;
Watchers.BaconWatcher.Change += UpdateEventHandler;
if (Core.Game.IsRunning) Update();
if (Core.Game.IsRunning) OnGameStart();

Extractor = new Extractor(
Path.Combine(Config.Instance.ConfigDir, "Plugins", "HearthstoneQuestOverlay", "TextureExtractor"),
Core.Game.MetaData.HearthstoneBuild.ToString());

Extractor.ExtractAsync(CreateBundlePath(QUEST_ICONS_LOC));

Reflection.Exception += Reflection_Exception;
}

private void Reflection_Exception(Exception obj)
{
// ReSharper disable once ExplicitCallerInfoArgument
Log.Error(obj, sourceFilePath: "HSReflection/Reflection.cs");
}

private void UpdateQuestWindow(Mode mode) => UpdateQuestWindow();

private void UpdateQuestWindow()
{
if (Settings.ShowPopupWindow) QuestListWindowVM.UpdateAsync();
}

private void OnGameStart(Mode mode) => OnGameStart();

private void OnGameStart()
{
if (_gameRunning)
Expand Down Expand Up @@ -258,6 +276,7 @@ private void InitSettings()
public void OnUnload()
{
Watchers.ExperienceWatcher.NewExperienceHandler -= UpdateEventHandler;
Watchers.BaconWatcher.Change -= UpdateEventHandler;

RemoveOverlay();
_questListWindow.Shutdown();
Expand Down Expand Up @@ -391,6 +410,11 @@ public static void UpdateEventHandler(object sender, ExperienceEventArgs args)
if (args.IsChanged) Update();
}

public static void UpdateEventHandler(object sender, BaconEventArgs args)
{
if (args.IsJournalOpen) Update();
}

internal static void Update(Mode mode) => Update();

internal static void Update()
Expand Down
21 changes: 16 additions & 5 deletions QuestOverlayPlugin/Util/Icon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public class Icon

public ImageSource ImageSource { get; }

public Icon(string? name, string assetBundle = Plugin.QUEST_ICONS_LOC)
public Icon(string? name, string? assetBundle = null)
{
assetBundle ??= Plugin.QUEST_ICONS_LOC;
Name = name;
AssetBundle = assetBundle;
try
Expand Down Expand Up @@ -101,9 +102,7 @@ private ImageSource NormalIcon
WriteableBitmap wb = new(image);

using (wb.GetBitmapContext())
{
wb = wb.Crop(0, 0, image.PixelWidth, image.PixelHeight / 2);
}

return wb;
}
Expand Down Expand Up @@ -142,9 +141,21 @@ private static string IconNameFilter(string name)
}
}

private ImageSource BrokenIcon => new BitmapImage(GetImageUri("3Class_Background-icon.png"));
private ImageSource BrokenIcon
{
get
{
BitmapImage image = new(GetImageUri("3Class_Background-icon.png"));
WriteableBitmap wb = new(image);

using (wb.GetBitmapContext())
wb = wb.Crop(0, 0, image.PixelWidth, image.PixelHeight / 2);

return wb;
}
}

private Uri GetImageUri(string fileName) =>
new(Path.Combine(Plugin.Instance.Extractor.OutputPath, AssetBundle, fileName),
UriKind.Absolute);
UriKind.RelativeOrAbsolute);
}
66 changes: 66 additions & 0 deletions TextureExtractor/Extractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,57 @@ public async Task ExtractAsync(string bundlePath, bool force = false)
await Task.WhenAll(t);
}

public async Task<string> FindBundleAsync(string bundleFolderPath, string bundleFileFilter, string assetName,
bool force = false)
{
Task<string> t = new(() => FindBundle(bundleFolderPath, bundleFileFilter, assetName, force));
t.Start();
return (await Task.WhenAll(t))[0];
}

public string FindBundle(string bundleFolderPath, string bundleFileFilter, string assetName, bool force = false)
{
if (!Validate(bundleFolderPath, bundleFileFilter, assetName, force))
return Properties.Settings.Default.AssetNameStore[bundleFileFilter + assetName];

IEnumerable<string> bundleFiles = Directory.GetFiles(bundleFolderPath, bundleFileFilter);

AssetsManager manager = new();

string foundBundle = null;

Directory.CreateDirectory(OutputPath);

foreach (string bundlePath in bundleFiles)
{
BundleFileInstance bundleInst = manager.LoadBundleFile(bundlePath);
AssetsFileInstance assetsFileInst = manager.LoadAssetsFileFromBundle(bundleInst, 0);

foreach (AssetFileInfo assetFile in assetsFileInst.file.GetAssetsOfType(AssetClassID.Texture2D))
{
AssetTypeValueField baseField = manager.GetBaseField(assetsFileInst, assetFile);
TextureFile textureField = TextureFile.ReadTextureFile(baseField);

if (textureField.m_Name != assetName)
continue;

foundBundle = bundlePath;
goto Break;
}

manager.UnloadAssetsFile(assetsFileInst);
}
Break:

manager.UnloadAll(true);

Properties.Settings.Default.VersionStore[bundleFileFilter + assetName] = HearthstoneBuild;
Properties.Settings.Default.AssetNameStore[bundleFileFilter + assetName] = foundBundle;
Properties.Settings.Default.Save();

return foundBundle;
}

public void Extract(string bundlePath, bool force = false)
{
if (!Validate(bundlePath, force)) return;
Expand Down Expand Up @@ -75,6 +126,21 @@ public void Extract(string bundlePath, bool force = false)
Properties.Settings.Default.Save();
}

private bool Validate(string bundleFolderPath, string bundleFileFilter, string assetName, bool forced)
{
if (!Directory.EnumerateFiles(bundleFolderPath, bundleFileFilter).Any())
throw new FileDoesNotExitException();

bool hasUpdated = HasUpdated(bundleFileFilter + assetName, HearthstoneBuild);

#if DEBUG
System.Diagnostics.Debug.WriteLine("Validate: " + !(forced || hasUpdated));
return true;
#else
return !(forced || hasUpdated);
#endif
}

private bool Validate(string bundlePath, bool forced)
{
if (!File.Exists(bundlePath))
Expand Down
11 changes: 11 additions & 0 deletions TextureExtractor/Properties/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ public SerializableStringDictionary VersionStore
return (SerializableStringDictionary)this[nameof(VersionStore)];
}
}

[UserScopedSetting]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public SerializableStringDictionary AssetNameStore
{
get
{
this[nameof(AssetNameStore)] ??= new SerializableStringDictionary();
return (SerializableStringDictionary)this[nameof(AssetNameStore)];
}
}
}
2 changes: 1 addition & 1 deletion TextureExtractor/TextureExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="ILRepack.Lib.MSBuild.Task" Version="2.0.18.2">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="PortableSettingsProvider" Version="0.2.4">
<PackageReference Include="PortableSettingsProvider" Version="0.2.5">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.5.7</Version>
<Version>1.5.8</Version>
</PropertyGroup>
</Project>

0 comments on commit 920cb40

Please sign in to comment.