1
0
Fork 0
mirror of https://github.com/jugeeya/UltimateTrainingModpack.git synced 2024-11-20 00:46:34 +00:00

Add skyline macros, begin work on Rust hooking syntax

This commit is contained in:
jam1garner 2020-04-08 06:28:33 -04:00
parent 1cf373d338
commit 5c1a2083c9
11 changed files with 317 additions and 111 deletions

132
Cargo.lock generated
View file

@ -1,140 +1,66 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "0.1.10"
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "no_std_test"
version = "0.1.0"
dependencies = [
"skyline",
"tempfile",
]
[[package]]
name = "ppv-lite86"
version = "0.2.6"
name = "proc-macro2"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b"
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
checksum = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
"unicode-xid",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
name = "quote"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
checksum = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "redox_syscall"
version = "0.1.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
[[package]]
name = "remove_dir_all"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e"
dependencies = [
"winapi",
"proc-macro2",
]
[[package]]
name = "skyline"
version = "0.0.1"
[[package]]
name = "tempfile"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
dependencies = [
"cfg-if",
"libc",
"rand",
"redox_syscall",
"remove_dir_all",
"winapi",
"skyline_macro",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "winapi"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
name = "skyline_macro"
version = "0.1.0"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
"lazy_static",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
name = "syn"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
checksum = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"

View file

@ -11,7 +11,3 @@ crate-type = ["cdylib"]
[dependencies]
skyline = { path = "./skyline" }
[build-dependencies]
tempfile = "3"

55
skyline/Cargo.lock generated
View file

@ -1,6 +1,61 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "skyline"
version = "0.0.1"
dependencies = [
"skyline_macro 0.1.0",
]
[[package]]
name = "skyline_macro"
version = "0.1.0"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3"
"checksum quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f"
"checksum syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"

View file

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
skyline_macro = { path = "./skyline_macro" }

52
skyline/skyline_macro/Cargo.lock generated Normal file
View file

@ -0,0 +1,52 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "proc-macro2"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f"
dependencies = [
"proc-macro2",
]
[[package]]
name = "skyline_macro"
version = "0.1.0"
dependencies = [
"lazy_static",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"

View file

@ -0,0 +1,16 @@
[package]
name = "skyline_macro"
version = "0.1.0"
authors = ["jam1garner <jam1.mcleod@hotmail.com>"]
edition = "2018"
[lib]
proc-macro = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
syn = { version = "1", features = ["full", "extra-traits"] }
quote = "1"
proc-macro2 = "1"
lazy_static = "1.4"

View file

@ -0,0 +1,103 @@
use quote::{ToTokens, quote};
use proc_macro::TokenStream;
use syn::{parse_macro_input, token, Token, Ident, Path, AttrStyle, punctuated::Punctuated};
use proc_macro2::{Span, TokenStream as TokenStream2};
use syn::parse::Parser;
fn new_attr(attr_name: &str) -> syn::Attribute {
syn::Attribute {
pound_token: token::Pound { spans: [Span::call_site()] },
style: AttrStyle::Outer,
bracket_token: token::Bracket { span: Span::call_site() },
path: Ident::new(attr_name, Span::call_site()).into(),
tokens: TokenStream2::new()
}
}
#[proc_macro_attribute]
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
let mut main_function = parse_macro_input!(item as syn::ItemFn);
main_function.attrs.push(
new_attr("no_mangle")
);
main_function.sig.ident = Ident::new("main", Span::call_site());
let mut output = TokenStream2::new();
quote!(
use skyline::prelude::*;
::skyline::setup!();
).to_tokens(&mut output);
main_function.to_tokens(&mut output);
output.into()
}
#[proc_macro_attribute]
pub fn hook(_: TokenStream, input: TokenStream) -> TokenStream {
let mod_fn = parse_macro_input!(input as syn::ItemFn);
let mut output = TokenStream2::new();
mod_fn.to_tokens(&mut output);
let ident = quote::format_ident!(
"{}_skyline_internal_hook_info",
mod_fn.sig.ident
);
quote!(
#[allow(non_upper_case_globals)]
const #ident: ::skyline::hooks::HookInfo = ::skyline::hooks::HookInfo {
name: None,
offset: None,
symbol: None,
inline: false
};
).to_tokens(&mut output);
output.into()
}
fn parse_hook_fns(input: TokenStream) -> syn::Result<Vec<Path>> {
Ok(
Punctuated::<Path, Token![,]>::parse_terminated
.parse(input)?
.into_iter()
.collect()
)
}
fn concat_path(path: &Path) -> Path {
let mut path = path.clone();
let last = path.segments.iter_mut().last().unwrap();
last.ident = quote::format_ident!("{}_skyline_internal_hook_info", last.ident);
path
}
#[proc_macro]
pub fn hooks(tokens: TokenStream) -> TokenStream {
parse_hook_fns(tokens)
.map(|hook_fns|{
let hook_fn_infos = hook_fns.iter().map(concat_path);
quote!{
::skyline::hooks::Hooks(::skyline::alloc::vec![
#(
::skyline::new_hook!(
#hook_fns,
#hook_fn_infos
)
),*
])
}
})
.unwrap_or_else(|e|{
e.to_compile_error()
})
.into()
}

39
skyline/src/hooks.rs Normal file
View file

@ -0,0 +1,39 @@
use crate::alloc::string::String;
use crate::alloc::vec::Vec;
pub struct HookInfo {
pub name: Option<String>,
pub offset: Option<u64>,
pub symbol: Option<String>,
pub inline: bool
}
pub struct Hook {
pub ptr: *const (),
pub info: &'static HookInfo
}
impl Hook {
pub fn install(&self) {
todo!()
}
}
pub struct Hooks(pub Vec<Hook>);
impl Hooks {
pub fn install_hooks(&self) {
for hook in &self.0 {
hook.install();
}
}
}
#[macro_export] macro_rules! new_hook {
($path:path, $info:path) => {
$crate::hooks::Hook {
ptr: $path as *const (),
info: &$info
}
};
}

View file

@ -1,13 +1,15 @@
#![no_std]
#![allow(incomplete_features)]
#![feature(alloc_error_handler, lang_items, start, global_asm, const_generics, impl_trait_in_bindings)]
#![feature(alloc_error_handler, lang_items, start, global_asm, const_generics, impl_trait_in_bindings, proc_macro_hygiene)]
pub extern crate alloc;
pub mod libc;
pub mod hooks;
pub mod build;
pub mod extern_alloc;
pub use extern_alloc::Allocator;
pub use skyline_macro::{main, hook, hooks};
extern "C" {
fn skyline_tcp_send_raw(bytes: *const u8, usize: u64);

View file

@ -1,12 +1,25 @@
#![no_std]
use skyline::prelude::*;
skyline::setup!();
#![feature(proc_macro_hygiene)]
#[no_mangle]
use skyline::{hook, hooks};
#[hook(sym = "nn::fs::MountSaveData")]
fn test1(path: *const u8, user_id: u64) {
println!("user id: {}", user_id);
}
#[hook(inline, offset = 0x71000030)]
fn test2(x: u32) -> u64 {
(x as u64) + 1
}
#[skyline::main]
pub fn main() {
println!("Hello from Skyline Rust Plugin!\n");
for i in 0..3 {
println!("{}", i);
}
hooks![test1, test2].install_hooks();
}

3
src/test.rs Normal file
View file

@ -0,0 +1,3 @@
fn test2() {
}