您的位置:首页 > 移动开发 > Unity3D

搜寻指定路径下所有带有中文的脚本

2016-07-24 16:13 525 查看
今天写了一个工具:让搜寻unity/Scripts/目录下所有用到中文(比如:string str="中文内容"  ;除:注释   //中文内容 外)的脚本,让后写到txt中。

至于目的是检索脚本的是否含有中文,如果有中文,替换中文,便于游戏多语言的开放。

就比如说我要搜寻这样的脚本:

using UnityEngine;
using System.Collections;

public class UI_01 : MonoBehaviour {

/// <summary>
/// 测试地方1
/// </summary>
void Start () {
string str = string.Format("{0}{1}", "测试地方3", "测试地方9");// string str = "测试地方3";
var i = 222;// string str = "测试地方2";
string str2 = "测试地方4";
string str3 = "测试地方7//测试地方8";
//测试地方5
}

// 测试地方6
void Update () {

}
}


这样的脚本:/// 测试地方1, // 测试地方6, // string str = "测试地方3"等; 除外,因为这样的不需要替换。我们只替换这样string str = string.Format("{0}{1}", "测试地方3", "测试地方9")的引号之间的中文内容:测试地方3,测试地方9。
代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

namespace SearchC
{
class Program
{

static void Main(string[] args)
{
//当前路径
string currentPath = Directory.GetCurrentDirectory();
//要搜索的路径
string search = currentPath.Substring(0, currentPath.IndexOf("SearchC")) + @"SearchC_LX\Assets\Scripts";
SearchFile(search);
CreateFile(AllCSInfo);
if (ListPaths.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("搜索结果:含有中文字体的脚本个数为" + ListPaths.Count+ "\n具体内容请查看SearchResult.txt");
}
Console.Read();
}

static public System.Text.StringBuilder STS = new System.Text.StringBuilder();
static public List<string> ListPaths = new List<string>();
static public string AllCSInfo = string.Empty;
/// <summary>
/// 搜索当前路径下".cs"格式文件
/// </summary>
/// <param name="path"></param>
static public void SearchFile(string path)
{
ListPaths.Clear();
AllCSInfo = string.Empty;
STS.Length = 0;
DirectoryInfo dir = new DirectoryInfo(path);
if (!dir.Exists)
return;
string csInfo = "";
try
{
//获得目录所有.cs文件
FileInfo[] filesInfo = dir.GetFiles("*.cs", SearchOption.AllDirectories);
foreach (FileInfo tempFI in filesInfo)
{
//在.doc上打印所有.cs文件路径
Console.WriteLine(tempFI.DirectoryName + "\\" + tempFI.Name);
FileStream fileStr = tempFI.OpenRead();
//将文件只读属性去掉
tempFI.IsReadOnly = false;
//根据.cs文件编码选择正确的编码方式打开
StreamReader sr = new StreamReader(fileStr, System.Text.Encoding.Default, true);
while (!sr.EndOfStream)
{
var str = sr.ReadLine();
//判断是否是注释(只考虑一般情况,特殊情况除外,如 string str3 = "测试地方7//测试地方8";)
if (str.Contains("//"))
str = str.Substring(0, str.IndexOf("//"));
//判断是否有引号
if (str.Contains("\""))
//判断引号内容之间是否存在中文
if (GetTabContent(str)) {
string chPath = tempFI.DirectoryName + "\\" + tempFI.Name;
if (!ListPaths.Contains(chPath))
{
ListPaths.Add(chPath);
}
}

}

}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
ListPaths.Sort();
for (int i = 0; i < ListPaths.Count; i++)
{

STS.AppendLine(string.Format("{0}.{1}", i + 1, ListPaths[i]));
}
AllCSInfo = STS.ToString();

}

/// <summary>
/// 解析一行中所有引号之间的内容(比:string str = string.Format("{0}{1}", "内容1", "内容2");)
/// </summary>
/// <param name="str"></param>
static public bool GetTabContent(string con)
{
bool isT = false;

while (con.Length > 0&& con.Contains("\""))
{
var startIndex = con.IndexOf('\"');
if (startIndex >= 0)
con = con.Remove(0, startIndex + 1);
var endIndex = con.IndexOf('\"');

var command = "";
if (endIndex > 0)
{
command = con.Substring(0, endIndex);
if (ISHasChinese(command))
isT = true;
con = con.Remove(0, endIndex + 1);
}
else {
command = con;
if (ISHasChinese(command))
isT = true;
}

//Console.ForegroundColor = ConsoleColor.Red;
//Console.WriteLine("解析结果:" + command);

}
return isT;
}

/// <summary>
/// 特殊中文字符
/// </summary>
static public string SpecialStr = "·-=、【】;‘,。、.+~!@#¥%……()——+|}{:“:”《》?";
/// <summary>
/// 判断是否存在中文字符
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public bool ISHasChinese(string str)
{
bool res = false;
for (int i = 0; i < str.Length; ++i)
{
if ((int)str[i] > 127 && !SpecialStr.Contains(str[i]))
{
return res = true;
}
}
return res;
}

/// <summary>
/// 文件写出
/// </summary>
/// <param name="info"></param>
static public void CreateFile(string info)
{
using (StreamWriter sw = new StreamWriter(@"SearchResult.txt"))
{
sw.Write(info);
sw.Close();
sw.Dispose();
}

}

}
}


我们会看到代码中有这么一行:static public string SpecialStr = "·-=、【】;‘,。、.+~!@#¥%……()——+|}{:“:”《》?";
首先,我们要知道在ascii码中大于127一般为中文,但是当我们中文输入法输入上面的 "·-=、【】;‘,。、.+~!@#¥%……()——+|}{:“:”《》?",结果也会大于127,。因为我们这里只替换汉字所以我们把这样特殊的排除掉。

我们看下结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息