Skip to content

Commit e8924cf

Browse files
Merge pull request #15 from CoderGamester/develop
0.9.0 DataService changes
2 parents 6c14efd + dc5339d commit e8924cf

4 files changed

Lines changed: 100 additions & 51 deletions

File tree

CHANGELOG.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,52 @@ All notable changes to this package will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.9.0] - 2024-08-10
8+
9+
- Updated interfaces and classes related to data services, enhancing modularity and improving version handling.
10+
- Added classes for Git commands, version management, and random number generation.
11+
12+
**Changed**:
13+
- Restructured the data service interfaces, consolidating functionality into a single IDataService interface and removing unnecessary interfaces.
14+
- Changed AddData to AddOrReplaceData in the DataService implementation.
15+
- Removed the isLocal state from data handling.
16+
17+
## [0.8.1] - 2023-08-27
18+
19+
- Added GitEditorProcess class to run Git commands as processes, enabling checks for valid Git repositories, retrieving current branch names, commit hashes, and diffs from given commits.
20+
- Introduced VersionEditorUtils class for managing application versioning. This includes setting and saving the internal version before building, loading version data from disk, and generating an internal version suffix based on Git information and build settings.
21+
22+
**Changed**:
23+
- Enhanced IInstaller interface with new methods for binding multiple type interfaces to a single instance, improving modularity and code organization.
24+
25+
## [0.8.0] - 2023-08-05
26+
27+
- Introduced MainInstaller, a singleton class for managing instances in the project.
28+
- Added RngService for generating and managing random numbers.
29+
- Implemented VersionServices to manage application version, including asynchronous loading of version data and comparison of version strings.
30+
31+
## [0.7.1] - 2023-07-28
32+
33+
**Fixed**:
34+
- Compilation errors in various test files and the PoolService class have been fixed.
35+
36+
**Changed**:
37+
- Tests have been moved to proper folders, and the package number has been updated.
38+
- An unused namespace import has been removed from the InstallerTest class.
39+
40+
## [0.7.0] - 2023-07-28
41+
42+
- Introduced a code review process using GitHub Actions workflow.
43+
- Added IInstaller interface and Installer implementation for binding and resolving instances.
44+
- Updated namespaces, removed unused code, and modified method calls in test classes.
45+
46+
**Changed**:
47+
- Removed dependency on ICommandNetworkService and SendCommand method in CommandService.
48+
- Updated IDataService interface and DataService class to handle local and online data saving.
49+
- Improved readability of MessageBrokerService class by using var for type inference.
50+
- Removed unused network service related interfaces, classes, and methods.
51+
- Modified calculation of overFlow in TickService to check for zero DeltaTime.
52+
753
## [0.6.2] - 2020-09-10
854

955
- Made *NetworkService* abstract and removed *INetworkService* to make easier to work with

Runtime/DataService.cs

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,85 +16,74 @@ public interface IDataProvider
1616
/// Requests the player's data of <typeparamref name="T"/> type
1717
/// </summary>
1818
T GetData<T>() where T : class;
19+
20+
/// <summary>
21+
/// Requests if the service has the given player's data of <typeparamref name="T"/> type
22+
/// </summary>
23+
bool HasData<T>() where T : class;
1924
}
20-
25+
2126
/// <summary>
22-
/// This interface provides the possibility to the current memory data to disk
27+
/// This service allows to manage all the persistent data in the game.
28+
/// Data are strictly reference types to guarantee that there is no boxing/unboxing and lost of referencing when changing it's data.
2329
/// </summary>
24-
public interface IDataSaver
30+
public interface IDataService : IDataProvider
2531
{
2632
/// <summary>
2733
/// Saves the game's given <typeparamref name="T"/> data to disk
2834
/// </summary>
2935
void SaveData<T>() where T : class;
30-
36+
3137
/// <summary>
3238
/// Saves all game's data to disk
3339
/// </summary>
3440
void SaveAllData();
35-
}
3641

37-
/// <summary>
38-
/// This interface provides the possibility to load data from disk
39-
/// </summary>
40-
public interface IDataLoader
41-
{
4242
/// <summary>
4343
/// Loads the game's given <typeparamref name="T"/> data from disk and returns it
4444
/// </summary>
4545
T LoadData<T>() where T : class;
46-
}
4746

48-
/// <summary>
49-
/// This service allows to manage all the persistent data in the game.
50-
/// Data are strictly reference types to guarantee that there is no boxing/unboxing and lost of referencing when changing it's data.
51-
/// </summary>
52-
public interface IDataService : IDataProvider, IDataSaver, IDataLoader
53-
{
5447
/// <summary>
55-
/// Adds the given <paramref name="data"/> to this logic state to be maintained in memory.
56-
/// If <paramref name="isLocal"/> then the given <paramref name="data"/> will be saved on the device HD.
48+
/// Adds or replaces the given <paramref name="data"/> to be maintained in memory.
5749
/// </summary>
58-
void AddData<T>(T data, bool isLocal = false) where T : class;
50+
void AddOrReplaceData<T>(T data) where T : class;
5951
}
6052

6153
/// <inheritdoc />
6254
public class DataService : IDataService
6355
{
64-
private readonly IDictionary<Type, DataInfo> _data = new Dictionary<Type, DataInfo>();
65-
56+
private readonly IDictionary<Type, object> _data = new Dictionary<Type, object>();
57+
58+
/// <inheritdoc />
59+
public bool HasData<T>() where T : class
60+
{
61+
return _data.ContainsKey(typeof(T));
62+
}
63+
6664
/// <inheritdoc />
6765
public T GetData<T>() where T : class
6866
{
69-
return _data[typeof(T)].Data as T;
67+
return _data[typeof(T)] as T;
7068
}
7169

7270
/// <inheritdoc />
7371
public void SaveData<T>() where T : class
7472
{
7573
var type = typeof(T);
7674

77-
if (_data[type].IsLocal)
78-
{
79-
PlayerPrefs.SetString(type.Name, JsonConvert.SerializeObject(_data[type].Data));
80-
PlayerPrefs.Save();
81-
return;
82-
}
83-
84-
SaveOnline(_data[type].Data, type);
75+
PlayerPrefs.SetString(type.Name, JsonConvert.SerializeObject(_data[type]));
76+
PlayerPrefs.Save();
77+
OnDataSaved(type.Name, _data[type], type);
8578
}
8679

8780
/// <inheritdoc />
8881
public void SaveAllData()
8982
{
9083
foreach (var data in _data)
9184
{
92-
if (data.Value.IsLocal)
93-
{
94-
PlayerPrefs.SetString(data.Key.Name, JsonConvert.SerializeObject(data.Value.Data));
95-
continue;
96-
}
97-
SaveOnline(data.Value.Data, data.Key);
85+
PlayerPrefs.SetString(data.Key.Name, JsonConvert.SerializeObject(data.Value));
86+
OnDataSaved(data.Key.Name, data.Value, data.Key);
9887
}
9988

10089
PlayerPrefs.Save();
@@ -105,26 +94,27 @@ public T LoadData<T>() where T : class
10594
{
10695
var json = PlayerPrefs.GetString(typeof(T).Name, "");
10796
var instance = string.IsNullOrEmpty(json) ? Activator.CreateInstance<T>() : JsonConvert.DeserializeObject<T>(json);
108-
109-
AddData(instance, true);
97+
98+
AddOrReplaceData(instance);
11099

111100
return instance;
112101
}
113102

114103
/// <inheritdoc />
115-
public void AddData<T>(T data, bool isLocal = false) where T : class
116-
{
117-
_data.Add(typeof(T), new DataInfo { Data = data, IsLocal = isLocal });
118-
}
119-
120-
protected virtual void SaveOnline(object data, Type type)
104+
public void AddOrReplaceData<T>(T data) where T : class
121105
{
106+
if(HasData<T>())
107+
{
108+
_data[typeof(T)] = data;
109+
}
110+
else
111+
{
112+
_data.Add(typeof(T), data);
113+
}
122114
}
123115

124-
private struct DataInfo
116+
protected virtual void OnDataSaved(string key, object data, Type type)
125117
{
126-
public object Data;
127-
public bool IsLocal;
128118
}
129119
}
130120
}

Tests/Editor/EditMode/DataServiceTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,24 @@ public void AddData_Successfully()
2626
{
2727
var data = Substitute.For<IDataMockup>();
2828

29-
_dataService.AddData(data, false);
29+
_dataService.AddOrReplaceData(data);
3030

3131
Assert.AreSame(data, _dataService.GetData<IDataMockup>());
3232
}
3333

34+
[Test]
35+
public void ReplaceData_Successfully()
36+
{
37+
var data = Substitute.For<IDataMockup>();
38+
var data1 = new object();
39+
40+
_dataService.AddOrReplaceData(data1);
41+
_dataService.AddOrReplaceData(data);
42+
43+
Assert.AreNotSame(data1, _dataService.GetData<IDataMockup>());
44+
Assert.AreSame(data, _dataService.GetData<IDataMockup>());
45+
}
46+
3447
[Test]
3548
public void GetData_NotFound_ThrowsException()
3649
{

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "com.gamelovers.services",
33
"displayName": "Services",
44
"author": "Miguel Tomas",
5-
"version": "0.8.1",
6-
"unity": "2021.4",
5+
"version": "0.9.0",
6+
"unity": "2022.4",
77
"license": "MIT",
88
"description": "The purpose of this package is to provide a set of services to ease the development of a basic game architecture",
99
"type": "library",

0 commit comments

Comments
 (0)