Skip to content

Commit

Permalink
update samples and readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cathei committed Feb 5, 2022
1 parent 090502d commit 6fbb081
Show file tree
Hide file tree
Showing 27 changed files with 258 additions and 19 deletions.
13 changes: 11 additions & 2 deletions BakingSheet/Src/Internal/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ public PropertyMap(SheetConvertingContext context, Type sheetType, Func<Type, bo

private List<object> _indexes = new List<object>();

private HashSet<string> _warned = null;

public void SetValue(ISheetRow row, int arrIndex, string path, string value, Func<Type, string, object> converter)
{
Node node = null;
Expand All @@ -477,7 +479,7 @@ public void SetValue(ISheetRow row, int arrIndex, string path, string value, Fun
{
if (arrIndex != 0)
{
_context.Logger.LogError("There is multiple value for a single column");
_context.Logger.LogError("There is multiple value for a non-array column");
return;
}

Expand All @@ -487,7 +489,14 @@ public void SetValue(ISheetRow row, int arrIndex, string path, string value, Fun
{
if (Arr == null || !Arr.Child.HasSubpath(subpath))
{
_context.Logger.LogError("Column name is invalid", path);
_warned = _warned ?? new HashSet<string>();

if (!_warned.Contains(path))
{
_context.Logger.LogError("Column name is invalid");
_warned.Add(path);
}

return;
}

Expand Down
97 changes: 88 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ BakingSheet's core concept is controlling datasheet schema from C# code, make th
Also, it helps to avoid having source datasheet files or parsing libraries for production applications. BakingSheet supports JSON serialization by default.

## First Step
BakingSheet manages datasheet schema as C# code. `Sheet` class represents a table and `SheetRow` class represents a record. Below is example content of file `Items.xlsx`.
| Id | Name | Price |
|----------------|-------------------|-------|
| ITEM_LVUP001 | Warrior's Shield | 10000 |
| ITEM_LVUP002 | Mage's Staff | 10000 |
| ITEM_LVUP003 | Assassin's Dagger | 10000 |
| ITEM_POTION001 | Health Potion | 30 |
| ITEM_POTION002 | Mana Potion | 50 |
BakingSheet manages datasheet schema as C# code. `Sheet` class represents a table and `SheetRow` class represents a record. Below is example content of file `Items.xlsx`. Also, any column starts with $ will be considered as comment and ignored.
| Id | Name | Price | $Comment |
|----------------|-------------------|-------|------------|
| ITEM_LVUP001 | Warrior's Shield | 10000 | Warrior Lv up material |
| ITEM_LVUP002 | Mage's Staff | 10000 | Mage Lv up material |
| ITEM_LVUP003 | Assassin's Dagger | 10000 | Assassin Lv up material |
| ITEM_POTION001 | Health Potion | 30 | Heal 20 Hp |
| ITEM_POTION002 | Mana Potion | 50 | Heal 20 Mp |

Code below is corresponding BakingSheet class.
```csharp
Expand Down Expand Up @@ -169,8 +169,87 @@ public class ConstantSheet : Sheet<GameConstant, ConstantSheet.Row>
```
Note that properties without setter are not serialized. Alternatively you can use `[NonSerialized]` attribute.

## Using List Column
List columns are used for simple array.
| Id | Name | Monsters:1 | Monsters:2 | Monsters:3 | Items:1 | Items:2 |
| ---------- | ------------- | ---------- | ---------- | ---------- | -------------- | ------------ |
| DUNGEON001 | Easy Field | MONSTER001 | | | ITEM_POTION001 | ITEM_LVUP001 |
| DUNGEON002 | Expert Zone | MONSTER001 | MONSTER002 | | ITEM_POTION002 | ITEM_LVUP002 |
| DUNGEON003 | Dragon’s Nest | MONSTER003 | MONSTER004 | MONSTER005 | ITEM_LVUP003 | |

```csharp
public class DungeonSheet : Sheet<DungeonSheet.Row>
{
public class Row : SheetRow
{
public string Name { get; private set; }

public List<MonsterSheet.Reference> Monsters { get; private set; }
public List<ItemSheet.Reference> Items { get; private set; }
}
}
```
Use it as simple as just including a column has type implmenting `IList<T>`.
Since spreadsheet is designer's area, index on sheet is 1-based. So be aware when you access it from code.

## Using Dictionary Column
Dictionary columns are used when key-based access of value is needed.
| Id | Name | Texts:Greeting | Texts:Purchasing | Texts:Leaving |
| ------ | ------------- | ----------------- | ---------------- | ----------------- |
| NPC001 | Fat Baker | Morning traveler! | Thank you! | Come again! |
| NPC002 | Blacksmith | G'day! | Good choice. | Take care. |
| NPC003 | Potion Master | What do you want? | Take it already. | Don't come again. |

```csharp
public enum Situation
{
Greeting,
Purchasing,
Leaving
}

public class NpcSheet : Sheet<NpcSheet.Row>
{
public class Row : SheetRow
{
public string Name { get; private set; }

public Dictionary<Situation, string> Texts { get; private set; }
}
}
```
Use it as simple as just including a column has type implmenting `IDictionary<TKey, TValue>`.

## Using Nested Type Column
Nested type columns are used for complex structure.
| Id | Name | Texts:Greeting | Texts:Purchasing | Texts:Leaving |
| ------ | ------------- | ----------------- | ---------------- | ----------------- |
| NPC001 | Fat Baker | Morning traveler! | Thank you! | Come again! |
| NPC002 | Blacksmith | G'day! | Good choice. | Take care. |
| NPC003 | Potion Master | What do you want? | Take it already. | Don't come again. |

```csharp
public struct SituationText
{
public string Greeting { get; private set; }
public string Purchasing { get; private set; }
public string Leaving { get; private set; }
}

public class NpcSheet : Sheet<NpcSheet.Row>
{
public class Row : SheetRow
{
public string Name { get; private set; }

public SituationText Texts { get; private set; }
}
}
```
Note that the content of datasheet is just same as when using Dictionary column. The data type of column determines how BakingSheet reads the column.

## Using Row Array
Row arrays are used for simple nested structure. Below is example content of file `Heroes.xlsx`.
Row arrays are used for 2-dimentional structure. Below is example content of file `Heroes.xlsx`.
| Id | Name | Strength | Inteligence | Vitality | StatMultiplier | RequiredExp | RequiredItem |
|---------|----------|----------|-------------|----------|----------------|-------------|--------------|
| HERO001 | Warrior | 100 | 80 | 140 | 1 | 0 | |
Expand Down
15 changes: 9 additions & 6 deletions UnityProject/Assets/PackageGeneration/PackageGenerationTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ public static void GeneratePackage()
// GITHUB_REF = refs/heads/v1.X.X
if (githubRef != null)
PlayerSettings.bundleVersion = githubRef.Substring(11);

var outputPath = Path.Combine(
Path.GetDirectoryName(Directory.GetCurrentDirectory()), "Build",
$"BakingSheet.{PlayerSettings.bundleVersion}.unitypackage");

AssetDatabase.ExportPackage(new string [] {
AssetDatabase.ExportPackage(new string[] {
"Assets/Plugins/BakingSheet"
}, outputPath, ExportPackageOptions.Recurse);
}, GetPackagePath("BakingSheet"), ExportPackageOptions.Recurse);

Debug.Log("Generating Unity Package Completed");
}

private static string GetPackagePath(string title)
{
return Path.Combine(
Path.GetDirectoryName(Directory.GetCurrentDirectory()), "Build",
$"{title}.{PlayerSettings.bundleVersion}.unitypackage");
}
}
}
Binary file not shown.
8 changes: 8 additions & 0 deletions UnityProject/Assets/Plugins/BakingSheet/Core.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Cathei.BakingSheet.Examples
{
public class DungeonSheet : Sheet<DungeonSheet.Row>
{
public class Row : SheetRow
{
public string Name { get; private set; }

public List<MonsterSheet.Reference> Monsters { get; private set; }
public List<ItemSheet.Reference> Items { get; private set; }
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Cathei.BakingSheet.Examples
{
public enum Situation
{
Greeting,
Purchasing,
Leaving
}

public struct SituationText
{
public string Greeting { get; private set; }
public string Purchasing { get; private set; }
public string Leaving { get; private set; }
}

public class NpcSheet : Sheet<NpcSheet.Row>
{
public class Row : SheetRow
{
public string Name { get; private set; }

public SituationText Texts { get; private set; }
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public SheetContainer(ILogger logger) : base(logger) {}
public HeroSheet Heroes { get; private set; }
public ItemSheet Items { get; private set; }
public MonsterSheet Monsters { get; private set; }
public DungeonSheet Dungeons { get; private set; }
public NpcSheet Npcs { get; private set; }
}
}
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions UnityProject/Assets/StreamingAssets/Excel/Dungeons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"Name":"Easy Field","Monsters":["MONSTER001"],"Items":["ITEM_POTION001","ITEM_LVUP001"],"Id":"DUNGEON001"},{"Name":"Expert Zone","Monsters":["MONSTER001","MONSTER002"],"Items":["ITEM_POTION002","ITEM_LVUP002"],"Id":"DUNGEON002"},{"Name":"Dragon’s Nest","Monsters":["MONSTER003","MONSTER004","MONSTER005"],"Items":["ITEM_LVUP003"],"Id":"DUNGEON003"}]
7 changes: 7 additions & 0 deletions UnityProject/Assets/StreamingAssets/Excel/Dungeons.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion UnityProject/Assets/StreamingAssets/Excel/Monsters.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"Name":"Slime","Damage":0,"DropGold":40,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[{"DropItem":null,"DropItemProb":0.0}],"Id":"MONSTER001"},{"Name":"Skeleton","Damage":0,"DropGold":200,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[{"DropItem":null,"DropItemProb":0.0}],"Id":"MONSTER002"},{"Name":"Orc","Damage":0,"DropGold":800,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[{"DropItem":null,"DropItemProb":0.0}],"Id":"MONSTER003"},{"Name":"Gargoyle","Damage":0,"DropGold":2000,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[{"DropItem":"ITEM_POTION001","DropItemProb":0.5},{"DropItem":"ITEM_POTION002","DropItemProb":0.5}],"Id":"MONSTER004"},{"Name":"Dragon","Damage":0,"DropGold":10000,"SpawnEventStart":"2020-10-01T00:00:00Z","SpawnEventEnd":"2020-10-31T23:59:59Z","Arr":[{"DropItem":"ITEM_LVUP001","DropItemProb":0.1},{"DropItem":"ITEM_LVUP002","DropItemProb":0.1},{"DropItem":"ITEM_LVUP003","DropItemProb":0.1}],"Id":"MONSTER005"}]
[{"Name":"Slime","Damage":0,"DropGold":40,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[],"Id":"MONSTER001"},{"Name":"Skeleton","Damage":0,"DropGold":200,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[],"Id":"MONSTER002"},{"Name":"Orc","Damage":0,"DropGold":800,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[],"Id":"MONSTER003"},{"Name":"Gargoyle","Damage":0,"DropGold":2000,"SpawnEventStart":null,"SpawnEventEnd":null,"Arr":[{"DropItem":"ITEM_POTION001","DropItemProb":0.5},{"DropItem":"ITEM_POTION002","DropItemProb":0.5}],"Id":"MONSTER004"},{"Name":"Dragon","Damage":0,"DropGold":10000,"SpawnEventStart":"2020-10-01T00:00:00Z","SpawnEventEnd":"2020-10-31T23:59:59Z","Arr":[{"DropItem":"ITEM_LVUP001","DropItemProb":0.1},{"DropItem":"ITEM_LVUP002","DropItemProb":0.1},{"DropItem":"ITEM_LVUP003","DropItemProb":0.1}],"Id":"MONSTER005"}]
1 change: 1 addition & 0 deletions UnityProject/Assets/StreamingAssets/Excel/Npcs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"Name":"Fat Baker","Texts":{"Greeting":"Morning traveler!","Purchasing":"Thank you!","Leaving":"Come again!"},"Id":"NPC001"},{"Name":"Blacksmith","Texts":{"Greeting":"G’day!","Purchasing":"Good choice.","Leaving":"Take care."},"Id":"NPC002"},{"Name":"Potion Master","Texts":{"Greeting":"What do you want?","Purchasing":"Take it already.","Leaving":"Don’t come again."},"Id":"NPC003"}]
7 changes: 7 additions & 0 deletions UnityProject/Assets/StreamingAssets/Excel/Npcs.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6fbb081

Please sign in to comment.