Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*.uxml text eol=lf
*.uss text eol=lf
*.sh text eol=lf
*.ps1 text eol=lf

# Exclude TextMesh Pro files from language statistics
"Assets/TextMesh Pro/**" linguist-generated=true linguist-vendored=true
71 changes: 71 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Docs

on:
pull_request:
paths:
- "Assets/Plugins/CSVLoader/Runtime/**/*.cs"
- "docs/**"
- ".github/workflows/docs.yml"
push:
branches: [main]
paths:
- "Assets/Plugins/CSVLoader/Runtime/**/*.cs"
- "docs/**"
- ".github/workflows/docs.yml"
release:
types: [published]
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: docs
cancel-in-progress: true

jobs:
build:
name: Build API site
runs-on: ubuntu-latest
container:
image: unityci/editor:ubuntu-6000.0.73f1-base-3
steps:
- name: Checkout
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

- name: Setup .NET
uses: actions/setup-dotnet@26b0ec14cb23fa6904739307f278c14f94c95bf1 # v5.4.0
with:
dotnet-version: "8.0.x"

- name: Install DocFX
run: dotnet tool install docfx --tool-path .tools --version 2.78.5

- name: Build documentation
env:
UnityEditorContents: /opt/unity/Editor/Data
run: |
dotnet restore docs/CSV4Unity.Docs.csproj
cd docs
../.tools/docfx metadata docfx.json
../.tools/docfx build docfx.json

- name: Upload Pages artifact
uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
with:
path: docs/_site

deploy:
name: Deploy to GitHub Pages
needs: build
if: github.event_name != 'pull_request'
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- name: Deploy
id: deployment
uses: actions/deploy-pages@cd2ce8fcbc39b97be8ca5fce6e763baed58fa128 # v5.0.0
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
ExportedObj/
.consulo/
*.csproj
!/docs/CSV4Unity.Docs.csproj
*.unityproj
*.sln
*.slnx
Expand Down Expand Up @@ -116,6 +117,12 @@ CaseSensitiveTest
*.coverage
*.coveragexml

# DocFX generated files
/docs/api/
/docs/_site/
/docs/bin/
/docs/obj/

# OS generated files
.DS_Store
Thumbs.db
Expand Down
34 changes: 33 additions & 1 deletion Assets/Plugins/CSVLoader/Runtime/CSVLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
namespace CSV4Unity
{
/// <summary>
/// UnityのTextAssetとPure C#のCSVコアを接続します。
/// Unityの<see cref="TextAsset"/>とPure C#のCSVコアを接続します。
/// </summary>
public static class CSVLoader
{
/// <summary>
/// TextAssetを解析し、ヘッダー名または列番号で参照できるドキュメントを返します。
/// </summary>
/// <param name="csvFile">解析するCSVを保持したTextAsset。</param>
/// <param name="options">解析方法。<see langword="null"/>の場合は既定値を使用します。</param>
/// <param name="dataName">ドキュメントの識別名。<see langword="null"/>の場合はTextAsset名を使用します。</param>
/// <returns>解析された読み取り専用ドキュメント。</returns>
/// <exception cref="ArgumentNullException"><paramref name="csvFile"/>が<see langword="null"/>です。</exception>
/// <exception cref="ArgumentException">区切り文字にダブルクォートまたは改行文字が指定されています。</exception>
/// <exception cref="CsvParseException">CSVの構文またはレコードの列数が不正です。</exception>
public static CsvDocument LoadDocument(
TextAsset csvFile,
CsvParseOptions options = null,
Expand All @@ -23,6 +30,13 @@ public static CsvDocument LoadDocument(
/// <summary>
/// CSV文字列を解析し、ヘッダー名または列番号で参照できるドキュメントを返します。
/// </summary>
/// <param name="csvText">解析するCSV文字列。</param>
/// <param name="options">解析方法。<see langword="null"/>の場合は既定値を使用します。</param>
/// <param name="dataName">ドキュメントの識別名。<see langword="null"/>の場合は空文字列を使用します。</param>
/// <returns>解析された読み取り専用ドキュメント。</returns>
/// <exception cref="ArgumentNullException"><paramref name="csvText"/>が<see langword="null"/>です。</exception>
/// <exception cref="ArgumentException">区切り文字にダブルクォートまたは改行文字が指定されています。</exception>
/// <exception cref="CsvParseException">CSVの構文またはレコードの列数が不正です。</exception>
public static CsvDocument LoadDocument(
string csvText,
CsvParseOptions options = null,
Expand All @@ -34,6 +48,15 @@ public static CsvDocument LoadDocument(
/// <summary>
/// TextAssetを解析し、Enumで列を指定できるテーブルを返します。
/// </summary>
/// <typeparam name="TField">CSVヘッダーと同名のフィールドを持つEnum型。</typeparam>
/// <param name="csvFile">解析するCSVを保持したTextAsset。</param>
/// <param name="options">解析方法。<see langword="null"/>の場合は既定値を使用します。</param>
/// <param name="dataName">ドキュメントの識別名。<see langword="null"/>の場合はTextAsset名を使用します。</param>
/// <returns>Enumで列を指定できるテーブル。</returns>
/// <exception cref="ArgumentNullException"><paramref name="csvFile"/>が<see langword="null"/>です。</exception>
/// <exception cref="ArgumentException">区切り文字にダブルクォートまたは改行文字が指定されています。</exception>
/// <exception cref="CsvParseException">CSVの構文またはレコードの列数が不正です。</exception>
/// <exception cref="CsvSchemaException">ヘッダーとEnumを一意に対応付けられません。</exception>
public static CsvTable<TField> LoadTable<TField>(
TextAsset csvFile,
CsvParseOptions options = null,
Expand All @@ -46,6 +69,15 @@ public static CsvTable<TField> LoadTable<TField>(
/// <summary>
/// CSV文字列を解析し、Enumで列を指定できるテーブルを返します。
/// </summary>
/// <typeparam name="TField">CSVヘッダーと同名のフィールドを持つEnum型。</typeparam>
/// <param name="csvText">解析するCSV文字列。</param>
/// <param name="options">解析方法。<see langword="null"/>の場合は既定値を使用します。</param>
/// <param name="dataName">ドキュメントの識別名。<see langword="null"/>の場合は空文字列を使用します。</param>
/// <returns>Enumで列を指定できるテーブル。</returns>
/// <exception cref="ArgumentNullException"><paramref name="csvText"/>が<see langword="null"/>です。</exception>
/// <exception cref="ArgumentException">区切り文字にダブルクォートまたは改行文字が指定されています。</exception>
/// <exception cref="CsvParseException">CSVの構文またはレコードの列数が不正です。</exception>
/// <exception cref="CsvSchemaException">ヘッダーとEnumを一意に対応付けられません。</exception>
public static CsvTable<TField> LoadTable<TField>(
string csvText,
CsvParseOptions options = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

namespace CSV4Unity
{
/// <summary>
/// CSVセルの文字列を要求された型へ変換できない場合に送出される例外です。
/// </summary>
public sealed class CsvConversionException : FormatException
{
/// <summary>変換できなかった値と変換先型を指定して例外を生成します。</summary>
/// <param name="value">変換できなかったCSVセルの文字列。</param>
/// <param name="targetType">要求された変換先型。</param>
/// <exception cref="NullReferenceException"><paramref name="targetType"/>が<see langword="null"/>です。</exception>
public CsvConversionException(string value, Type targetType)
: base($"CSV value '{value}' cannot be converted to {targetType.Name}.")
{
Value = value;
TargetType = targetType;
}

/// <summary>変換できなかったCSVセルの文字列を取得します。</summary>
public string Value { get; }

/// <summary>要求された変換先型を取得します。</summary>
public Type TargetType { get; }
}
}
56 changes: 56 additions & 0 deletions Assets/Plugins/CSVLoader/Runtime/Conversion/CsvValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ namespace CSV4Unity
/// </summary>
public static class CsvValueConverter
{
/// <summary>文字列をBoolean値へ変換します。</summary>
/// <param name="value">変換する文字列。</param>
/// <param name="result">変換に成功した場合の値。</param>
/// <returns>変換に成功した場合は<see langword="true"/>、それ以外は<see langword="false"/>。</returns>
/// <remarks><see cref="bool.TryParse(string, out bool)"/>と同じ文字列表現を受け付けます。</remarks>
public static bool TryConvertBoolean(ReadOnlySpan<char> value, out bool result)
{
return bool.TryParse(value, out result);
}

/// <summary>文字列を32ビット符号付き整数へ変換します。</summary>
/// <param name="value">変換する文字列。</param>
/// <param name="result">変換に成功した場合の値。</param>
/// <param name="formatProvider">数値形式。<see langword="null"/>の場合は<see cref="CultureInfo.InvariantCulture"/>を使用します。</param>
/// <returns>変換に成功した場合は<see langword="true"/>、それ以外は<see langword="false"/>。</returns>
/// <remarks><see cref="NumberStyles.Integer"/>として解析します。</remarks>
public static bool TryConvertInt32(
ReadOnlySpan<char> value,
out int result,
Expand All @@ -21,6 +32,12 @@ public static bool TryConvertInt32(
return int.TryParse(value, NumberStyles.Integer, formatProvider ?? CultureInfo.InvariantCulture, out result);
}

/// <summary>文字列を64ビット符号付き整数へ変換します。</summary>
/// <param name="value">変換する文字列。</param>
/// <param name="result">変換に成功した場合の値。</param>
/// <param name="formatProvider">数値形式。<see langword="null"/>の場合は<see cref="CultureInfo.InvariantCulture"/>を使用します。</param>
/// <returns>変換に成功した場合は<see langword="true"/>、それ以外は<see langword="false"/>。</returns>
/// <remarks><see cref="NumberStyles.Integer"/>として解析します。</remarks>
public static bool TryConvertInt64(
ReadOnlySpan<char> value,
out long result,
Expand All @@ -29,6 +46,12 @@ public static bool TryConvertInt64(
return long.TryParse(value, NumberStyles.Integer, formatProvider ?? CultureInfo.InvariantCulture, out result);
}

/// <summary>文字列を単精度浮動小数点数へ変換します。</summary>
/// <param name="value">変換する文字列。</param>
/// <param name="result">変換に成功した場合の値。</param>
/// <param name="formatProvider">数値形式。<see langword="null"/>の場合は<see cref="CultureInfo.InvariantCulture"/>を使用します。</param>
/// <returns>変換に成功した場合は<see langword="true"/>、それ以外は<see langword="false"/>。</returns>
/// <remarks><see cref="NumberStyles.Float"/>として解析します。</remarks>
public static bool TryConvertSingle(
ReadOnlySpan<char> value,
out float result,
Expand All @@ -37,6 +60,12 @@ public static bool TryConvertSingle(
return float.TryParse(value, NumberStyles.Float, formatProvider ?? CultureInfo.InvariantCulture, out result);
}

/// <summary>文字列を倍精度浮動小数点数へ変換します。</summary>
/// <param name="value">変換する文字列。</param>
/// <param name="result">変換に成功した場合の値。</param>
/// <param name="formatProvider">数値形式。<see langword="null"/>の場合は<see cref="CultureInfo.InvariantCulture"/>を使用します。</param>
/// <returns>変換に成功した場合は<see langword="true"/>、それ以外は<see langword="false"/>。</returns>
/// <remarks><see cref="NumberStyles.Float"/>として解析します。</remarks>
public static bool TryConvertDouble(
ReadOnlySpan<char> value,
out double result,
Expand All @@ -45,12 +74,29 @@ public static bool TryConvertDouble(
return double.TryParse(value, NumberStyles.Float, formatProvider ?? CultureInfo.InvariantCulture, out result);
}

/// <summary>文字列を指定型へ変換します。</summary>
/// <typeparam name="T">変換先型。</typeparam>
/// <param name="value">変換する文字列。</param>
/// <param name="formatProvider">数値および日時形式。<see langword="null"/>の場合は<see cref="CultureInfo.InvariantCulture"/>を使用します。</param>
/// <returns>変換された値。</returns>
/// <exception cref="CsvConversionException"><typeparamref name="T"/>へ変換できません。</exception>
/// <remarks>
/// string、bool、short、int、uint、long、ulong、float、double、decimal、
/// <see cref="DateTime"/>、<see cref="Guid"/>、Enum、およびこれらのNullable型をサポートします。
/// Enum名は大文字小文字を区別します。空文字列はNullable型の<see langword="null"/>へ変換されます。
/// </remarks>
public static T Convert<T>(ReadOnlySpan<char> value, IFormatProvider formatProvider = null)
{
if (TryConvert(value, out T result, formatProvider)) return result;
throw new CsvConversionException(value.ToString(), typeof(T));
}

/// <summary>文字列を指定型へ変換できるか確認します。</summary>
/// <param name="value">確認する文字列。</param>
/// <param name="targetType">変換先型。</param>
/// <param name="formatProvider">数値および日時形式。<see langword="null"/>の場合は<see cref="CultureInfo.InvariantCulture"/>を使用します。</param>
/// <returns>変換可能な場合は<see langword="true"/>、それ以外は<see langword="false"/>。</returns>
/// <exception cref="ArgumentNullException"><paramref name="targetType"/>が<see langword="null"/>です。</exception>
public static bool CanConvert(
ReadOnlySpan<char> value,
Type targetType,
Expand All @@ -73,6 +119,16 @@ public static bool CanConvert(
out _);
}

/// <summary>文字列を指定型へ変換します。</summary>
/// <typeparam name="T">変換先型。</typeparam>
/// <param name="value">変換する文字列。</param>
/// <param name="result">変換に成功した場合の値。失敗した場合は<see langword="default"/>。</param>
/// <param name="formatProvider">数値および日時形式。<see langword="null"/>の場合は<see cref="CultureInfo.InvariantCulture"/>を使用します。</param>
/// <returns>変換に成功した場合は<see langword="true"/>、それ以外は<see langword="false"/>。</returns>
/// <remarks>
/// 対応型とEnumの比較規則は<see cref="Convert{T}(ReadOnlySpan{char}, IFormatProvider)"/>と同じです。
/// 変換先型ごとの変換処理はジェネリックキャッシュへ保存されます。
/// </remarks>
public static bool TryConvert<T>(
ReadOnlySpan<char> value,
out T result,
Expand Down
Loading
Loading