Skip to content

Commit

Permalink
Some new handlers and an item module
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Mar 17, 2022
1 parent f2f20c8 commit b58dfeb
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
max_width = 100
max_width = 108
newline_style = "Unix"
use_field_init_shorthand = true
overflow_delimited_expr = true
Expand Down
86 changes: 75 additions & 11 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ features = ["single_threaded"]
[dependencies.egui-hook]
git = "https://github.com/jac3km4/egui-hook"
rev = "v0.0.4"

[dependencies.memhack]
git = "https://github.com/jac3km4/memhack"
package = "memhack"
rev = "v0.0.1"

[dependencies.memhack-derive]
git = "https://github.com/jac3km4/memhack"
package = "memhack-derive"
rev = "v0.0.1"
11 changes: 7 additions & 4 deletions doc/FUNCTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ All game functions are in a `game` namespace.
// give yourself 1 XP
game::give_xp(1)
// give player 10 energy ammo
item::give(entity::get_player(), item::resolve("It_Ammo_Energy"), 10, 0, true)
// add the NPC you're looking at to the party
game::join_player_party(entity::get_look_at())
Expand All @@ -28,13 +31,17 @@ game::on_quest_success_player_traitor_to_humanity()
### function list
- `advance_time(hours: i64)`
- `auto_loot(looter: Entity, target: Entity)`
- `dismiss_player_party(npc: Entity)`
- `give_quest_xp(amount: i64)`
- `give_xp(amount: i64)`
- `join_player_party(npc: Entity)`
- `kill(instigator: Entity, target: Entity)`
- `on_info_advance_playing_time_by_hours(hours: i64)`
- `place_summoned_party_member(leader: Entity, member: Entity)`
- `remove_npc(npc: Entity)`
- `set_player_rank(rank: i64)`
- `set_target_hour(hour: i64)`
- `spawn_new_entity(pos: Entity, entity: Entity)`
- `movement_callback()`
- `movement_sneaking_callback()`
- `movement_blocked_callback()`
Expand Down Expand Up @@ -3811,7 +3818,6 @@ game::on_quest_success_player_traitor_to_humanity()
- `can_wait_player_party()`
- `is_party_under_attack()`
- `can_dismiss_player_party()`
- `dismiss_player_party()`
- `dismiss_party_member()`
- `can_join_player_party()`
- `is_not_summoned()`
Expand Down Expand Up @@ -3847,8 +3853,6 @@ game::on_quest_success_player_traitor_to_humanity()
- `set_routine_continue_routine()`
- `set_human_rtn_dead()`
- `on_quest_lock_ai_result()`
- `spawn_new_entity()`
- `remove_npc()`
- `rtn_teleport_to_location()`
- `teleport_player_with_routine()`
- `teleport_player()`
Expand Down Expand Up @@ -4113,7 +4117,6 @@ game::on_quest_success_player_traitor_to_humanity()
- `on_process_projection()`
- `on_end_projection()`
- `routine_power_projection()`
- `place_summoned_party_member()`
- `can_cast_power_projection()`
- `power_projection()`
- `spell_camouflage_on_ui_info()`
Expand Down
63 changes: 50 additions & 13 deletions src/elex.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
use std::borrow::Cow;
use std::ffi::CString;
use std::{mem, ptr};

use egui_hook::import_foreign;
use memhack_derive::foreign_fn;
use pelite::pe64::{Pe, PeView};

pub fn get_all_functions<'a>() -> impl Iterator<Item = (Cow<'a, str>, FunctionPtr)> {
unsafe { &*get_function_registry() }
get_function_registry()
.functions()
.map(|fun| (fun.name(), fun.ptr()))
}

#[inline]
pub fn get_player() -> Entity {
Entity(get_player_ptr())
}

#[inline]
pub fn get_player_look_at() -> Entity {
let player = get_player();
let mut entity = Entity(ptr::null());
get_player_look_at_ptr(&player, &mut entity);
let mut entity = Entity::null();
get_player_look_at_ref(&player, &mut entity);
entity
}

import_foreign!(0x0867080, get_function_registry() -> *const FunctionRegistry);
import_foreign!(0x040B710, get_player_ptr() -> *const GameObject);
import_foreign!(0x0B17FF0, get_player_look_at_ptr(player: *const Entity, entity: *mut Entity) -> ());
pub fn resolve_item(name: &str) -> Entity {
let mut item = Entity::null();
if let Ok(str) = CString::new(name) {
resolve_item_by_cstr(&mut item, str.as_ptr());
}
item
}

#[inline]
#[foreign_fn(0x040B710)]
pub fn get_player() -> Entity {}
#[inline]
#[foreign_fn(0x0867080)]
fn get_function_registry<'a>() -> &'a FunctionRegistry {}
#[inline]
#[foreign_fn(0x0B17FF0)]
fn get_player_look_at_ref(player: &Entity, entity: &mut Entity) -> () {}
#[inline]
#[foreign_fn(0x0B30C00)]
fn resolve_item_by_cstr(item: &mut Entity, name: *const i8) -> () {}
#[inline]
#[foreign_fn(0x0B13020)]
pub fn give_item(target: &Entity, item: &Entity, quantity: u32, x: u32, notify: Notify) -> () {}

#[derive(Debug, Clone, Copy)]
#[repr(C)]
Expand All @@ -36,6 +51,28 @@ impl Entity {
pub fn null() -> Entity {
Entity(std::ptr::null())
}

#[inline]
pub fn is_null(&self) -> bool {
self.0.is_null()
}
}

#[repr(u32)]
pub enum Notify {
Never = 0,
Always = 2,
}

impl From<bool> for Notify {
#[inline]
fn from(notify: bool) -> Self {
if notify {
Notify::Always
} else {
Notify::Never
}
}
}

#[derive(Debug)]
Expand Down
12 changes: 12 additions & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ static CUSTOM_HANDLERS: phf::Map<&'static str, CustomHandler> = phf_map! {
"join_player_party" => custom_handler! { |ptr: FunctionPtr|
move |entity: Entity| ptr.invoke_with(entity, Entity::null(), ())
},
"dismiss_player_party" => custom_handler! { |ptr: FunctionPtr|
move |entity: Entity| ptr.invoke_with(entity, Entity::null(), ())
},
"spawn_new_entity" => custom_handler! { |ptr: FunctionPtr|
move |pos: Entity, entity: Entity| ptr.invoke_with(pos, entity, ())
},
"remove_npc" => custom_handler! { |ptr: FunctionPtr|
move |npc: Entity| ptr.invoke_with(npc, Entity::null(), ())
},
"place_summoned_party_member" => custom_handler! { |ptr: FunctionPtr|
move |leader: Entity, member: Entity| ptr.invoke_with(leader, member, ())
},
};
22 changes: 17 additions & 5 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Default for ScriptHost {

engine.register_static_module("game", ScriptHost::create_game_module().into());
engine.register_static_module("entity", ScriptHost::create_entity_module().into());
engine.register_static_module("item", ScriptHost::create_item_module().into());

engine.register_fn("log", move |val: Dynamic| {
tx.send(val.to_string()).ok();
Expand Down Expand Up @@ -86,6 +87,20 @@ impl ScriptHost {
module.set_native_fn("get_player", || Ok(elex::get_player()));
module.set_native_fn("get_look_at", || Ok(elex::get_player_look_at()));
module.set_native_fn("none", || Ok(elex::Entity::null()));
module.set_native_fn("is_none", |entity: elex::Entity| Ok(entity.is_null()));
module
}

fn create_item_module() -> Module {
let mut module = Module::new();
module.set_native_fn("resolve", |name: &str| Ok(elex::resolve_item(name)));
module.set_native_fn(
"give",
|target: elex::Entity, item: elex::Entity, quantity: i64, x: i64, notify: bool| {
elex::give_item(&target, &item, quantity as u32, x as u32, notify.into());
Ok(())
},
);
module
}
}
Expand All @@ -105,8 +120,7 @@ impl egui_hook::App for ScriptHost {
Window::new("CRONY GUI")
.default_size(DEFAULT_SIZE)
.show(ctx, |ui| {
let (resp, painter) =
ui.allocate_painter(DEFAULT_SIZE, Sense::focusable_noninteractive());
let (resp, painter) = ui.allocate_painter(DEFAULT_SIZE, Sense::focusable_noninteractive());

painter.rect_filled(resp.rect, Rounding::same(4.), Color32::BLACK);
painter.text(
Expand Down Expand Up @@ -139,9 +153,7 @@ impl egui_hook::App for ScriptHost {
}

fn init() -> bool {
let log_file = FileSpec::default()
.directory("plugins/logs")
.basename("crony");
let log_file = FileSpec::default().directory("plugins/logs").basename("crony");
Logger::with(LogSpecification::info())
.log_to_file(log_file)
.write_mode(WriteMode::BufferAndFlush)
Expand Down

0 comments on commit b58dfeb

Please sign in to comment.