-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
166 lines (141 loc) · 6.31 KB
/
Program.cs
File metadata and controls
166 lines (141 loc) · 6.31 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using pckkey.Core;
using pckkey.Core.ArchiveEngine;
using pckkey.Interfaces;
using static pckkey.Core.Events;
namespace pckkey
{
internal class Program
{
public static List<ArchiveKey> Keys = new List<ArchiveKey>();
private static readonly object lockObject = new object();
private static Dictionary<string, int> fileCursorPositions = new Dictionary<string, int>();
private static int initialCursorTop;
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine("PCK Key Changer - Haly");
Console.WriteLine();
Console.WriteLine("GitHub: https://github.com/halysondev/PckKeyChanger");
Console.WriteLine();
// Prompt the user to enter the old keys, with default values if not provided
Console.WriteLine("Enter the old key 1 (leave empty for default -1466731422):");
string oldKey1Input = Console.ReadLine();
int oldKey1 = string.IsNullOrWhiteSpace(oldKey1Input) ? -1466731422 : int.Parse(oldKey1Input);
Console.WriteLine("Enter the old key 2 (leave empty for default -240896429):");
string oldKey2Input = Console.ReadLine();
int oldKey2 = string.IsNullOrWhiteSpace(oldKey2Input) ? -240896429 : int.Parse(oldKey2Input);
Keys.Add(new ArchiveKey
{
Name = "Perfect World",
KEY_1 = oldKey1,
KEY_2 = oldKey2,
ASIG_1 = -33685778,
ASIG_2 = -267534609,
FSIG_1 = 1305093103,
FSIG_2 = 1453361591
});
// Prompt the user to enter the directory where the .pck files are located
Console.WriteLine("Enter the folder path where the .pck files are located (leave empty to use current directory):");
string folderPath = Console.ReadLine();
// Use current directory if no path is provided
if (string.IsNullOrWhiteSpace(folderPath))
{
folderPath = Directory.GetCurrentDirectory();
}
// Prompt the user to enter the new keys and compression level
Console.WriteLine("Enter the new key 1:");
string newKey1Input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(newKey1Input))
{
Console.WriteLine("New key 1 is required. Exiting...");
return;
}
int newKey1 = int.Parse(newKey1Input);
Console.WriteLine("Enter the new key 2:");
string newKey2Input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(newKey2Input))
{
Console.WriteLine("New key 2 is required. Exiting...");
return;
}
int newKey2 = int.Parse(newKey2Input);
Console.WriteLine("Enter the compression level (1-9):");
int compressionLevel = int.Parse(Console.ReadLine());
// Get the initial cursor position
initialCursorTop = Console.CursorTop;
// Get the list of .pck files in the specified directory and subdirectories
string[] pckFiles = Directory.GetFiles(folderPath, "*.pck", SearchOption.AllDirectories);
// Process files in parallel
Parallel.ForEach(pckFiles, (pckFile, state, index) =>
{
string fileName = Path.GetFileName(pckFile);
int progressMax = 0;
int progressCurrent = 0;
int filePosition;
lock (lockObject)
{
// Calculate the cursor position for the file
filePosition = initialCursorTop + (int)index;
if (filePosition >= Console.BufferHeight)
{
Console.SetBufferSize(Console.BufferWidth, filePosition + 1);
}
fileCursorPositions[fileName] = filePosition;
Console.SetCursorPosition(0, filePosition);
Console.WriteLine($"Processing file: {fileName}");
}
using (ArchiveManager archive = new ArchiveManager(pckFile, Keys[0]))
{
// Subscribe to progress events
archive.SetProgressMax += (max) =>
{
progressMax = max;
UpdateProgress(fileName, progressCurrent, progressMax);
};
archive.SetProgress += (progress) =>
{
progressCurrent = progress;
UpdateProgress(fileName, progressCurrent, progressMax);
};
archive.SetProgressNext += () =>
{
progressCurrent++;
UpdateProgress(fileName, progressCurrent, progressMax);
};
archive.ReadFileTable();
archive.ChangeKeys(newKey1, newKey2, compressionLevel);
archive.Dispose();
}
lock (lockObject)
{
filePosition = fileCursorPositions[fileName];
Console.SetCursorPosition(0, filePosition);
Console.WriteLine($"File {fileName} processed successfully.");
}
});
Console.WriteLine("Processing completed.");
Console.ReadLine();
}
static void UpdateProgress(string fileName, int progressCurrent, int progressMax)
{
lock (lockObject)
{
if (progressMax > 0 && progressCurrent > 0)
{
int filePosition = fileCursorPositions[fileName];
// Move the cursor to the stored position for the file and update the progress
if (filePosition < Console.BufferHeight)
{
Console.SetCursorPosition(0, filePosition);
Console.WriteLine($"Progress for {fileName}: {progressCurrent}/{progressMax}");
}
}
}
}
}
}