Skip to content

Commit 0bcb8d4

Browse files
committed
20241225
1 parent 5010763 commit 0bcb8d4

File tree

10 files changed

+124
-8
lines changed

10 files changed

+124
-8
lines changed

src/PAL3Apatch/include/PAL3Apatch/common.h

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
#include <io.h>
178178
#include <errno.h>
179179
#include <stdint.h>
180+
#include <wchar.h>
180181

181182
// windows headers
182183
#include <windows.h>

src/PAL3Apatch/include/PAL3Apatch/wstr.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define PAL3APATCH_WSTR_H
33
// PATCHAPI DEFINITIONS
44

5+
extern PATCHAPI char *unicode_to_chinese(const wchar_t *s, const wchar_t *table);
56
extern PATCHAPI wchar_t *chinese_to_unicode(const char *s, const wchar_t *table);
67
extern PATCHAPI char *utf16_to_utf8(const wchar_t *s);
78
extern PATCHAPI wchar_t *utf8_to_utf16(const char *s);

src/PAL3Apatch/src/patch_fixpunctuation.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include "common.h"
22

3+
static char *convert_puncstring(const wchar_t *wstr)
4+
{
5+
switch (target_codepage) {
6+
case CODEPAGE_CHS: return unicode_to_chinese(wstr, gbktable);
7+
case CODEPAGE_CHT: return unicode_to_chinese(wstr, big5table);
8+
default: return wcs2cs_alloc(wstr, target_codepage);
9+
}
10+
}
11+
312
struct punc_node {
413
struct punc_node *next;
514
char *mbch;
@@ -20,7 +29,7 @@ static struct punc_node *make_punc_list(const wchar_t *wstr)
2029

2130
node = malloc(sizeof(struct punc_node));
2231
node->next = NULL;
23-
node->mbch = wcs2cs_alloc(buf, target_codepage);
32+
node->mbch = convert_puncstring(buf);
2433
assert(strlen(node->mbch) == 2);
2534

2635
if (!head) {

src/PAL3Apatch/src/patch_relativetimer.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ static DWORD WINAPI My_timeGetTime(VOID)
66
return timeGetTime() - timeOffset;
77
}
88

9+
static int usePerformanceCounter;
10+
11+
static LARGE_INTEGER countFrequency;
12+
static BOOL WINAPI My_QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
13+
{
14+
*lpFrequency = countFrequency;
15+
return TRUE;
16+
}
17+
918
static LARGE_INTEGER countOffset;
1019
static BOOL WINAPI My_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
1120
{
21+
if (!usePerformanceCounter) {
22+
lpPerformanceCount->QuadPart = My_timeGetTime();
23+
return TRUE;
24+
}
1225
BOOL ret = QueryPerformanceCounter(lpPerformanceCount);
1326
if (ret) {
1427
lpPerformanceCount->QuadPart -= countOffset.QuadPart;
@@ -18,16 +31,27 @@ static BOOL WINAPI My_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
1831

1932
MAKE_PATCHSET(relativetimer)
2033
{
21-
// set offsets
34+
TIMECAPS tc;
35+
if (timeGetDevCaps(&tc, sizeof(tc)) == TIMERR_NOERROR) {
36+
timeBeginPeriod(tc.wPeriodMin);
37+
}
2238
timeOffset = timeGetTime();
23-
if (!QueryPerformanceCounter(&countOffset)) {
24-
countOffset.QuadPart = 0;
39+
40+
if (QueryPerformanceFrequency(&countFrequency) && QueryPerformanceCounter(&countOffset)) {
41+
usePerformanceCounter = 1;
42+
} else {
43+
usePerformanceCounter = 0;
44+
countFrequency.QuadPart = 1000;
2545
}
2646

2747
// hook timeGetTime
2848
make_pointer(0x005581C8, My_timeGetTime);
2949
make_pointer(gboffset + 0x100D61F4, My_timeGetTime);
3050

3151
// hook QueryPerformanceCounter
52+
make_pointer(gboffset + 0x100D6084, My_QueryPerformanceFrequency);
3253
make_pointer(gboffset + 0x100D6080, My_QueryPerformanceCounter);
54+
55+
double highFactor = 4294967296.0;
56+
memcpy_to_process(gboffset + 0x100D6B20, &highFactor, sizeof(highFactor));
3357
}

src/PAL3Apatch/src/wstr.c

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
#include "common.h"
22

3+
char *unicode_to_chinese(const wchar_t *s, const wchar_t *table)
4+
{
5+
char *buf = malloc(wcslen(s) * 2 + 1);
6+
char *p = buf;
7+
while (*s) {
8+
unsigned short w = *s++;
9+
if (w < 0x80) {
10+
*p++ = w;
11+
} else {
12+
wchar_t *c = wmemchr(table, w, 0x8000); // very slow!!!
13+
if (c) {
14+
int i = (c - table) + 0x8000;
15+
*p++ = i >> 8;
16+
*p++ = i;
17+
} else {
18+
*p++ = '?';
19+
}
20+
}
21+
}
22+
*p = 0;
23+
return buf;
24+
}
25+
326
wchar_t *chinese_to_unicode(const char *s, const wchar_t *table)
427
{
528
wchar_t *buf = malloc(sizeof(wchar_t) * (strlen(s) + 1));

src/PAL3patch/include/PAL3patch/common.h

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
#include <io.h>
178178
#include <errno.h>
179179
#include <stdint.h>
180+
#include <wchar.h>
180181

181182
// windows headers
182183
#include <windows.h>

src/PAL3patch/include/PAL3patch/wstr.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define PAL3PATCH_WSTR_H
33
// PATCHAPI DEFINITIONS
44

5+
extern PATCHAPI char *unicode_to_chinese(const wchar_t *s, const wchar_t *table);
56
extern PATCHAPI wchar_t *chinese_to_unicode(const char *s, const wchar_t *table);
67
extern PATCHAPI char *utf16_to_utf8(const wchar_t *s);
78
extern PATCHAPI wchar_t *utf8_to_utf16(const char *s);

src/PAL3patch/src/patch_fixpunctuation.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include "common.h"
22

3+
static char *convert_puncstring(const wchar_t *wstr)
4+
{
5+
switch (target_codepage) {
6+
case CODEPAGE_CHS: return unicode_to_chinese(wstr, gbktable);
7+
case CODEPAGE_CHT: return unicode_to_chinese(wstr, big5table);
8+
default: return wcs2cs_alloc(wstr, target_codepage);
9+
}
10+
}
11+
312
struct punc_node {
413
struct punc_node *next;
514
char *mbch;
@@ -20,7 +29,7 @@ static struct punc_node *make_punc_list(const wchar_t *wstr)
2029

2130
node = malloc(sizeof(struct punc_node));
2231
node->next = NULL;
23-
node->mbch = wcs2cs_alloc(buf, target_codepage);
32+
node->mbch = convert_puncstring(buf);
2433
assert(strlen(node->mbch) == 2);
2534

2635
if (!head) {

src/PAL3patch/src/patch_relativetimer.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ static DWORD WINAPI My_timeGetTime(VOID)
66
return timeGetTime() - timeOffset;
77
}
88

9+
static int usePerformanceCounter;
10+
11+
static LARGE_INTEGER countFrequency;
12+
static BOOL WINAPI My_QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
13+
{
14+
*lpFrequency = countFrequency;
15+
return TRUE;
16+
}
17+
918
static LARGE_INTEGER countOffset;
1019
static BOOL WINAPI My_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
1120
{
21+
if (!usePerformanceCounter) {
22+
lpPerformanceCount->QuadPart = My_timeGetTime();
23+
return TRUE;
24+
}
1225
BOOL ret = QueryPerformanceCounter(lpPerformanceCount);
1326
if (ret) {
1427
lpPerformanceCount->QuadPart -= countOffset.QuadPart;
@@ -18,16 +31,27 @@ static BOOL WINAPI My_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
1831

1932
MAKE_PATCHSET(relativetimer)
2033
{
21-
// set offsets
34+
TIMECAPS tc;
35+
if (timeGetDevCaps(&tc, sizeof(tc)) == TIMERR_NOERROR) {
36+
timeBeginPeriod(tc.wPeriodMin);
37+
}
2238
timeOffset = timeGetTime();
23-
if (!QueryPerformanceCounter(&countOffset)) {
24-
countOffset.QuadPart = 0;
39+
40+
if (QueryPerformanceFrequency(&countFrequency) && QueryPerformanceCounter(&countOffset)) {
41+
usePerformanceCounter = 1;
42+
} else {
43+
usePerformanceCounter = 0;
44+
countFrequency.QuadPart = 1000;
2545
}
2646

2747
// hook timeGetTime
2848
make_pointer(0x0056A1C0, My_timeGetTime);
2949
make_pointer(gboffset + 0x100F5228, My_timeGetTime);
3050

3151
// hook QueryPerformanceCounter
52+
make_pointer(gboffset + 0x100F5080, My_QueryPerformanceFrequency);
3253
make_pointer(gboffset + 0x100F507C, My_QueryPerformanceCounter);
54+
55+
double highFactor = 4294967296.0;
56+
memcpy_to_process(gboffset + 0x100F5B30, &highFactor, sizeof(highFactor));
3357
}

src/PAL3patch/src/wstr.c

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
#include "common.h"
22

3+
char *unicode_to_chinese(const wchar_t *s, const wchar_t *table)
4+
{
5+
char *buf = malloc(wcslen(s) * 2 + 1);
6+
char *p = buf;
7+
while (*s) {
8+
unsigned short w = *s++;
9+
if (w < 0x80) {
10+
*p++ = w;
11+
} else {
12+
wchar_t *c = wmemchr(table, w, 0x8000); // very slow!!!
13+
if (c) {
14+
int i = (c - table) + 0x8000;
15+
*p++ = i >> 8;
16+
*p++ = i;
17+
} else {
18+
*p++ = '?';
19+
}
20+
}
21+
}
22+
*p = 0;
23+
return buf;
24+
}
25+
326
wchar_t *chinese_to_unicode(const char *s, const wchar_t *table)
427
{
528
wchar_t *buf = malloc(sizeof(wchar_t) * (strlen(s) + 1));

0 commit comments

Comments
 (0)