-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #685 from b-editor/fix-clipboard
タイムラインの要素のコピー操作を修正
- Loading branch information
Showing
6 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
|
||
using Beutl.Utilities; | ||
|
||
namespace Beutl.Services; | ||
|
||
public static class CoreObjectReborn | ||
{ | ||
private const int DefaultGuidStringSize = 36; | ||
private const int BufferSizeDefault = 16 * 1024; | ||
|
||
private static void RebornCore<T>(T obj, PooledArrayBufferWriter<byte> output) | ||
where T : class, ICoreObject, new() | ||
{ | ||
var searcher = new ObjectSearcher(obj, v => v is ICoreObject); | ||
|
||
Guid[] ids = searcher.SearchAll() | ||
.Cast<ICoreObject>() | ||
.Select(v => v.Id) | ||
.Distinct() | ||
.ToArray(); | ||
|
||
// JsonObjectに変換 | ||
var jsonObject = new JsonObject(); | ||
obj.WriteToJson(jsonObject); | ||
|
||
// UTF-8に書き込む | ||
JsonSerializerOptions options = JsonHelper.SerializerOptions; | ||
var writerOptions = new JsonWriterOptions | ||
{ | ||
Encoder = options.Encoder, | ||
Indented = options.WriteIndented, | ||
MaxDepth = options.MaxDepth | ||
}; | ||
|
||
using (var writer = new Utf8JsonWriter(output, writerOptions)) | ||
{ | ||
jsonObject.WriteTo(writer, options); | ||
} | ||
|
||
// Idを置き換える | ||
Span<byte> buffer = PooledArrayBufferWriter<byte>.GetArray(output).AsSpan().Slice(0, output.WrittenCount); | ||
Span<byte> oldStr = stackalloc byte[DefaultGuidStringSize]; | ||
Span<byte> newStr = stackalloc byte[DefaultGuidStringSize]; | ||
foreach (Guid oldId in ids) | ||
{ | ||
Guid newId = Guid.NewGuid(); | ||
GuidToUtf8(oldId, oldStr); | ||
GuidToUtf8(newId, newStr); | ||
Span<byte> localBuffer = buffer; | ||
|
||
int index; | ||
while ((index = localBuffer.IndexOf(oldStr)) >= 0) | ||
{ | ||
localBuffer = localBuffer.Slice(index); | ||
newStr.CopyTo(localBuffer); | ||
} | ||
} | ||
} | ||
|
||
public static void Reborn<T>(T obj, out T newInstance) | ||
where T : class, ICoreObject, new() | ||
{ | ||
using var output = new PooledArrayBufferWriter<byte>(BufferSizeDefault); | ||
RebornCore(obj, output); | ||
|
||
Span<byte> buffer = PooledArrayBufferWriter<byte>.GetArray(output).AsSpan().Slice(0, output.WrittenCount); | ||
|
||
JsonObject jsonObj = JsonNode.Parse(buffer)!.AsObject(); | ||
var instance = new T(); | ||
instance.ReadFromJson(jsonObj); | ||
|
||
newInstance = instance; | ||
} | ||
|
||
public static void Reborn<T>(T obj, out string json) | ||
where T : class, ICoreObject, new() | ||
{ | ||
using var output = new PooledArrayBufferWriter<byte>(BufferSizeDefault); | ||
RebornCore(obj, output); | ||
|
||
Span<byte> buffer = PooledArrayBufferWriter<byte>.GetArray(output).AsSpan().Slice(0, output.WrittenCount); | ||
json = Encoding.UTF8.GetString(buffer); | ||
} | ||
|
||
private static void GuidToUtf8(Guid id, Span<byte> utf8) | ||
{ | ||
Span<char> utf16 = stackalloc char[DefaultGuidStringSize]; | ||
|
||
if (!id.TryFormat(utf16, out _)) | ||
throw new Exception("Failed to 'Guid.TryFormat'."); | ||
|
||
Encoding.UTF8.GetBytes(utf16, utf8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters