Skip to content

Commit

Permalink
Merge pull request #1 from Poulpinou/Release_v0.0.4
Browse files Browse the repository at this point in the history
Release v0.0.4
  • Loading branch information
Donovan Persent authored Aug 7, 2019
2 parents 0175001 + 081ca54 commit 27e2557
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 13 deletions.
25 changes: 21 additions & 4 deletions Editor/Windows/ManageDatabaseWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ void SchemaWindow()
GUILayout.Label("Tables", skin.FindStyle("Title"));

leftscrollPosition = GUILayout.BeginScrollView(leftscrollPosition);

if (schemaBuilder.activeSchema == null)
{
GUILayout.Label("No Schema found", skin.FindStyle("Italic"));
Expand All @@ -212,7 +213,11 @@ void SchemaWindow()
}
}
else {
schemaBuilder.activeSchema.RemoveTableSchema(schema);
GUILayout.BeginHorizontal();
GUILayout.Label("Unknown table");
if(GUILayout.Button("Remove"))
schemaBuilder.activeSchema.RemoveTableSchema(schema);
GUILayout.EndHorizontal();
}

}
Expand Down Expand Up @@ -490,10 +495,22 @@ void OnGUI()
if (settings != null)
{
if (schemaBuilder == null)
schemaBuilder = new SchemaBuilder(settings);
{
try
{
schemaBuilder = new SchemaBuilder(settings);
}
catch (Exception e)
{
log = e.Message;
}
}

if (GUILayout.Button("Schema")) activeWindow = WindowType.Schema;
if (GUILayout.Button("Explore")) activeWindow = WindowType.Explore;
if (schemaBuilder != null)
{
if (GUILayout.Button("Schema")) activeWindow = WindowType.Schema;
if (GUILayout.Button("Explore")) activeWindow = WindowType.Explore;
}
}
GUILayout.EndHorizontal();

Expand Down
18 changes: 16 additions & 2 deletions Runtime/Scripts/Models/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class Database : StaticManager<Database>
/// The <see cref="databaseFolderPath"/> of the active <see cref="Database"/>
/// </summary>
public static DatabaseSettings Settings => active.settings;

/// <summary>
/// The current progress of loading, 1 = loaded
/// </summary>
public static float loadProgress { get; private set; }
#endregion

#region Static Methods
Expand Down Expand Up @@ -142,11 +147,15 @@ IEnumerator RunLoad(UnityAction onLoadDone = null)
LoadSettings();
LoadDatabaseFile();

loadProgress = 0.1f;
yield return RunSchema();
yield return RunLoadTables();

loadProgress = 1f;

isLoaded = true;
onLoadDone.Invoke();
if(onLoadDone != null)
onLoadDone.Invoke();
OnDatabaseLoaded.Invoke();
}

Expand All @@ -168,14 +177,16 @@ IEnumerator RunSchema()

currentVersion = schemaBuilder.activeSchema.version;

int schemasCount = schemaBuilder.activeSchema.GetTableSchemas().Count;

foreach (TableSchema tableSchema in schemaBuilder.activeSchema.GetTableSchemas())
{
if (Time.realtimeSinceStartup - startTime > settings.coroutinesMaxExecutionTime)
{
yield return new WaitForEndOfFrame();
startTime = Time.realtimeSinceStartup;
}

try
{
Table table = Table.Build(tableSchema);
Expand All @@ -185,6 +196,8 @@ IEnumerator RunSchema()
{
Debug.LogError(string.Format("Failed to load {0} table : {1}", tableSchema.itemType, e));
}

loadProgress += 0.4f / schemasCount;
}

Save();
Expand All @@ -196,6 +209,7 @@ IEnumerator RunLoadTables()
{
table.LoadTable();
yield return null;
loadProgress += 0.4f / tables.Count;
}
}
#endregion
Expand Down
File renamed without changes.
51 changes: 51 additions & 0 deletions Runtime/Scripts/Models/Requests/FillRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Newtonsoft.Json.Linq;

namespace DGTools.Database {
/// <summary>
/// Fills a <see cref="IFillable{Titem}"/> container with database results. Can be executed asynchronously
/// </summary>
public class FillRequest<Titem> : GetRequest<Titem>, IRequestAsyncable where Titem : IDatabasable, new()
{
public IFillable<Titem> container { get; protected set; }

public FillRequest(Func<JToken, bool> filter, IFillable<Titem> container) : base(filter)
{
this.container = container;
}

public IEnumerator OnExecuteAsync()
{
float startTime = Time.realtimeSinceStartup;
int lastResultIndex = 0;
IEnumerator<List<Titem>> execution = table.GetManyAsync(filter);

while (execution.MoveNext())
{
int diff = execution.Current.Count - lastResultIndex;
if (diff > 0)
{
for (int i = lastResultIndex; i < lastResultIndex + diff; i++)
{
container.AddItem(execution.Current[i]);
}
lastResultIndex += diff - 1;
}

if (Time.realtimeSinceStartup - startTime > Database.Settings.coroutinesMaxExecutionTime)
{
yield return new WaitForEndOfFrame();
startTime = Time.realtimeSinceStartup;
}
}
}

protected override void OnExecute()
{
container.AddItems(table.GetMany(filter).ToArray());
}
}
}
14 changes: 13 additions & 1 deletion Runtime/Scripts/Models/SchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ public class SchemaBuilder
#region Constructors
public SchemaBuilder(DatabaseSettings settings)
{
path = Path.Combine(PathUtilities.absolutePath, settings.databaseFolderPath, settings.schemasFolderName);
try
{
path = Path.Combine(PathUtilities.absolutePath, settings.databaseFolderPath, settings.schemasFolderName);
}
catch {
throw new Exception("Invalid path settings");
}

prefix = settings.schemaFilePrefix;

if (!Directory.Exists(path))
Expand All @@ -37,9 +44,13 @@ public SchemaBuilder(DatabaseSettings settings)
ReloadVersions();

if (availableVersions.Count > 0)
{
LoadSchema(lastVersion);
}
else
{
CreateSchemaForCurrentVersion();
}
}
#endregion

Expand All @@ -63,6 +74,7 @@ public void LoadSchema(string version)
if (activeSchema != null && activeSchema.version == version) return;

activeSchema = new Schema(LoadVersion(version));

ReloadVersions();
}

Expand Down
11 changes: 9 additions & 2 deletions Runtime/Scripts/Models/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ public static Table Build(TableSchema schema)
/// <param name="ID">The ID to test</param>
public bool IDExists(int ID)
{
return datas.Where(d => (int)d["ID"] == ID).FirstOrDefault() != null;
return datas.Count(d => (int)d["ID"] == ID) > 0;
}

public bool IsUnique(string key, string value) {
return datas.Count(d => (string)d[key] == value) == 0;
}

/// <summary>
Expand Down Expand Up @@ -190,7 +194,10 @@ public void SaveItem(Titem item, bool authorizeCreation = true)

if (IDExists(item.ID))
{
datas.Remove(item.ID.ToString());
JToken[] datasToRemove = datas.Where(t => (int)t["ID"] == item.ID).ToArray();
foreach (JToken toRemove in datasToRemove) {
datas.Remove(toRemove);
}
}

datas.Add(itemDatas);
Expand Down
12 changes: 10 additions & 2 deletions Runtime/Scripts/Models/TableSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public TableSchema(Type type)
throw new Exception(string.Format("{0} should have an empty constructor", type));

itemType = type;

fields = new List<TableField>();

foreach (FieldInfo field in type.GetFields())
Expand Down Expand Up @@ -63,7 +64,11 @@ public TableSchema(JObject datas)
{
try
{
itemType = Type.GetType((string)datas.SelectToken("itemType"));
itemType = TypeUtilities.GetTypeFromString((string)datas.SelectToken("itemType"));

if (itemType == null) {
throw new Exception(string.Format("Type {0} doesn't exist", (string)datas.SelectToken("itemType")));
}

JArray fieldsdatas = (JArray)datas.SelectToken("fields");
fields = new List<TableField>();
Expand All @@ -75,8 +80,11 @@ public TableSchema(JObject datas)
(bool)field.SelectToken("isProperty")
));
}

isValid = true;
}
catch {
catch (Exception e){
Debug.Log(e.Message);
isValid = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "com.dgtools.database",
"displayName": "DGTools Database",
"version": "0.0.2",
"version": "0.0.4",
"dependencies": {
"com.dgtools.core": ""
"com.dgtools.core": "0.1.0"
},
"description": "Database manager for Unity",
"type": "tool",
Expand Down

0 comments on commit 27e2557

Please sign in to comment.