Skip to content

Commit

Permalink
feat: add standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
progre committed May 13, 2024
1 parent f96cfaa commit baaa507
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 66 deletions.
24 changes: 18 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ jobs:
- name: Build
shell: bash
run: |
cargo build --locked --release --package junowen --features simple-dll-injection
mv \
target/i686-pc-windows-msvc/release/th19_junowen.dll \
target/i686-pc-windows-msvc/release/junowen.dll
cargo build --locked --release --package junowen --bin junowen-standalone
mv \
target/i686-pc-windows-msvc/release/junowen-standalone.exe \
target/i686-pc-windows-msvc/release/junowen.exe
cargo build --locked --release --workspace
cargo bundle-licenses --format yaml --output THIRDPARTY.yml
mkdir --parents artifact/modules
mv \
./target/i686-pc-windows-msvc/release/th19_*.dll \
./artifact/modules/
target/i686-pc-windows-msvc/release/th19_*.dll \
artifact/modules/
mv \
./target/i686-pc-windows-msvc/release/*.dll \
./target/i686-pc-windows-msvc/release/*.exe \
./artifact/
target/i686-pc-windows-msvc/release/*.dll \
target/i686-pc-windows-msvc/release/*.exe \
artifact/
cp --recursive LICENSE README.md THIRDPARTY.yml ./artifact/
- uses: actions/upload-artifact@v3
with:
Expand All @@ -51,6 +59,7 @@ jobs:
runs-on: ubuntu-latest
env:
FILENAME: ${{ github.ref_name }}.zip
FILENAME_STANDALONE: ${{ github.ref_name }}-standalone.zip
steps:
- uses: actions/download-artifact@v3
with:
Expand All @@ -60,6 +69,7 @@ jobs:
run: |
cd artifact
zip ../${{ env.FILENAME }} d3d9.dll modules/th19_junowen.dll LICENSE README.md THIRDPARTY.yml
zip ../${{ env.FILENAME_STANDALONE }} junowen.dll junowen.exe LICENSE README.md THIRDPARTY.yml
ref_name=${{github.ref_name}}
echo "RELEASE_NAME=${ref_name/junowen-/Ju.N.Owen }" >> "$GITHUB_ENV"
Expand All @@ -68,7 +78,9 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
name: ${{ env.RELEASE_NAME }}
files: ${{ env.FILENAME }}
files: |
${{ env.FILENAME }}
${{ env.FILENAME_STANDALONE }}
build-server:
runs-on: ubuntu-latest
steps:
Expand Down
29 changes: 10 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion junowen-lib/src/find_process_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn find_process_id(exe_file: &str) -> Result<u32> {
let snapshot = SafeHandle(unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }?);

let process_id = find_process_id_in_snapshot(snapshot, exe_file)
.ok_or_else(|| anyhow!("Process not found"))?;
.ok_or_else(|| anyhow!("process not found"))?;

Ok(process_id)
}
19 changes: 17 additions & 2 deletions junowen-lib/src/hook_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@ use std::{fs::File, io::Read};
use sha3::digest::Digest; // using for Sha3_224::new()
use sha3::{digest::generic_array::GenericArray, Sha3_224};
use windows::{
core::PCWSTR,
Win32::{Foundation::MAX_PATH, System::LibraryLoader::GetModuleFileNameW},
core::{HSTRING, PCWSTR},
Win32::{
Foundation::{HWND, MAX_PATH},
System::LibraryLoader::GetModuleFileNameW,
UI::WindowsAndMessaging::{MessageBoxW, MB_ICONWARNING, MB_OK},
},
};

pub use dll_injection::{do_dll_injection, DllInjectionError};

pub fn show_warn_dialog(msg: &str) {
unsafe {
MessageBoxW(
HWND::default(),
&HSTRING::from(msg),
&HSTRING::from(env!("CARGO_PKG_NAME")),
MB_ICONWARNING | MB_OK,
)
};
}

pub fn calc_th19_hash() -> Vec<u8> {
let mut buf = [0u16; MAX_PATH as usize];
if unsafe { GetModuleFileNameW(None, &mut buf) } == 0 {
Expand Down
1 change: 1 addition & 0 deletions junowen-lib/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{collections::HashMap, fs};

use sys_locale::get_locales;

#[derive(derive_new::new)]
pub struct Lang {
lang: HashMap<String, String>,
}
Expand Down
5 changes: 3 additions & 2 deletions junowen-lib/src/memory_accessors/hooked_process.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
ffi::c_void,
mem::{size_of, transmute},
ptr::{copy_nonoverlapping, replace},
ptr::{copy_nonoverlapping, read_unaligned, write_unaligned},
};

use anyhow::Result;
Expand Down Expand Up @@ -55,7 +55,8 @@ unsafe fn assemble_call_and_manage_register(mut addr: *mut u8, target: usize) {
unsafe fn assemble_jmp_target(addr: *mut u8, target: usize) -> usize {
let jump_base_addr = addr.wrapping_add(5) as i64;
let p_jump_target = addr.wrapping_add(1) as *mut i32;
let old_value = replace(p_jump_target, (target as i64 - jump_base_addr) as i32);
let old_value = read_unaligned(p_jump_target);
write_unaligned(p_jump_target, (target as i64 - jump_base_addr) as i32);
(jump_base_addr + old_value as i64) as usize
}

Expand Down
5 changes: 5 additions & 0 deletions junowen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ license.workspace = true
name = "th19_junowen"
crate-type = ['cdylib']

[features]
simple-dll-injection = []

[build-dependencies]
static_vcruntime.workspace = true

Expand All @@ -25,6 +28,7 @@ reqwest = { version = "0.12.4", features = ["json"] }
rmp-serde = "1.1.2"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.108"
sys-locale = "0.3.1"
thiserror = "1.0.50"
time = { version = "0.3.29", features = [] }
tokio = { version = "1.32.0", features = [
Expand All @@ -33,6 +37,7 @@ tokio = { version = "1.32.0", features = [
"rt-multi-thread",
"time"
] }
toml = "0.8.12"
toml_edit = "0.22.12"
tracing.workspace = true
tracing-appender = "0.2.2"
Expand Down
45 changes: 45 additions & 0 deletions junowen/src/bin/junowen-standalone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
mod lang;

use std::{env::current_exe, process::ExitCode};

use junowen_lib::{hook_utils::do_dll_injection, lang::Lang};
use sys_locale::get_locales;

use crate::lang::to_lang_source;

fn create_lang() -> Lang {
let lang = get_locales()
.flat_map(|tag| {
let primary_lang = tag.split('-').next().unwrap_or(&tag).to_owned();
[tag, primary_lang]
})
.filter_map(|tag| to_lang_source(&tag))
.find_map(|file| toml::from_str(file).ok())
.unwrap_or_default();
Lang::new(lang)
}

fn main() -> ExitCode {
let lang = create_lang();

let dll_path = current_exe()
.unwrap()
.as_path()
.parent()
.unwrap()
.join(concat!(env!("CARGO_PKG_NAME"), ".dll"));
if let Err(err) = do_dll_injection("th19.exe", &dll_path) {
lang.print("failed injection into th19.exe");
println!(": {}", err);
println!();
lang.println("you can close this window by pressing enter...");
let _ = std::io::stdin().read_line(&mut String::new());
return ExitCode::FAILURE;
}

lang.println("completed injection into th19.exe");
println!();
lang.println("you can close this window by pressing enter...");
let _ = std::io::stdin().read_line(&mut String::new());
ExitCode::SUCCESS
}
3 changes: 3 additions & 0 deletions junowen/src/bin/lang/ja.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"failed injection into th19.exe" = "th19.exe への注入に失敗しました"
"completed injection into th19.exe" = "th19.exe への注入が完了しました"
"you can close this window by pressing enter..." = "エンターキーを押してこのウィンドウを閉じることができます..."
6 changes: 6 additions & 0 deletions junowen/src/bin/lang/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn to_lang_source(tag: &str) -> Option<&'static str> {
match tag {
"ja" => Some(include_str!("ja.toml")),
_ => None,
}
}
25 changes: 10 additions & 15 deletions junowen/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,27 @@ use windows::{
},
};

pub async fn ini_file_path_log_dir_path_log_file_name_old_log_path(
module: HMODULE,
) -> (String, String, String, String) {
let dll_path = {
let mut buf = [0u16; u16::MAX as usize];
if unsafe { GetModuleFileNameW(module, &mut buf) } == 0 {
panic!();
}
let dll_path = unsafe { PCWSTR::from_raw(buf.as_ptr()).to_string() }.unwrap();
PathBuf::from(dll_path)
};
pub fn to_dll_path(module: HMODULE) -> PathBuf {
let mut buf = [0u16; u16::MAX as usize];
if unsafe { GetModuleFileNameW(module, &mut buf) } == 0 {
panic!();
}
let dll_path = unsafe { PCWSTR::from_raw(buf.as_ptr()).to_string() }.unwrap();
PathBuf::from(dll_path)
}

pub fn to_ini_file_path_log_dir_path_log_file_name(dll_stem: &str) -> (String, String, String) {
let module_dir = {
let guid = FOLDERID_RoamingAppData;
let res = unsafe { SHGetKnownFolderPath(&guid, KNOWN_FOLDER_FLAG(0), HANDLE::default()) };
let app_data_dir = unsafe { res.unwrap().to_string() }.unwrap();
format!("{}/ShanghaiAlice/th19/modules", app_data_dir)
};

let dll_stem = dll_path.file_stem().unwrap().to_string_lossy();
let ini_file_path = format!("{}/{}.ini", module_dir, dll_stem);
let log_file_name = format!("{}.log", dll_stem);
let dll_dir_path = dll_path.parent().unwrap().to_string_lossy();
let old_log_path = format!("{}/{}", dll_dir_path, log_file_name);

(ini_file_path, module_dir, log_file_name, old_log_path)
(ini_file_path, module_dir, log_file_name)
}

pub async fn move_old_log_to_new_path(old_log_path: &str, module_dir: &str, log_file_name: &str) {
Expand Down
Loading

0 comments on commit baaa507

Please sign in to comment.