Skip to content

Commit

Permalink
Documetn engine update. Path delimiter change.
Browse files Browse the repository at this point in the history
  • Loading branch information
LostBeard committed Aug 23, 2024
1 parent 53e3795 commit 7c35234
Show file tree
Hide file tree
Showing 19 changed files with 1,071 additions and 408 deletions.
6 changes: 3 additions & 3 deletions BlazorEBMLViewer/Components/BinaryElementCellEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
mem.Position = 0;
var length = mem.Length;
// If this is:
// \Segment\Attachments\AttachedFile\FileData
// /Segment/Attachments/AttachedFile/FileData
// then the filename is located here
// \Segment\Attachments\AttachedFile\FileName
// /Segment/Attachments/AttachedFile/FileName
var filename = Element.Parent!.ReadString("FileName");
if (string.IsNullOrWhiteSpace(filename))
{
Expand All @@ -53,7 +53,7 @@
var filename = file.Name;
// offer to set the filename field if it exists
var fileNameElement = Element.Parent!.GetElement<UTF8Element>("FileName");
var isMkvAttachedFile = Element.Path == @"\Segment\Attachments\AttachedFile\FileData";
var isMkvAttachedFile = Element.Path == @"/Segment/Attachments/AttachedFile/FileData";
if (fileNameElement != null || isMkvAttachedFile)
{
var confirm = await DialogService.Confirm("Set FileName field?");
Expand Down
2 changes: 1 addition & 1 deletion BlazorEBMLViewer/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RadzenMenu>
<RadzenMenuItem Text="File">
<RadzenMenuItem Text="New" Icon="add_circle_outline">
@foreach (var schema in EBMLSchemaService.SchemaSet.Schemas.Values)
@foreach (var schema in EBMLSchemaService.Parser.Schemas.Values)
{
<RadzenMenuItem Click="@(() => NewDocument(schema))" Text="@schema.DocType" Icon="schema"></RadzenMenuItem>
}
Expand Down
44 changes: 26 additions & 18 deletions BlazorEBMLViewer/Pages/Home.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
using BlazorEBMLViewer.Layout;
using BlazorEBMLViewer.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Radzen;
using SpawnDev.BlazorJS;
using SpawnDev.BlazorJS.JSObjects;
using SpawnDev.BlazorJS.Toolbox;
using SpawnDev.EBML;
using SpawnDev.EBML.Elements;
using SpawnDev.EBML.Elements;
using File = SpawnDev.BlazorJS.JSObjects.File;

namespace BlazorEBMLViewer.Pages
Expand All @@ -20,8 +18,8 @@ public partial class Home : IDisposable
public SpawnDev.EBML.Document? Document { get; set; }
public string? ActiveContainerTypeName => ActiveContainer?.GetType().Name;
public MasterElement? ActiveContainer { get; set; }
public string Path => ActiveContainer?.Path ?? "";
bool CanGoUp => Path.Split('\\', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Length > 0;
public string Path => ActiveContainer?.InstancePath ?? "";
bool CanGoUp => ActiveContainer?.Parent != null;
[Inject]
BlazorJSRuntime JS { get; set; }
[Inject]
Expand Down Expand Up @@ -53,15 +51,15 @@ async Task NewDocument(Schema schema)
DocumentBusy = true;
StateHasChanged();
await Task.Delay(50);
Document = new SpawnDev.EBML.Document(schema.DocType, EBMLSchemaService.SchemaSet, filename);
Document = EBMLSchemaService.Parser.CreateDocument(schema.DocType, filename);
Document.OnElementAdded += Document_OnElementAdded;
Document.OnElementRemoved += Document_OnElementRemoved;
Document.OnChanged += Document_OnChanged;
MainLayoutService.Title = Document.Filename;
ActiveContainer = Document;
DocumentBusy = false;
StateHasChanged();
await SetPath(@"\\");
await SetPath(Document);
}
private void Document_OnChanged(IEnumerable<BaseElement> elements)
{
Expand Down Expand Up @@ -90,16 +88,24 @@ async Task SetPath(string path)
DocumentBusy = true;
StateHasChanged();
await Task.Delay(50);
var element = Document?.GetContainer(path);
if (element is MasterElement source)
if (string.IsNullOrEmpty(path) || path.Trim(EBMLParser.PathDelimiters) == "")
{
ActiveContainer = source;
await SetPath(Document);
}
else
{
var element = Document?.GetContainer(path);
if (element != null)
{
await SetPath(element);
}
}
DocumentBusy = false;
StateHasChanged();
}
async Task SetPath(BaseElement element)
{
if (element == null) return;
DocumentBusy = true;
StateHasChanged();
await Task.Delay(50);
Expand All @@ -112,10 +118,10 @@ async Task SetPath(BaseElement element)
}
async Task GoUp()
{
var parts = Path.Split('\\', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (parts.Length == 0) return;
var newPath = "\\" + string.Join('\\', parts.Take(parts.Length - 1));
await SetPath(newPath);
if (ActiveContainer?.Parent != null)
{
await SetPath(ActiveContainer.Parent);
}
}
IEnumerable<ContextMenuItem> GetAddElementOptions()
{
Expand Down Expand Up @@ -158,14 +164,16 @@ IEnumerable<ContextMenuItem> GetAddElementOptions()
}
void AddElementClicked(MenuItemEventArgs args)
{
if (ActiveContainer == null) return;
if (ActiveContainer == null || Document == null) return;
ContextMenuService.Close();
if (args.Value is List<SchemaElement> missing)
{
Document.DisableDocumentEngines();
foreach (var addable in missing)
{
ActiveContainer.AddElement(addable);
}
Document.EnabledDocumentEngines();
}
else if (args.Value is SchemaElement addable)
{
Expand Down Expand Up @@ -212,12 +220,12 @@ async Task ShowOpenFileDialogFallback()
await Task.Delay(50);
var arrayBuffer = await file.ArrayBuffer();
var fileStream = new ArrayBufferStream(arrayBuffer);
Document = new SpawnDev.EBML.Document(fileStream, EBMLSchemaService.SchemaSet, file.Name);
Document = EBMLSchemaService.Parser.ParseDocument(fileStream, file.Name);
MainLayoutService.Title = file.Name;
ActiveContainer = Document;
DocumentBusy = false;
StateHasChanged();
await SetPath(@"\\");
await SetPath(Document);
}
async Task GridRowContextMenu(RowContextMenuArgs args)
{
Expand Down Expand Up @@ -284,12 +292,12 @@ async Task ShowOpenFileDialog()
using var f = await file.GetFile();
using var arrayBuffer = await f.ArrayBuffer();
var fileStream = new ArrayBufferStream(arrayBuffer);
Document = new SpawnDev.EBML.Document(fileStream, EBMLSchemaService.SchemaSet, file.Name);
Document = EBMLSchemaService.Parser.ParseDocument(fileStream, file.Name);
MainLayoutService.Title = file.Name;
ActiveContainer = Document;
DocumentBusy = false;
StateHasChanged();
await SetPath(@"\\");
await SetPath(Document);
}
finally
{
Expand Down
8 changes: 4 additions & 4 deletions BlazorEBMLViewer/Services/EBMLSchemaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ namespace BlazorEBMLViewer.Services
{
public class EBMLSchemaService
{
public EBMLParser SchemaSet { get; }
public EBMLParser Parser { get; }
public EBMLSchemaService()
{
SchemaSet = new EBMLParser();
SchemaSet.LoadDefaultSchemas();
SchemaSet.RegisterDocumentEngine<MatroskaDocumentEngine>();
Parser = new EBMLParser();
Parser.LoadDefaultSchemas();
Parser.RegisterDocumentEngine<MatroskaDocumentEngine>();
}
}
}
97 changes: 97 additions & 0 deletions ConsoleEBMLViewer/EBMLConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using SpawnDev.EBML;
//using SpawnDev.EBML.Elements;

//namespace ConsoleEBMLViewer
//{
// public class MenuOption
// {
// public string Name { get; set; }
// public Action Action { get; set; }
// public Func<bool> Available { get; set; }
// public bool IsGlobal { get; set; }
// }
// public class EBMLConsole
// {
// public EBMLParser? ebml = null;
// public Document? Document = null;
// public FileStream? fileStream = null;
// public string CurrentMenu = "";
// public Dictionary<string, List<MenuOption>> Menus = new Dictionary<string, List<MenuOption>>();
// public async Task RunAsync(string[]? args = null)
// {
// // Create the EBML parser with default configuration
// // default configuration supports matroska and webm reading and modification
// ebml = new EBMLParser();
// Menus.Add("main", new List<MenuOption>
// {
// new MenuOption{
// Name = "Main menu",
// Available = () => fileStream == null
// },
// new MenuOption{
// Name = "Open file",
// Available = () => fileStream == null
// },
// new MenuOption{
// Name = "Close file",
// Available = () => fileStream == null
// },
// });
// SetMenu();
// }
// void DrawMenu()
// {
// if (!Menus.TryGetValue(CurrentMenu, out var menu))
// {
// if (Menus.Count > 0)
// {
// SetMenu(Menus.First().Key);
// }
// return;
// }

// }
// void SetMenu(string menuName)
// {
// if (Menus.TryGetValue(menuName, out var menu))
// {
// CurrentMenu = menuName;
// DrawMenu();
// }
// }
// void OpenFile()
// {
// // get a stream containing an EBML document (or multiple documents)
// var fileStream = File.Open(@"TestData/Big_Buck_Bunny_180 10s.webm", FileMode.Open);
// // parse the EBML document stream (ParseDocuments can be used to parse all documents in the stream)
// var document = ebml.ParseDocument(fileStream);
// if (document != null)
// {
// Console.WriteLine($"DocType: {document.DocType}");
// // or using path
// Console.WriteLine($"DocType: {document.ReadString(@"/EBML/DocType")}");

// // Get an element using the path
// var durationElement = document.GetElement<FloatElement>(@"/Segment/Info/Duration");
// if (durationElement != null)
// {
// var duration = durationElement.Data;
// var durationTime = TimeSpan.FromMilliseconds(duration);
// Console.WriteLine($"Duration: {durationTime}");
// }
// }

// // Create a new matroska EBML file
// var matroskaDoc = ebml.CreateDocument("matroska");
// Console.WriteLine($"DocType: {matroskaDoc.DocType}");

// // ...

// }
// }
//}
6 changes: 3 additions & 3 deletions ConsoleEBMLViewer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
// default configuration supports matroska and webm reading and modification
var ebml = new EBMLParser();
// get a stream containing an EBML document (or multiple documents)
using var fileStream = File.Open(@"TestData\Big_Buck_Bunny_180 10s.webm", FileMode.Open);
using var fileStream = File.Open(@"TestData/Big_Buck_Bunny_180 10s.webm", FileMode.Open);
// parse the EBML document stream (ParseDocuments can be used to parse all documents in the stream)
var document = ebml.ParseDocument(fileStream);
if (document != null)
{
Console.WriteLine($"DocType: {document.DocType}");
// or using path
Console.WriteLine($"DocType: {document.ReadString(@"\EBML\DocType")}");
Console.WriteLine($"DocType: {document.ReadString(@"/EBML/DocType")}");

// Get an element using the path
var durationElement = document.GetElement<FloatElement>(@"\Segment\Info\Duration");
var durationElement = document.GetElement<FloatElement>(@"/Segment/Info/Duration");
if (durationElement != null)
{
var duration = durationElement.Data;
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ Note: The Matroska schema xml is currently also used for WebM ebml documents.
using SpawnDev.EBML;
using SpawnDev.EBML.Elements;

// Create the EBML parser with default configuration
// Create the EBML parser with the default configuration
// default configuration supports matroska and webm reading and modification
var ebml = new EBMLParser();
// get a stream containing an EBML document (or multiple documents)
using var fileStream = File.Open(@"TestData\Big_Buck_Bunny_180 10s.webm", FileMode.Open);
using var fileStream = File.Open(@"TestData/Big_Buck_Bunny_180 10s.webm", FileMode.Open);
// parse the EBML document stream (ParseDocuments can be used to parse all documents in the stream)
var document = ebml.ParseDocument(fileStream);
if (document != null)
{
Console.WriteLine($"DocType: {document.DocType}");
// or using path
Console.WriteLine($"DocType: {document.ReadString(@"\EBML\DocType")}");
Console.WriteLine($"DocType: {document.ReadString(@"/EBML/DocType")}");

// Get an element using the path
var durationElement = document.GetElement<FloatElement>(@"\Segment\Info\Duration");
var durationElement = document.GetElement<FloatElement>(@"/Segment/Info/Duration");
if (durationElement != null)
{
var duration = durationElement.Data;
Expand Down
Loading

0 comments on commit 7c35234

Please sign in to comment.