Report local Word snapshot results and handle missing ranges
This commit is contained in:
parent
a0c2d252e9
commit
726a3d5bc2
@ -76,7 +76,10 @@ namespace PlotDirector.WordAddIn
|
|||||||
link.Received += message => {
|
link.Received += message => {
|
||||||
var description = message.Status + (string.IsNullOrEmpty(message.ChapterTitle) ? "" : " · " + message.ChapterTitle)
|
var description = message.Status + (string.IsNullOrEmpty(message.ChapterTitle) ? "" : " · " + message.ChapterTitle)
|
||||||
+ (string.IsNullOrEmpty(message.SceneTitle) ? "" : " / " + message.SceneTitle);
|
+ (string.IsNullOrEmpty(message.SceneTitle) ? "" : " / " + message.SceneTitle);
|
||||||
|
if (message.Status == "awaiting-structure" && message.SnapshotId == Guid.Empty) return;
|
||||||
if (message.Status != "ok" || !string.IsNullOrEmpty(message.ChapterTitle)) PostToUi(() => {
|
if (message.Status != "ok" || !string.IsNullOrEmpty(message.ChapterTitle)) PostToUi(() => {
|
||||||
|
if (message.Status == "awaiting-structure")
|
||||||
|
description = "Local structure refreshed. No chapter or scene found at the cursor.";
|
||||||
if(message.Status!="ok"&&message.Status!="ready"&&message.Status!="awaiting-structure")
|
if(message.Status!="ok"&&message.Status!="ready"&&message.Status!="awaiting-structure")
|
||||||
{
|
{
|
||||||
var failed=documents.Values.FirstOrDefault(d=>d.Session==message.SessionId&&d.Revision==message.Revision);
|
var failed=documents.Values.FirstOrDefault(d=>d.Session==message.SessionId&&d.Revision==message.Revision);
|
||||||
@ -199,6 +202,7 @@ namespace PlotDirector.WordAddIn
|
|||||||
public void RequestSnapshot()
|
public void RequestSnapshot()
|
||||||
{
|
{
|
||||||
if (active == null) return;
|
if (active == null) return;
|
||||||
|
lastResult = ConnectionState == "connected" ? "Refreshing local structure…" : "Waiting for the local agent";
|
||||||
active.Revision++; active.NeedsSnapshot = true; CancelExtraction(active);
|
active.Revision++; active.NeedsSnapshot = true; CancelExtraction(active);
|
||||||
link.Register(Descriptor(active)); ScheduleSnapshot(active);
|
link.Register(Descriptor(active)); ScheduleSnapshot(active);
|
||||||
}
|
}
|
||||||
@ -206,7 +210,7 @@ namespace PlotDirector.WordAddIn
|
|||||||
{
|
{
|
||||||
if (doc.Extraction != null || ConnectionState != "connected") return;
|
if (doc.Extraction != null || ConnectionState != "connected") return;
|
||||||
doc.Extraction = new Extraction { Snapshot = Guid.NewGuid(), Revision=doc.Revision };
|
doc.Extraction = new Extraction { Snapshot = Guid.NewGuid(), Revision=doc.Revision };
|
||||||
scheduler.Post("extract:"+doc.Session,() => ExtractBatch(doc));
|
if (!scheduler.Post("extract:"+doc.Session,() => ExtractBatch(doc))) doc.Extraction = null;
|
||||||
}
|
}
|
||||||
private void ExtractBatch(LocalDocument doc)
|
private void ExtractBatch(LocalDocument doc)
|
||||||
{
|
{
|
||||||
@ -243,8 +247,10 @@ namespace PlotDirector.WordAddIn
|
|||||||
}
|
}
|
||||||
catch(COMException)
|
catch(COMException)
|
||||||
{ busyFailures++;CancelExtraction(doc);doc.NeedsSnapshot=true;scheduler.PrioritizeAuthor(TimeSpan.FromSeconds(1)); }
|
{ busyFailures++;CancelExtraction(doc);doc.NeedsSnapshot=true;scheduler.PrioritizeAuthor(TimeSpan.FromSeconds(1)); }
|
||||||
catch(InvalidOperationException)
|
catch(InvalidOperationException error)
|
||||||
{ CancelExtraction(doc);doc.NeedsSnapshot=false;lastResult="Snapshot stopped: unsupported range/marker density. Use Diagnostics; Word remains usable."; }
|
{ CancelExtraction(doc);doc.NeedsSnapshot=false;lastResult="Snapshot stopped: " + error.Message; }
|
||||||
|
catch(NullReferenceException)
|
||||||
|
{ CancelExtraction(doc);doc.NeedsSnapshot=false;lastResult="Snapshot stopped while reading Word.";throw; }
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
lastBatchMs=watch.Elapsed.TotalMilliseconds;Metric("snapshot-batch",lastBatchMs);
|
lastBatchMs=watch.Elapsed.TotalMilliseconds;Metric("snapshot-batch",lastBatchMs);
|
||||||
@ -260,8 +266,10 @@ namespace PlotDirector.WordAddIn
|
|||||||
if(extraction.Position>=extraction.ParagraphEnd)
|
if(extraction.Position>=extraction.ParagraphEnd)
|
||||||
{
|
{
|
||||||
point=Com(()=>doc.Document.Range(extraction.Position,extraction.Position));
|
point=Com(()=>doc.Document.Range(extraction.Position,extraction.Position));
|
||||||
paragraphs=Com(()=>point.Paragraphs);paragraph=Com(()=>paragraphs[1]);
|
paragraphs=Com(()=>point?.Paragraphs);paragraph=Com(()=>paragraphs?[1]);
|
||||||
|
if (paragraph == null) throw new InvalidOperationException("Word did not return a paragraph.");
|
||||||
paragraphRange=Com(()=>paragraph.Range);
|
paragraphRange=Com(()=>paragraph.Range);
|
||||||
|
if (paragraphRange == null) throw new InvalidOperationException("Word did not return a paragraph range.");
|
||||||
extraction.ParagraphEnd=Com(()=>paragraphRange.End);
|
extraction.ParagraphEnd=Com(()=>paragraphRange.End);
|
||||||
extraction.OutlineLevel=(int)Com(()=>paragraph.OutlineLevel);
|
extraction.OutlineLevel=(int)Com(()=>paragraph.OutlineLevel);
|
||||||
extraction.ParagraphIndex++;
|
extraction.ParagraphIndex++;
|
||||||
@ -269,7 +277,9 @@ namespace PlotDirector.WordAddIn
|
|||||||
}
|
}
|
||||||
var start=extraction.Position;var end=Math.Min(doc.End,Math.Min(extraction.ParagraphEnd,start+ProtocolVersion.MaxParagraphCharacters));
|
var start=extraction.Position;var end=Math.Min(doc.End,Math.Min(extraction.ParagraphEnd,start+ProtocolVersion.MaxParagraphCharacters));
|
||||||
if(end<=start)throw new InvalidOperationException("Non-advancing Word range.");
|
if(end<=start)throw new InvalidOperationException("Non-advancing Word range.");
|
||||||
textRange=Com(()=>doc.Document.Range(start,end));var text=Com(()=>textRange.Text) ?? "";
|
textRange=Com(()=>doc.Document.Range(start,end));
|
||||||
|
if (textRange == null) throw new InvalidOperationException("Word did not return the requested text range.");
|
||||||
|
var text=Com(()=>textRange.Text) ?? "";
|
||||||
extraction.Position=end;
|
extraction.Position=end;
|
||||||
return new Message{Kind="snapshot-fragment",SessionId=doc.Session,Revision=doc.Revision,SnapshotId=extraction.Snapshot,
|
return new Message{Kind="snapshot-fragment",SessionId=doc.Session,Revision=doc.Revision,SnapshotId=extraction.Snapshot,
|
||||||
Metrics=DrainMetrics(),QueueDepth=link.QueueDepth,Fragment=new ParagraphFragment{ParagraphIndex=extraction.ParagraphIndex,Start=start,End=end,OutlineLevel=extraction.OutlineLevel,
|
Metrics=DrainMetrics(),QueueDepth=link.QueueDepth,Fragment=new ParagraphFragment{ParagraphIndex=extraction.ParagraphIndex,Start=start,End=end,OutlineLevel=extraction.OutlineLevel,
|
||||||
@ -306,17 +316,19 @@ namespace PlotDirector.WordAddIn
|
|||||||
if(parent!=null)throw new InvalidOperationException("Content-control ancestry exceeds bounded read.");
|
if(parent!=null)throw new InvalidOperationException("Content-control ancestry exceeds bounded read.");
|
||||||
}
|
}
|
||||||
finally{Release(parent);}
|
finally{Release(parent);}
|
||||||
controls=Com(()=>range.ContentControls);var count=Com(()=>controls.Count);
|
controls=Com(()=>range.ContentControls);var count=Com(()=>controls?.Count ?? 0);
|
||||||
if(count>8)throw new InvalidOperationException("Too many nested controls for a bounded marker read.");
|
if(count>8)throw new InvalidOperationException("Too many nested controls for a bounded marker read.");
|
||||||
for(var index=1;index<=count;index++)
|
for(var index=1;index<=count;index++)
|
||||||
{ Word.ContentControl control=null;try{control=Com(()=>controls[index]);var tag=Com(()=>control.Tag);if(!string.IsNullOrEmpty(tag)&&tag.Length<=128)tags.Add(tag);}finally{Release(control);} }
|
{ Word.ContentControl control=null;try{control=Com(()=>controls[index]);var tag=Com(()=>control?.Tag);if(!string.IsNullOrEmpty(tag)&&tag.Length<=128)tags.Add(tag);}finally{Release(control);} }
|
||||||
bookmarks=Com(()=>range.Bookmarks);var hidden=Com(()=>bookmarks.ShowHidden);
|
bookmarks=Com(()=>range.Bookmarks);
|
||||||
|
if (bookmarks == null) return tags.Distinct().ToArray();
|
||||||
|
var hidden=Com(()=>bookmarks.ShowHidden);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Com(()=>{bookmarks.ShowHidden=true;return true;});count=Com(()=>bookmarks.Count);
|
Com(()=>{bookmarks.ShowHidden=true;return true;});count=Com(()=>bookmarks.Count);
|
||||||
if(count>8)throw new InvalidOperationException("Too many bookmarks for a bounded marker read.");
|
if(count>8)throw new InvalidOperationException("Too many bookmarks for a bounded marker read.");
|
||||||
for(var index=1;index<=count;index++)
|
for(var index=1;index<=count;index++)
|
||||||
{ Word.Bookmark bookmark=null;try{object key=index;bookmark=Com(()=>bookmarks.get_Item(ref key));var name=Com(()=>bookmark.Name);if(name.Length<=128)tags.Add(name);}finally{Release(bookmark);} }
|
{ Word.Bookmark bookmark=null;try{object key=index;bookmark=Com(()=>bookmarks.get_Item(ref key));var name=Com(()=>bookmark?.Name);if(!string.IsNullOrEmpty(name)&&name.Length<=128)tags.Add(name);}finally{Release(bookmark);} }
|
||||||
}
|
}
|
||||||
finally {Com(()=>{bookmarks.ShowHidden=hidden;return true;});}
|
finally {Com(()=>{bookmarks.ShowHidden=hidden;return true;});}
|
||||||
if(tags.Distinct().Count()>16)throw new InvalidOperationException("Marker density exceeds bounded read.");
|
if(tags.Distinct().Count()>16)throw new InvalidOperationException("Marker density exceeds bounded read.");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user