Skip to content

Latest commit

 

History

History
245 lines (207 loc) · 4.9 KB

README.md

File metadata and controls

245 lines (207 loc) · 4.9 KB

Relational Tables

Maintainability Coverage Status Actions Status

A simple game development spreadsheet conversion tool.

Introduce

Often, working in the game industry requires that the game designer take edit of the game configuration.
However, the difficulty of facing various data table-read associations was the reason for the creation of this tool.

Usage

If you have a table like it.

Field1 Field2 Field3
1 2 3

Define the class.

public class TestConfig1 
{        
    public int Field1;
    public string Field2;
    public float Field3;
}

Query from database.

var db = new Regulus.RelationalTables.Database(new IRowProvidable[] { /*Data source ...*/ });
var config = db.Query<TestConfig1>().First();
// config.Field1 == 1
// config.Field2 == "2"
// config.Field3 == 3f

Types of support

Separate tables.

Field1 Field2 Field3
1 2 3
public class TestConfig1 
{        
    public int Field1;
    public string Field2;
    public float Field3;
}

Array field.

Field1 Field2 Field3
1 2 3
public class TestConfig1 
{ 
    [Regulus.RelationalTables.Attributes.Merge("Field1","Field2","Field3")]       
    public int[] Field;    
}

Related Table

Table A

Field1 Field2 Field3
1 2 3

Table B

Field1 Field2
1 1
public class TableA : Regulus.RelationalTables.IRelatable
{        
    public int Field1;
    public string Field2;
    public float Field3;

    bool IRelatable.Compare(string val)
    {
        int outVal;
        if (int.TryParse(val, out outVal))
        {
            return outVal == Field1;
        }
        return false;
    }

}

public class TableB
{        
    public int Field1;
    public TestConfig1 Field2;    
}

Inversely related
Table A

Table2s
1

Table B

Owner Data
1 1
1 2
class TableA
{            
    [Attributes.InverselyRelated]
    public TableB[] Table2s;
}

class TableB : IRelatable
{
    public int Owner;
    public int Data;
    bool IRelatable.Compare(string val)
    {
        int owner;
        if (int.TryParse(val , out owner))
        {
            return owner == Owner;
        }
        return false;
    }
}

Inversely related by column
Table A

Id
1

Table B

Owner Data
1 1
1 2
class TableA
{            
    [Attributes.InverselyRelatedByColumn("Id")]
    public TableB[] Table2s;
}

class TableB : IRelatable
{
    public int Owner;
    public int Data;
    bool IRelatable.Compare(string val)
    {
        int owner;
        if (int.TryParse(val , out owner))
        {
            return owner == Owner;
        }
        return false;
    }
}

Custom Parser

public class CustomFieldParser : Regulus.RelationalTables.Attributes.FieldParser
{
    public override object Parse(FieldInfo field, IEnumerable<Column> row, ITableable table)
    {
        //todo : Implement your method... 
    }
}

public class Table
{
    [CustomFieldParser()]
    public int Field1;        
}

Create database

Whether you are using Excel, CSV, Google Sheet, or another spreadsheet source, you just need to implement the following interface...

Row provider

namespace Regulus.RelationalTables.Raw
{
    public interface IRowProvidable
    {
        Type GetTableType();
        IEnumerable<IColumnProvidable> GetRows();
    }
}

Column provider

namespace Regulus.RelationalTables.Raw
{
    public interface IColumnProvidable
    {
        IEnumerable<Column> GetColumns();
    }
}

New a database

var db = new Regulus.RelationalTables.Database(/* IRowProvidable */);

Serialization

Because it takes time to set up the database, it is recommended that the tables be serialized at edit time for runtime use.

Write to stream

var stream = // file stream.
var db = new Regulus.RelationalTables.Database(/* IRowProvidable */);
var converter = new Regulus.RelationalTables.Serialization.BinaryConverter();
converter.WriteToStream(db.Tables, stream , /* ITypeProviable */);

Read from stream

var stream = // file stream.
var converter = new Regulus.RelationalTables.Serialization.BinaryConverter();
var tables = converter.ReadFromStream(stream, /* ITypeProviable */);
var db = new Regulus.RelationalTables.Database(tables);

ITypeProviable
Need to implement ITypeProviable to provide type information.