| 赞 | 1 |
| VIP | |
| 好人卡 | |
| 积分 | 1 |
| 经验 | |
| 最后登录 | 2017-7-23 |
| 在线时间 | 56 小时 |
- 梦石
- 0
- 星屑
- 50
- 在线时间
- 56 小时
- 注册时间
- 2016-3-23
- 回帖
- 13
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有账号?注册会员
×
本帖最后由 大钊 于 2016-4-2 13:43 编辑
之前看到图书馆里有脚本拆分的。。。但苦于没法运行。。。所以自己动手丰衣足食。。。
写了个工具来拆分脚本。。
使用方法,将 RMMVScriptSpliter.exe 扔到项目根目录,然后运行。
运行完成之后js目录会变成这样:
index.html文件会变成这样:
index.html
如果不满意的话可以再次运行程序,然后按R还原,不过需要说明的是,这里的还原只是把备份还原了,并不是重新拼接,所以如果在拆分后改动了原生脚本里面的东西还原后是不会保留的。
当然一般也不提倡直接修改原生脚本,有bug可以用插件修。
然后运行结果:
源代码:
Program.cs- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace RMMVScriptSpliter {
- class Program {
- static Regex FileHeaderPattern = new Regex(@"\s*//\s*={10,}\s*//\s*(?<name>.*?)\s*//\s*={10,}\s*", RegexOptions.Compiled);
- static Regex SectionPattern = new Regex(@"//\s*-{10,}\s*(//\s*(?<name>.*?)|/\*[^/]*?@class\s*(?<name>\w+)[^/]*\*/)[\r\n][\s\S]*?(?:(?=//\s*-{10,})|$\s*)", RegexOptions.Compiled);
- static Regex IndexBodyPattern = new Regex(@"(?<=<body[^>]*>)[\s\S]*?(?=\s+</body>)", RegexOptions.Compiled);
- static string JsDir = "js";
- static string JsDirBackup = "js.bak";
- static string JsExt = ".js";
- static string JsBackupExt = ".js.bak";
- static string IndexFile = "index.html";
- static string IndexBackup = "index.html.bak";
- static string[] LibDirs = { "libs" };
- static string[] FilesToKeep = { "plugins", "main", };
- static string[] FilesToProcess = { "rpg_core", "rpg_managers", "rpg_objects", "rpg_scenes", "rpg_sprites", "rpg_windows" };
- static int start;
- static List<string> files = new List<string>();
- static string I(string s) {
- return Path.Combine(JsDirBackup, s + JsBackupExt);
- }
- static string O(string s) {
- return Path.Combine(JsDir, s + JsExt);
- }
- static void Main(string[] args) {
- try {
- if (!File.Exists(IndexFile) || !Directory.Exists(JsDir)) {
- Console.WriteLine("请将该程序放置到项目目录下");
- Console.WriteLine("按任意键退出...");
- Console.ReadKey();
- return;
- }
- if (Directory.Exists(JsDirBackup) || File.Exists(IndexBackup)) {
- Console.WriteLine("该目录已经经过处理,不用再次处理。");
- Console.WriteLine("按R还原,其他任意键退出...");
- if (Console.ReadKey().Key == ConsoleKey.R) Rollback();
- return;
- }
- Backup();
- foreach (var e in LibDirs) files.AddRange(Directory.GetFiles(Path.Combine(JsDir, e)));
- foreach (var e in FilesToProcess) files.AddRange(SplitFile(e));
- foreach (var e in FilesToKeep) Keep(e);
- GenerateIndexFile();
- Console.WriteLine("处理完成,按任意键退出...");
- Console.ReadKey();
- Console.WriteLine();
- } catch (Exception e) {
- Console.WriteLine("处理时出错。有问题请联系作者并附上错误内容,错误内容:");
- Console.WriteLine(e.Message);
- Console.WriteLine("按任意键退出...");
- Console.ReadKey();
- Console.WriteLine();
- }
- }
- static void Keep(string file) {
- var m = O(file);
- File.Copy(I(file), m);
- files.Add(m);
- }
- static void assert(string file, Match m) {
- if (m.Index != start) throw new Exception("something has been lost when processing file '" + file + "' before match: \n" + m.Value.Substring(0, 100));
- start += m.Length;
- }
- static IEnumerable<String> SplitFile(string filename) {
- var text = File.ReadAllText(I(filename));
- start = 0;
- var m = FileHeaderPattern.Match(text);
- var header = m.Value;
- assert(filename, m);
- var ms = SectionPattern.Matches(text);
- LinkedList<String> files = new LinkedList<string>();
- foreach (Match e in ms) {
- var name = e.Groups["name"].Value;
- var content = e.Value;
- if (!Directory.Exists(Path.Combine(JsDir, filename))) Directory.CreateDirectory(Path.Combine(JsDir, filename));
- var outName = O(Path.Combine(filename, name));
- files.AddLast(outName);
- File.WriteAllText(outName, content);
- assert(filename, e);
- }
- if (start != text.Length) throw new Exception("something has been lost at the end of file: " + filename);
- return files;
- }
- static void GenerateIndexFile() {
- string s = File.ReadAllText(IndexBackup);
- s = IndexBodyPattern.Replace(s, g => "<!--Processed-->\r\n" + String.Join("\r\n", files.Select(f => String.Format("\t\t<script type="text/javascript" src="{0}"></script>", f))));
- File.WriteAllText(IndexFile, s);
- }
- static void Backup() {
- Console.WriteLine("创建备份...");
- Directory.CreateDirectory(JsDirBackup);
- var ss = Directory.GetFiles(JsDir, "*" + JsExt);
- if (ss != null) foreach (var s in ss) File.Move(s, s.Replace(JsDir, JsDirBackup));
- File.Copy(IndexFile, IndexBackup);
- }
- static void Rollback() {
- Console.WriteLine();
- Console.WriteLine("开始还原");
- var ss = Directory.GetFiles(JsDir, "*" + JsExt);
- if (ss != null) foreach (var s in ss) File.Delete(s);
- foreach (var e in FilesToProcess) {
- var d = Path.Combine(JsDir, e);
- if (Directory.Exists(d)) Directory.Delete(d, true);
- }
- ss = Directory.GetFiles(JsDirBackup, "*" + JsBackupExt);
- if (ss != null) foreach (var s in ss) File.Move(s, s.Replace(JsDirBackup, JsDir));
- Directory.Delete(JsDirBackup, true);
- if (File.Exists(IndexBackup)) {
- if (File.Exists(IndexFile)) File.Delete(IndexFile);
- File.Move(IndexBackup, IndexFile);
- }
- Console.WriteLine("还原完成,按任意键退出...");
- Console.ReadKey();
- }
- }
- }
复制代码
东西在这,用之前建议先手动备份下,毕竟昨晚才做出来的,测试不一定充分,要把各位项目玩坏了罪过就大了。
RMMVScriptSpliter.zip
(4.57 KB, 下载次数: 73)
另外。我之前写的那个消息插件
https://rpg.blue/forum.php?mod=v ... d=390820&page=1
听到说最好支持fuki对话框,今天已经搞出来了,那边已经更新。 |
|