Files
Main/Assets/Editor/DIY/SelfTesting/TestToolEditor.cs
2025-01-25 04:38:09 +08:00

253 lines
10 KiB
C#

#define _USE_FUNCATION_TEST_ON
using BestHTTP;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Thousandto.Code.Center;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using XLua;
namespace Assets.Editor.DIY.Test
{
/// <summary>
/// 测试工具
/// </summary>
public class TestToolEditor : EditorWindow
{
[MenuItem("Funcell/MiniEasyTools/功能自测工具")]
static void Init()
{
var win = (TestToolEditor)EditorWindow.GetWindow(typeof(TestToolEditor));
win.titleContent = new GUIContent("功能自测面板");
win.Show();
}
[MenuItem("Funcell/MiniEasyTools/临时处理")]
static void AAA()
{
var bytes = System.Text.Encoding.UTF8.GetBytes("这是一个测试222222!!");
HTTPRequest w = new HTTPRequest(new System.Uri("https://hangzhou-tzj.oss-cn-hangzhou.aliyuncs.com/unity.txt"), HTTPMethods.Put,delegate(HTTPRequest originalRequest, HTTPResponse response) {
Debug.LogError("上传完毕:" + response.StatusCode + "::" + response.Message);
});
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Content-Type"] = "text/plain";
headers["Content-Length"] = bytes.Length.ToString();
headers["Date"] = DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T", CultureInfo.InvariantCulture);
var accessKeyId = "LTAI5tA7oGzkJJhTABJTQqrV";
var accessKeySecret = "NO0EOn6y2nJ0zpIdwROXKs1jTR0seF";
var httpMethod = "PUT";
// Because the resource path to is different from the one in the request uri,
// can't use ServiceRequest.ResourcePath here.
var resourcePath = CreateSigner("hangzhou-tzj", "unity.txt");
var canonicalString = BuildCanonicalString(httpMethod, resourcePath, headers);
var signature = ComputeSignatureCore(accessKeySecret, canonicalString);
// request could be retried and the Authorization header may exist already.
// Fix for #OSS-1579/11349300
headers["Authorization"] = "OSS " + accessKeyId + ":" + signature;
foreach (var key in headers.Keys)
{
Debug.LogError("设置请求的头::" + key+":::"+headers[key]);
w.AddHeader(key, headers[key]);
}
w.UploadStream = new MemoryStream(bytes);
w.Send();
}
private static string CreateSigner(string bucket, string key)
{
var resourcePath = "/" + (bucket ?? string.Empty) +
((key != null ? "/" + key : ""));
// Hacked. the sign path is /bucket/key for two-level-domain mode
// but /bucket/key/ for the three-level-domain mode.
if (bucket != null && key == null)
{
resourcePath = resourcePath + "/";
}
return resourcePath;
}
private const string NewLineMarker = "\n";
public static string BuildCanonicalString(string method, string resourcePath,
Dictionary<string, string> headers)
{
var canonicalString = new StringBuilder();
canonicalString.Append(method).Append(NewLineMarker);
IDictionary<string, string> headersToSign = new Dictionary<string, string>();
if (headers != null)
{
foreach (var header in headers)
{
var lowerKey = header.Key.ToLowerInvariant();
if (lowerKey == "Content-Type".ToLowerInvariant()
|| lowerKey == "Content - MD5".ToLowerInvariant()
|| lowerKey == "Date".ToLowerInvariant())
{
headersToSign.Add(lowerKey, header.Value);
}
}
}
if (!headersToSign.ContainsKey("Content-Type".ToLowerInvariant()))
headersToSign.Add("Content-Type".ToLowerInvariant(), "");
if (!headersToSign.ContainsKey("Content - MD5".ToLowerInvariant()))
headersToSign.Add("Content - MD5".ToLowerInvariant(), "");
// Add all headers to sign into canonical string,
// note that these headers should be ordered before adding.
var sortedHeaders = new List<string>(headersToSign.Keys);
sortedHeaders.Sort(new KeyComparer());
foreach (var key in sortedHeaders)
{
var value = headersToSign[key];
canonicalString.Append(value);
canonicalString.Append(NewLineMarker);
}
// Add canonical resource
canonicalString.Append(resourcePath);
return canonicalString.ToString();
}
public static string ComputeSignatureCore(string key, string data)
{
Debug.Assert(!string.IsNullOrEmpty(data));
using (var algorithm = KeyedHashAlgorithm.Create("HmacSHA1"))
{
algorithm.Key = Encoding.UTF8.GetBytes(key.ToCharArray());
return System.Convert.ToBase64String(
algorithm.ComputeHash(Encoding.UTF8.GetBytes(data.ToCharArray())));
}
}
private class KeyComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return String.Compare(x, y, StringComparison.Ordinal);
}
}
private static void WorkerExited(object sender, System.EventArgs arg)
{
Debug.LogError(":::" + sender.ToString());
Debug.LogError(":::"+arg.ToString());
}
private Vector2 _scrollPos = Vector2.zero;
private string[] _LuaMethods = null;
private void OnGUI()
{
if (GUILayout.Button("测试上传文件"))
{
var upLoadURL = "http://10.0.1.182:8081/photocheck/photodata/upload";
var filePath = EditorUtility.OpenFilePanel("上传文件", ".", "*.*");
if (string.IsNullOrEmpty(filePath)) return;
var bytes = File.ReadAllBytes(filePath);
WWWForm form = new WWWForm();
form.AddBinaryData("photoData", bytes, filePath);
form.AddField("desc1","20013");
form.AddField("desc2", "a55c9410a53994fc6a9d73331d8a02a6");
form.AddField("desc3", "无名");
form.AddField("photoId","");
form.AddField("extName", Path.GetExtension(filePath));
// send to http server.
var w = UnityWebRequest.Post(upLoadURL, form);
var v = w.SendWebRequest();
v.completed += (x) =>
{
UnityEngine.Debug.Log("upload success " + filePath + ";;" + upLoadURL+"::"+w.error);
};
}
#if _USE_FUNCATION_TEST_ON
if (Application.isPlaying)
{
//if (GameCenter.IsInitialized)
{
GUILayout.Label("可以到Editor\\TestToolEditor中修改代码,不需要重启,只需要点击刷新脚本就可以了.");
_scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
if (GUILayout.Button("刷新脚本"))
{
GameCenter.LuaSystem.LuaEnv.DoString("Utils.RemoveRequiredByName('Editor.TestToolEditor')");
GameCenter.LuaSystem.LuaEnv.DoString("TestToolEditor = require 'Editor.TestToolEditor'");
object[] ret = GameCenter.LuaSystem.LuaEnv.DoString("return TestToolEditor.GetAllMethods()");
if (ret != null && ret.Length > 0)
{
var lt = (LuaTable)(ret[0]);
if (lt != null)
{
_LuaMethods = lt.Cast<string[]>();
if (_LuaMethods == null)
{
Debug.LogError("TestToolEditor.GetAllMethods() 第一个返回值不是一个String[]");
}
}
else {
Debug.LogError("TestToolEditor.GetAllMethods() 第一个返回值不是一个LuaTable");
}
}
else
{
Debug.LogError("TestToolEditor.GetAllMethods() 返回值为null");
}
}
if (_LuaMethods != null)
{
for (int i = 0; i < _LuaMethods.Length-1; i+=2)
{
if (!string.IsNullOrEmpty(_LuaMethods[i]))
{
if (GUILayout.Button(_LuaMethods[i]))
{
GameCenter.LuaSystem.LuaEnv.DoString("TestToolEditor." + _LuaMethods[i + 1] + "()");
}
}
}
}
EditorGUILayout.EndScrollView();
}
//else
// {
// EditorGUILayout.LabelField("需要在GameCenter初始化完成后才可以使用!");
// }
}
else
{
EditorGUILayout.LabelField("需要在运行模式才可以使用!");
}
#else
EditorGUILayout.LabelField("请在文件Editor/DIY/SelfTesting/TestToolEditor.cs第一行处,打开宏 _USE_FUNCATION_TEST_ON");
#endif
}
}
}