Skip to content

Commit

Permalink
fix: remove manager caching, it loads fast enough
Browse files Browse the repository at this point in the history
  • Loading branch information
rfuzzo committed Feb 9, 2023
1 parent eaf4b4b commit 857964a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 370 deletions.
6 changes: 3 additions & 3 deletions WolvenKit.App/Controllers/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
Expand Down Expand Up @@ -40,7 +40,7 @@ public static string ConfigurationPath
public bool IsWelcomeFormDisabled { get; set; }
public bool IsAutoInstallModsDisabled { get; set; }

public string[] ManagerVersions { get; set; } = new string[(int)EManagerType.Max];
//public string[] ManagerVersions { get; set; } = new string[(int)EManagerType.Max];

public string GameModDir { get; set; }
public string GameDlcDir { get; set; }
Expand Down Expand Up @@ -94,4 +94,4 @@ public static Configuration Load()
};
}
}
}
}
218 changes: 12 additions & 206 deletions WolvenKit.App/Controllers/MainController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -225,36 +225,7 @@ public static void LogProgress(int value)
#endregion

#region Methods
private static string GetManagerPath(EManagerType type)
{
switch (type)
{
case EManagerType.BundleManager: return Path.Combine(ManagerCacheDir, "bundle_cache.json");
case EManagerType.CollisionManager: return Path.Combine(ManagerCacheDir, "collision_cache.json");
case EManagerType.SoundManager: return Path.Combine(ManagerCacheDir, "sound_cache.json");
case EManagerType.W3StringManager: return Path.Combine(ManagerCacheDir, "string_cache.bin");
case EManagerType.TextureManager: return Path.Combine(ManagerCacheDir, "texture_cache.json");
case EManagerType.Max:
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
private static string GetManagerVersion(EManagerType type)
{
switch (type)
{
case EManagerType.BundleManager: return BundleManager.SerializationVersion;
case EManagerType.CollisionManager: return WolvenKit.Cache.CollisionManager.SerializationVersion;
case EManagerType.SoundManager: return WolvenKit.Cache.SoundManager.SerializationVersion;
case EManagerType.W3StringManager: return W3Strings.W3StringManager.SerializationVersion;
case EManagerType.TextureManager: return WolvenKit.Cache.TextureManager.SerializationVersion;
case EManagerType.Max:
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}



/// <summary>
/// Initializes the archive managers in an async thread
/// </summary>
Expand All @@ -263,20 +234,6 @@ public async Task Initialize()
{
try
{
// add a mechanism to update individual cache managers
for (var j = 0; j < Configuration.ManagerVersions.Length; j++)
{
var savedversions = Configuration.ManagerVersions[j];
var e = (EManagerType)j;
var curversion = GetManagerVersion(e);

if (savedversions != curversion)
{
if (File.Exists(GetManagerPath(e)))
File.Delete(GetManagerPath(e));
}
}

//multithread these
var loadStringsManagerTask = LoadStringsManager();
var loadBundleManagerTask = LoadBundleManager();
Expand Down Expand Up @@ -362,35 +319,8 @@ private async Task LoadStringsManager()
sw.Start();
if (W3StringManager == null)
{
try
{
if (File.Exists(GetManagerPath(EManagerType.W3StringManager)) && new FileInfo(GetManagerPath(EManagerType.W3StringManager)).Length > 0)
{
using (var file = File.Open(GetManagerPath(EManagerType.W3StringManager), FileMode.Open))
{
W3StringManager = ProtoBuf.Serializer.Deserialize<W3StringManager>(file);
}
}
else
{
W3StringManager = new W3StringManager();
W3StringManager.Load(Configuration.TextLanguage, Path.GetDirectoryName(Configuration.ExecutablePath));
Directory.CreateDirectory(ManagerCacheDir);
using (var file = File.Open(GetManagerPath(EManagerType.W3StringManager), FileMode.Create))
{
ProtoBuf.Serializer.Serialize(file, W3StringManager);
}

Configuration.ManagerVersions[(int)EManagerType.W3StringManager] = W3StringManager.SerializationVersion;
}
}
catch (System.Exception)
{
if (File.Exists(GetManagerPath(EManagerType.W3StringManager)))
File.Delete(GetManagerPath(EManagerType.W3StringManager));
W3StringManager = new W3StringManager();
W3StringManager.Load(Configuration.TextLanguage, Path.GetDirectoryName(Configuration.ExecutablePath));
}
W3StringManager = new W3StringManager();
W3StringManager.Load(Configuration.TextLanguage, Path.GetDirectoryName(Configuration.ExecutablePath));
}

var i = sw.ElapsedMilliseconds;
Expand All @@ -404,39 +334,8 @@ private async Task LoadBundleManager()
#region Load bundle manager
if (BundleManager == null)
{
try
{
if (File.Exists(GetManagerPath(EManagerType.BundleManager)))
{
using (StreamReader file = File.OpenText(GetManagerPath(EManagerType.BundleManager)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
serializer.TypeNameHandling = TypeNameHandling.Auto;
BundleManager = (BundleManager)serializer.Deserialize(file, typeof(BundleManager));
}
}
else
{
BundleManager = new BundleManager();
BundleManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
File.WriteAllText(GetManagerPath(EManagerType.BundleManager), JsonConvert.SerializeObject(BundleManager, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto
}));
Configuration.ManagerVersions[(int)EManagerType.BundleManager] = BundleManager.SerializationVersion;
}
}
catch (System.Exception ex)
{
if (File.Exists(GetManagerPath(EManagerType.BundleManager)))
File.Delete(GetManagerPath(EManagerType.BundleManager));
BundleManager = new BundleManager();
BundleManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
BundleManager = new BundleManager();
BundleManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
#endregion
}
Expand All @@ -447,39 +346,8 @@ private async Task LoadTextureManager()
#region Load texture manager
if (TextureManager == null)
{
try
{
if (File.Exists(GetManagerPath(EManagerType.TextureManager)))
{
using (StreamReader file = File.OpenText(GetManagerPath(EManagerType.TextureManager)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
serializer.TypeNameHandling = TypeNameHandling.Auto;
TextureManager = (TextureManager)serializer.Deserialize(file, typeof(TextureManager));
}
}
else
{
TextureManager = new TextureManager();
TextureManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
File.WriteAllText(GetManagerPath(EManagerType.TextureManager), JsonConvert.SerializeObject(TextureManager, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto
}));
Configuration.ManagerVersions[(int)EManagerType.TextureManager] = TextureManager.SerializationVersion;
}
}
catch (System.Exception)
{
if (File.Exists(GetManagerPath(EManagerType.TextureManager)))
File.Delete(GetManagerPath(EManagerType.TextureManager));
TextureManager = new TextureManager();
TextureManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
TextureManager = new TextureManager();
TextureManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
#endregion
}
Expand All @@ -490,39 +358,8 @@ private async Task LoadCollisionManager()
#region Load collision manager
if (CollisionManager == null)
{
try
{
if (File.Exists(GetManagerPath(EManagerType.CollisionManager)))
{
using (StreamReader file = File.OpenText(GetManagerPath(EManagerType.CollisionManager)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
serializer.TypeNameHandling = TypeNameHandling.Auto;
CollisionManager = (CollisionManager)serializer.Deserialize(file, typeof(CollisionManager));
}
}
else
{
CollisionManager = new CollisionManager();
CollisionManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
File.WriteAllText(GetManagerPath(EManagerType.CollisionManager), JsonConvert.SerializeObject(CollisionManager, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto
}));
Configuration.ManagerVersions[(int)EManagerType.CollisionManager] = CollisionManager.SerializationVersion;
}
}
catch (System.Exception)
{
if (File.Exists(GetManagerPath(EManagerType.CollisionManager)))
File.Delete(GetManagerPath(EManagerType.CollisionManager));
CollisionManager = new CollisionManager();
CollisionManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
CollisionManager = new CollisionManager();
CollisionManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
#endregion
}
Expand All @@ -533,39 +370,8 @@ private async Task LoadSoundManager()
#region Load sound manager
if (SoundManager == null)
{
try
{
if (File.Exists(GetManagerPath(EManagerType.SoundManager)))
{
using (StreamReader file = File.OpenText(GetManagerPath(EManagerType.SoundManager)))
{
JsonSerializer serializer = new JsonSerializer();
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
serializer.TypeNameHandling = TypeNameHandling.Auto;
SoundManager = (SoundManager)serializer.Deserialize(file, typeof(SoundManager));
}
}
else
{
SoundManager = new SoundManager();
SoundManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
File.WriteAllText(GetManagerPath(EManagerType.SoundManager), JsonConvert.SerializeObject(SoundManager, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
TypeNameHandling = TypeNameHandling.Auto
}));
Configuration.ManagerVersions[(int)EManagerType.SoundManager] = SoundManager.SerializationVersion;
}
}
catch (System.Exception ex)
{
if (File.Exists(GetManagerPath(EManagerType.SoundManager)))
File.Delete(GetManagerPath(EManagerType.SoundManager));
SoundManager = new SoundManager();
SoundManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
SoundManager = new SoundManager();
SoundManager.LoadAll(Path.GetDirectoryName(Configuration.ExecutablePath));
}
#endregion
}
Expand Down
15 changes: 2 additions & 13 deletions WolvenKit.App/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WolvenKit.App
{
/// <summary>
/// Enumeration of serialized Managers
/// </summary>
public enum EManagerType
{
BundleManager = 0,
CollisionManager = 1,
SoundManager = 2,
W3StringManager = 3,
TextureManager = 4,
Max = 5,
}




Expand Down
7 changes: 2 additions & 5 deletions WolvenKit.Bundles/Bundle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -107,9 +107,6 @@ private void Read()
Console.WriteLine("Warning: Bundle '" + ArchiveAbsolutePath + "' could not be fully loaded as resource '" + item.Name + "' is defined more than once. Thus, only the first definition was loaded.");
}
}


reader.Close();
}
}

Expand Down Expand Up @@ -238,4 +235,4 @@ public static string GetRelativePath(string filespec, string folder)
return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}
}
}
}
4 changes: 2 additions & 2 deletions WolvenKit.Bundles/BundleManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -69,7 +69,7 @@ public override void LoadBundle(string filename, bool ispatch = false)
// if file is already in another bundle
if (ispatch && Items[item.Key].Count > 0)
{
// check if file is already in contentN directory (content0, content1 etc)
// check if file is already in content directory (content0, content1 etc)
List<IWitcherFile> filesInBundles = Items[item.Key];
var splits = filesInBundles.First().Bundle.ArchiveAbsolutePath.Split(Path.DirectorySeparatorChar);
var contentdir = splits[splits.Length - 3];
Expand Down
2 changes: 1 addition & 1 deletion WolvenKit.CR2W/WolvenKit.CR2W.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<NoWarn>CS1591</NoWarn>
<DefineConstants>TRACE;NGE_VERSION</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
<Optimize>true</Optimize>
<!--<Optimize>true</Optimize>-->
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<DebugType>pdbonly</DebugType>
Expand Down
Loading

0 comments on commit 857964a

Please sign in to comment.