Skip to content

Commit

Permalink
game: move pc-encode-utf8-string to common kmachine code (#3742)
Browse files Browse the repository at this point in the history
Also adds formatter rules for a couple of macros.
  • Loading branch information
Hat-Kid authored Nov 3, 2024
1 parent ba9085f commit 10d7dab
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 17 deletions.
38 changes: 38 additions & 0 deletions common/formatter/rules/rule_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,39 @@ static FormFormattingConfig new_deftype_rule(
return cfg;
}

static FormFormattingConfig new_defproc_rule(
int start_index,
int num_columns_to_compute_widths,
const std::vector<int>& inlining_preventation_indices) {
FormFormattingConfig cfg;
cfg.has_constant_pairs = true;
cfg.config_set = true;
cfg.hang_forms = false;
cfg.inline_until_index = [start_index](std::vector<std::string> curr_lines) {
// if (curr_lines.size() >= 4 && curr_lines.at(3) == "()") {
// return 4;
// }
return start_index;
};
for (const auto& index : inlining_preventation_indices) {
auto temp_config = std::make_shared<FormFormattingConfig>();
temp_config->config_set = true;
temp_config->prevent_inlining = true;
temp_config->hang_forms = false;
temp_config->indentation_width = 1;
auto temp_list_config = std::make_shared<FormFormattingConfig>();
temp_list_config->force_inline = false;
temp_list_config->hang_forms = false;
temp_config->default_index_config = temp_list_config;
if (index == 3) {
temp_config->determine_column_widths_for_list_elements = true;
temp_config->num_columns_to_compute_widths = num_columns_to_compute_widths;
}
cfg.index_configs.emplace(index, temp_config);
}
return cfg;
}

static FormFormattingConfig new_binding_rule(int form_head_width) {
FormFormattingConfig cfg;
cfg.config_set = true;
Expand Down Expand Up @@ -253,6 +286,9 @@ const std::unordered_map<std::string, FormFormattingConfig> opengoal_form_config
{"defmethod", new_defmethod_rule(3)},
{"lambda", new_lambda_rule(2)},
{"deftype", new_deftype_rule(3, 1, {3, 4, 5, 6})},
{"defproc", new_defproc_rule(3, 1, {3, 4, 5, 6})},
{"suspend-for", new_flow_rule(2)},
{"spawn-proc", new_flow_rule(2)},
{"defun", new_flow_rule(3)},
{"defun-recursive", new_flow_rule(4)},
{"defun-debug-recursive", new_flow_rule(4)},
Expand Down Expand Up @@ -281,12 +317,14 @@ const std::unordered_map<std::string, FormFormattingConfig> opengoal_form_config
{"protect", new_binding_rule(4)},
{"let*", new_binding_rule(5)},
{"rlet", new_binding_rule(5)},
{"mlet", new_binding_rule(5)},
{"when", new_flow_rule(2)},
{"unless", new_flow_rule(2)},
{"with-profiler", new_flow_rule(2)},
{"with-pc", new_flow_rule(0)},
{"#unless", new_flow_rule(2)},
{"#when", new_flow_rule(2)},
{"#when-game", new_flow_rule(2)},
{"countdown", new_flow_rule(2)},
{"until", new_flow_rule(2)},
{"loop", new_flow_rule(0)},
Expand Down
2 changes: 1 addition & 1 deletion common/util/gltf_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ void setup_alpha_from_material(const tinygltf::Material& material, DrawMode* mod

void setup_draw_mode_from_sampler(const tinygltf::Sampler& sampler, DrawMode* mode) {
if (sampler.magFilter == TINYGLTF_TEXTURE_FILTER_NEAREST) {
ASSERT(sampler.minFilter == TINYGLTF_TEXTURE_FILTER_NEAREST);
ASSERT(sampler.minFilter == TINYGLTF_TEXTURE_FILTER_NEAREST_MIPMAP_NEAREST);
mode->set_filt_enable(false);
} else {
ASSERT(sampler.minFilter != TINYGLTF_TEXTURE_FILTER_NEAREST);
Expand Down
12 changes: 12 additions & 0 deletions game/kernel/common/kmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,15 @@ void pc_register_screen_shot_settings(u32 ptr) {
register_screen_shot_settings(Ptr<ScreenShotSettings>(ptr).c());
}

void pc_encode_utf8_string(u32 src_str_ptr, u32 str_dest_ptr) {
auto str = std::string(Ptr<String>(src_str_ptr).c()->data());
std::string version = version_to_game_name(g_game_version);
const std::string font_bank_name = version == "jak1" ? "jak1-v2" : version;
std::string converted =
get_font_bank(get_text_version_from_name(font_bank_name))->convert_utf8_to_game(str);
strcpy(Ptr<String>(str_dest_ptr).c()->data(), converted.c_str());
}

/// Initializes all functions that are common across all game versions
/// These functions have the same implementation and do not use any game specific functions (other
/// than the one to create a function in the first place)
Expand Down Expand Up @@ -1005,6 +1014,9 @@ void init_common_pc_port_functions(
// RNG
make_func_symbol_func("pc-rand", (void*)pc_rand);

// text
make_func_symbol_func("pc-encode-utf8-string", (void*)pc_encode_utf8_string);

// debugging tools
make_func_symbol_func("pc-filter-debug-string?", (void*)pc_filter_debug_string);
make_func_symbol_func("pc-screen-shot", (void*)pc_screen_shot);
Expand Down
1 change: 0 additions & 1 deletion game/kernel/jak2/kmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ void InitMachine_PCPort() {
make_function_symbol_from_c("__pc-get-tex-remap", (void*)lookup_jak2_texture_dest_offset);
make_function_symbol_from_c("pc-init-autosplitter-struct",
(void*)kmachine_extras::init_autosplit_struct);
make_function_symbol_from_c("pc-encode-utf8-string", (void*)kmachine_extras::encode_utf8_string);

// discord rich presence
make_function_symbol_from_c("pc-discord-rpc-update", (void*)kmachine_extras::update_discord_rpc);
Expand Down
7 changes: 0 additions & 7 deletions game/kernel/jak2/kmachine_extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,6 @@ inline bool symbol_to_bool(const u32 symptr) {
return symptr != s7.offset;
}

// TODO - move to common
void encode_utf8_string(u32 src_str_ptr, u32 str_dest_ptr) {
auto str = std::string(Ptr<String>(src_str_ptr).c()->data());
std::string converted = get_font_bank(GameTextVersion::JAK2)->convert_utf8_to_game(str);
strcpy(Ptr<String>(str_dest_ptr).c()->data(), converted.c_str());
}

void init_autosplit_struct() {
g_auto_splitter_block_jak2.pointer_to_symbol =
(u64)g_ee_main_mem + (u64)intern_from_c("*autosplit-info-jak2*")->value();
Expand Down
1 change: 0 additions & 1 deletion game/kernel/jak3/kmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ void InitMachine_PCPort() {
(void*)kmachine_extras::pc_set_active_levels);
make_function_symbol_from_c("__pc-get-tex-remap", (void*)lookup_jak3_texture_dest_offset);
// make_function_symbol_from_c("pc-init-autosplitter-struct", (void*)init_autosplit_struct);
make_function_symbol_from_c("pc-encode-utf8-string", (void*)kmachine_extras::encode_utf8_string);

// discord rich presence
make_function_symbol_from_c("pc-discord-rpc-update", (void*)kmachine_extras::update_discord_rpc);
Expand Down
7 changes: 0 additions & 7 deletions game/kernel/jak3/kmachine_extras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,5 @@ inline bool symbol_to_bool(const u32 symptr) {
return symptr != s7.offset;
}

// TODO - move to common
void encode_utf8_string(u32 src_str_ptr, u32 str_dest_ptr) {
auto str = std::string(Ptr<String>(src_str_ptr).c()->data());
std::string converted = get_font_bank(GameTextVersion::JAK3)->convert_utf8_to_game(str);
strcpy(Ptr<String>(str_dest_ptr).c()->data(), converted.c_str());
}

} // namespace kmachine_extras
} // namespace jak3
2 changes: 2 additions & 0 deletions goal_src/jak1/kernel-defs.gc
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@

(define-extern pc-rand (function int))

(define-extern pc-encode-utf8-string (function string string none))

;; Constants generated within the C++ runtime
(define-extern *pc-user-dir-base-path* string)

Expand Down

0 comments on commit 10d7dab

Please sign in to comment.