Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 6f35cc0

Browse files
committed
feat: c# nullable enable
1 parent 4838805 commit 6f35cc0

File tree

7 files changed

+500
-58
lines changed

7 files changed

+500
-58
lines changed

Packages/com.nuclearband.sodatabase/Editor/DataNodeCreator.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ namespace NuclearBand.Editor
1111
{
1212
public static class DataNodeCreator
1313
{
14-
public static void ShowDialog<T>(string defaultDestinationPath, Action<T> onScritpableObjectCreated = null)
14+
public static void ShowDialog<T>(string defaultDestinationPath, Action<T>? onScriptableObjectCreated = null)
1515
where T : ScriptableObject
1616
{
17-
var selector = new ScriptableObjectSelector<T>(defaultDestinationPath, onScritpableObjectCreated);
17+
var selector = new ScriptableObjectSelector<T>(defaultDestinationPath, onScriptableObjectCreated);
1818

1919
if (selector.SelectionTree.EnumerateTree().Count() == 1)
2020
{
@@ -29,12 +29,12 @@ public static void ShowDialog<T>(string defaultDestinationPath, Action<T> onScri
2929

3030
private class ScriptableObjectSelector<T> : OdinSelector<Type> where T : ScriptableObject
3131
{
32-
private Action<T> onScritpableObjectCreated;
33-
private string defaultDestinationPath;
32+
private readonly Action<T>? onScriptableObjectCreated;
33+
private readonly string defaultDestinationPath;
3434

35-
public ScriptableObjectSelector(string defaultDestinationPath, Action<T> onScritpableObjectCreated = null)
35+
public ScriptableObjectSelector(string defaultDestinationPath, Action<T>? onScriptableObjectCreated = null)
3636
{
37-
this.onScritpableObjectCreated = onScritpableObjectCreated;
37+
this.onScriptableObjectCreated = onScriptableObjectCreated;
3838
this.defaultDestinationPath = defaultDestinationPath;
3939
this.SelectionConfirmed += this.Save;
4040
}
@@ -52,17 +52,14 @@ protected override void BuildSelectionTree(OdinMenuTree tree)
5252

5353
private void Save(IEnumerable<Type> selection)
5454
{
55-
var obj = ScriptableObject.CreateInstance(selection.FirstOrDefault()) as T;
55+
var obj = (ScriptableObject.CreateInstance(selection.FirstOrDefault()) as T)!;
5656

57-
string dest = this.defaultDestinationPath.TrimEnd('/');
57+
var dest = this.defaultDestinationPath.TrimEnd('/');
5858

5959
AssetDatabase.CreateAsset(obj, AssetDatabase.GenerateUniqueAssetPath(dest + "/" + "New.asset"));
6060
AssetDatabase.Refresh();
6161

62-
if (this.onScritpableObjectCreated != null)
63-
{
64-
this.onScritpableObjectCreated(obj);
65-
}
62+
onScriptableObjectCreated?.Invoke(obj);
6663
}
6764
}
6865
}

Packages/com.nuclearband.sodatabase/Editor/ScriptableObjectDatabaseEditorWindow.cs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ protected override OdinMenuTree BuildMenuTree()
3434
Config = {DrawSearchToolbar = true}
3535
};
3636

37-
if (SODatabaseSettings.Path == "")
37+
if (SODatabaseSettings.Path == string.Empty)
3838
{
3939
inSettings = true;
40-
tree.AddMenuItemAtPath(new HashSet<OdinMenuItem>(), "", new OdinMenuItem(tree, "Settings", SODatabaseSettings.Instance));
40+
tree.AddMenuItemAtPath(new HashSet<OdinMenuItem>(), string.Empty, new OdinMenuItem(tree, "Settings", SODatabaseSettings.Instance));
4141
return tree;
4242
}
4343

@@ -54,7 +54,7 @@ private void SelectionChanged(SelectionChangedType obj)
5454
switch (obj)
5555
{
5656
case SelectionChangedType.ItemAdded:
57-
(MenuTree.Selection.SelectedValue as Holder).Select();
57+
((Holder) MenuTree.Selection.SelectedValue)!.Select();
5858
break;
5959
case SelectionChangedType.ItemRemoved:
6060
break;
@@ -75,18 +75,18 @@ void AddAllAssetsAtPath(
7575
var odinMenuItemSet = new HashSet<OdinMenuItem>();
7676
foreach (var str1 in strings)
7777
{
78-
var @object = AssetDatabase.LoadAssetAtPath(str1, type);
79-
var path = "";
80-
var name = "";
81-
var str2 = "";
82-
if (@object == null)
78+
var asset = AssetDatabase.LoadAssetAtPath(str1, type);
79+
var path = string.Empty;
80+
var name = string.Empty;
81+
var str2 = string.Empty;
82+
if (asset == null)
8383
{
8484
//it's a directory
8585
str2 = str1.Substring(assetFolderPath.Length);
8686
int length = str2.LastIndexOf('/');
8787
if (length == -1)
8888
{
89-
path = "";
89+
path = string.Empty;
9090
name = str2;
9191
}
9292
else
@@ -95,7 +95,7 @@ void AddAllAssetsAtPath(
9595
name = str2.Substring(length + 1);
9696
}
9797

98-
if (name == "")
98+
if (name == string.Empty)
9999
continue;
100100
tree.AddMenuItemAtPath(odinMenuItemSet, path, new OdinMenuItem(tree, name, new FolderHolder(path, name)));
101101

@@ -110,7 +110,7 @@ void AddAllAssetsAtPath(
110110

111111
path = path.Trim('/') + "/" + withoutExtension;
112112
SplitMenuPath(path, out path, out name);
113-
var menuItem = new OdinMenuItem(tree, name, new DataNodeHolder(path, name, (DataNode) @object));
113+
var menuItem = new OdinMenuItem(tree, name, new DataNodeHolder(path, name, (DataNode) asset));
114114
tree.AddMenuItemAtPath(odinMenuItemSet, path, menuItem);
115115
AddDragHandles(menuItem);
116116
}
@@ -123,7 +123,7 @@ private static void SplitMenuPath(string menuPath, out string path, out string n
123123
int length = menuPath.LastIndexOf('/');
124124
if (length == -1)
125125
{
126-
path = "";
126+
path = string.Empty;
127127
name = menuPath;
128128
}
129129

@@ -139,7 +139,7 @@ protected override void OnBeginDrawEditors()
139139
if (inSettings)
140140
{
141141
base.OnBeginDrawEditors();
142-
if (SODatabaseSettings.Path != "")
142+
if (SODatabaseSettings.Path != string.Empty)
143143
{
144144
Close();
145145
Open();
@@ -156,23 +156,22 @@ protected override void OnBeginDrawEditors()
156156
{
157157
if (selected != null)
158158
{
159-
var type = selected.Value is DataNodeHolder ? (selected.Value as DataNodeHolder).DataNode.GetType().ToString() : "Directory";
159+
var type = selected.Value switch
160+
{
161+
DataNodeHolder holder => holder.DataNode.GetType().ToString(),
162+
_ => "Directory"
163+
};
160164
GUILayout.Label(type);
161165
}
162166

163167
var path = SODatabaseSettings.Path;
164168
if (MenuTree.Selection.SelectedValue != null)
165-
path += string.IsNullOrEmpty((MenuTree.Selection.SelectedValue as Holder).Path) ? "" : (MenuTree.Selection.SelectedValue as Holder).Path + "/";
169+
path += string.IsNullOrEmpty((MenuTree.Selection.SelectedValue as Holder)!.Path) ? string.Empty : (MenuTree.Selection.SelectedValue as Holder)!.Path + "/";
166170
if (MenuTree.Selection.SelectedValue is FolderHolder folderHolder)
167171
path += folderHolder.Name + "/";
168172
path = path.Substring(0, path.Length - 1);
169173
if (SirenixEditorGUI.ToolbarButton(new GUIContent("Create DataNode")))
170-
{
171-
DataNodeCreator.ShowDialog<DataNode>(path, obj =>
172-
{
173-
TrySelectMenuItemWithObject(obj); // Selects the newly created item in the editor
174-
});
175-
}
174+
DataNodeCreator.ShowDialog<DataNode>(path, TrySelectMenuItemWithObject);
176175

177176
if (SirenixEditorGUI.ToolbarButton(new GUIContent("Create Folder")))
178177
{
@@ -188,7 +187,7 @@ protected override void OnBeginDrawEditors()
188187

189188
private void AddDragHandles(OdinMenuItem menuItem)
190189
{
191-
menuItem.OnDrawItem += x => DragAndDropUtilities.DragZone(menuItem.Rect, (menuItem.Value as DataNodeHolder).DataNode, false, false);
190+
menuItem.OnDrawItem += x => DragAndDropUtilities.DragZone(menuItem.Rect, (menuItem.Value as DataNodeHolder)!.DataNode, false, false);
192191
}
193192
}
194193
}

Packages/com.nuclearband.sodatabase/Runtime/Holders/Holder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ namespace NuclearBand
77
[JsonObject(MemberSerialization.OptIn)]
88
public abstract class Holder
99
{
10-
public Holder()
10+
protected Holder()
1111
{
1212

1313
}
1414
#if UNITY_EDITOR
1515
[HideInInspector]
16-
public string Path;
16+
public string Path = string.Empty;
1717
[HideInInspector]
18-
public string Name;
18+
public string Name = string.Empty;
1919

2020
[ShowInInspector]
2121
[HorizontalGroup("Path")]
22-
protected string tempPath;
22+
protected string tempPath = string.Empty;
2323
[ShowInInspector]
2424
[HorizontalGroup("Name")]
25-
protected string tempName;
25+
protected string tempName = string.Empty;
2626

2727
public Holder(string path, string name)
2828
{

Packages/com.nuclearband.sodatabase/Runtime/SODatabase.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Threading.Tasks;
8-
using JetBrains.Annotations;
98
using Newtonsoft.Json;
109
using Sirenix.Utilities;
1110
using UnityEngine;
@@ -15,10 +14,9 @@ namespace NuclearBand
1514
{
1615
public static class SODatabase
1716
{
18-
private static FolderHolder root;
19-
private static Dictionary<string, string> dataNodeJSONs;
20-
21-
public static async Task Init(Action<float> onProgress, Action onComplete)
17+
private static FolderHolder root = null!;
18+
19+
public static async Task Init(Action<float>? onProgress, Action? onComplete)
2220
{
2321
var loadHandler = Addressables.LoadResourceLocationsAsync(SODatabaseSettings.Label);
2422
#pragma warning disable 4014
@@ -66,7 +64,7 @@ public static T GetModel<T>(string path) where T : DataNode
6664
curFolder = curFolder.FolderHolders[pathElements[i]];
6765

6866
var dataNodeName = pathElements[pathElements.Length - 1];
69-
return curFolder.DataNodes[dataNodeName] as T;
67+
return ((T) curFolder.DataNodes[dataNodeName])!;
7068
}
7169

7270
public static List<T> GetModels<T>(string path) where T : DataNode
@@ -81,7 +79,7 @@ public static List<T> GetModels<T>(string path) where T : DataNode
8179

8280
public static void Save()
8381
{
84-
var res = SaveFolderHolder(root, "");
82+
var res = SaveFolderHolder(root, string.Empty);
8583
foreach (var pair in res)
8684
PlayerPrefs.SetString(pair.Key, pair.Value);
8785
PlayerPrefs.Save();
@@ -112,7 +110,7 @@ static Dictionary<string, string> SaveFolderHolder(FolderHolder folderHolder, st
112110

113111
public static void Load()
114112
{
115-
LoadFolderHolder(root, "");
113+
LoadFolderHolder(root, string.Empty);
116114
}
117115

118116
static void LoadFolderHolder(FolderHolder folderHolder, string path)

Packages/com.nuclearband.sodatabase/Runtime/SODatabaseInternal.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if UNITY_EDITOR
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using UnityEditor;
@@ -10,7 +11,8 @@ public static class SODatabaseInternal
1011
{
1112
public static T GetModelForEdit<T>(string path) where T : DataNode
1213
{
13-
return AssetDatabase.LoadAssetAtPath(SODatabaseSettings.Path + path + ".asset", typeof(T)) as T;
14+
return AssetDatabase.LoadAssetAtPath<T>(SODatabaseSettings.Path + path + ".asset") ??
15+
throw new ArgumentException($"Could not get model at path {path}");
1416
}
1517

1618
public static List<T> GetModelsForEdit<T>(string path) where T : DataNode
@@ -41,12 +43,15 @@ public static void CreateFolder(string path)
4143

4244
public static T CreateModel<T>(string path, string name) where T : DataNode
4345
{
44-
var model = GetModelForEdit<T>(path + "/" + name);
45-
if (model != null)
46+
try
47+
{
48+
var model = GetModelForEdit<T>(path + "/" + name);
4649
return model;
47-
50+
}
51+
catch{}
52+
4853
CreateFolder(path);
49-
var obj = ScriptableObject.CreateInstance(typeof(T)) as T;
54+
var obj = (ScriptableObject.CreateInstance(typeof(T)) as T)!;
5055
var fullPath = SODatabaseSettings.Path + path + "/" + name;
5156
AssetDatabase.CreateAsset(obj, fullPath + ".asset");
5257
AssetDatabase.SaveAssets();

Packages/com.nuclearband.sodatabase/Runtime/SODatabaseSettings.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace NuclearBand
1010
{
1111
public class SODatabaseSettings : SerializedScriptableObject
1212
{
13-
private static SODatabaseSettings instance;
13+
private static SODatabaseSettings instance = null!;
1414
public static SODatabaseSettings Instance
1515
{
1616
get
@@ -20,11 +20,11 @@ public static SODatabaseSettings Instance
2020
#if UNITY_EDITOR
2121
if (instance == null)
2222
{
23-
instance = CreateInstance(typeof(SODatabaseSettings)) as SODatabaseSettings;
23+
instance = CreateInstance<SODatabaseSettings>();
2424
AssetDatabase.CreateFolder("Assets", "com.nuclearband.sodatabase");
2525
AssetDatabase.CreateFolder("Assets/com.nuclearband.sodatabase", "Resources");
26-
var dest = "Assets/com.nuclearband.sodatabase/Resources/";
27-
AssetDatabase.CreateAsset(instance, (dest + "SODatabaseSettings.asset"));
26+
const string destination = "Assets/com.nuclearband.sodatabase/Resources/";
27+
AssetDatabase.CreateAsset(instance, (destination + "SODatabaseSettings.asset"));
2828
AssetDatabase.SaveAssets();
2929
AssetDatabase.Refresh();
3030
}
@@ -39,7 +39,7 @@ public static SODatabaseSettings Instance
3939
[SerializeField]
4040
[ReadOnly]
4141
[Title("Don't forget to set this folder as Addressable with label")]
42-
private string path = "";
42+
private string path = string.Empty;
4343

4444

4545
[SerializeField]
@@ -49,7 +49,7 @@ public static SODatabaseSettings Instance
4949

5050
[FolderPath(AbsolutePath = false)]
5151
[NonSerialized, ShowInInspector]
52-
public string SavePath;
52+
public string SavePath = string.Empty;
5353

5454
[Button]
5555
void Save()

0 commit comments

Comments
 (0)