Skip to content

Commit

Permalink
verify path match
Browse files Browse the repository at this point in the history
use dictionary instead of hashtable
  • Loading branch information
kianzarrin committed Oct 19, 2022
1 parent 99d6473 commit fe32231
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
70 changes: 44 additions & 26 deletions LoadOrder/CO/IO/DataLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,28 @@ public static class DataLocation {
static DataLocation() {
var sw = System.Diagnostics.Stopwatch.StartNew();
string m = "Delayed messages: "; // delayed message;
CSCache csCache = null;
try {
var data = CSCache.Deserialize(LocalLOMData);
if(data == null) {
data = new CSCache();
csCache = CSCache.Deserialize(LocalLOMData);
if(csCache == null) {
csCache = new CSCache();
// backward compatibility.
var data2 = LoadOrderConfig.Deserialize(LocalLOMData);
if(data2 != null) {
data.WorkShopContentPath = data2.WorkShopContentPath;
data.GamePath = data2.GamePath;
data.SteamPath = data2.SteamPath;
csCache.WorkShopContentPath = data2.WorkShopContentPath;
csCache.GamePath = data2.GamePath;
csCache.SteamPath = data2.SteamPath;
}
}

sw.Stop();

try {
m += "\ndata is " + (data is null ? "null" : "not null");
m += $"\ndata?.GamePath={data?.GamePath ?? "<null>"}";
if (Util.IsGamePath(data?.GamePath)) {
m += "\ngame path found: " + data.GamePath;
GamePath = RealPath(data.GamePath);
m += "\ndata is " + (csCache is null ? "null" : "not null");
m += $"\ndata?.GamePath={csCache?.GamePath ?? "<null>"}";
if (Util.IsGamePath(csCache?.GamePath)) {
m += "\ngame path found: " + csCache.GamePath;
GamePath = RealPath(csCache.GamePath);
m += "\nreal game path is: " + GamePath;
} else if (!Util.IsGamePath(GamePath)) {
m += "\ngetting game path from registry ...";
Expand All @@ -72,16 +73,16 @@ public static class DataLocation {
}

try {
m += $"\ndata?.SteamPath={data?.SteamPath ?? "<null>"}";
if (Util.IsSteamPath(data?.SteamPath)) {
m += "\nSteamPath found: " + data.SteamPath;
SteamPath = RealPath(data.SteamPath);
m += $"\ndata?.SteamPath={csCache?.SteamPath ?? "<null>"}";
if (Util.IsSteamPath(csCache?.SteamPath)) {
m += "\nSteamPath found: " + csCache.SteamPath;
SteamPath = RealPath(csCache.SteamPath);
m += "\nreal SteamPath is: " + SteamPath;
} else if (!Util.IsSteamPath(SteamPath)) {
m += "\ngetting SteamPath from registry ...";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(SteamPathSubKey_)) {
SteamPath = key?.GetValue(SteamPathKey_) as string;
if (Util.IsSteamPath(data?.SteamPath)) {
if (Util.IsSteamPath(csCache?.SteamPath)) {
SteamPath = RealPath(SteamPath);
} else {
SteamPath = null;
Expand All @@ -94,9 +95,9 @@ public static class DataLocation {
Log.Exception(ex);
}

m += $"\ndata?.WorkShopContentPath={data?.WorkShopContentPath ?? "<null>"}";
if (Util.IsWSPath(data?.WorkShopContentPath)) {
WorkshopContentPath = RealPath(data.WorkShopContentPath);
m += $"\ndata?.WorkShopContentPath={csCache?.WorkShopContentPath ?? "<null>"}";
if (Util.IsWSPath(csCache?.WorkShopContentPath)) {
WorkshopContentPath = RealPath(csCache.WorkShopContentPath);
m += "\nWorkshopContentPath found: " + WorkshopContentPath;
}
m += "\n[P3]WorkshopContentPath so far is: " + WorkshopContentPath;
Expand All @@ -123,11 +124,11 @@ public static class DataLocation {
m += "\nSteamPath=" + (SteamPath ?? "<null>");
m += "\nWorkshopContentPath=" + (WorkshopContentPath ?? "<null>");

data ??= new CSCache();
data.GamePath = GamePath;
data.SteamPath = SteamPath;
data.WorkShopContentPath = WorkshopContentPath;
data.Serialize(LocalLOMData);
csCache ??= new CSCache();
csCache.GamePath = GamePath;
csCache.SteamPath = SteamPath;
csCache.WorkShopContentPath = WorkshopContentPath;
csCache.Serialize(LocalLOMData);
} else {
Log.Info(m);
Process.GetCurrentProcess().Kill();
Expand All @@ -148,6 +149,7 @@ public static class DataLocation {
} finally {
Log.Info(m);
VerifyPaths();
if (csCache != null) ValidatePathMatch(csCache: csCache);
Log.Debug($"LoadOrderConfig.Deserialize took {sw.ElapsedMilliseconds}ms");
DataLocation.DisplayStatus();
}
Expand Down Expand Up @@ -288,6 +290,23 @@ public static class Util {
}
}

public static void ValidatePathMatch(CSCache csCache) {
foreach (var mod in csCache.ItemTable.Values.OfType<CSCache.Mod>()) {
string WSDirName = "255710";
int i = mod.IncludedPath.IndexOf(WSDirName);
if (i > 0) {
string WSDirPath = mod.IncludedPath.Substring(0, i + WSDirName.Length);
string m =
$"tool uses '{WorkshopContentPath}'\n" +
$"game uses '{WSDirPath}'";
Log.Debug(m);
if (WorkshopContentPath != WSDirPath) {
new Exception($"Path mismatch! enabling/disabling may not work.\n" + m).Log();
}
}
}
}

public static bool VerifyPaths() {
try {
bool good =
Expand All @@ -296,13 +315,12 @@ public static class Util {
Util.IsSteamPath(SteamPath);
if (good)
return true;
MessageBox.Show("could not find paths\n" + PrintPaths()); ;
MessageBox.Show("could not find paths\n" + PrintPaths());
} catch (Exception ex) {
MessageBox.Show("could not find paths\n" + PrintPaths()); ;
ex.Log();
}
return false;

}

public static string CitiesExe {
Expand Down
7 changes: 4 additions & 3 deletions LoadOrderMod/Data/CSCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ namespace LoadOrderShared {
using System.Xml.Serialization;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System;

public class CSCache {
public class Item {
Expand Down Expand Up @@ -33,13 +35,13 @@ public class Asset : Item {
/// </summary>
public ulong[] MissingDir = new ulong[0];

internal Hashtable ItemTable = new Hashtable(100000);
internal Dictionary<string, Item> ItemTable = new (100000);

public void AddItem(Item item) {
ItemTable[item.IncludedPath] = item;
}

public Item GetItem(string path) => ItemTable[path] as Item;
public Item GetItem(string path) => ItemTable.GetValueOrDefault(path) as Item;

public void Serialize(string dir) {
Mods = ItemTable.Values.OfType<Mod>().ToArray();
Expand All @@ -50,7 +52,6 @@ public class Asset : Item {
ser.Serialize(fs, this, LoadOrderConfig.NoNamespaces);
}
}

public static CSCache Deserialize(string dir) {
try {
XmlSerializer ser = new XmlSerializer(typeof(CSCache));
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>
<AssemblyVersion>1.12.8.*</AssemblyVersion>
<AssemblyVersion>1.12.9.*</AssemblyVersion>
</PropertyGroup>
</Project>

0 comments on commit fe32231

Please sign in to comment.