|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Gil Barbosa Reis |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | +using System.IO; |
| 23 | +using SQLite.Csv; |
| 24 | +using UnityEditor.AssetImporters; |
| 25 | +using UnityEngine; |
| 26 | + |
| 27 | +namespace SQLite.Editor.Csv |
| 28 | +{ |
| 29 | + [ScriptedImporter(0, null, new[] { "csv" })] |
| 30 | + public class SQLiteAssetCsvImporter : ScriptedImporter |
| 31 | + { |
| 32 | + [Header("SQLite asset options")] |
| 33 | + [Tooltip("Name of the table that will be created for holding the CSV data inside the database.")] |
| 34 | + [SerializeField] private string _tableName = "data"; |
| 35 | + |
| 36 | + [Tooltip("Flags controlling how the SQLite connection should be opened. 'ReadWrite' and 'Create' flags will be ignored, since SQLite assets are read-only.")] |
| 37 | + [SerializeField] private SQLiteOpenFlags _openFlags = SQLiteOpenFlags.ReadOnly; |
| 38 | + |
| 39 | + [Tooltip("Whether to store DateTime properties as ticks (true) or strings (false).")] |
| 40 | + [SerializeField] private bool _storeDateTimeAsTicks = true; |
| 41 | + |
| 42 | + [Tooltip("Name of the file created for the database inside Streaming Assets folder during builds.\n\n" |
| 43 | + + "If empty, the database bytes will be stored in the asset itself.\n\n" |
| 44 | + + "Loading databases from Streaming Assets is not supported in Android and WebGL platforms.")] |
| 45 | + [SerializeField] private string _streamingAssetsPath; |
| 46 | + |
| 47 | + |
| 48 | + [Header("CSV options")] |
| 49 | + [Tooltip("Which separator character will be used when parsing the CSV file.")] |
| 50 | + [SerializeField] private CsvReader.SeparatorChar _CSVSeparator = CsvReader.SeparatorChar.Comma; |
| 51 | + |
| 52 | + [Tooltip("If true, the original CSV file will also be imported as a TextAsset")] |
| 53 | + [SerializeField] private bool _importCSVTextAsset = false; |
| 54 | + |
| 55 | + [Header("Additional SQL")] |
| 56 | + [Tooltip("SQL script that will be run before reading CSV data. Use this for configuring the generated database using PRAGMAs like 'page_size'.")] |
| 57 | + [SerializeField, Multiline] private string _SQLBeforeReadingCSV = ""; |
| 58 | + |
| 59 | + [Tooltip("SQL script that will be run after reading CSV data. Use this for changing the table's schema, creating indices, etc.")] |
| 60 | + [SerializeField, Multiline] private string _SQLAfterReadingCSV = ""; |
| 61 | + |
| 62 | + public override void OnImportAsset(AssetImportContext ctx) |
| 63 | + { |
| 64 | + SQLiteAsset asset; |
| 65 | + using (var tempDb = new SQLiteConnection("")) |
| 66 | + using (var file = File.OpenRead(assetPath)) |
| 67 | + using (var stream = new StreamReader(file)) |
| 68 | + { |
| 69 | + if (!string.IsNullOrWhiteSpace(_SQLBeforeReadingCSV)) |
| 70 | + { |
| 71 | + tempDb.Execute(_SQLBeforeReadingCSV); |
| 72 | + } |
| 73 | + tempDb.ImportCsvToTable(_tableName, stream, _CSVSeparator); |
| 74 | + if (!string.IsNullOrWhiteSpace(_SQLAfterReadingCSV)) |
| 75 | + { |
| 76 | + tempDb.Execute(_SQLAfterReadingCSV); |
| 77 | + } |
| 78 | + |
| 79 | + asset = tempDb.SerializeToAsset(null, _openFlags, _storeDateTimeAsTicks, _streamingAssetsPath); |
| 80 | + } |
| 81 | + ctx.AddObjectToAsset("sqlite", asset); |
| 82 | + ctx.SetMainObject(asset); |
| 83 | + |
| 84 | + if (_importCSVTextAsset) |
| 85 | + { |
| 86 | + var textAsset = new TextAsset(File.ReadAllText(assetPath)) |
| 87 | + { |
| 88 | + name = $"{Path.GetFileNameWithoutExtension(assetPath)}", |
| 89 | + }; |
| 90 | + ctx.AddObjectToAsset("text", textAsset); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments