-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
142 lines (123 loc) · 5.13 KB
/
Copy pathProgram.cs
File metadata and controls
142 lines (123 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.IO;
namespace JMDParser
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
string sourceDir = null;
string targetDir = null;
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "-i" && i + 1 < args.Length)
{
sourceDir = args[++i];
}
else if (args[i] == "-o" && i + 1 < args.Length)
{
targetDir = args[++i];
}
}
if (string.IsNullOrEmpty(sourceDir))
{
sourceDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
}
if (string.IsNullOrEmpty(targetDir))
{
targetDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataRaw");
}
sourceDir = Path.GetFullPath(sourceDir);
targetDir = Path.GetFullPath(targetDir);
if (!Directory.Exists(sourceDir))
{
return;
}
var jmdFiles = Directory.GetFiles(sourceDir, "*.jmd", SearchOption.AllDirectories);
Array.Sort(jmdFiles);
bool movieCopied = false;
foreach (var jmdPath in jmdFiles)
{
string relJmd = GetRelativePath(sourceDir, jmdPath);
if (!movieCopied && string.Compare(relJmd, "movie", StringComparison.OrdinalIgnoreCase) > 0)
{
string sourceMovieDir = Path.Combine(sourceDir, "movie");
string targetMovieDir = Path.Combine(targetDir, "movie");
if (Directory.Exists(sourceMovieDir))
{
Console.WriteLine("Data\\movie");
CopyDirectory(sourceMovieDir, targetMovieDir);
}
movieCopied = true;
}
try
{
Console.WriteLine(Path.Combine("Data", relJmd));
string subpath = Path.GetDirectoryName(relJmd) ?? "";
string name = Path.GetFileNameWithoutExtension(relJmd);
string targetName = name;
if (targetName.EndsWith("_tex", StringComparison.OrdinalIgnoreCase))
{
targetName = targetName.Substring(0, targetName.Length - 4);
}
string parentDirName = Path.GetFileName(subpath);
bool isSoundBgm = relJmd.Replace('\\', '/').StartsWith("sound/bgm", StringComparison.OrdinalIgnoreCase);
bool shouldUnpackToParent = string.Equals(targetName, parentDirName, StringComparison.OrdinalIgnoreCase) || isSoundBgm;
string targetOutputDir;
if (shouldUnpackToParent)
{
targetOutputDir = Path.Combine(targetDir, subpath);
}
else
{
targetOutputDir = Path.Combine(targetDir, subpath, targetName);
}
using (var parser = new JmdLogic())
{
parser.Open(jmdPath);
parser.ExtractAll(targetOutputDir);
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Failed to process JMD {Path.GetFileName(jmdPath)}: {ex.Message}");
Console.ResetColor();
}
}
if (!movieCopied)
{
string sourceMovieDir = Path.Combine(sourceDir, "movie");
string targetMovieDir = Path.Combine(targetDir, "movie");
if (Directory.Exists(sourceMovieDir))
{
Console.WriteLine("Data\\movie");
CopyDirectory(sourceMovieDir, targetMovieDir);
}
}
}
private static string GetRelativePath(string relativeTo, string path)
{
var uri1 = new Uri(relativeTo.EndsWith("\\") || relativeTo.EndsWith("/") ? relativeTo : relativeTo + "\\");
var uri2 = new Uri(path);
var relativeUri = uri1.MakeRelativeUri(uri2);
string relPath = Uri.UnescapeDataString(relativeUri.ToString());
return relPath.Replace('/', '\\');
}
private static void CopyDirectory(string source, string target)
{
Directory.CreateDirectory(target);
foreach (string file in Directory.GetFiles(source))
{
string dest = Path.Combine(target, Path.GetFileName(file));
File.Copy(file, dest, true);
}
foreach (string folder in Directory.GetDirectories(source))
{
string dest = Path.Combine(target, Path.GetFileName(folder));
CopyDirectory(folder, dest);
}
}
}
}