-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcia-unix.cr
160 lines (136 loc) · 4.73 KB
/
cia-unix.cr
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require "colorize"
log : File = File.new "cia-unix.log", "w"
log.puts Time.utc.to_s
# dependencies check
tools = ["./ctrtool", "./ctrdecrypt", "./makerom", "seeddb.bin"]
tools.each do |tool|
case tool
when "./ctrtool", "./ctrdecrypt", "./makerom"
if !File.exists? %x[which #{tool}].chomp
log.delete if File.exists? "cia-unix.log"
download_dep
abort "#{tool.lchop("./").colorize.mode(:bold)} not found. Make sure it's located in the #{"same directory".colorize.mode(:underline)}" if !File.exists? tool
end
when "seeddb.bin"
if !File.exists? tool
log.delete if File.exists? "cia-unix.log"
download_dep
abort "#{tool.colorize.mode(:bold)} not found. Make sure it's located in the #{"same directory".colorize.mode(:underline)}" if !File.exists? tool
end
end
end
def download_dep
print "Some #{"tools".colorize.mode(:bold)} are missing, do you want to download them? (y/n): "
if ["y", "Y"].includes? gets.to_s
system "./dltools.sh"
end
end
# roms presence check
if Dir["*.cia"].size.zero? && Dir["*.3ds"].size.zero?
log.delete if File.exists? "cia-unix.log"
abort "No #{"CIA".colorize.mode(:bold)}/#{"3DS".colorize.mode(:bold)} roms were found."
end
def check_decrypt(name : String, ext : String)
if File.exists? "#{name}-decrypted.#{ext}"
puts "Decryption completed\n".colorize.mode(:underline)
else
puts "Decryption failed\n".colorize.mode(:underline)
end
end
def gen_args(name : String, part_count : Int32) : String
args : String = ""
part_count.times do |partition|
if File.exists? "#{name}.#{partition}.ncch"
args += "-i '#{name}.#{partition}.ncch:#{partition}:#{partition}' "
end
end
return args
end
# cache cleanup
def remove_cache
puts "Removing cache..."
Dir["*-decfirst.cia"].each do |fname| File.delete(fname) end
Dir["*.ncch"].each do |fname| File.delete(fname) end
end
args : String = ""
# 3ds decrypting
Dir["*.3ds"].each do |ds|
next if ds.includes? "decrypted"
args = ""
i : UInt8 = 0
dsn : String = ds.chomp ".3ds"
puts "Decrypting: #{ds.colorize.mode(:bold)}..."
log.puts %x[./ctrdecrypt '#{ds}']
Dir["#{dsn}.*.ncch"].each do |ncch|
case ncch
when "#{dsn}.Main.ncch"
i = 0
when "#{dsn}.Manual.ncch"
i = 1
when "#{dsn}.DownloadPlay.ncch"
i = 2
when "#{dsn}.Partition4.ncch"
i = 3
when "#{dsn}.Partition5.ncch"
i = 4
when "#{dsn}.Partition6.ncch"
i = 5
when "#{dsn}.N3DSUpdateData.ncch"
i = 6
when "#{dsn}.UpdateData.ncch"
i = 7
end
args += "-i '#{ncch}:#{i}:#{i}' "
end
puts "Building decrypted #{dsn} 3DS..."
log.puts %x[./makerom -f cci -ignoresign -target p -o '#{dsn}-decrypted.3ds' #{args}]
check_decrypt(dsn, "3ds")
remove_cache
end
# cia decrypting
Dir["*.cia"].each do |cia|
next if cia.includes? "decrypted"
puts "Decrypting: #{cia.colorize.mode(:bold)}..."
cutn : String = cia.chomp ".cia"
args = ""
content = %x[./ctrtool --seeddb=seeddb.bin '#{cia}']
# game
if content.match /T.*d.*00040000/
puts "CIA Type: Game"
log.puts %x[./ctrdecrypt '#{cia}']
i : UInt8 = 0
Dir["*.ncch"].sort.each do |ncch|
args += "-i '#{ncch}:#{i}:#{i}' "
i += 1
end
log.puts %x[./makerom -f cia -ignoresign -target p -o '#{cutn}-decfirst.cia' #{args}]
# patch
elsif content.match /T.*d.*0004000(e|E)/
puts "CIA Type: #{"Patch".colorize.mode(:bold)}"
log.puts %x[./ctrdecrypt '#{cia}']
patch_parts : Int32 = Dir["#{cutn}.*.ncch"].size
args = gen_args(cutn, patch_parts)
log.puts %x[./makerom -f cia -ignoresign -target p -o '#{cutn} (Patch)-decrypted.cia' #{args}]
check_decrypt("#{cutn} (Patch)", "cia")
# dlc
elsif content.match /T.*d.*0004008(c|C)/
puts "CIA Type: #{"DLC".colorize.mode(:bold)}"
log.puts %x[./ctrdecrypt '#{cia}']
dlc_parts : Int32 = Dir["#{cutn}.*.ncch"].size
args = gen_args(cutn, dlc_parts)
log.puts %x[./makerom -f cia -dlc -ignoresign -target p -o '#{cutn} (DLC)-decrypted.cia' #{args}]
check_decrypt("#{cutn} (DLC)", "cia")
else
abort "Unsupported CIA"
end
Dir["*-decfirst.cia"].each do |decfirst|
cutn = decfirst.chomp "-decfirst.cia"
puts "Building decrypted #{cutn} CCI..."
log.puts %x[./makerom -ciatocci '#{decfirst}' -o '#{cutn}-decrypted.cci']
check_decrypt(cutn, "cci")
end
remove_cache
end
log.flush
log.close
puts "Log saved"