forked from kiwon0319/GFDecompress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
205 lines (176 loc) · 6.8 KB
/
Program.cs
File metadata and controls
205 lines (176 loc) · 6.8 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using GFDecompress.STC;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
namespace GFDecompress
{
class Program
{
private static readonly Logger log = LogManager.GetCurrentClassLogger();
#region .dat 복호화
// .dat 복호화
public static string DatFileDecompress(byte[] data, byte[] key)
{
byte[] temp = Xor(data, key);
byte[] output;
using (var compressedStream = new MemoryStream(temp))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
output = resultStream.ToArray();
}
return Encoding.UTF8.GetString(output);
}
public static byte[] Xor(byte[] text, byte[] key)
{
byte[] xor = new byte[text.Length];
for (int i = 0; i < text.Length; i++)
{
xor[i] = (byte)(text[i] ^ key[i % key.Length]);
}
return xor;
}
#endregion
#region .stc 파싱
// .stc 파싱
public static JArray ParseStc(string stcFile, int startOffset = 0)
{
log.Info(".stc parse >> {0}", stcFile);
JArray output = new JArray();
// stc 읽기
byte[] stcStream = File.ReadAllBytes(stcFile);
StcBinaryReader reader = new StcBinaryReader(stcStream);
int code = reader.ReadUShort(); // 예: 5005
int unknown = reader.ReadUShort(); // ??
log.Debug("file: {0}, code: {1}, unknown: {2}", stcFile, code, unknown);
int row = reader.ReadUShort();
int col = reader.ReadByte();
if (row > 0 && col > 0)
{
// 컬럼별 크기
List<string> colSizes = new List<string>();
for (int i = 0; i < col; i++)
{
int size = reader.ReadByte();
switch (size)
{
case 5:
colSizes.Add(i + ":" + "Integer");
break;
case 11:
colSizes.Add(i + ":" + "String");
break;
default:
colSizes.Add(i + ":" + "Unknown");
break;
}
}
log.Debug("column_info >> {0}", string.Join("|", colSizes));
// 실제 정보가 있는 오프셋으로 이동
reader._offset = startOffset;
try
{
for (int i = 0; i < row; i++)
{
switch (stcFile)
{
// GunList
case "5005.stc":
output.Add(JObject.FromObject(new Gun(reader)));
break;
// SquadList
case "5006.stc":
output.Add(JObject.FromObject(new Squad(reader)));
break;
}
}
}
catch (Exception ex)
{
log.Error(ex);
}
}
return output;
}
#endregion
static void Main(string[] args)
{
#region NLog Configuration
var config = new LoggingConfiguration();
var logconsole = new ColoredConsoleTarget("logconsole");
logconsole.Layout = new SimpleLayout() { Text = "${message} ${exception:format=tostring}" };
config.AddRule(LogLevel.Debug, LogLevel.Fatal, logconsole);
LogManager.Configuration = config;
#endregion
Console.OutputEncoding = Encoding.UTF8;
// catchdata.dat 복호화
try
{
// 복호화
log.Info(".dat decrypt >> {0}", "catchdata.dat");
byte[] data = File.ReadAllBytes("catchdata.dat");
byte[] key = Encoding.ASCII.GetBytes("c88d016d261eb80ce4d6e41a510d4048");
string output = DatFileDecompress(data, key);
// 폴더 생성
if (!Directory.Exists("output_catchdata"))
Directory.CreateDirectory("output_catchdata");
File.WriteAllText("output_catchdata\\catchdata.txt", output);
// 정보별 추출
string[] lines = output.Split('\n').Select(p => p.Trim()).ToArray();
foreach (string line in lines)
{
try
{
if (string.IsNullOrEmpty(line))
continue;
JObject json = JObject.Parse(line);
string jKey = json.Properties().Select(p => p.Name).FirstOrDefault();
log.Debug(".dat export >> " + jKey);
File.WriteAllText("output_catchdata\\" + jKey + ".txt", json.ToString());
}
catch (Exception ex)
{
log.Error(ex);
}
}
// 폴더 열기
Process.Start(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\output_catchdata");
}
catch (Exception ex)
{
log.Error(ex);
}
// *.stc 파싱
try
{
// 폴더 생성
if (!Directory.Exists("output_stc"))
Directory.CreateDirectory("output_stc");
// 인형 정보
JArray GunList = ParseStc("5005.stc", 102);
File.WriteAllText("output_stc\\gun_list.txt", GunList.ToString());
// 중장비 정보 - 파싱 전 중장비 컬럼 순서 정렬 필요! (Squad.cs)
//JArray SquadList = ParseStc("input\\5006.stc", 73);
//File.WriteAllText("output_stc\\squad_list.txt", SquadList.ToString());
// 폴더 열기
Process.Start(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\output_stc");
}
catch (Exception ex)
{
log.Error(ex);
}
}
}
}