Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 922d32c

Browse files
author
zhangyang
committed
增加litedb的简单接口
1 parent 8e20758 commit 922d32c

File tree

7 files changed

+150
-0
lines changed

7 files changed

+150
-0
lines changed

Base/GameMode.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public partial class GameMode : MonoBehaviour
3434
public static DebuggerManager Debugger;
3535
public static ConfigManager Config;
3636
public static TimerManager Timer;
37+
public static LiteDBManager LiteDB;
3738
public static GameMode Self;
3839

3940

@@ -110,6 +111,7 @@ IEnumerator Start()
110111
Pool = GameFrameworkMode.GetModule<PoolManager>();
111112
Debugger = GameFrameworkMode.GetModule<DebuggerManager>();
112113
Timer = GameFrameworkMode.GetModule<TimerManager>();
114+
LiteDB = GameFrameworkMode.GetModule<LiteDBManager>();
113115
#endregion
114116

115117
#region resource

GameFramework/Runtime/LiteDB.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using LiteDB;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using UnityEngine;
6+
7+
namespace Wanderer.GameFramework
8+
{
9+
public class LiteDBManager : GameFrameworkModule
10+
{
11+
private Dictionary<string, LiteDBMapper> _litedbs = new Dictionary<string, LiteDBMapper>();
12+
13+
14+
public LiteDBMapper Select(string dbName)
15+
{
16+
LiteDBMapper mapper;
17+
if (!_litedbs.TryGetValue(dbName, out mapper))
18+
{
19+
string dbPath = Path.Combine(Application.persistentDataPath, $"{dbName}.litedb");
20+
mapper = new LiteDBMapper(dbPath);
21+
_litedbs.Add(dbName, mapper);
22+
}
23+
return mapper;
24+
}
25+
26+
27+
public override void OnClose()
28+
{
29+
foreach (var item in _litedbs.Values)
30+
{
31+
item.Dispose();
32+
}
33+
_litedbs.Clear();
34+
}
35+
}
36+
37+
public class LiteDBMapper:System.IDisposable
38+
{
39+
private LiteDatabase _db;
40+
public LiteDatabase LiteDB => _db;
41+
42+
public LiteDBMapper(string path)
43+
{
44+
_db = new LiteDatabase(path);
45+
}
46+
47+
public ILiteCollection<T> GetCollection<T>()
48+
{
49+
return _db.GetCollection<T>();
50+
}
51+
52+
public ILiteQueryable<T> Query<T>()
53+
{
54+
return GetCollection<T>().Query();
55+
}
56+
57+
public T[] QueryAll<T>()
58+
{
59+
return GetCollection<T>().Query().ToArray();
60+
}
61+
62+
public void Insert<T>(T t)
63+
{
64+
GetCollection<T>().Insert(t);
65+
}
66+
67+
public void Upload(string key, string file)
68+
{
69+
var storage = _db.GetStorage<string>();
70+
storage.Upload(key, file);
71+
}
72+
73+
public void Download(string key, string localPath)
74+
{
75+
var storage = _db.GetStorage<string>();
76+
storage.Download(key, localPath,true);
77+
}
78+
79+
public void Dispose()
80+
{
81+
if (_db != null)
82+
{
83+
_db.Dispose();
84+
}
85+
}
86+
}
87+
88+
}

GameFramework/Runtime/LiteDB/LiteDBManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Libraries/litedb.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Libraries/litedb/LiteDB.dll

476 KB
Binary file not shown.

Libraries/litedb/LiteDB.dll.meta

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)