////////////////////////////////////////////////////////////////////////////////
//
// @module Quick Save for Unity3D
// @author Michael Clayton
// @support clayton.inds+support@gmail.com
//
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using CI.QuickSave.Core.Storage;
namespace CI.QuickSave
{
public class QuickSaveCsv
{
///
/// Returns the number of rows in the csv file
///
public int RowCount => _values.Count;
private readonly List> _values;
private QuickSaveCsv(List> items)
{
_values = items;
}
public QuickSaveCsv()
{
_values = new List>();
}
///
/// Loads a csv file from the specified path
///
/// The path to the file
/// A QuickSaveCsv object that represents the loaded csv file
///
public static QuickSaveCsv Load(string path)
{
var lines = FileAccess.LoadLines(path);
if (lines == null)
{
throw new QuickSaveException("File does not exist");
}
List> values = new List>();
try
{
foreach (var line in lines)
{
values.Add(new List());
var cells = line.Split(',');
foreach (var cell in cells)
{
values.Last().Add(cell);
}
}
}
catch
{
throw new QuickSaveException("Failed to load file");
}
return new QuickSaveCsv(values);
}
///
/// Saves the QuickSaveCsv to a csv file
///
/// The path to the file
///
public void Save(string path)
{
var lines = _values.Select(x => string.Join(",", x)).ToList();
if (!FileAccess.SaveLines(path, lines))
{
throw new QuickSaveException("Failed to save file");
}
}
///
/// Gets the count of the cells in the specified row
///
/// The zero based index of the row
/// Count of the cells in the row
///
public int GetCellCount(int row)
{
if (_values.Count <= row)
{
throw new QuickSaveException("The specified row does not exist");
}
return _values[row].Count;
}
///
/// Gets value of the specified cell
///
/// The zero based index of the row
/// The zero based index of the column
/// The value of the cell
///
public string GetCell(int row, int column)
{
if (_values.Count <= row || _values[row].Count <= column)
{
throw new QuickSaveException("The specified cell does not exist");
}
return _values[row][column];
}
///
/// Gets the value of the specified cell
///
/// The type to return the value as
/// The zero based index of the row
/// The zero based index of the column
/// The value of the cell
///
public T GetCell(int row, int column)
{
if (_values.Count <= row || _values[row].Count <= column)
{
throw new QuickSaveException("The specified cell does not exist");
}
return (T)Convert.ChangeType(_values[row][column], typeof(T));
}
///
/// Sets the value of the specified cell
///
/// The zero based index of the row
/// The zero based index of the column
/// The value to set
public void SetCell(int row, int column, string value)
{
if (_values.Count <= row)
{
while (_values.Count <= row)
{
_values.Add(new List());
}
}
if (_values[row].Count <= column)
{
while (_values[row].Count <= column)
{
_values[row].Add(string.Empty);
}
}
_values[row][column] = value;
}
///
/// Sets the value of the specified cell
///
/// The type of the value
/// The zero based index of the row
/// The zero based index of the column
/// The value to set
public void SetCell(int row, int column, T value) => SetCell(row, column, Convert.ToString(value));
///
/// Deletes the specified row
///
/// The zero based index of the row
public void DeleteRow(int row)
{
if (_values.Count > row)
{
_values.RemoveAt(row);
}
}
}
}