Skip to content

Commit ac263dd

Browse files
authored
fix: bracket issue in bulk file (#13)
1 parent ec6b78f commit ac263dd

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

Runtime/Core/Snapshots/Upload/UploadManager.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private async Task<string> CreateBulkFileInBackgroundAsync(SnapshotSession sessi
146146
_cancellationTokenSource.Token.ThrowIfCancellationRequested();
147147

148148
var chunk = chunks[i];
149-
StreamChunkContent(writer, chunk.chunkFilePath, i > 0);
149+
BulkChunkContent(writer, chunk.chunkFilePath, i != chunks.Count - 1);
150150
chunk.status = ChunkMeta.ChunkStatus.Uploading;
151151
ChunkManager.UpdateChunkMeta(chunk);
152152
}
@@ -171,7 +171,7 @@ private async Task<string> CreateBulkFileInBackgroundAsync(SnapshotSession sessi
171171
}, _cancellationTokenSource.Token);
172172
}
173173

174-
private void StreamChunkContent(StreamWriter writer, string chunkFilePath, bool addComma)
174+
private void BulkChunkContent(StreamWriter writer, string chunkFilePath, bool addComma)
175175
{
176176
try
177177
{
@@ -184,42 +184,29 @@ private void StreamChunkContent(StreamWriter writer, string chunkFilePath, bool
184184
using (var chunkFileStream = new FileStream(chunkFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
185185
using (var chunkReader = new StreamReader(chunkFileStream, Encoding.UTF8))
186186
{
187-
var firstChar = chunkReader.Peek();
188-
if (firstChar == '[')
189-
{
190-
chunkReader.Read();
191-
}
187+
string line;
192188

193-
if (addComma)
189+
var startBracket = chunkReader.ReadLine();
190+
if (startBracket != "[")
194191
{
195-
writer.Write(",\n");
192+
Debug.LogError($"Corrupted chunk file: {chunkFilePath}");
193+
return;
196194
}
197195

198-
var buffer = new char[4096];
199-
int charsRead;
200-
bool foundClosingBracket = false;
201-
202-
while ((charsRead = chunkReader.Read(buffer, 0, buffer.Length)) > 0)
196+
while ((line = chunkReader.ReadLine()) != null)
203197
{
204-
var content = new string(buffer, 0, charsRead);
205-
206-
var closingBracketIndex = content.LastIndexOf(']');
207-
if (closingBracketIndex >= 0)
198+
if (line == "]" && chunkReader.EndOfStream)
208199
{
209-
var contentToWrite = content.Substring(0, closingBracketIndex);
210-
writer.Write(contentToWrite);
211-
foundClosingBracket = true;
212-
break;
213200
}
214201
else
215202
{
216-
writer.Write(content);
203+
writer.Write(line);
217204
}
218205
}
219206

220-
if (!foundClosingBracket)
207+
if (addComma)
221208
{
222-
Debug.LogWarning($"No closing bracket found in chunk file: {chunkFilePath}");
209+
writer.Write(",\n");
223210
}
224211
}
225212
}

0 commit comments

Comments
 (0)