Skip to content

Commit 0687901

Browse files
committed
Show time offset in gen 7
1 parent b95a859 commit 0687901

9 files changed

Lines changed: 52 additions & 25 deletions

File tree

3gx/includes/pnp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ u32 host_just_pressed();
1414
u32 host_is_just_pressed(u32 io_bits);
1515
void host_set_print_max_len(u32 max_len);
1616
u64 host_get_game_title_id();
17-
void hid_init();
17+
void set_game_start_ms(u64 time);
18+
u64 host_game_start_ms();

3gx/includes/pokereader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#pragma once
22

33
void initialize();
4-
void run_frame();
4+
void run_frame();

3gx/sources/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ void main(void)
168168
plgLdrInit();
169169
initialize();
170170

171+
// NTP epoch (milliseconds since 1st Jan 1900 00:00)
172+
u64 ms = osGetTime();
173+
// Adjust to Jan 2000, which is what the games use.
174+
u64 game_ms = ms - 3155673600000;
175+
set_game_start_ms(game_ms);
176+
171177
// Get memory layout changed event
172178
svcControlProcess(CUR_PROCESS_HANDLE, PROCESSOP_GET_ON_MEMORY_CHANGE_EVENT, (u32)&memLayoutChanged, 0);
173179

3gx/sources/pnp.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const u32 default_print_max_len = 30;
1717
u32 print_x = default_print_x;
1818
u32 print_y = default_print_y;
1919
u32 print_max_len = default_print_max_len;
20+
u64 game_start_ms = 0;
2021

2122
#define MAX_LINES 18
2223
#define MAX_LINE_LENGTH 46
@@ -97,3 +98,13 @@ u64 host_get_game_title_id()
9798
{
9899
return get_title_id();
99100
}
101+
102+
void set_game_start_ms(u64 ms)
103+
{
104+
game_start_ms = ms;
105+
}
106+
107+
u64 host_game_start_ms()
108+
{
109+
return game_start_ms;
110+
}

reader_core/src/gen7/draw.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ pub fn draw_citra_info(reader: &Gen7Reader) {
2626
let main_rng_seed_context = reader.main_rng_seed_context();
2727
let date = main_rng_seed_context.init_datetime;
2828
pnp::println!("Seed ticks: {:08X}", main_rng_seed_context.ticks);
29-
pnp::println!("Init date: {}", date.format("%b %d %Y"));
30-
pnp::println!("Init time: {}", date.format("%H:%M:%S"));
29+
pnp::println!("Seed date: {}", date.format("%b %d %Y"));
30+
pnp::println!("Seed time: {}", date.format("%H:%M:%S"));
31+
pnp::println!("Time offset: {}", main_rng_seed_context.time_offset_ms);
3132
}
3233

3334
pub fn draw_sos(reader: &Gen7Reader) {

reader_core/src/gen7/hook.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::utils;
1+
use crate::{pnp, utils};
22
use chrono::NaiveDateTime;
33
static mut MAIN_RNG_SEED_TICKS: u32 = 0;
44
static mut MAIN_RNG_DATE_TIME: NaiveDateTime = NaiveDateTime::MIN;
5+
static mut MAIN_RNG_TIME_OFFSET_MS: u64 = 0;
56
static mut SOS_SEED: u32 = 0;
67

78
pub fn sos_seed() -> u32 {
@@ -16,33 +17,34 @@ fn init_sfmt_hook(regs: &[u32], _stack_pointer: *mut u32) {
1617

1718
pub struct RngSeedContext {
1819
pub init_datetime: NaiveDateTime,
20+
pub time_offset_ms: u64,
1921
pub ticks: u32,
2022
}
2123

2224
pub fn main_rng_seed_context() -> RngSeedContext {
2325
unsafe {
2426
RngSeedContext {
2527
init_datetime: MAIN_RNG_DATE_TIME,
28+
time_offset_ms: MAIN_RNG_TIME_OFFSET_MS,
2629
ticks: MAIN_RNG_SEED_TICKS,
2730
}
2831
}
2932
}
3033

31-
const THIRTY_YEARS_MS: u64 = 946684800000;
32-
3334
fn init_main_rng_hook(_regs: &[u32], stack_pointer: *mut u32) {
3435
let ticks = unsafe { stack_pointer.add(4).read() };
3536
let console_ms_epoch_low = unsafe { stack_pointer.add(12).read() };
3637
let console_ms_epoch_high = unsafe { stack_pointer.add(13).read() };
3738
let console_ms_epoch = ((console_ms_epoch_high as u64) << 32) | (console_ms_epoch_low as u64);
38-
let standard_ms_epoch = console_ms_epoch.saturating_add(THIRTY_YEARS_MS);
39-
let ms_epoch: i64 = standard_ms_epoch.try_into().unwrap_or_default();
39+
let current_time = pnp::datetime_from_console_ms(console_ms_epoch);
40+
// Assume we didn't start with any milliseconds and any extra milliseconds was spent loading
41+
let start_ms = (pnp::game_start_ms() / 1000) * 1000;
42+
let time_offset_ms = console_ms_epoch.saturating_sub(start_ms);
4043

4144
unsafe {
4245
MAIN_RNG_SEED_TICKS = ticks;
43-
if let Some(date_time) = NaiveDateTime::from_timestamp_millis(ms_epoch) {
44-
MAIN_RNG_DATE_TIME = date_time;
45-
}
46+
MAIN_RNG_DATE_TIME = current_time;
47+
MAIN_RNG_TIME_OFFSET_MS = time_offset_ms;
4648
}
4749
}
4850

reader_core/src/pnp/bindings.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern "C" {
55
pub fn host_is_just_pressed(io_bits: u32) -> u32;
66
pub fn host_set_print_max_len(max_len: u32);
77
pub fn host_get_game_title_id() -> u64;
8+
pub fn host_game_start_ms() -> u64;
89
}
910

1011
#[cfg(feature = "test_stubs")]
@@ -16,29 +17,17 @@ pub mod test_stubs {
1617
#[no_mangle]
1718
pub extern "C" fn host_write_mem(_game_addr: u32, _size: u32, _in_ptr: u32) {}
1819
#[no_mangle]
19-
pub extern "C" fn host_just_pressed() -> u32 {
20-
0
21-
}
22-
#[no_mangle]
2320
pub extern "C" fn host_is_just_pressed(_io_bits: u32) -> u32 {
2421
0
2522
}
2623
#[no_mangle]
27-
pub extern "C" fn host_reset_print() {}
28-
#[no_mangle]
29-
pub extern "C" fn host_set_print_colors(_text_color: u32, _background_color: u32) {}
30-
#[no_mangle]
3124
pub extern "C" fn host_set_print_max_len(_max_len: u32) {}
3225
#[no_mangle]
33-
pub extern "C" fn host_set_print_x(_x: u32) {}
34-
#[no_mangle]
35-
pub extern "C" fn host_set_print_y(_y: u32) {}
36-
#[no_mangle]
3726
pub extern "C" fn host_get_game_title_id() -> u64 {
3827
0
3928
}
4029
#[no_mangle]
41-
pub extern "C" fn host_get_is_mode3() -> u32 {
30+
pub extern "C" fn host_game_start_ms() -> u64 {
4231
0
4332
}
4433
}

reader_core/src/pnp/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ mod input;
55
mod memory;
66
/// Tools for printing text to the console's screen.
77
mod print;
8+
/// Tools related to time.
9+
mod time;
810
/// Various utilities.
911
mod utils;
1012

1113
pub use input::*;
1214
pub use memory::*;
1315
pub use print::*;
16+
pub use time::*;
1417
pub use utils::*;

reader_core/src/pnp/time.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::bindings;
2+
use chrono::NaiveDateTime;
3+
4+
const THIRTY_YEARS_MS: u64 = 946684800000;
5+
6+
pub fn datetime_from_console_ms(console_ms: u64) -> NaiveDateTime {
7+
let standard_ms = console_ms.saturating_add(THIRTY_YEARS_MS);
8+
let ms: i64 = standard_ms.try_into().unwrap_or_default();
9+
NaiveDateTime::from_timestamp_millis(ms).unwrap_or_default()
10+
}
11+
12+
pub fn game_start_ms() -> u64 {
13+
unsafe { bindings::host_game_start_ms() }
14+
}

0 commit comments

Comments
 (0)