209 lines
7.1 KiB
C#
209 lines
7.1 KiB
C#
using UnityEngine;
|
||
using UnityEditor;
|
||
using System.IO;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
|
||
public class SpriteLoaderTool : ScriptableObject
|
||
{
|
||
private static string[] LuaPath = new string[] {
|
||
"Assets/ManagedResources/~Lua/Modules",
|
||
"Assets/ManagedResources/~Lua/View"
|
||
};
|
||
[MenuItem("Tools/SpriteLoaderTool/Add SpriteLoader To Code")]
|
||
static void AddToCode()
|
||
{
|
||
// 检索
|
||
for (int i = 0; i < LuaPath.Length; i++)
|
||
{
|
||
string folderPath = LuaPath[i];
|
||
string[] pathList = Directory.GetFiles(folderPath, "*.lua", SearchOption.AllDirectories);
|
||
for (int pIndex = 0; pIndex < pathList.Length; pIndex++)
|
||
{
|
||
EditorUtility.DisplayProgressBar(string.Format(pathList[pIndex] + "({0}/{1})", pIndex, pathList.Length), "正在检索数据:", (float)pIndex / pathList.Length);
|
||
DoAdd(pathList[pIndex]);
|
||
}
|
||
}
|
||
EditorUtility.ClearProgressBar();
|
||
}
|
||
|
||
static void DoAdd(string path)
|
||
{
|
||
string[] lines = File.ReadAllLines(path, System.Text.Encoding.UTF8);
|
||
if (lines.Length <= 0)
|
||
{
|
||
return;
|
||
}
|
||
bool isAdded = false;
|
||
bool isThis = false;
|
||
int initNum = 0;
|
||
int destroyNum = 0;
|
||
for (int lIndex = 0; lIndex < lines.Length; lIndex++)
|
||
{
|
||
if (lines[lIndex].Contains("SpriteLoader"))
|
||
{
|
||
isAdded = true;
|
||
return;
|
||
}
|
||
if (lines[lIndex].Contains("local this"))
|
||
{
|
||
isThis = true;
|
||
}
|
||
if (lines[lIndex].Contains("InitComponent") && lines[lIndex].StartsWith("function"))
|
||
{
|
||
initNum = lIndex;
|
||
}
|
||
if (lines[lIndex].Contains("OnDestroy") && lines[lIndex].StartsWith("function"))
|
||
{
|
||
destroyNum = lIndex;
|
||
}
|
||
}
|
||
List<string> ll = lines.ToList();
|
||
string o = isThis ? "this" : "self";
|
||
int DeltaAdd = 1;
|
||
if (initNum != 0)
|
||
{
|
||
ll.Insert(initNum + DeltaAdd, " " + o + ".spLoader = SpriteLoader.New()");
|
||
DeltaAdd++;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("文件未找到init方法:" + path);//没有init方法直接中断
|
||
return;
|
||
}
|
||
if (destroyNum != 0)
|
||
{
|
||
ll.Insert(destroyNum + DeltaAdd, " " + o + ".spLoader:Destroy()");
|
||
DeltaAdd++;
|
||
}
|
||
else {
|
||
Debug.LogWarning("文件未找到Destroy方法:" + path);
|
||
}
|
||
|
||
string lastLine = ll[ll.Count - 1];
|
||
ll.RemoveAt(ll.Count - 1);
|
||
string[] wlines = ll.ToArray();
|
||
for (int i = 0; i < wlines.Length; i++)
|
||
{
|
||
if (wlines[i].Contains("Util.LoadSprite"))
|
||
wlines[i] = wlines[i].Replace("Util.LoadSprite", o + ".spLoader:LoadSprite");
|
||
if (wlines[i].Contains("this:LoadSprite"))
|
||
wlines[i] = wlines[i].Replace("this:LoadSprite", o + ".spLoader:LoadSprite");
|
||
if (wlines[i].Contains("self:LoadSprite"))
|
||
wlines[i] = wlines[i].Replace("self:LoadSprite", o + ".spLoader:LoadSprite");
|
||
}
|
||
|
||
File.WriteAllLines(path, wlines, System.Text.Encoding.UTF8);
|
||
File.AppendAllText(path, lastLine, System.Text.Encoding.UTF8);
|
||
}
|
||
|
||
|
||
private static string[] functionName = new string[] {
|
||
"Util_SetHeadImage",
|
||
"Util_SetToDefaultHeadImage",
|
||
"GetPlayerHeadSprite",
|
||
"GetPlayerHeadFrameSprite",
|
||
"SetRankNumFrame",
|
||
"SetHeroIcon",
|
||
"SetHeroStars",
|
||
"SetHeroBg",
|
||
"SetIcon",
|
||
"SetFrame",
|
||
"GetQuantityImage",
|
||
};
|
||
|
||
[MenuItem("Tools/SpriteLoaderTool/common function deal")]
|
||
static void CommonFuncDeal()
|
||
{
|
||
// 检索
|
||
for (int i = 0; i < LuaPath.Length; i++)
|
||
{
|
||
string folderPath = LuaPath[i];
|
||
string[] pathList = Directory.GetFiles(folderPath, "*.lua", SearchOption.AllDirectories);
|
||
for (int pIndex = 0; pIndex < pathList.Length; pIndex++)
|
||
{
|
||
EditorUtility.DisplayProgressBar(string.Format(pathList[pIndex] + "({0}/{1})", pIndex, pathList.Length), "正在检索数据:", (float)pIndex / pathList.Length);
|
||
DoCommonFunc(pathList[pIndex]);
|
||
}
|
||
}
|
||
EditorUtility.ClearProgressBar();
|
||
}
|
||
|
||
static void DoCommonFunc(string path)
|
||
{
|
||
string[] wlines = File.ReadAllLines(path, System.Text.Encoding.UTF8);
|
||
if (wlines.Length <= 0)
|
||
{
|
||
return;
|
||
}
|
||
bool isThis = false;
|
||
for (int lIndex = 0; lIndex < wlines.Length; lIndex++)
|
||
{
|
||
if (wlines[lIndex].Contains("local this"))
|
||
{
|
||
isThis = true;
|
||
}
|
||
}
|
||
bool isChange = false;
|
||
for (int i = 0; i < wlines.Length; i++)
|
||
{
|
||
string checkLine = checkReplace(isThis, wlines[i]);
|
||
if(!checkLine.Equals(wlines[i]))
|
||
{
|
||
isChange = true;
|
||
}
|
||
wlines[i] = checkLine;
|
||
}
|
||
if (isChange)
|
||
{
|
||
Debug.LogWarning("文件改变:" + path);
|
||
File.WriteAllLines(path, wlines, System.Text.Encoding.UTF8);
|
||
}
|
||
}
|
||
static string checkReplace(bool isThis, string line)
|
||
{
|
||
string o = isThis ? "this.spLoader" : "self.spLoader";
|
||
for (int i = 0; i < functionName.Length; i++)
|
||
{
|
||
string c = " " + functionName[i] + "(";
|
||
if (line.Contains(c) && !line.Contains(c + o))
|
||
{
|
||
line = line.Replace(c, c + o + ", ");
|
||
}
|
||
}
|
||
return line;
|
||
}
|
||
|
||
[MenuItem("Tools/SpriteLoaderTool/DeleteDoubleReturn")]
|
||
static void DeleteDoubleReturn()
|
||
{
|
||
// 检索
|
||
for (int i = 0; i < LuaPath.Length; i++)
|
||
{
|
||
string folderPath = LuaPath[i];
|
||
string[] pathList = Directory.GetFiles(folderPath, "*.lua", SearchOption.AllDirectories);
|
||
for (int pIndex = 0; pIndex < pathList.Length; pIndex++)
|
||
{
|
||
EditorUtility.DisplayProgressBar(string.Format(pathList[pIndex] + "({0}/{1})", pIndex, pathList.Length), "正在检索数据:", (float)pIndex / pathList.Length);
|
||
|
||
string[] wlines = File.ReadAllLines(pathList[pIndex], System.Text.Encoding.UTF8);
|
||
if (wlines.Length <= 0)
|
||
{
|
||
return;
|
||
}
|
||
if(wlines[wlines.Length - 1].Equals(wlines[wlines.Length - 2])&&wlines[wlines.Length - 1].StartsWith("return"))
|
||
{
|
||
wlines[wlines.Length - 1] = null;
|
||
Debug.LogWarning("文件改变:" + pathList[pIndex]);
|
||
File.WriteAllLines(pathList[pIndex], wlines, System.Text.Encoding.UTF8);
|
||
}
|
||
}
|
||
}
|
||
EditorUtility.ClearProgressBar();
|
||
}
|
||
[MenuItem("Tools/SpriteLoaderTool/ClearProgressBar")]
|
||
static void ClearProgressBar()
|
||
{
|
||
EditorUtility.ClearProgressBar();
|
||
}
|
||
} |