Skip to content

Commit 676fe5c

Browse files
committed
Remove erroneous hardcoded custom instruction to be inserted and switch to proper lookup
- Added frozen external depedency (https://github.com/serge-sans-paille/frozen/) - Simplify custom VM instruction implementation (DRY) - Added compile-time checks for custom instruction insertion - Switched to C++-style lambda function-like macros
1 parent 183a6db commit 676fe5c

5 files changed

Lines changed: 261 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.25)
22
project(subsdk CXX C ASM)
33

4+
include(FetchContent)
5+
46
## Error if not using switch toolchain file
57
if (NOT SWITCH)
68
message(FATAL_ERROR "Not targeting switch, make sure to specify -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain.cmake")
@@ -44,6 +46,14 @@ set_target_properties(cJSON PROPERTIES
4446
C_STANDARD 90
4547
)
4648

49+
FetchContent_Declare(
50+
frozen
51+
GIT_REPOSITORY https://github.com/serge-sans-paille/frozen
52+
GIT_TAG "1.2.0"
53+
)
54+
55+
FetchContent_MakeAvailable(frozen)
56+
4757
add_executable(subsdk9
4858
${SOURCES_ASM}
4959
${SOURCES_C}
@@ -55,7 +65,7 @@ add_executable(subsdk9
5565
${SKYLINE_SOURCES}
5666
)
5767

58-
target_link_libraries(subsdk9 cJSON)
68+
target_link_libraries(subsdk9 cJSON frozen)
5969

6070
set_target_properties(subsdk9 PROPERTIES
6171
C_STANDARD 17

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ all:
44
cmake --preset Release . && cmake --build . --preset Release
55

66
clean:
7-
rm -r build || true
7+
cmake --build . --preset Release --target clean

THIRDPARTY.md

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
- `vendor/exlaunch`: [exlaunch](https://github.com/shadowninja108/exlaunch) as the current code injection framework
77
- `vendor/skyline`: [skyline](https://github.com/skyline-dev/skyline), formerly used as the code injection framework and currently used for filesystem utilities
88

9-
Additionally, craftyboss's project template and build scripts from [Scarlet-Exlaunch-Base](https://github.com/craftyboss/scarlet-exlaunch-base) are used to build RegionalDialect.
9+
**Additionally, RegionalDialect depends on the following external libraries:**
10+
11+
- [frozen](https://github.com/serge-sans-paille/frozen/tree/master)
12+
13+
Furthermore, craftyboss's project template and build scripts from [Scarlet-Exlaunch-Base](https://github.com/craftyboss/scarlet-exlaunch-base) are used to build RegionalDialect.
1014

1115
All third-party code mentioned above is mandatory, included in the build process and compiled into the output module for RegionalDialect on every build configuration.
1216

@@ -40,6 +44,12 @@ Copyright (c) 2020 The Skyline Project
4044

4145
See below for license text (MIT).
4246

47+
## frozen
48+
49+
https://github.com/serge-sans-paille/frozen/tree/master
50+
51+
See below for license text (Apache-2.0).
52+
4353
# Generic license texts
4454

4555
## MIT
@@ -410,4 +420,211 @@ proprietary programs. If your program is a subroutine library, you may
410420
consider it more useful to permit linking proprietary applications with the
411421
library. If this is what you want to do, use the GNU Lesser General
412422
Public License instead of this License.
423+
```
424+
425+
## Apache-2.0
426+
427+
```
428+
429+
Apache License
430+
Version 2.0, January 2004
431+
http://www.apache.org/licenses/
432+
433+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
434+
435+
1. Definitions.
436+
437+
"License" shall mean the terms and conditions for use, reproduction,
438+
and distribution as defined by Sections 1 through 9 of this document.
439+
440+
"Licensor" shall mean the copyright owner or entity authorized by
441+
the copyright owner that is granting the License.
442+
443+
"Legal Entity" shall mean the union of the acting entity and all
444+
other entities that control, are controlled by, or are under common
445+
control with that entity. For the purposes of this definition,
446+
"control" means (i) the power, direct or indirect, to cause the
447+
direction or management of such entity, whether by contract or
448+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
449+
outstanding shares, or (iii) beneficial ownership of such entity.
450+
451+
"You" (or "Your") shall mean an individual or Legal Entity
452+
exercising permissions granted by this License.
453+
454+
"Source" form shall mean the preferred form for making modifications,
455+
including but not limited to software source code, documentation
456+
source, and configuration files.
457+
458+
"Object" form shall mean any form resulting from mechanical
459+
transformation or translation of a Source form, including but
460+
not limited to compiled object code, generated documentation,
461+
and conversions to other media types.
462+
463+
"Work" shall mean the work of authorship, whether in Source or
464+
Object form, made available under the License, as indicated by a
465+
copyright notice that is included in or attached to the work
466+
(an example is provided in the Appendix below).
467+
468+
"Derivative Works" shall mean any work, whether in Source or Object
469+
form, that is based on (or derived from) the Work and for which the
470+
editorial revisions, annotations, elaborations, or other modifications
471+
represent, as a whole, an original work of authorship. For the purposes
472+
of this License, Derivative Works shall not include works that remain
473+
separable from, or merely link (or bind by name) to the interfaces of,
474+
the Work and Derivative Works thereof.
475+
476+
"Contribution" shall mean any work of authorship, including
477+
the original version of the Work and any modifications or additions
478+
to that Work or Derivative Works thereof, that is intentionally
479+
submitted to Licensor for inclusion in the Work by the copyright owner
480+
or by an individual or Legal Entity authorized to submit on behalf of
481+
the copyright owner. For the purposes of this definition, "submitted"
482+
means any form of electronic, verbal, or written communication sent
483+
to the Licensor or its representatives, including but not limited to
484+
communication on electronic mailing lists, source code control systems,
485+
and issue tracking systems that are managed by, or on behalf of, the
486+
Licensor for the purpose of discussing and improving the Work, but
487+
excluding communication that is conspicuously marked or otherwise
488+
designated in writing by the copyright owner as "Not a Contribution."
489+
490+
"Contributor" shall mean Licensor and any individual or Legal Entity
491+
on behalf of whom a Contribution has been received by Licensor and
492+
subsequently incorporated within the Work.
493+
494+
2. Grant of Copyright License. Subject to the terms and conditions of
495+
this License, each Contributor hereby grants to You a perpetual,
496+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
497+
copyright license to reproduce, prepare Derivative Works of,
498+
publicly display, publicly perform, sublicense, and distribute the
499+
Work and such Derivative Works in Source or Object form.
500+
501+
3. Grant of Patent License. Subject to the terms and conditions of
502+
this License, each Contributor hereby grants to You a perpetual,
503+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
504+
(except as stated in this section) patent license to make, have made,
505+
use, offer to sell, sell, import, and otherwise transfer the Work,
506+
where such license applies only to those patent claims licensable
507+
by such Contributor that are necessarily infringed by their
508+
Contribution(s) alone or by combination of their Contribution(s)
509+
with the Work to which such Contribution(s) was submitted. If You
510+
institute patent litigation against any entity (including a
511+
cross-claim or counterclaim in a lawsuit) alleging that the Work
512+
or a Contribution incorporated within the Work constitutes direct
513+
or contributory patent infringement, then any patent licenses
514+
granted to You under this License for that Work shall terminate
515+
as of the date such litigation is filed.
516+
517+
4. Redistribution. You may reproduce and distribute copies of the
518+
Work or Derivative Works thereof in any medium, with or without
519+
modifications, and in Source or Object form, provided that You
520+
meet the following conditions:
521+
522+
(a) You must give any other recipients of the Work or
523+
Derivative Works a copy of this License; and
524+
525+
(b) You must cause any modified files to carry prominent notices
526+
stating that You changed the files; and
527+
528+
(c) You must retain, in the Source form of any Derivative Works
529+
that You distribute, all copyright, patent, trademark, and
530+
attribution notices from the Source form of the Work,
531+
excluding those notices that do not pertain to any part of
532+
the Derivative Works; and
533+
534+
(d) If the Work includes a "NOTICE" text file as part of its
535+
distribution, then any Derivative Works that You distribute must
536+
include a readable copy of the attribution notices contained
537+
within such NOTICE file, excluding those notices that do not
538+
pertain to any part of the Derivative Works, in at least one
539+
of the following places: within a NOTICE text file distributed
540+
as part of the Derivative Works; within the Source form or
541+
documentation, if provided along with the Derivative Works; or,
542+
within a display generated by the Derivative Works, if and
543+
wherever such third-party notices normally appear. The contents
544+
of the NOTICE file are for informational purposes only and
545+
do not modify the License. You may add Your own attribution
546+
notices within Derivative Works that You distribute, alongside
547+
or as an addendum to the NOTICE text from the Work, provided
548+
that such additional attribution notices cannot be construed
549+
as modifying the License.
550+
551+
You may add Your own copyright statement to Your modifications and
552+
may provide additional or different license terms and conditions
553+
for use, reproduction, or distribution of Your modifications, or
554+
for any such Derivative Works as a whole, provided Your use,
555+
reproduction, and distribution of the Work otherwise complies with
556+
the conditions stated in this License.
557+
558+
5. Submission of Contributions. Unless You explicitly state otherwise,
559+
any Contribution intentionally submitted for inclusion in the Work
560+
by You to the Licensor shall be under the terms and conditions of
561+
this License, without any additional terms or conditions.
562+
Notwithstanding the above, nothing herein shall supersede or modify
563+
the terms of any separate license agreement you may have executed
564+
with Licensor regarding such Contributions.
565+
566+
6. Trademarks. This License does not grant permission to use the trade
567+
names, trademarks, service marks, or product names of the Licensor,
568+
except as required for reasonable and customary use in describing the
569+
origin of the Work and reproducing the content of the NOTICE file.
570+
571+
7. Disclaimer of Warranty. Unless required by applicable law or
572+
agreed to in writing, Licensor provides the Work (and each
573+
Contributor provides its Contributions) on an "AS IS" BASIS,
574+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
575+
implied, including, without limitation, any warranties or conditions
576+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
577+
PARTICULAR PURPOSE. You are solely responsible for determining the
578+
appropriateness of using or redistributing the Work and assume any
579+
risks associated with Your exercise of permissions under this License.
580+
581+
8. Limitation of Liability. In no event and under no legal theory,
582+
whether in tort (including negligence), contract, or otherwise,
583+
unless required by applicable law (such as deliberate and grossly
584+
negligent acts) or agreed to in writing, shall any Contributor be
585+
liable to You for damages, including any direct, indirect, special,
586+
incidental, or consequential damages of any character arising as a
587+
result of this License or out of the use or inability to use the
588+
Work (including but not limited to damages for loss of goodwill,
589+
work stoppage, computer failure or malfunction, or any and all
590+
other commercial damages or losses), even if such Contributor
591+
has been advised of the possibility of such damages.
592+
593+
9. Accepting Warranty or Additional Liability. While redistributing
594+
the Work or Derivative Works thereof, You may choose to offer,
595+
and charge a fee for, acceptance of support, warranty, indemnity,
596+
or other liability obligations and/or rights consistent with this
597+
License. However, in accepting such obligations, You may act only
598+
on Your own behalf and on Your sole responsibility, not on behalf
599+
of any other Contributor, and only if You agree to indemnify,
600+
defend, and hold each Contributor harmless for any liability
601+
incurred by, or claims asserted against, such Contributor by reason
602+
of your accepting any such warranty or additional liability.
603+
604+
END OF TERMS AND CONDITIONS
605+
606+
APPENDIX: How to apply the Apache License to your work.
607+
608+
To apply the Apache License to your work, attach the following
609+
boilerplate notice, with the fields enclosed by brackets "[]"
610+
replaced with your own identifying information. (Don't include
611+
the brackets!) The text should be enclosed in the appropriate
612+
comment syntax for the file format. We also recommend that a
613+
file or class name and description of purpose be included on the
614+
same "printed page" as the copyright notice for easier
615+
identification within third-party archives.
616+
617+
Copyright 2017 Quarkslab
618+
619+
Licensed under the Apache License, Version 2.0 (the "License");
620+
you may not use this file except in compliance with the License.
621+
You may obtain a copy of the License at
622+
623+
http://www.apache.org/licenses/LICENSE-2.0
624+
625+
Unless required by applicable law or agreed to in writing, software
626+
distributed under the License is distributed on an "AS IS" BASIS,
627+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
628+
See the License for the specific language governing permissions and
629+
limitations under the License.
413630
```

src/RegionalDialect/Hook.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
HOOK_DEFINE_TRAMPOLINE(name) { static ret Callback(__VA_ARGS__); };
1010

1111
#define HOOK_FUNC(category, name) \
12-
do { \
13-
if (rd::config::config["gamedef"]["signatures"][#category].has(#name)) { \
14-
name::InstallAtPtr(rd::hook::SigScan(#category, #name)); \
15-
} \
16-
} while (0)
12+
[&]{ \
13+
if (!rd::config::config["gamedef"]["signatures"][#category].has(#name)) return; \
14+
name::InstallAtPtr(rd::hook::SigScan(#category, #name)); \
15+
}()
1716

1817
#define HOOK_VAR(category, name) \
19-
do { \
20-
name = reinterpret_cast<decltype(name)>(rd::hook::SigScan(#category, #name)); \
21-
} while (0)
18+
name = reinterpret_cast<decltype(name)>(rd::hook::SigScan(#category, #name));
2219

2320
namespace rd {
2421
namespace hook {

src/RegionalDialect/Vm.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <cstdint>
22
#include <concepts>
33

4+
#include <frozen/unordered_map.h>
5+
#include <frozen/string.h>
6+
47
#include "Vm.h"
58
#include "System.h"
69
#include "Mem.h"
@@ -10,6 +13,21 @@ namespace vm {
1013

1114
using VmInstruction = void (*)(ScriptThreadState*);
1215

16+
#define CUSTOM_INST_LIST \
17+
CUSTOM_INST(GetDic)
18+
19+
#define CUSTOM_INST(name) \
20+
void name(ScriptThreadState *);
21+
CUSTOM_INST_LIST
22+
#undef CUSTOM_INST
23+
24+
#define CUSTOM_INST(name) \
25+
{ #name, &name },
26+
constexpr static auto CustomInstructions = frozen::make_unordered_map<frozen::string, VmInstruction>({
27+
CUSTOM_INST_LIST
28+
});
29+
#undef CUSTOM_INST
30+
1331
static VmInstruction *SCRuser1 = nullptr;
1432
static VmInstruction *SCRgraph = nullptr;
1533
static VmInstruction *SCRsystem = nullptr;
@@ -81,7 +99,7 @@ static void InsertCustomInstruction(std::string_view name) {
8199
return;
82100
}
83101

84-
rd::mem::Overwrite(address, reinterpret_cast<uintptr_t>(&GetDic));
102+
rd::mem::Overwrite(address, reinterpret_cast<uintptr_t>(CustomInstructions.find(name)->second));
85103
Logging.Log("%s inserted at %02X %02X!", name.data(), table, opcode);
86104
}
87105

@@ -96,7 +114,13 @@ void Init() {
96114

97115
HOOK_FUNC(game, CalMain);
98116

99-
InsertCustomInstruction("GetDic");
117+
#define CUSTOM_INST(name) \
118+
[&]{ \
119+
static_assert(CustomInstructions.contains(#name)); \
120+
InsertCustomInstruction(#name); \
121+
}();
122+
CUSTOM_INST_LIST;
123+
#undef CUSTOM_INST
100124
}
101125

102126
} // namespace vm

0 commit comments

Comments
 (0)