Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions source/ObjectViewer/ProgramS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ internal static class Program {
/// <summary>The number of failed auto-reloads</summary>
internal static int AutoReloadFailureCounter = 0;
private static double reloadCheckTimer;
/// <summary>The list of loaded objects, cached to allow partial reloads</summary>
internal static UnifiedObject[] LoadedObjects = new UnifiedObject[0];

// mouse
internal static Vector3 MouseCameraPosition = Vector3.Zero;
Expand Down Expand Up @@ -162,6 +164,7 @@ internal static void Main(string[] args)
if (filesToLoad.Count != 0)
{
Files = filesToLoad;
LoadedObjects = new UnifiedObject[Files.Count];
}
}

Expand Down Expand Up @@ -311,8 +314,24 @@ internal static void MouseMovement()
}
}

internal static void RefreshObjects(bool autoReload = false)
internal static void RefreshObjects(int fileIndex = -1)
{
// If a specific file index is provided, only reload that file from disk.
// Otherwise (or if the array is out of sync), do a full reload.
if (fileIndex >= 0 && fileIndex < Files.Count && LoadedObjects.Length == Files.Count) {
try {
if (CurrentHost.LoadObject(Files[fileIndex], Encoding.UTF8, out UnifiedObject o)) {
LoadedObjects[fileIndex] = o;
}
} catch {
// If failed, we don't return here but continue to the reset/refresh logic
// which will handle error reporting.
}
} else {
LoadedObjects = new UnifiedObject[Files.Count];
fileIndex = -1; // Force full load of all objects below
}

LightingRelative = -1.0;
Renderer.Reset();
Game.Reset();
Expand All @@ -323,6 +342,7 @@ internal static void RefreshObjects(bool autoReload = false)
{
if(Files[i].EndsWith(".dat", StringComparison.InvariantCultureIgnoreCase) || Files[i].EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase) || Files[i].EndsWith(".cfg", StringComparison.InvariantCultureIgnoreCase) || Files[i].EndsWith(".con", StringComparison.InvariantCultureIgnoreCase))
{
// Trains are still reloaded fully as they are complex collections
string currentTrain = Files[i];
if (currentTrain.EndsWith("extensions.cfg", StringComparison.InvariantCultureIgnoreCase))
{
Expand Down Expand Up @@ -370,19 +390,24 @@ internal static void RefreshObjects(bool autoReload = false)
}
else
{
if (CurrentHost.LoadObject(Files[i], Encoding.UTF8, out UnifiedObject o))
{
o.CreateObject(Vector3.Zero, 0.0, 0.0, 0.0);
if (fileIndex == -1 || fileIndex == i) {
if (CurrentHost.LoadObject(Files[i], Encoding.UTF8, out UnifiedObject o)) {
LoadedObjects[i] = o;
}
}
if (LoadedObjects[i] != null) {
LoadedObjects[i].CreateObject(Vector3.Zero, 0.0, 0.0, 0.0);
}

}

}
catch (Exception ex)
{
if (!autoReload || AutoReloadFailureCounter > 5)
// Check if we are in an auto-reload context (passed from CheckFileChanges) via a flag if needed,
// but here we use fileIndex != -1 as a proxy for auto-reload.
if (fileIndex == -1 || AutoReloadFailureCounter > 5)
{
if (autoReload)
if (fileIndex != -1)
{
// failure counter must be above 5
Interface.CurrentOptions.AutoReloadObjects = false;
Expand Down Expand Up @@ -450,7 +475,7 @@ internal static void CheckFileChanges(double timeElapsed)
}
if (System.IO.File.GetLastWriteTimeUtc(Files[i]) > LastReloadTime)
{
RefreshObjects();
RefreshObjects(i);
return;
}
}
Expand Down
17 changes: 13 additions & 4 deletions source/OpenBveApi/Math/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,20 @@ public static double ToRadians(this double degrees)

private static string TrimInside(string Expression)
{
System.Text.StringBuilder Builder = new System.Text.StringBuilder(Expression.Length);
foreach (char c in Expression.Where(c => !char.IsWhiteSpace(c)))
if (string.IsNullOrEmpty(Expression)) return Expression;

// Simple loop to remove whitespace without LINQ or StringBuilder overhead
int n = Expression.Length;
char[] result = new char[n];
int count = 0;
for (int i = 0; i < n; i++)
{
Builder.Append(c);
} return Builder.ToString();
if (!char.IsWhiteSpace(Expression[i]))
{
result[count++] = Expression[i];
}
}
return count == n ? Expression : new string(result, 0, count);
}


Expand Down
104 changes: 64 additions & 40 deletions source/Plugins/Object.CsvB3d/Plugin.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
}
// read lines
List<string> Lines = System.IO.File.ReadAllLines(FileName, Encoding).ToList();
// Cache results of File.Exists locally during this load operation to avoid redundant disk IO.
// This is created fresh every time the object is loaded/reloaded, so it is safe against file additions/deletions between loads.
Dictionary<string, bool> fileExistsCache = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
if (enabledHacks.BveTsHacks && multiColumn)
{
/*
Expand Down Expand Up @@ -150,6 +153,9 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
List<Vector3> Normals = new List<Vector3>();
bool CommentStarted = false;
Color24? lastTransparentColor = null;
// HashSet to store face vertex indices to avoid O(N^2) search during Z-fighting check.
// Using a string key of sorted indices since MeshFace is a struct and we need a reliable key.
HashSet<string> faceCache = new HashSet<string>();
for (int i = 0; i < Lines.Count; i++) {
{
// Strip OpenBVE original standard comments
Expand Down Expand Up @@ -207,9 +213,10 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
}
}
}
// collect arguments
// collect arguments by splitting the line by commas
string[] Arguments = Lines[i].Split(new[] { ',' }, StringSplitOptions.None);
for (int j = 0; j < Arguments.Length; j++) {
// Trim each argument to remove leading/trailing whitespace
Arguments[j] = Arguments[j].Trim();
}
{
Expand Down Expand Up @@ -291,6 +298,7 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
Builder.Apply(ref Object, enabledHacks.BveTsHacks);
Builder = new MeshBuilder(currentHost);
Normals = new List<Vector3>();
faceCache.Clear(); // reset face cache for the new mesh builder
} break;
case B3DCsvCommands.AddVertex:
case B3DCsvCommands.Vertex:
Expand Down Expand Up @@ -519,45 +527,33 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
}
}

int[] vertexIndices = (int[])a.Clone();
Array.Sort(vertexIndices);
for (int k = 0; k < Builder.Faces.Count; k++)
{
/*
* Some BVE2 content declares a Face2 twice with the vertices in a differing order, e.g.
*
* Face2 0, 1, 3, 2
* Face2 1, 0, 2, 3
*
* Doing this in OpenBVE causes some very funky glitches with Z-fighting,
* as it attempts to render both faces in the same space
*
* With this hack, the lighting may be off but the Z-fighting is gone
* (BVE2 / BVE4 operate in a strict draw-order, so the most recent face is always on top,
* wheras OpenBVE has no fixed draw order)
*/
MeshFace currentFace = Builder.Faces[k];
if (!isFace2 || (currentFace.Flags & FaceFlags.Face2Mask) == 0)
{
continue;
}
/*
* Some BVE2 content declares a Face2 twice with the vertices in a differing order, e.g.
* Face2 0, 1, 3, 2
* Face2 1, 0, 2, 3
* Doing this in OpenBVE causes some very funky glitches with Z-fighting,
* as it attempts to render both faces in the same space
*
* With this hack, the lighting may be off but the Z-fighting is gone
* (BVE2 / BVE4 operate in a strict draw-order, so the most recent face is always on top,
* wheras OpenBVE has no fixed draw order)
*/

if (currentFace.Vertices.Length != a.Length)
{
continue;
}

int[] faceVertexIndices = new int[a.Length];
for (int v = 0; v < currentFace.Vertices.Length; v++)
// Optimization: Use a HashSet to track existing faces by their vertex indices.
// This avoids the O(N^2) loop through all previous faces in the MeshBuilder.
if (isFace2)
{
int[] vertexIndices = (int[])a.Clone();
Array.Sort(vertexIndices);
string faceKey = string.Join("|", vertexIndices);
if (faceCache.Contains(faceKey))
{
faceVertexIndices[v] = currentFace.Vertices[v].Index;
q = false;
}
Array.Sort(faceVertexIndices);
if (vertexIndices.SequenceEqual(faceVertexIndices))
else
{
q = false;
faceCache.Add(faceKey);
}

}
}
if (q) {
Expand Down Expand Up @@ -1167,7 +1163,14 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
tday = null;
}

if (!System.IO.File.Exists(tday))
// Cache results of File.Exists to avoid hitting the disk for the same file multiple times
bool tdayExists;
if (!fileExistsCache.TryGetValue(tday, out tdayExists))
{
tdayExists = System.IO.File.Exists(tday);
fileExistsCache[tday] = tdayExists;
}
if (!tdayExists)
{
bool hackFound = false;
if (enabledHacks.BveTsHacks)
Expand All @@ -1185,7 +1188,12 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
{
Arguments[0] = Arguments[0].Substring(7);
tday = Path.CombineFile(Path.GetDirectoryName(FileName), Arguments[0]);
if (System.IO.File.Exists(tday))
if (!fileExistsCache.TryGetValue(tday, out tdayExists))
{
tdayExists = System.IO.File.Exists(tday);
fileExistsCache[tday] = tdayExists;
}
if (tdayExists)
{
hackFound = true;
}
Expand All @@ -1195,7 +1203,12 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
{
Arguments[0] = Arguments[0].Substring(4);
tday = Path.CombineFile(Path.GetDirectoryName(FileName), Arguments[0]);
if (System.IO.File.Exists(tday))
if (!fileExistsCache.TryGetValue(tday, out tdayExists))
{
tdayExists = System.IO.File.Exists(tday);
fileExistsCache[tday] = tdayExists;
}
if (tdayExists)
{
hackFound = true;
}
Expand All @@ -1205,7 +1218,12 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
{
Arguments[0] = Arguments[0].Substring(3);
tday = Path.CombineFile(Path.GetDirectoryName(FileName), Arguments[0]);
if (System.IO.File.Exists(tday))
if (!fileExistsCache.TryGetValue(tday, out tdayExists))
{
tdayExists = System.IO.File.Exists(tday);
fileExistsCache[tday] = tdayExists;
}
if (tdayExists)
{
hackFound = true;
}
Expand Down Expand Up @@ -1245,7 +1263,13 @@ private static StaticObject ReadObject(string FileName, Encoding Encoding) {
}
}

if (!System.IO.File.Exists(tnight) && !ignoreAsInvalid) {
bool tnightExists;
if (!fileExistsCache.TryGetValue(tnight, out tnightExists))
{
tnightExists = System.IO.File.Exists(tnight);
fileExistsCache[tnight] = tnightExists;
}
if (!tnightExists && !ignoreAsInvalid) {
currentHost.AddMessage(MessageType.Error, true, "The NighttimeTexture " + tnight + " could not be found in " + cmd + " at line " + (i + 1).ToString(Culture) + " in file " + FileName);
tnight = null;
}
Expand Down