要求
前后端源码各1500行,一共60页,每页50行。
最后一页是后端的结束页。
需剔除空行、注释。
实际做法
输入
- 代码文件所处目录
- 指定代码文件的路径(该文件会被放在文本的最后)
- 所需总行数:3000
输出:一个txt文档,其中包含了代码内容。
工具的使用方式
工具的代码
GenerateSoftwareCopyrightCodeTxt.cs
#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
public class GenerateSoftwareCopyrightCodeTxt
{
[MenuItem("Tools/生成软著代码文件")]
private static void GenerateCodeTxt()
{
// 输入信息
// 1. 所有代码文件所处目录:获取该目录下的所有.cs文件
string dirPath = "Assets/Scripts";
// 2. 指定.cs文件的路径:这个文件的内容会放在txt的最后部分
string filePath = "Assets/Scripts/Game/GameSys.cs";
// 3. 总行数
int lineCnt = 3000;
// 生成str
List<string> strs = new List<string>();
var fileStrs = PathToStrs(filePath);
int remainLineCnt = lineCnt - fileStrs.Count;
string[] filePaths = Directory.GetFiles(dirPath, "*.cs", SearchOption.AllDirectories);
var fileName = Path.GetFileNameWithoutExtension(filePath);
foreach (var curFilePath in filePaths)
{
if (Path.GetFileNameWithoutExtension(curFilePath) == fileName)
{
continue;
}
var curStrs = PathToStrs(curFilePath);
strs.AddRange(curStrs);
remainLineCnt -= curStrs.Count;
if (remainLineCnt <= 0)
{
break;
}
}
strs.AddRange(fileStrs);
// 输出创建txt文件
StringBuilder sb = new StringBuilder();
foreach (var s in strs)
{
sb.Append(s+ "\r\n");
}
var resultPath = Environment.CurrentDirectory + "\\软著代码.txt";
if (File.Exists(resultPath))
{
File.Delete(resultPath);
}
System.IO.File.AppendAllText(resultPath, sb.ToString());
string message = "生成了文件:" + resultPath + "\n" + "实际行数:" + strs.Count;
EditorUtility.DisplayDialog("输出成功", message, "好的", "好的");
}
// 输入路径,输出文件内容(去除注释、空行)
private static List<string> PathToStrs(string path)
{
StringBuilder sb = new StringBuilder();
StreamReader sr = new StreamReader(path);
string curStr = sr.ReadLine();
while (curStr != null)
{
sb.Append(curStr + "\r\n");
curStr = sr.ReadLine();
}
var str = sb.ToString();
// 剔除注释和空行
str = Regex.Replace(str, @"/\*[\s\S]*?\*/", string.Empty, RegexOptions.IgnoreCase);
str = Regex.Replace(str, @"^\s*//[\s\S]*?$", string.Empty, RegexOptions.Multiline);
str = Regex.Replace(str, @"^\s*$\n", string.Empty, RegexOptions.Multiline);
str = Regex.Replace(str, @"^\s*//[\s\S]*", string.Empty, RegexOptions.Multiline);
List<string> strs = new List<string>();
var strsArray = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
strs.AddRange(strsArray);
return strs;
}
}
#endif
输出总代码行数
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
public class Tools
{
[MenuItem("输出总代码行数/输出")]
private static void PrintTotalLine()
{
string str = "Assets/Scripts";
str = "Assets";
string[] fileName = Directory.GetFiles(str, "*.cs", SearchOption.AllDirectories);
int totalLine = 0;
foreach (var temp in fileName)
{
int nowLine = 0;
StreamReader sr = new StreamReader(temp);
while (sr.ReadLine() != null)
{
nowLine++;
}
//文件名+文件行数
//Debug.Log(String.Format("{0}——{1}", temp, nowLine));
totalLine += nowLine;
}
Debug.Log(string.Format("总代码行数:{0}", totalLine));
}
}