Skip to content

Commit

Permalink
Make sure cache also uses copy of game db
Browse files Browse the repository at this point in the history
Configs can be applied after in either case
  • Loading branch information
blowfishpro committed Jan 10, 2019
1 parent 72f37f1 commit 122d4bf
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions ModuleManager/MMPatchLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ private IEnumerator ProcessPatch()
#endif
yield return null;

IEnumerable<IProtoUrlConfig> databaseConfigs = null;

if (!useCache)
{
IPatchProgress progress = new PatchProgress(logger);
Expand Down Expand Up @@ -178,8 +180,6 @@ private IEnumerator ProcessPatch()

logger.Info("Starting patch thread");

IEnumerable<IProtoUrlConfig> databaseConfigs = null;

ITaskStatus patchThread = BackgroundTask.Start(delegate
{
databaseConfigs = applier.ApplyPatches(patchList);
Expand Down Expand Up @@ -236,16 +236,6 @@ float updateTimeRemaining()
yield break;
}

foreach (UrlDir.UrlFile file in GameDatabase.Instance.root.AllConfigFiles)
{
file.configs.Clear();
}

foreach (IProtoUrlConfig protoConfig in databaseConfigs)
{
protoConfig.UrlFile.AddConfig(protoConfig.Node);
}

logger.Info("Done patching");
yield return null;

Expand Down Expand Up @@ -286,7 +276,7 @@ float updateTimeRemaining()
status = "Saving Cache";
logger.Info(status);
yield return null;
CreateCache(progress.Counter.patchedNodes);
CreateCache(databaseConfigs, progress.Counter.patchedNodes);
}

StatusUpdate(progress);
Expand All @@ -301,7 +291,17 @@ float updateTimeRemaining()
status = "Loading from Cache";
logger.Info(status);
yield return null;
LoadCache();
databaseConfigs = LoadCache();
}

foreach (UrlDir.UrlFile file in GameDatabase.Instance.root.AllConfigFiles)
{
file.configs.Clear();
}

foreach (IProtoUrlConfig protoConfig in databaseConfigs)
{
protoConfig.UrlFile.AddConfig(protoConfig.Node);
}

logger.Info(status + "\n" + errors);
Expand Down Expand Up @@ -476,7 +476,7 @@ private ConfigNode GetFileNode(ConfigNode shaConfigNode, string filename)
}


private void CreateCache(int patchedNodeCount)
private void CreateCache(IEnumerable<IProtoUrlConfig> databaseConfigs, int patchedNodeCount)
{
ConfigNode shaConfigNode = new ConfigNode();
shaConfigNode.AddValue("SHA", configSha);
Expand All @@ -488,13 +488,11 @@ private void CreateCache(int patchedNodeCount)

cache.AddValue("patchedNodeCount", patchedNodeCount.ToString());

foreach (UrlDir.UrlConfig config in GameDatabase.Instance.root.AllConfigs)
foreach (IProtoUrlConfig urlConfig in databaseConfigs)
{
ConfigNode node = cache.AddNode("UrlConfig");
node.AddValue("name", config.name);
node.AddValue("type", config.type);
node.AddValue("parentUrl", config.parent.url);
node.AddNode(config.config);
node.AddValue("parentUrl", urlConfig.UrlFile.url);
node.AddNode(urlConfig.Node);
}

foreach (var file in GameDatabase.Instance.root.AllConfigFiles)
Expand Down Expand Up @@ -567,15 +565,8 @@ private void SaveModdedTechTree()
techNode.Save(techTreePath);
}

private void LoadCache()
private IEnumerable<IProtoUrlConfig> LoadCache()
{
// Clear the config DB
foreach (UrlDir.UrlFile files in GameDatabase.Instance.root.AllConfigFiles)
{
files.configs.Clear();
}

// And then load all the cached configs
ConfigNode cache = ConfigNode.Load(cachePath);

if (cache.HasValue("patchedNodeCount") && int.TryParse(cache.GetValue("patchedNodeCount"), out int patchedNodeCount))
Expand All @@ -587,16 +578,16 @@ private void LoadCache()
physicsUrlFile = new UrlDir.UrlFile(gameDataDir, new FileInfo(defaultPhysicsPath));
gameDataDir.files.Add(physicsUrlFile);

List<IProtoUrlConfig> databaseConfigs = new List<IProtoUrlConfig>(cache.nodes.Count);

foreach (ConfigNode node in cache.nodes)
{
string name = node.GetValue("name");
string type = node.GetValue("type");
string parentUrl = node.GetValue("parentUrl");

UrlDir.UrlFile parent = GameDatabase.Instance.root.AllConfigFiles.FirstOrDefault(f => f.url == parentUrl);
if (parent != null)
{
parent.AddConfig(node.nodes[0]);
databaseConfigs.Add(new ProtoUrlConfig(parent, node.nodes[0]));
}
else
{
Expand All @@ -605,6 +596,8 @@ private void LoadCache()
}
progressFraction = 1;
logger.Info("Cache Loaded");

return databaseConfigs;
}

private void StatusUpdate(IPatchProgress progress)
Expand Down

0 comments on commit 122d4bf

Please sign in to comment.