Initial commit

This commit is contained in:
Zhongwei Li
2025-11-29 18:19:28 +08:00
commit 1da7b24c8e
254 changed files with 43797 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
using System;
using Newtonsoft.Json;
namespace UnityEditorToolkit.Protocol
{
/// <summary>
/// JSON-RPC 2.0 Error object
/// </summary>
[Serializable]
public class JsonRpcError
{
[JsonProperty("code")]
public int Code { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("data", NullValueHandling = NullValueHandling.Ignore)]
public object Data { get; set; }
public JsonRpcError() { }
public JsonRpcError(int code, string message, object data = null)
{
Code = code;
Message = message;
Data = data;
}
// Standard JSON-RPC 2.0 error codes
public static readonly int PARSE_ERROR = -32700;
public static readonly int INVALID_REQUEST = -32600;
public static readonly int METHOD_NOT_FOUND = -32601;
public static readonly int INVALID_PARAMS = -32602;
public static readonly int INTERNAL_ERROR = -32603;
// Custom Unity error codes
public static readonly int UNITY_NOT_CONNECTED = -32000;
public static readonly int UNITY_COMMAND_FAILED = -32001;
public static readonly int UNITY_OBJECT_NOT_FOUND = -32002;
public static readonly int UNITY_SCENE_NOT_FOUND = -32003;
public static readonly int UNITY_COMPONENT_NOT_FOUND = -32004;
// Factory methods for common errors
public static JsonRpcError ParseError(string details = null)
{
return new JsonRpcError(PARSE_ERROR, "Parse error", details);
}
public static JsonRpcError InvalidRequest(string details = null)
{
return new JsonRpcError(INVALID_REQUEST, "Invalid Request", details);
}
public static JsonRpcError MethodNotFound(string method)
{
return new JsonRpcError(METHOD_NOT_FOUND, $"Method not found: {method}");
}
public static JsonRpcError InvalidParams(string details)
{
return new JsonRpcError(INVALID_PARAMS, "Invalid params", details);
}
public static JsonRpcError InternalError(string details)
{
return new JsonRpcError(INTERNAL_ERROR, "Internal error", details);
}
public static JsonRpcError ObjectNotFound(string objectName)
{
return new JsonRpcError(UNITY_OBJECT_NOT_FOUND, $"GameObject not found: {objectName}");
}
public static JsonRpcError SceneNotFound(string sceneName)
{
return new JsonRpcError(UNITY_SCENE_NOT_FOUND, $"Scene not found: {sceneName}");
}
public static JsonRpcError CommandFailed(string details)
{
return new JsonRpcError(UNITY_COMMAND_FAILED, "Command failed", details);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6c34766799e11684ca74b00411016acb

View File

@@ -0,0 +1,80 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace UnityEditorToolkit.Protocol
{
/// <summary>
/// JSON-RPC 2.0 Request
/// </summary>
[Serializable]
public class JsonRpcRequest
{
[JsonProperty("jsonrpc")]
public string JsonRpc { get; set; } = "2.0";
[JsonProperty("id")]
public object Id { get; set; }
[JsonProperty("method")]
public string Method { get; set; }
[JsonProperty("params")]
public JToken Params { get; set; }
/// <summary>
/// Get strongly-typed parameters (✅ 에러 처리 개선)
/// </summary>
public T GetParams<T>() where T : class
{
if (Params == null || Params.Type == JTokenType.Null)
{
return null;
}
try
{
return Params.ToObject<T>();
}
catch (Newtonsoft.Json.JsonException ex)
{
// JSON 역직렬화 실패 시 예외를 다시 던져서 호출자가 처리하도록
UnityEngine.Debug.LogError($"Failed to deserialize params to {typeof(T).Name}: {ex.Message}");
throw new ArgumentException($"Invalid parameter format for {typeof(T).Name}: {ex.Message}", ex);
}
catch (Exception ex)
{
// 기타 예외도 동일하게 처리
UnityEngine.Debug.LogError($"Unexpected error deserializing params: {ex.Message}");
throw new ArgumentException($"Failed to deserialize parameters: {ex.Message}", ex);
}
}
/// <summary>
/// Check if request is valid
/// </summary>
public bool IsValid()
{
return JsonRpc == "2.0" && !string.IsNullOrEmpty(Method);
}
/// <summary>
/// Serialize request to JSON string
/// </summary>
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.None, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
}
/// <summary>
/// Deserialize JSON string to request object
/// </summary>
public static JsonRpcRequest FromJson(string json)
{
return JsonConvert.DeserializeObject<JsonRpcRequest>(json);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 840ef3c5f8e0d8241ba5a475dbf83da0

View File

@@ -0,0 +1,66 @@
using System;
using Newtonsoft.Json;
namespace UnityEditorToolkit.Protocol
{
/// <summary>
/// JSON-RPC 2.0 Success Response
/// </summary>
[Serializable]
public class JsonRpcResponse
{
[JsonProperty("jsonrpc")]
public string JsonRpc { get; set; } = "2.0";
[JsonProperty("id")]
public object Id { get; set; }
[JsonProperty("result")]
public object Result { get; set; }
public JsonRpcResponse() { }
public JsonRpcResponse(object id, object result)
{
Id = id;
Result = result;
}
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.None, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
}
}
/// <summary>
/// JSON-RPC 2.0 Error Response
/// </summary>
[Serializable]
public class JsonRpcErrorResponse
{
[JsonProperty("jsonrpc")]
public string JsonRpc { get; set; } = "2.0";
[JsonProperty("id")]
public object Id { get; set; }
[JsonProperty("error")]
public JsonRpcError Error { get; set; }
public JsonRpcErrorResponse() { }
public JsonRpcErrorResponse(object id, JsonRpcError error)
{
Id = id;
Error = error;
}
public string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.None);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a9b7796e1d31f1944a4925d7deb2c5b0