Skip to content

Commit

Permalink
- Added status for loaded game data during startup.
Browse files Browse the repository at this point in the history
- Fixed issue that allowed you to launch the app multiple times.
- Improved startup time.
- Moved game data.
  • Loading branch information
josdemmers committed Feb 5, 2023
1 parent fd7a9ac commit df8e325
Show file tree
Hide file tree
Showing 30 changed files with 291 additions and 134 deletions.
8 changes: 8 additions & 0 deletions NewWorldCompanion.Events/CraftingRecipeManagerEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Prism.Events;

namespace NewWorldCompanion.Events
{
public class CraftingRecipeManagerUpdated : PubSubEvent
{
}
}
12 changes: 12 additions & 0 deletions NewWorldCompanion.Events/NewWorldDataStoreEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Prism.Events;

namespace NewWorldCompanion.Events
{
public class NewWorldDataStoreUpdated : PubSubEvent
{
}

public class NewWorldDataStoreStatusUpdated : PubSubEvent
{
}
}
2 changes: 2 additions & 0 deletions NewWorldCompanion.Interfaces/ICraftingRecipeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace NewWorldCompanion.Interfaces
{
public interface ICraftingRecipeManager
{
bool Available { get; }

List<CraftingRecipe> CraftingRecipes
{
get;
Expand Down
7 changes: 7 additions & 0 deletions NewWorldCompanion.Interfaces/INewWorldDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace NewWorldCompanion.Interfaces
{
public interface INewWorldDataStore
{
bool Available { get; }
string LoadStatusItemDefinitions { get; }
string LoadStatusCraftingRecipes { get; }
string LoadStatusHouseItems { get; }
string LoadStatusLocalisation { get; }

List<CraftingRecipe> GetCraftingRecipes();
bool IsBindOnPickup(string itemName);
string GetItemId(string itemName);
Expand All @@ -18,5 +24,6 @@ public interface INewWorldDataStore
string GetItemLocalisation(string itemMasterName);
List<CraftingRecipeJson> GetRelatedRecipes(string itemId);
CraftingRecipeJson GetCraftingRecipeDetails(string itemId);
void UpdateStoreData();
}
}
19 changes: 16 additions & 3 deletions NewWorldCompanion.Services/CraftingRecipeManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NewWorldCompanion.Entities;
using NewWorldCompanion.Events;
using NewWorldCompanion.Interfaces;
using Prism.Events;
using System;
Expand All @@ -16,6 +17,8 @@ public class CraftingRecipeManager : ICraftingRecipeManager

private List<CraftingRecipe> _craftingRecipes = new List<CraftingRecipe>();

private bool _available = false;

// Start of Constructor region

#region Constructor
Expand All @@ -24,12 +27,10 @@ public CraftingRecipeManager(IEventAggregator eventAggregator, INewWorldDataStor
{
// Init IEventAggregator
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<NewWorldDataStoreUpdated>().Subscribe(HandleNewWorldDataStoreUpdatedEvent);

// Init stores
_newWorldDataStore = newWorldDataStore;

// Init recipes
LoadRecipes();
}

#endregion
Expand All @@ -40,12 +41,20 @@ public CraftingRecipeManager(IEventAggregator eventAggregator, INewWorldDataStor

public List<CraftingRecipe> CraftingRecipes { get => _craftingRecipes; }

public bool Available { get => _available; set => _available = value; }

#endregion

// Start of Events region

#region Events

private void HandleNewWorldDataStoreUpdatedEvent()
{
// Init recipes
LoadRecipes();
}

#endregion

// Start of Methods region
Expand Down Expand Up @@ -89,6 +98,10 @@ private void LoadRecipes()

// Save recipe progress
SaveRecipes();

// Finished initializing data. Inform subscribers.
Available = true;
_eventAggregator.GetEvent<CraftingRecipeManagerUpdated>().Publish();
}

public void SaveRecipes()
Expand Down
28 changes: 0 additions & 28 deletions NewWorldCompanion.Services/NewWorldCompanion.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<None Remove="Data\CraftingRecipes.json" />
<None Remove="Data\HouseItems.json" />
<None Remove="Data\javelindata_housingitems.loc.xml" />
<None Remove="Data\javelindata_itemdefinitions_master.loc.xml" />
<None Remove="Data\MasterItemDefinitions_Common.json" />
<None Remove="Data\MasterItemDefinitions_Crafting.json" />
<None Remove="Data\MasterItemDefinitions_Loot.json" />
<None Remove="Data\MasterItemDefinitions_Quest.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Emgu.CV" Version="4.6.0.5131" />
<PackageReference Include="Emgu.CV.Bitmap" Version="4.6.0.5131" />
Expand All @@ -38,21 +27,4 @@
<ProjectReference Include="..\NewWorldCompanion.Interfaces\NewWorldCompanion.Interfaces.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Data\CraftingRecipes.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Data\HouseItems.json" />
<EmbeddedResource Include="Data\javelindata_housingitems.loc.xml" />
<EmbeddedResource Include="Data\javelindata_itemdefinitions_master.loc.xml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Data\MasterItemDefinitions_Common.json" />
<EmbeddedResource Include="Data\MasterItemDefinitions_Crafting.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Data\MasterItemDefinitions_Loot.json" />
<EmbeddedResource Include="Data\MasterItemDefinitions_Quest.json" />
</ItemGroup>

</Project>
99 changes: 70 additions & 29 deletions NewWorldCompanion.Services/NewWorldDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NewWorldCompanion.Constants;
using NewWorldCompanion.Entities;
using NewWorldCompanion.Events;
using NewWorldCompanion.Helpers;
using NewWorldCompanion.Interfaces;
using Prism.Events;
Expand All @@ -12,6 +13,7 @@
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace NewWorldCompanion.Services
Expand All @@ -25,6 +27,14 @@ public class NewWorldDataStore : INewWorldDataStore
private List<HouseItemsJson> _houseItemsJson = new List<HouseItemsJson>();
private Dictionary<string, string> _itemDefinitionsLocalisation = new Dictionary<string, string>();

private bool _available = false;

private string _loadStatusItemDefinitions = string.Empty;
private string _loadStatusCraftingRecipes = string.Empty;
private string _loadStatusHouseItems = string.Empty;
private string _loadStatusLocalisation = string.Empty;


// Start of Constructor region

#region Constructor
Expand All @@ -35,7 +45,7 @@ public NewWorldDataStore(IEventAggregator eventAggregator)
_eventAggregator = eventAggregator;

// Init store data
UpdateStoreData();
Task.Run(() => UpdateStoreData());
}

#endregion
Expand All @@ -44,23 +54,31 @@ public NewWorldDataStore(IEventAggregator eventAggregator)

#region Properties

public bool Available { get => _available; set => _available = value; }
public string LoadStatusItemDefinitions { get => _loadStatusItemDefinitions; set => _loadStatusItemDefinitions = value; }
public string LoadStatusCraftingRecipes { get => _loadStatusCraftingRecipes; set => _loadStatusCraftingRecipes = value; }
public string LoadStatusHouseItems { get => _loadStatusHouseItems; set => _loadStatusHouseItems = value; }
public string LoadStatusLocalisation { get => _loadStatusLocalisation; set => _loadStatusLocalisation = value; }

#endregion

// Start of Methods region

#region Methods

private void UpdateStoreData()
public void UpdateStoreData()
{
var assembly = Assembly.GetExecutingAssembly();
string resourcePath = string.Empty;

_loadStatusItemDefinitions = $"ItemDefinitions: 0. Loading common items";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();

// MasterItemDefinitions Common
_masterItemDefinitionsJson.Clear();
var masterItemDefinitionsJson = new List<MasterItemDefinitionsJson>();
resourcePath = "MasterItemDefinitions_Common.json";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\MasterItemDefinitions_Common.json";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand All @@ -78,11 +96,13 @@ private void UpdateStoreData()
}
}

_loadStatusItemDefinitions = $"ItemDefinitions: {_masterItemDefinitionsJson.Count}. Loading crafting items";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();

// MasterItemDefinitions Crafting
masterItemDefinitionsJson.Clear();
resourcePath = "MasterItemDefinitions_Crafting.json";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\MasterItemDefinitions_Crafting.json";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand All @@ -100,11 +120,13 @@ private void UpdateStoreData()
}
}

_loadStatusItemDefinitions = $"ItemDefinitions: {_masterItemDefinitionsJson.Count}. Loading loot items";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();

// MasterItemDefinitions Loot
masterItemDefinitionsJson.Clear();
resourcePath = "MasterItemDefinitions_Loot.json";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\MasterItemDefinitions_Loot.json";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand All @@ -122,11 +144,13 @@ private void UpdateStoreData()
}
}

_loadStatusItemDefinitions = $"ItemDefinitions: {_masterItemDefinitionsJson.Count}. Loading quest items";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();

// MasterItemDefinitions Quest
masterItemDefinitionsJson.Clear();
resourcePath = "MasterItemDefinitions_Quest.json";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\MasterItemDefinitions_Quest.json";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand All @@ -144,11 +168,14 @@ private void UpdateStoreData()
}
}

_loadStatusItemDefinitions = $"ItemDefinitions: {_masterItemDefinitionsJson.Count}";
_loadStatusCraftingRecipes = $"CraftingRecipes: 0. Loading recipes";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();

// CraftingRecipe Json
_craftingRecipesJson.Clear();
resourcePath = "CraftingRecipes.json";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\CraftingRecipes.json";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand All @@ -165,23 +192,29 @@ private void UpdateStoreData()
}
}

_loadStatusCraftingRecipes = $"CraftingRecipes: {_craftingRecipesJson.Count}";
_loadStatusHouseItems = $"HouseItems: 0. Loading items";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();

// HouseItems Json
_houseItemsJson.Clear();
resourcePath = "HouseItems.json";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\HouseItems.json";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
_houseItemsJson = JsonSerializer.Deserialize<List<HouseItemsJson>>(stream) ?? new List<HouseItemsJson>();
}
}

_loadStatusHouseItems = $"HouseItems: {_houseItemsJson.Count}";
_loadStatusLocalisation = $"Localisation: 0. Loading localisations";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();

// ItemDefinitionsLocalisation - Itemdefinitions
_itemDefinitionsLocalisation.Clear();
resourcePath = "javelindata_itemdefinitions_master.loc.xml";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\javelindata_itemdefinitions_master.loc.xml";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand All @@ -204,6 +237,9 @@ private void UpdateStoreData()
{
_itemDefinitionsLocalisation.TryAdd(key.ToLower(), value);
}

_loadStatusLocalisation = $"Localisation data: {_itemDefinitionsLocalisation.Count}";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();
}
}
}
Expand All @@ -214,9 +250,8 @@ private void UpdateStoreData()
_itemDefinitionsLocalisation.Remove("ArrowBT5_MasterName".ToLower());

// ItemDefinitionsLocalisation - HouseItems
resourcePath = "javelindata_housingitems.loc.xml";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\javelindata_housingitems.loc.xml";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand All @@ -236,9 +271,16 @@ private void UpdateStoreData()
{
_itemDefinitionsLocalisation.TryAdd(key.ToLower(), value);
}

_loadStatusLocalisation = $"Localisation data: {_itemDefinitionsLocalisation.Count}";
_eventAggregator.GetEvent<NewWorldDataStoreStatusUpdated>().Publish();
}
}
}

// Finished initializing data. Inform subscribers.
Available = true;
_eventAggregator.GetEvent<NewWorldDataStoreUpdated>().Publish();
}

public List<CraftingRecipe> GetCraftingRecipes()
Expand Down Expand Up @@ -298,9 +340,8 @@ public List<MasterItemDefinitionsJson> GetOverlayResources()

// MasterItemDefinitions Crafting
var masterItemDefinitionsJson = new List<MasterItemDefinitionsJson>();
resourcePath = "MasterItemDefinitions_Crafting.json";
resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resourcePath));
using (Stream? stream = assembly.GetManifestResourceStream(resourcePath))
resourcePath = @".\Data\MasterItemDefinitions_Crafting.json";
using (FileStream? stream = File.OpenRead(resourcePath))
{
if (stream != null)
{
Expand Down
2 changes: 1 addition & 1 deletion NewWorldCompanion.Services/OcrHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void HandleOcrImageReadyEvent()
{
lock (nameLock)
{
if (_screenProcessHandler.OcrImage != null)
if (_screenProcessHandler.OcrImage != null && _newWorldDataStore.Available)
{
try
{
Expand Down
Loading

0 comments on commit df8e325

Please sign in to comment.