/
ReplayReader.cs
169 lines (154 loc) · 6.37 KB
/
ReplayReader.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;
namespace PYHHelper
{
class ReplayReader
{
public static Dictionary<string, int> PropIndex = new Dictionary<string, int>()
{
{"分", 1},
{"P1副机", 9},
{"P2副机", 11},
{"P1", 14},
{"P2", 16},
{"时", 37},
{"P1主机", 40},
{"P2主机", 42},
{"日", 44},
{"年", 46},
{"秒", 52},
{"月", 57},
};
public static int Switch12P(int index)
{
if (index == 14) return 16;
if (index == 16) return 14;
if (index == 9) return 11;
if (index == 11) return 9;
if (index == 40) return 42;
if (index == 42) return 40;
return index;
}
private static byte[] PacketBegin = new byte[] {0x10, 0x00, 0x00, 0x08};
private static byte[] IntBegin = new byte[] {0x02, 0x00, 0x00, 0x05};
static byte[] Slice(byte[] arr, int offset, int length)
{
return arr.Skip(offset).Take(length).ToArray();
}
public static List<string> Open(string fileName)
{
byte[] rep = File.ReadAllBytes(fileName);
Int32 metaLength = BitConverter.ToInt32(Slice(rep, 13, 4), 0);
byte[] CompressedMetaData = Slice(rep, 21, metaLength);
byte[] RestData = Slice(rep, 21 + metaLength, rep.Length - 21 + metaLength);
//File.WriteAllBytes("E:\\restData.txt", RestData);
byte[] MetaData = Inflate(CompressedMetaData);
var Names = new List<string>();
for (int i = 0; i < MetaData.Length; i++)
{
if (Slice(MetaData, i, 4).SequenceEqual(PacketBegin))
{
int nameLength = BitConverter.ToInt32(Slice(MetaData, i + 4, 4), 0);
byte[] packetNameArray = Slice(MetaData, i + 8, nameLength);
string packetName = Encoding.GetEncoding("Shift_JIS").GetString(packetNameArray);
Names.Add(packetName);
i += 8;
i += nameLength;
i--;
}
else if(Slice(MetaData, i, 4).SequenceEqual(IntBegin))// Int
{
int value = BitConverter.ToInt32(Slice(MetaData, i + 4, 4), 0);
Names.Add(value.ToString());
i += 8;
i--;
}
}
return Names;
}
public static void ModifyRep(string fileName)
{
byte[] rep = File.ReadAllBytes(fileName);
Int32 metaLength = BitConverter.ToInt32(Slice(rep, 13, 4), 0);
byte[] CompressedMetaData = Slice(rep, 21, metaLength);
byte[] RestData = Slice(rep, 21 + metaLength, rep.Length - 21 + metaLength);
//File.WriteAllBytes("E:\\restData.txt", RestData);
byte[] MetaData = Inflate(CompressedMetaData);
MetaData = SetValue(MetaData,"slave_name",1,"usami");
MetaData = SetValue(MetaData, "slave_name", 0, "usami");
var compressedMeta = Ionic.Zlib.ZlibStream.CompressBuffer(MetaData);
List<byte> ret = new List<byte>();
ret.AddRange(Slice(rep, 0, 9));
byte[] placeholder = {0, 0, 0, 0};
byte[] length_8 = BitConverter.GetBytes(compressedMeta.Length + 8);
byte[] length = BitConverter.GetBytes(compressedMeta.Length);
byte[] uncompresslength = BitConverter.GetBytes(MetaData.Length);
ret.AddRange(length_8);
ret.AddRange(length);
ret.AddRange(uncompresslength);
ret.AddRange(compressedMeta);
ret.AddRange(RestData);
File.WriteAllBytes(fileName + ".mod.rep",ret.ToArray());
//File.Open(fileName + ".mod.rep")
}
private static byte[] SetValue(byte[] MetaData, string prop , int index, string value)
{
bool found = false;
bool found_index = false;
for (int i = 0; i < MetaData.Length; i++)
{
if (Slice(MetaData, i, 4).SequenceEqual(PacketBegin))
{
int nameLength = BitConverter.ToInt32(Slice(MetaData, i + 4, 4), 0);
byte[] packetNameArray = Slice(MetaData, i + 8, nameLength);
string packetName = Encoding.GetEncoding("Shift_JIS").GetString(packetNameArray);
if (packetName == prop)
found = true;
if (found_index)
{
List<byte> ret = new List<byte>();
ret.AddRange(Slice(MetaData,0,i));
byte[] str = Encoding.GetEncoding("Shift_JIS").GetBytes(value);
byte[] length = BitConverter.GetBytes(str.Length);
//byte[] placeholder = {0, 0, 0, 0};
ret.AddRange(PacketBegin);
ret.AddRange(length);
ret.AddRange(str);
ret.AddRange(MetaData.Skip(i + 8 + nameLength));
return ret.ToArray();
}
i += 8;
i += nameLength;
i--;
}
else if (Slice(MetaData, i, 4).SequenceEqual(IntBegin))// Int
{
if (found)
{
int index_value = BitConverter.ToInt32(Slice(MetaData, i + 4, 4), 0);
if (index_value == index)
found_index = true;
i += 8;
i--;
}
}
}
return MetaData;
}
private static byte[] Inflate(byte[] data)
{
MemoryStream outStream = new MemoryStream();
MemoryStream stream = new MemoryStream();
var ds = new DeflateStream(stream, CompressionMode.Decompress);
stream.Write(data, 2, data.Length - 6);
stream.Seek(0, SeekOrigin.Begin);
ds.CopyTo(outStream);
return outStream.ToArray();
}
}
}