Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Aug 31, 2024
1 parent 65f8de3 commit 8228f5e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
14 changes: 7 additions & 7 deletions ATL/AudioData/IO/MKA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ private List<Tuple<long, ulong>> readSeekHead(EBMLReader reader)
return result;
}

private Tuple<long, ulong> readSeek(EBMLReader reader)
private static Tuple<long, ulong> readSeek(EBMLReader reader)
{
var seekOffset = reader.Position;

Expand Down Expand Up @@ -924,7 +924,7 @@ private bool hasWritablePics(TagData tag)
return tag.Pictures.Any(isPictureWritable);
}

private bool hasWritableChapters(TagData tag)
private static bool hasWritableChapters(TagData tag)
{
return tag.Chapters != null && tag.Chapters.Count > 0;
}
Expand Down Expand Up @@ -1045,7 +1045,7 @@ private int writeTags(Stream w, TagData data)
return result;
}

private void writeTag(Stream w, int typeCode, ISet<Tuple<string, string>> fields)
private static void writeTag(Stream w, int typeCode, ISet<Tuple<string, string>> fields)
{
using MemoryStream tagStream = new MemoryStream();

Expand All @@ -1059,7 +1059,7 @@ private void writeTag(Stream w, int typeCode, ISet<Tuple<string, string>> fields
EBMLHelper.WriteElt(w, ID_TAG, tagStream);
}

private void writeSimpleTag(Stream w, string code, string value)
private static void writeSimpleTag(Stream w, string code, string value)
{
using MemoryStream memStream = new MemoryStream();

Expand Down Expand Up @@ -1089,7 +1089,7 @@ private int writeAttachments(Stream w, TagData data)
return result;
}

private int writeAttachedFile(Stream w, byte[] data, string mimeType, PictureInfo.PIC_TYPE type, string description)
private static int writeAttachedFile(Stream w, byte[] data, string mimeType, PictureInfo.PIC_TYPE type, string description)
{
var name = type.Equals(PictureInfo.PIC_TYPE.Front) ? "cover" : type.ToString().ToLower();
switch (ImageUtils.GetImageFormatFromMimeType(mimeType))
Expand Down Expand Up @@ -1174,7 +1174,7 @@ private void writeEditionEntry(Stream w, TagData data)
w.Seek(finalOffset, SeekOrigin.Begin);
}

private void writeChapterAtom(Stream w, ChapterInfo data)
private static void writeChapterAtom(Stream w, ChapterInfo data)
{
using MemoryStream memStream = new MemoryStream();

Expand All @@ -1197,7 +1197,7 @@ private void writeChapterAtom(Stream w, ChapterInfo data)
EBMLHelper.WriteElt(w, ID_CHAPTERATOM, memStream);
}

private void writeChapterDisplay(Stream w, string data)
private static void writeChapterDisplay(Stream w, string data)
{
using MemoryStream memStream = new MemoryStream();

Expand Down
10 changes: 10 additions & 0 deletions ATL/AudioData/MetaDataIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,22 @@ private bool rewriteHeaders(Stream s, Zone zone)
return true;
}

/// <summary>
/// Indicate whether the given field can be written to the current file, according to their properties
/// </summary>
/// <param name="fieldInfo">Field to test</param>
/// <returns>True if the given field can be written; false if not</returns>
protected bool isMetaFieldWritable(MetaFieldInfo fieldInfo)
{
return (fieldInfo.TagType.Equals(MetaDataIOFactory.TagType.ANY) ||
fieldInfo.TagType.Equals(getImplementedTagType())) && !fieldInfo.MarkedForDeletion;
}

/// <summary>
/// Indicate whether the given picture can be written to the current file, according to their properties
/// </summary>
/// <param name="picInfo">Picture to test</param>
/// <returns>True if the given picture can be written; false if not</returns>
protected bool isPictureWritable(PictureInfo picInfo)
{
// Picture has either to be supported, or to come from the right tag standard
Expand Down
39 changes: 11 additions & 28 deletions ATL/AudioData/Utils/FileStructureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ public enum TYPE
/// </summary>
public readonly bool IsLittleEndian;
/// <summary>
/// True if header value is stored using EBML VINT (variable-size integer) convention; false for classic representation
/// </summary>
public readonly bool IsEbmlVint;
/// <summary>
/// Zone where the header is located physically
/// </summary>
public readonly string ParentZone;
Expand All @@ -116,10 +112,10 @@ public enum TYPE
/// <summary>
/// Constructs a new frame header using the given field values
/// </summary>
public FrameHeader(TYPE type, long position, object value, bool isLittleEndian = true, bool isEbmlVint = false, string parentZone = "", string valueZone = "", long relativityOffset = 0)
public FrameHeader(TYPE type, long position, object value, bool isLittleEndian = true, string parentZone = "", string valueZone = "", long relativityOffset = 0)
{
Type = type; Position = position; Value = value; IsLittleEndian = isLittleEndian;
IsEbmlVint = isEbmlVint; ParentZone = parentZone; ValueZone = valueZone;
ParentZone = parentZone; ValueZone = valueZone;
RelativityOffset = relativityOffset;
}
}
Expand Down Expand Up @@ -361,32 +357,23 @@ public void RemoveZonesStartingWith(string name)
/// </summary>
public void AddCounter(long position, object value, string zone = DEFAULT_ZONE_NAME, string parentZone = "")
{
addZoneHeader(zone, FrameHeader.TYPE.Counter, position, value, isLittleEndian, false, parentZone);
addZoneHeader(zone, FrameHeader.TYPE.Counter, position, value, isLittleEndian, parentZone);
}

/// <summary>
/// Record a new Size-type header using the given fields and attach it to the zone of given name
/// </summary>
public void AddSize(long position, object value, string zone = DEFAULT_ZONE_NAME, string parentZone = "")
{
addZoneHeader(zone, FrameHeader.TYPE.Size, position, value, isLittleEndian, false, parentZone);
}

/// <summary>
/// Record a new Size-type header using the given fields and attach it to the zone of given name
/// Representation of the header's value will use EBML VINT
/// </summary>
public void AddVintSize(long position, object value, string zone = DEFAULT_ZONE_NAME, string parentZone = "")
{
addZoneHeader(zone, FrameHeader.TYPE.Size, position, value, false, true, parentZone);
addZoneHeader(zone, FrameHeader.TYPE.Size, position, value, isLittleEndian, parentZone);
}

/// <summary>
/// Record a new Index-type header using the given fields and attach it to the zone of given name
/// </summary>
public void AddIndex(long position, object value, bool relative = false, string zone = DEFAULT_ZONE_NAME, string parentZone = "")
{
addZoneHeader(zone, relative ? FrameHeader.TYPE.RelativeIndex : FrameHeader.TYPE.Index, position, value, isLittleEndian, false, parentZone);
addZoneHeader(zone, relative ? FrameHeader.TYPE.RelativeIndex : FrameHeader.TYPE.Index, position, value, isLittleEndian, parentZone);
}

/// <summary>
Expand All @@ -398,7 +385,7 @@ public void AddPostProcessingIndex(long pendingPosition, object value, bool rela

string zoneName = POST_PROCESSING_ZONE_NAME + "." + ++postProcessingIndex;
AddZone(finalPosition, 0, zoneName);
addZoneHeader(zoneName, relative ? FrameHeader.TYPE.RelativeIndex : FrameHeader.TYPE.Index, finalPosition, value, isLittleEndian, false, parentZone, valueZone, relativityOffset);
addZoneHeader(zoneName, relative ? FrameHeader.TYPE.RelativeIndex : FrameHeader.TYPE.Index, finalPosition, value, isLittleEndian, parentZone, valueZone, relativityOffset);
}

/// <summary>
Expand All @@ -413,10 +400,10 @@ public void DeclareZone(string zone)
/// <summary>
/// Record a new header using the given fields and attach it to the zone of given name
/// </summary>
private void addZoneHeader(string zone, FrameHeader.TYPE type, long position, object value, bool iisLittleEndian, bool isEbmlVint = false, string parentZone = "", string valueZone = "", long relativityOffset = 0)
private void addZoneHeader(string zone, FrameHeader.TYPE type, long position, object value, bool iisLittleEndian, string parentZone = "", string valueZone = "", long relativityOffset = 0)
{
if (!zones.ContainsKey(zone)) DeclareZone(zone);
zones[zone].Headers.Add(new FrameHeader(type, position, value, iisLittleEndian, isEbmlVint, parentZone, valueZone, relativityOffset));
zones[zone].Headers.Add(new FrameHeader(type, position, value, iisLittleEndian, parentZone, valueZone, relativityOffset));
}

/// <summary>
Expand Down Expand Up @@ -447,9 +434,8 @@ private void updateAllHeadersAtPosition(long position, FrameHeader.TYPE type, ob
/// <param name="value">Reference value</param>
/// <param name="delta">Value to add</param>
/// <param name="updatedValue">Updated value (out parameter; will be returned as same type as reference value)</param>
/// <param name="isEbmlVint">True if the result has to be encoded using the EBML VINT convention</param>
/// <returns>Resulting value after the addition, encoded into an array of bytes, as the same type of the reference value</returns>
private static byte[] addToValue(object value, long delta, out object updatedValue, bool isEbmlVint)
private static byte[] addToValue(object value, long delta, out object updatedValue)
{
switch (value)
{
Expand All @@ -470,7 +456,6 @@ private static byte[] addToValue(object value, long delta, out object updatedVal
return BitConverter.GetBytes((uint)updatedValue);
case long l:
updatedValue = l + delta;
if (isEbmlVint) return EBMLHelper.EncodeVint((ulong)(long)updatedValue, false); // Yes, that _is_ a successive double cast
return BitConverter.GetBytes((long)updatedValue);
// Need to tweak because ulong + int is illegal according to the compiler
case ulong value1:
Expand All @@ -483,8 +468,6 @@ private static byte[] addToValue(object value, long delta, out object updatedVal
{
updatedValue = value1 - (ulong)-delta;
}

if (isEbmlVint) return EBMLHelper.EncodeVint((ulong)updatedValue, false);
return BitConverter.GetBytes((ulong)updatedValue);
}
default:
Expand Down Expand Up @@ -655,14 +638,14 @@ public bool RewriteHeaders(

s.Seek(header.Position + offsetPositionCorrection, SeekOrigin.Begin);

value = addToValue(header.Value, delta, out updatedValue, header.IsEbmlVint);
value = addToValue(header.Value, delta, out updatedValue);

if (null == value) throw new NotSupportedException("Value type not supported for " + zoneName + "@" + header.Position + " : " + header.Value.GetType());

// The very same frame header is referenced from another frame and must be updated to its new value
updateAllHeadersAtPosition(header.Position, header.Type, updatedValue);

if (!header.IsLittleEndian && !header.IsEbmlVint) Array.Reverse(value);
if (!header.IsLittleEndian) Array.Reverse(value);

s.Write(value, 0, value.Length);
}
Expand Down

0 comments on commit 8228f5e

Please sign in to comment.