Skip to content

Commit 8274877

Browse files
authored
Merge pull request #647 from twpol/feature/binpat-cache-directory
fix: Put all cache files into the designated area away from user files
2 parents ea2738c + 4e3b696 commit 8274877

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

Source/ORTS.Settings/UserSettings.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using System.IO;
2121
using System.Linq;
2222
using System.Reflection;
23+
using System.Security.Cryptography;
24+
using System.Text;
2325
using System.Windows.Forms;
2426
using ORTS.Common;
2527

@@ -479,6 +481,20 @@ public override object GetDefaultValue(string name)
479481
throw new InvalidDataException(String.Format("UserSetting {0} has no default value.", property.Name));
480482
}
481483

484+
public string GetCacheFilePath(string type, string key)
485+
{
486+
var hasher = new MD5CryptoServiceProvider();
487+
hasher.ComputeHash(Encoding.Default.GetBytes(key));
488+
var hash = String.Join("", hasher.Hash.Select(h => h.ToString("x2")));
489+
490+
var directory = Path.Combine(UserSettings.UserDataFolder, "Cache", type);
491+
492+
if (!Directory.Exists(directory))
493+
Directory.CreateDirectory(directory);
494+
495+
return Path.Combine(directory, hash + ".cache-or");
496+
}
497+
482498
PropertyInfo GetProperty(string name)
483499
{
484500
return GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

Source/Orts.Simulation/Simulation/Timetables/ProcessTimetable.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,9 +1193,7 @@ public AIPath LoadPath(string pathstring, out bool validPath)
11931193
{
11941194
// try to load binary path if required
11951195
bool binaryloaded = false;
1196-
string formedpathFilefullBinary = Path.Combine(Path.GetDirectoryName(formedpathFilefull), "OpenRails");
1197-
formedpathFilefullBinary = Path.Combine(formedpathFilefullBinary, Path.GetFileNameWithoutExtension(formedpathFilefull));
1198-
formedpathFilefullBinary = Path.ChangeExtension(formedpathFilefullBinary, "or-binpat");
1196+
var formedpathFilefullBinary = simulator.Settings.GetCacheFilePath("Path", formedpathFilefull);
11991197

12001198
if (BinaryPaths && File.Exists(formedpathFilefullBinary))
12011199
{
@@ -1210,14 +1208,22 @@ public AIPath LoadPath(string pathstring, out bool validPath)
12101208
try
12111209
{
12121210
var infpath = new BinaryReader(new FileStream(formedpathFilefullBinary, FileMode.Open, FileAccess.Read));
1213-
outPath = new AIPath(simulator.TDB, simulator.TSectionDat, infpath);
1214-
infpath.Close();
1215-
1216-
if (outPath.Nodes != null)
1211+
var cachePath = infpath.ReadString();
1212+
if (cachePath != formedpathFilefull)
12171213
{
1218-
Paths.Add(formedpathFilefull, outPath);
1219-
binaryloaded = true;
1214+
Trace.TraceWarning($"Expected cache file for '{formedpathFilefull}'; got '{cachePath}' in {formedpathFilefullBinary}");
1215+
}
1216+
else
1217+
{
1218+
outPath = new AIPath(simulator.TDB, simulator.TSectionDat, infpath);
1219+
1220+
if (outPath.Nodes != null)
1221+
{
1222+
Paths.Add(formedpathFilefull, outPath);
1223+
binaryloaded = true;
1224+
}
12201225
}
1226+
infpath.Close();
12211227
}
12221228
catch
12231229
{
@@ -1267,6 +1273,7 @@ public AIPath LoadPath(string pathstring, out bool validPath)
12671273
try
12681274
{
12691275
var outfpath = new BinaryWriter(new FileStream(formedpathFilefullBinary, FileMode.Create));
1276+
outfpath.Write(formedpathFilefull);
12701277
outPath.Save(outfpath);
12711278
outfpath.Close();
12721279
}

Source/RunActivity/Viewer3D/Processes/GameStateRunActivity.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
using System.IO;
3434
using System.Linq;
3535
using System.Runtime.InteropServices;
36-
using System.Security.Cryptography;
37-
using System.Text;
3836
using System.Text.RegularExpressions;
3937
using System.Windows.Forms;
4038

@@ -174,27 +172,27 @@ internal override void Load()
174172
case "start":
175173
case "start-profile":
176174
InitLogging(settings, args);
177-
InitLoading(args);
175+
InitLoading(settings, args);
178176
Start(settings, acttype, data);
179177
break;
180178
case "resume":
181179
InitLogging(settings, args);
182-
InitLoading(args);
180+
InitLoading(settings, args);
183181
Resume(settings, data);
184182
break;
185183
case "replay":
186184
InitLogging(settings, args);
187-
InitLoading(args);
185+
InitLoading(settings, args);
188186
Replay(settings, data);
189187
break;
190188
case "replay_from_save":
191189
InitLogging(settings, args);
192-
InitLoading(args);
190+
InitLoading(settings, args);
193191
ReplayFromSave(settings, data);
194192
break;
195193
case "test":
196194
InitLogging(settings, args, true);
197-
InitLoading(args);
195+
InitLoading(settings, args);
198196
Test(settings, data);
199197
break;
200198

@@ -816,23 +814,16 @@ void InitLogging(UserSettings settings, string[] args, bool appendLog)
816814
DateTime LoadingNextSample = DateTime.MinValue;
817815
float LoadedPercent = -1;
818816

819-
void InitLoading(string[] args)
817+
void InitLoading(UserSettings settings, string[] args)
820818
{
821819
// Get the initial bytes; this is subtracted from all further uses of GetProcessBytesLoaded().
822820
LoadingBytesInitial = GetProcessBytesLoaded();
823821

824822
// We hash together all the appropriate arguments to the program as the key for the loading cache file.
825823
// Arguments without a '.' in them and those starting '/' are ignored, since they are explore activity
826824
// configuration (time, season, etc.) or flags like /test which we don't want to change on.
827-
LoadingDataKey = String.Join(" ", args.Where(a => a.Contains('.') && !a.StartsWith("-") && !a.StartsWith("/")).ToArray()).ToLowerInvariant();
828-
var hash = new MD5CryptoServiceProvider();
829-
hash.ComputeHash(Encoding.Default.GetBytes(LoadingDataKey));
830-
var loadingHash = String.Join("", hash.Hash.Select(h => h.ToString("x2")).ToArray());
831-
var dataPath = Path.Combine(UserSettings.UserDataFolder, "Load Cache");
832-
LoadingDataFilePath = Path.Combine(dataPath, loadingHash + ".dat");
833-
834-
if (!Directory.Exists(dataPath))
835-
Directory.CreateDirectory(dataPath);
825+
LoadingDataKey = String.Join(" ", args.Where(a => a.Contains('.') && !a.StartsWith("-") && !a.StartsWith("/"))).ToLowerInvariant();
826+
LoadingDataFilePath = settings.GetCacheFilePath("Load", LoadingDataKey);
836827

837828
var loadingTime = 0;
838829
var bytesExpected = new long[LoadingSampleCount];

0 commit comments

Comments
 (0)