| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.IO; |
| | 7 | | using System.Text; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace GDX.DataTables.DataBinding.Formats |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A JavaScript Object Notation format. |
| | 14 | | /// </summary> |
| | 15 | | class JavaScriptObjectNotationFormat : FormatBase |
| | 16 | | { |
| 1 | 17 | | public JavaScriptObjectNotationFormat() |
| 1 | 18 | | { |
| | 19 | | // We can register the format this way because we automatically include these formats in the |
| | 20 | | // DataBindingsProvider as private members. |
| 1 | 21 | | DataBindingProvider.RegisterFormat(this); |
| 1 | 22 | | } |
| | 23 | |
|
| | 24 | | ~JavaScriptObjectNotationFormat() |
| 0 | 25 | | { |
| 0 | 26 | | DataBindingProvider.UnregisterFormat(this); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <inheritdoc /> |
| | 30 | | public override DateTime GetBindingTimestamp(string uri) |
| 0 | 31 | | { |
| 0 | 32 | | return File.GetLastWriteTimeUtc(uri); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <inheritdoc /> |
| | 36 | | public override string GetFilePreferredExtension() |
| 0 | 37 | | { |
| 0 | 38 | | return "json"; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| | 42 | | public override string GetFriendlyName() |
| 4 | 43 | | { |
| 4 | 44 | | return "JSON"; |
| 4 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <inheritdoc /> |
| | 48 | | public override bool IsFileHeader(string header) |
| 0 | 49 | | { |
| 0 | 50 | | return header.StartsWith("{\"DataVersion\":", StringComparison.OrdinalIgnoreCase); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <inheritdoc /> |
| | 54 | | public override string[] GetImportDialogExtensions() |
| 0 | 55 | | { |
| 0 | 56 | | return new[] { GetFriendlyName(), GetFilePreferredExtension() }; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| | 60 | | public override bool IsOnDiskFormat() |
| 4 | 61 | | { |
| 4 | 62 | | return true; |
| 4 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <inheritdoc /> |
| | 66 | | public override bool IsUri(string uri) |
| 0 | 67 | | { |
| 0 | 68 | | return uri.EndsWith(GetFilePreferredExtension(), StringComparison.OrdinalIgnoreCase); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <inheritdoc /> |
| | 72 | | public override SerializableTable Pull(string uri, ulong currentDataVersion, int currentStructuralVersion) |
| 0 | 73 | | { |
| 0 | 74 | | if (uri == null || !File.Exists(uri)) |
| 0 | 75 | | { |
| 0 | 76 | | return null; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | return JsonUtility.FromJson<SerializableTable>(File.ReadAllText(uri)); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | /// <inheritdoc /> |
| | 83 | | public override bool Push(string uri, SerializableTable serializableTable) |
| 0 | 84 | | { |
| 0 | 85 | | if (uri == null) |
| 0 | 86 | | { |
| 0 | 87 | | return false; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | File.WriteAllText(uri, JsonUtility.ToJson(serializableTable), new UTF8Encoding()); |
| 0 | 91 | | return File.Exists(uri); |
| 0 | 92 | | } |
| | 93 | | } |
| | 94 | | } |