@@ -5,8 +5,12 @@ use walkdir::WalkDir;
5
5
use crate :: command:: patch:: xml_patcher;
6
6
7
7
pub fn patch_assets ( patch : & PathBuf , unpacked : & PathBuf , temp_dir : & PathBuf ) -> anyhow:: Result < PathBuf > {
8
+ println ! ( "Patching assets.." ) ;
8
9
let patched_assets = temp_dir. join ( "patched" ) ;
10
+ std:: fs:: create_dir_all ( & patched_assets)
11
+ . context ( "Failed to create patched assets directory" ) ?;
9
12
13
+ // copy over original files and if they have a patch, apply the patch
10
14
for file in WalkDir :: new ( unpacked) {
11
15
let file = file. map_err ( |e| anyhow:: anyhow!( "Failed to walk directory: {}" , e) ) ?;
12
16
let rel_path = file. path ( ) . strip_prefix ( unpacked)
@@ -28,7 +32,7 @@ pub fn patch_assets(patch: &PathBuf, unpacked: &PathBuf, temp_dir: &PathBuf) ->
28
32
// check if file exists in patch directory
29
33
let patch_file = patch. join ( rel_path) ;
30
34
if !patch_file. exists ( ) { // patch file doesn't exist, so copy over the original
31
- copy_file ( & file, rel_path, & patched_assets) ?;
35
+ copy_file ( & file. path ( ) , rel_path, & patched_assets) ?;
32
36
continue ;
33
37
}
34
38
@@ -38,34 +42,63 @@ pub fn patch_assets(patch: &PathBuf, unpacked: &PathBuf, temp_dir: &PathBuf) ->
38
42
// copy over the patch file if it's a png, csv or txt file
39
43
// TODO: csv and txt patching
40
44
if ext == OsStr :: new ( "png" ) || ext == OsStr :: new ( "csv" ) || ext == OsStr :: new ( "txt" ) {
41
- copy_file ( & file, rel_path, & patched_assets) ?;
42
- } else if ext == OsStr :: new ( "xml" ) || ext == OsStr :: new ( "xml" ) {
45
+ println ! ( "Copying patch file for: {}" , rel_path. display( ) ) ;
46
+ copy_file ( & patch_file. as_path ( ) , rel_path, & patched_assets) ?;
47
+ } else if ext == OsStr :: new ( "xml" ) || ext == OsStr :: new ( "fnt" ) {
48
+ println ! ( "Patching xml file: {}" , rel_path. display( ) ) ;
43
49
patch_xml ( & file, & patch_file, rel_path, & patched_assets) ?;
44
50
} else {
45
51
anyhow:: bail!( "Unsupported file type: {}" , patch_file. display( ) ) ;
46
52
}
47
53
48
54
}
49
55
56
+ // Loop over any files newly added with the patch
57
+ for file in WalkDir :: new ( patch) {
58
+ let file = file. map_err ( |e| anyhow:: anyhow!( "Failed to walk directory: {}" , e) ) ?;
59
+ let rel_path = file. path ( ) . strip_prefix ( patch)
60
+ . context ( "Failed to strip prefix" ) ?;
61
+ let target = patched_assets. join ( rel_path) ;
62
+ let file_type = file. file_type ( ) ;
63
+
64
+ // if file is a directory, just create it in the patched assets directory
65
+ if file_type. is_dir ( ) && !target. exists ( ) {
66
+ std:: fs:: create_dir_all ( & target) . context ( "Failed to create directory" ) ?;
67
+ continue ;
68
+ }
69
+
70
+ // skip symlinks etc.
71
+ if !file_type. is_file ( ) {
72
+ continue ;
73
+ }
74
+
75
+ // copy over the file if it doesn't exist already
76
+ if !target. exists ( ) {
77
+ println ! ( "Adding new file: {}" , rel_path. display( ) ) ;
78
+ copy_file ( & file. path ( ) , rel_path, & patched_assets) ?;
79
+ }
80
+ }
81
+
50
82
return Ok ( patched_assets) ;
51
83
}
52
84
53
- /// Copies a file from the unpacked assets to the patched assets directory and makes sure the directory structure is created
54
- fn copy_file ( file : & walkdir:: DirEntry , rel_path : & Path , patched_assets : & PathBuf ) -> anyhow:: Result < ( ) > {
85
+ /// Copies a file from one of the input directories to the patched assets directory and makes sure
86
+ /// the directory structure is created
87
+ fn copy_file ( file : & Path , rel_path : & Path , patched_assets : & PathBuf ) -> anyhow:: Result < ( ) > {
55
88
let output = patched_assets. join ( rel_path) ;
56
89
if let Some ( parent) = output. parent ( ) {
57
90
std:: fs:: create_dir_all ( parent) . context ( "Failed to create directory" ) ?;
58
91
}
59
- std:: fs:: copy ( file. path ( ) , & output) . context ( "Failed to copy file" ) ?;
92
+ std:: fs:: copy ( file, & output) . context ( "Failed to copy file" ) ?;
60
93
Ok ( ( ) )
61
94
}
62
95
63
96
/// Patches an XML file using the given patch file and writes the output to the patched assets directory
64
- fn patch_xml ( file : & walkdir:: DirEntry , patch_file : & PathBuf , rel_path : & Path , patched_assets : & PathBuf ) -> anyhow:: Result < ( ) > {
97
+ fn patch_xml ( original : & walkdir:: DirEntry , patch_file : & PathBuf , rel_path : & Path , patched_assets : & PathBuf ) -> anyhow:: Result < ( ) > {
65
98
let output = patched_assets. join ( rel_path) ;
66
99
if let Some ( parent) = output. parent ( ) {
67
100
std:: fs:: create_dir_all ( parent)
68
101
. context ( "Failed to create directory" ) ?;
69
102
}
70
- xml_patcher:: patch ( file . path ( ) , patch_file, & output)
103
+ xml_patcher:: patch ( original . path ( ) , patch_file, & output)
71
104
}
0 commit comments