Skip to content

Commit

Permalink
UniMasterLinker v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ayutaz committed Aug 22, 2023
1 parent 9181b98 commit 3d6de56
Show file tree
Hide file tree
Showing 30 changed files with 650 additions and 22 deletions.
8 changes: 8 additions & 0 deletions Assets/UniMasterLinker.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/UniMasterLinker/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Assets/UniMasterLinker/Editor/UniMasterLinkerEditor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "UniMasterLinkerEditor",
"rootNamespace": "",
"references": [
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:6e39405796c454faf92785f4f1c7ff0e",
"GUID:343deaaf83e0cee4ca978e7df0b80d21"
],
"includePlatforms": [],
"excludePlatforms": [
"Editor"
],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions Assets/UniMasterLinker/Editor/UpdateGameDataObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#if UNITY_EDITOR

using System.Threading;
using Cysharp.Threading.Tasks;
using UniMasterLinker.API;
using UniMasterLinker.Util;
using UnityEditor;

namespace UniMasterLinker.Editor
{
/// <summary>
/// ゲームデータオブジェクトの更新
/// </summary>
public class UpdateGameDataObject : EditorWindow
{
private static CancellationTokenSource _cancellationTokenSource;

/// <summary>
/// ゲームデータオブジェクトの更新
/// </summary>
[MenuItem("UniMasterLinker/ゲームデータオブジェクトの更新")]
private static void UpdateDataObject()
{
_cancellationTokenSource = new CancellationTokenSource();
UpdateDataObjectInternal(_cancellationTokenSource.Token).Forget();
}

/// <summary>
/// ゲームデータオブジェクトの更新
/// </summary>
/// <param name="token"></param>
public static async UniTask UpdateDataObject(CancellationToken token)
{
await UpdateDataObjectInternal(token);
}

/// <summary>
/// ゲームデータオブジェクトの更新
/// </summary>
/// <param name="token"></param>
private static async UniTask UpdateDataObjectInternal(CancellationToken token)
{
// 実装例
// var enemyResponse = GoogleSheetUtil.GetGameInfo<EnemyResponse>(Constant.Constant.GameMasterSheetURL,
// Constant.Constant.EnemySheetName, token);
// var enemy = await UniTask.WhenAll(enemyResponse);
//
// UpdateDataObject(enemy);

// ゲームデータオブジェクトの更新
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}

/// <summary>
/// ゲームデータオブジェクトの更新
/// </summary>
/// <param name="responseBase"></param>
/// <typeparam name="T"></typeparam>
private static DataObjectBase<T> UpdateDataObject<T>(ResponseBase<T> responseBase)
{
var data = AssetDatabase.LoadAssetAtPath<DataObjectBase<T>>(Constant.Constant.DataObjectPath +
GetDataObjectName(responseBase));
data.SetData(responseBase.gameInfo);

EditorUtility.SetDirty(data);
return data;
}

/// <summary>
/// レスポンスクラス名からデータオブジェクト名を取得する
/// </summary>
/// <param name="responseClassName"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static string GetDataObjectName<T>(T responseClassName)
{
return responseClassName.GetType().Name.Replace("Response", "DataObject.asset");
}
}
}
#endif
3 changes: 3 additions & 0 deletions Assets/UniMasterLinker/Editor/UpdateGameDataObject.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 154 additions & 0 deletions Assets/UniMasterLinker/Editor/UpdateGameMasterAPIClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using System.Text;
using Cysharp.Threading.Tasks;
using UniMasterLinker.Util;
using UnityEditor;
using UnityEngine;

namespace UniMasterLinker.Editor
{
/// <summary>
/// ゲームマスターのAPIクラスを更新するエディタ拡張
/// </summary>
public class UpdateGameMasterAPIClass : EditorWindow
{
private const string DataRootPath = "/UniMasterLinker/Scripts/API/";

/// <summary>
/// APIクラスの更新
/// </summary>
[MenuItem("UniMasterLinker/APIクラスの更新")]
private static async void UpdateAPIClassFile()
{
// 実装例
// var playerGameInfo = GoogleSheetUtil.GetGameInfo(Constant.Constant.GameMasterSheetURL,
// Constant.Constant.PlayerSheetName);
//
// var (playerPramJson) =
// await UniTask.WhenAll(playerGameInfo);
//
// // プレイヤーの初期パラメータAPIクラスを生成
// CreateParamAPIClassFile(playerPramJson, Constant.Constant.PlayerSheetName);
}

/// <summary>
/// パラメータのAPIクラスの作成
/// </summary>
/// <param name="paramJson"></param>
/// <param name="fileName"></param>
private static void CreateParamAPIClassFile(string paramJson, string fileName)
{
var paramString = CreateParameterContents(paramJson);
var scriptContent = CreateScriptContent(fileName, paramString);
CreateScript(fileName, scriptContent);
}

/// <summary>
/// スクリプトファイルを生成
/// </summary>
/// <param name="path"></param>
/// <param name="content"></param>
private static void CreateScript(string path, string content)
{
path = Application.dataPath + "/" + path;

using (var writer = new StreamWriter(path, false))
{
writer.WriteLine(content);
}

AssetDatabase.Refresh();
}

/// <summary>
/// スクリプトの内容を生成
/// </summary>
/// <param name="className"></param>
/// <param name="parameterContents"></param>
/// <returns></returns>
private static string CreateScriptContent(string className, string parameterContents)
{
return $@"// <auto-generated/>
// このコードは自動生成されたものです。手動で編集しないでください。
using System.Collections.Generic;
using UnityEngine;
namespace API
{{
/// <summary>
/// マスターデータから取得する際の{className}パラメータクラス
/// </summary>
[System.Serializable]
public partial class {className}Data
{{
{parameterContents}
}}
/// <summary>
/// マスターデータから取得する際にレスポンスクラス
/// </summary>
[System.Serializable]
public class {className}Response : ResponseBase<{className}Data>
{{
}}
}}";
}

/// <summary>
/// パラメータの内容を生成
/// </summary>
/// <returns></returns>
private static string CreateParameterContents(string paramJson)
{
var parameterKeyList = GoogleSheetUtil.GetParameterKeyList(paramJson);
var parameterTypeList = GoogleSheetUtil.GetParameterTypeList(paramJson);
var parameterDescriptionList = GoogleSheetUtil.GetParameterDescriptionList(paramJson);
return CreateParameterClassContents(parameterKeyList, parameterTypeList, parameterDescriptionList);
}

/// <summary>
/// パラメータの内容を生成
/// </summary>
/// <param name="keys"></param>
/// <param name="types"></param>
/// <param name="descriptions"></param>
/// <returns></returns>
private static string CreateParameterClassContents(IReadOnlyList<string> keys, IReadOnlyList<string> types,
IReadOnlyList<string> descriptions)
{
var parameterString = new StringBuilder();

for (var keyIndex = 0; keyIndex < keys.Count; keyIndex++)
{
var key = keys[keyIndex];
// Summary文の作成
var descriptionValue = string.IsNullOrEmpty(descriptions[keyIndex]) ? key : descriptions[keyIndex];
var summaryString = $"/// <summary>\n /// {descriptionValue}\n /// </summary>\n";
if (keyIndex == 0)
{
parameterString.Append(
$"{summaryString} public {types[keyIndex]} " +
key +
";\n\n");
}
else if (keyIndex == keys.Count - 1)
{
parameterString.Append(
$"\t\t{summaryString} public {types[keyIndex]} " +
key + ";");
}
else
{
parameterString.Append(
$"\t\t{summaryString} public {types[keyIndex]} " +
key + ";\n\n");
}
}

return parameterString.ToString();
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/UniMasterLinker/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/UniMasterLinker/Scripts/API.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Assets/UniMasterLinker/Scripts/API/ResponseBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;

namespace UniMasterLinker.API
{
/// <summary>
/// マスターデータから取得する際にレスポンスBaseクラス
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class ResponseBase<T>
{
public List<T> gameInfo;
}
}
3 changes: 3 additions & 0 deletions Assets/UniMasterLinker/Scripts/API/ResponseBase.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/UniMasterLinker/Scripts/Constant.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Assets/UniMasterLinker/Scripts/Constant/Constant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace UniMasterLinker.Constant
{
public static class Constant
{
/// <summary>
/// ゲームマスターデータのURL
/// </summary>
public const string GameMasterSheetURL =
"GASのURL";

/// <summary>
/// ゲームデータオブジェクトのパス
/// </summary>
public const string DataObjectPath = "Assets/UniMasterLinker/DataObject/";
}
}
3 changes: 3 additions & 0 deletions Assets/UniMasterLinker/Scripts/Constant/Constant.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/UniMasterLinker/Scripts/DataObject.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3d6de56

Please sign in to comment.