Skip to content

Commit 67b5783

Browse files
Merge pull request #2 from CoderGamester/develop
0.1.1
2 parents 61e9c26 + 9c54127 commit 67b5783

File tree

8 files changed

+277
-46
lines changed

8 files changed

+277
-46
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ 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.1.1] - 2020-01-08
8+
9+
- Added UnityWebRequestAwaiter to remove the dependency of the AsyncAwait Package
10+
- Added GameIdsImporter example
11+
712
## [0.1.0] - 2020-01-06
813

914
- Initial submission for package distribution

Editor/GoogleSheetToolImporter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Reflection;
4-
using GameLovers.AsyncAwait;
54
using UnityEditor;
65
using UnityEditor.Callbacks;
76
using UnityEngine;

Editor/UnityWebRequestAwaiter.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using UnityEngine;
4+
using UnityEngine.Networking;
5+
6+
// ReSharper disable once CheckNamespace
7+
8+
namespace GameLoversEditor.GoogleSheetImporter
9+
{
10+
/// <summary>
11+
/// Allows to use <seealso cref="UnityWebRequest"/> with async/await mechanism
12+
/// </summary>
13+
public class UnityWebRequestAwaiter : INotifyCompletion
14+
{
15+
private readonly UnityWebRequestAsyncOperation _asyncOp;
16+
private Action _continuation;
17+
private bool _invoked;
18+
19+
public UnityWebRequestAsyncOperation AsyncOperation => _asyncOp;
20+
21+
private UnityWebRequestAwaiter() {}
22+
23+
public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp)
24+
{
25+
_asyncOp = asyncOp;
26+
asyncOp.completed += OnRequestCompleted;
27+
}
28+
29+
public bool IsCompleted => _asyncOp.isDone;
30+
31+
public void GetResult() { }
32+
33+
public void OnCompleted(Action continuation)
34+
{
35+
_continuation = continuation;
36+
37+
if (_invoked)
38+
{
39+
_continuation();
40+
}
41+
}
42+
43+
private void OnRequestCompleted(AsyncOperation obj)
44+
{
45+
_invoked = true;
46+
47+
_continuation?.Invoke();
48+
}
49+
50+
public static implicit operator UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOp)
51+
{
52+
return new UnityWebRequestAwaiter(asyncOp);
53+
}
54+
55+
public static implicit operator UnityWebRequestAsyncOperation(UnityWebRequestAwaiter awaiter)
56+
{
57+
return awaiter.AsyncOperation;
58+
}
59+
}
60+
61+
public static class UnityWebRequestAwaiterExtension
62+
{
63+
public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOp)
64+
{
65+
return new UnityWebRequestAwaiter(asyncOp);
66+
}
67+
}
68+
}

Samples/Example/.sample.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

Samples/Example/SampleExample.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

Samples/Example/SampleExample.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
using GameLoversEditor.GoogleSheetImporter;
5+
using UnityEditor;
6+
7+
namespace SheetImporters
8+
{
9+
/// <inheritdoc />
10+
[GoogleSheetImportOrder(0)]
11+
public class GameIdsImporter : IGoogleSheetImporter
12+
{
13+
private const string _name = "GameId";
14+
private const string _nameGroup = "GameIdGroup";
15+
private const string _idTag = "Id";
16+
private const string _groupsTag = "Groups";
17+
18+
/// <inheritdoc />
19+
public string GoogleSheetUrl => "https://docs.google.com/spreadsheets/d/1pxV7Fp8T9ea-Bp1ts0kn0JwAi1M3RyLpibq4LyOYQT8/edit#gid=1793530949";
20+
21+
/// <inheritdoc />
22+
public void Import(List<Dictionary<string, string>> data)
23+
{
24+
var idList = new List<string>();
25+
var groupList = new List<string>();
26+
var mapGroups = new Dictionary<string, List<string>>();
27+
var mapIds = new Dictionary<string, List<string>>();
28+
29+
foreach (var entry in data)
30+
{
31+
var groups = new List<string>(CsvParser.ArrayParse<string>(entry[_groupsTag]));
32+
var id = GetCleanName(entry[_idTag]);
33+
34+
idList.Add(id);
35+
mapGroups.Add(id, groups);
36+
37+
foreach (var group in groups)
38+
{
39+
var groupName = GetCleanName(group);
40+
if (!groupList.Contains(groupName))
41+
{
42+
groupList.Add(groupName);
43+
mapIds.Add(groupName, new List<string>());
44+
}
45+
46+
mapIds[groupName].Add(id);
47+
}
48+
}
49+
50+
var script = GenerateScript(idList, groupList, mapGroups, mapIds);
51+
52+
SaveScript(script);
53+
AssetDatabase.Refresh();
54+
}
55+
56+
private static string GenerateScript(IList<string> ids, IList<string> groups, Dictionary<string, List<string>> mapGroups,
57+
Dictionary<string, List<string>> mapIds)
58+
{
59+
var stringBuilder = new StringBuilder();
60+
61+
stringBuilder.AppendLine("using System.Collections.Generic;");
62+
stringBuilder.AppendLine("using System.Collections.ObjectModel;");
63+
stringBuilder.AppendLine("");
64+
stringBuilder.AppendLine("/* AUTO GENERATED CODE */");
65+
stringBuilder.AppendLine("namespace Ids");
66+
stringBuilder.AppendLine("{");
67+
68+
stringBuilder.AppendLine($"\tpublic enum {_name}");
69+
stringBuilder.AppendLine("\t{");
70+
GenerateEnums(stringBuilder, ids);
71+
stringBuilder.AppendLine("\t}");
72+
73+
stringBuilder.AppendLine("");
74+
stringBuilder.AppendLine($"\tpublic enum {_nameGroup}");
75+
stringBuilder.AppendLine("\t{");
76+
GenerateEnums(stringBuilder, groups);
77+
stringBuilder.AppendLine("\t}");
78+
79+
stringBuilder.AppendLine("");
80+
stringBuilder.AppendLine($"\tpublic static class {_name}Lookup");
81+
stringBuilder.AppendLine("\t{");
82+
GenerateLoopUpMethods(stringBuilder);
83+
GenerateLoopUpMaps(stringBuilder, mapGroups, _name, _nameGroup, "groups");
84+
GenerateLoopUpMaps(stringBuilder, mapIds, _nameGroup, _name, "ids");
85+
stringBuilder.AppendLine("\t}");
86+
87+
stringBuilder.AppendLine("}");
88+
89+
return stringBuilder.ToString();
90+
}
91+
92+
private static void GenerateLoopUpMethods(StringBuilder stringBuilder)
93+
{
94+
stringBuilder.AppendLine($"\t\tpublic static bool IsInGroup(this {_name} id, {_nameGroup} group)");
95+
stringBuilder.AppendLine("\t\t{");
96+
stringBuilder.AppendLine("\t\t\tif (!_groups.TryGetValue(id, out var groups))");
97+
stringBuilder.AppendLine("\t\t\t{");
98+
stringBuilder.AppendLine("\t\t\t\treturn false;");
99+
stringBuilder.AppendLine("\t\t\t}");
100+
stringBuilder.AppendLine("\t\t\treturn groups.Contains(group);");
101+
stringBuilder.AppendLine("\t\t}");
102+
103+
stringBuilder.AppendLine("");
104+
stringBuilder.AppendLine($"\t\tpublic static IList<{_name}> GetIds(this {_nameGroup} group)");
105+
stringBuilder.AppendLine("\t\t{");
106+
stringBuilder.AppendLine("\t\t\treturn _ids[group];");
107+
stringBuilder.AppendLine("\t\t}");
108+
109+
stringBuilder.AppendLine("");
110+
stringBuilder.AppendLine($"\t\tpublic static IList<{_nameGroup}> GetGroups(this {_name} id)");
111+
stringBuilder.AppendLine("\t\t{");
112+
stringBuilder.AppendLine("\t\t\treturn _groups[id];");
113+
stringBuilder.AppendLine("\t\t}");
114+
115+
stringBuilder.AppendLine("");
116+
stringBuilder.AppendLine($"\t\tpublic class {_name}Comparer : IEqualityComparer<{_name}>");
117+
stringBuilder.AppendLine("\t\t{");
118+
stringBuilder.AppendLine($"\t\t\tpublic bool Equals({_name} x, {_name} y)");
119+
stringBuilder.AppendLine("\t\t\t{");
120+
stringBuilder.AppendLine("\t\t\t\treturn x == y;");
121+
stringBuilder.AppendLine("\t\t\t}");
122+
stringBuilder.AppendLine("");
123+
stringBuilder.AppendLine($"\t\t\tpublic int GetHashCode({_name} obj)");
124+
stringBuilder.AppendLine("\t\t\t{");
125+
stringBuilder.AppendLine("\t\t\t\treturn (int)obj;");
126+
stringBuilder.AppendLine("\t\t\t}");
127+
stringBuilder.AppendLine("\t\t}");
128+
129+
stringBuilder.AppendLine("");
130+
stringBuilder.AppendLine($"\t\tpublic class {_nameGroup}Comparer : IEqualityComparer<{_nameGroup}>");
131+
stringBuilder.AppendLine("\t\t{");
132+
stringBuilder.AppendLine($"\t\t\tpublic bool Equals({_nameGroup} x, {_nameGroup} y)");
133+
stringBuilder.AppendLine("\t\t\t{");
134+
stringBuilder.AppendLine("\t\t\t\treturn x == y;");
135+
stringBuilder.AppendLine("\t\t\t}");
136+
stringBuilder.AppendLine("");
137+
stringBuilder.AppendLine($"\t\t\tpublic int GetHashCode({_nameGroup} obj)");
138+
stringBuilder.AppendLine("\t\t\t{");
139+
stringBuilder.AppendLine("\t\t\t\treturn (int)obj;");
140+
stringBuilder.AppendLine("\t\t\t}");
141+
stringBuilder.AppendLine("\t\t}");
142+
}
143+
144+
private static void GenerateLoopUpMaps(StringBuilder stringBuilder, Dictionary<string, List<string>> map,
145+
string element1Type, string element2Type, string fieldName)
146+
{
147+
stringBuilder.AppendLine("");
148+
stringBuilder.AppendLine($"\t\tprivate static readonly Dictionary<{element1Type}, ReadOnlyCollection<{element2Type}>> _{fieldName} =");
149+
stringBuilder.AppendLine($"\t\t\tnew Dictionary<{element1Type}, ReadOnlyCollection<{element2Type}>> (new {element1Type}Comparer())");
150+
stringBuilder.AppendLine("\t\t\t{");
151+
152+
foreach (var pair in map)
153+
{
154+
stringBuilder.AppendLine("\t\t\t\t{");
155+
stringBuilder.AppendLine($"\t\t\t\t\t{element1Type}.{pair.Key}, new List<{element2Type}>");
156+
stringBuilder.AppendLine("\t\t\t\t\t{");
157+
for (var i = 0; i < pair.Value.Count; i++)
158+
{
159+
stringBuilder.Append("\t\t\t\t\t\t");
160+
stringBuilder.Append($"{element2Type}.{pair.Value[i]}");
161+
stringBuilder.Append(i + 1 == pair.Value.Count ? "\n" : ",\n");
162+
}
163+
stringBuilder.AppendLine("\t\t\t\t\t}.AsReadOnly()");
164+
stringBuilder.AppendLine("\t\t\t\t},");
165+
}
166+
167+
stringBuilder.AppendLine("\t\t\t};");
168+
}
169+
170+
private static void GenerateEnums(StringBuilder stringBuilder, IList<string> list)
171+
{
172+
for (var i = 0; i < list.Count; i++)
173+
{
174+
stringBuilder.Append("\t\t");
175+
stringBuilder.Append(list[i]);
176+
stringBuilder.Append(i + 1 == list.Count ? "\n" : ",\n");
177+
}
178+
}
179+
180+
private static string GetCleanName(string name)
181+
{
182+
return name.Replace(' ', '_');
183+
}
184+
185+
private static void SaveScript(string scriptString)
186+
{
187+
var scriptAssets = AssetDatabase.FindAssets($"t:Script {_name}");
188+
var scriptPath = $"Assets/{_name}.cs";
189+
190+
foreach (var scriptAsset in scriptAssets)
191+
{
192+
var path = AssetDatabase.GUIDToAssetPath(scriptAsset);
193+
if (path.EndsWith($"/{_name}.cs"))
194+
{
195+
scriptPath = path;
196+
break;
197+
}
198+
}
199+
200+
File.WriteAllText(scriptPath, scriptString);
201+
}
202+
}
203+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.gamelovers.googlesheetimporter",
33
"displayName": "GoogleSheet Importer",
4-
"version": "0.1.0",
4+
"version": "0.1.1",
55
"unity": "2019.3",
66
"description": "This package provides a tool to import GoogleSheet data into ScriptableObject persistent config data.\n\nTo help configure the GoogleSheet Import data you need to create a configuration ScriptableObject by:\n- Right Click on the Project View > Create > ScriptableObjects > Editor > GoogleSheetImporter.\n- You can import all the GoogleSheet data without a ScriptableObject by clicking in Tools > Import Google Sheet Data",
77
"type": "tool",

0 commit comments

Comments
 (0)