Skip to content

Commit

Permalink
wip rework script handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nmeylan committed Feb 14, 2024
1 parent 7dc8115 commit 7443f66
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 90 deletions.
13 changes: 3 additions & 10 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 server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async-trait = "0.1.74"
testcontainers = { version= "0.15.0" }
testcontainers-modules = { version = "0.2.1", features = ["postgres"] }

rathena_script_lang_interpreter = { git = "https://github.com/nmeylan/rathena-script-lang-interpreter", rev = "afb25ccef013548f88badc899501e94c7490606f"}
rathena_script_lang_interpreter = { git = "https://github.com/nmeylan/rathena-script-lang-interpreter", rev = "da2c74a79a37c303a615bab1a10cdcefc8f7f82a"}

configuration = { path = "../lib/configuration" }
accessor = { path = "../lib/accessor" }
Expand Down
5 changes: 4 additions & 1 deletion server/src/server/request_handler/action/npc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ pub fn handle_contact_npc(server: Arc<Server>, context: Request) {
thread::Builder::new().name(format!("script-player-{}-thread", session.account_id)).spawn(move || {
let script = server_clone.state().map_item_script(&map_item, &map_name, map_instance).expect("Expect to retrieve script from map instance");
Vm::run_main_function(server_clone.vm.clone(), script.class_reference, script.instance_reference,
Box::new(&PlayerScriptHandler { client_notification_channel, npc_id, server: server_clone.clone(), player_action_receiver: RwLock::new(rx), runtime, session: session.clone(), configuration_service: GlobalConfigService::instance() })).unwrap()
Box::new(&PlayerScriptHandler { client_notification_channel, server: server_clone.clone(), player_action_receiver: RwLock::new(rx), runtime, configuration_service: GlobalConfigService::instance() }),
vec![npc_id, session.char_id.unwrap(), session.account_id]
)
.unwrap()
}).unwrap();
}

Expand Down
6 changes: 3 additions & 3 deletions server/src/server/request_handler/atcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn handle_atcommand(server: &Server, context: Request, packet: &PacketCzPlay
packet_zc_notify_playerchat.set_msg(result);
}
"job" | "jobchange" => {
let result = handle_set_job(server, context.session(), context.runtime(), args);
let result = handle_set_job(server, context.session().char_id(), context.runtime(), args);
packet_zc_notify_playerchat.set_msg(result);
}
"rates" => {
Expand Down Expand Up @@ -246,7 +246,7 @@ pub fn handle_set_job_level(server: &Server, session: Arc<Session>, _runtime: &R
String::new()
}

pub fn handle_set_job(server: &Server, session: Arc<Session>, _runtime: &Runtime, args: Vec::<&str>) -> String {
pub fn handle_set_job(server: &Server, char_id: u32, _runtime: &Runtime, args: Vec::<&str>) -> String {
if args.is_empty() {
return "@job command accept 1 parameters but received none".to_string();
}
Expand All @@ -256,7 +256,7 @@ pub fn handle_set_job(server: &Server, session: Arc<Session>, _runtime: &Runtime
JobName::try_from_string(args.first().unwrap())
};
if let Ok(job) = maybe_job {
server.add_to_next_tick(GameEvent::CharacterChangeJob(CharacterChangeJob { char_id: session.char_id(), job, should_reset_skills: true }));
server.add_to_next_tick(GameEvent::CharacterChangeJob(CharacterChangeJob { char_id, job, should_reset_skills: true }));
return "Your job has been changed.".to_string();
}
format!("Job {} not found", args.first().unwrap())
Expand Down
9 changes: 3 additions & 6 deletions server/src/server/script/bonus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,7 @@ mod tests {
let vm = Arc::new(Vm::new("../native_functions_list.txt", DebugFlag::None.value()));
// When
let script_handler = BonusScriptHandler { bonuses: RwLock::new(vec![]) };
Vm::repl(vm.clone(), compilation_result.unwrap().pop().as_ref().unwrap(),
Box::new(&script_handler));
Vm::repl(vm.clone(), compilation_result.unwrap().pop().as_ref().unwrap(), Box::new(&script_handler), vec![]);
// Then
assert!(matches!(script_handler.bonuses.read().unwrap()[0], BonusType::Str(10)))
}
Expand All @@ -920,8 +919,7 @@ mod tests {
let vm = Arc::new(Vm::new("../native_functions_list.txt", DebugFlag::All.value()));
// When
let script_handler = BonusScriptHandler { bonuses: RwLock::new(vec![]) };
Vm::repl(vm.clone(), compilation_result.unwrap().pop().as_ref().unwrap(),
Box::new(&script_handler));
Vm::repl(vm.clone(), compilation_result.unwrap().pop().as_ref().unwrap(), Box::new(&script_handler), vec![]);
// Then
assert!(matches!(script_handler.bonuses.read().unwrap()[0], BonusType::Str(-10)))
}
Expand All @@ -934,8 +932,7 @@ mod tests {
let vm = Arc::new(Vm::new("../native_functions_list.txt", DebugFlag::None.value()));
// When
let script_handler = BonusScriptHandler { bonuses: RwLock::new(vec![]) };
Vm::repl(vm.clone(), compilation_result.unwrap().pop().as_ref().unwrap(),
Box::new(&script_handler));
Vm::repl(vm.clone(), compilation_result.unwrap().pop().as_ref().unwrap(), Box::new(&script_handler), vec![]);
// Then
assert!(matches!(script_handler.bonuses.read().unwrap()[0], BonusType::ElementWeapon(Element::Water)))
}
Expand Down
39 changes: 20 additions & 19 deletions server/src/server/script/global_variable_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use regex::Regex;
use crate::server::state::character::Character;
use crate::server::model::events::game_event::CharacterZeny;
use crate::server::model::events::game_event::GameEvent::CharacterUpdateZeny;
use crate::server::script::{VM_THREAD_CONSTANT_INDEX_CHAR_ID, VM_THREAD_CONSTANT_INDEX_ACCOUNT_ID};

use crate::server::script::constant::load_constant;
use crate::server::script::{GlobalVariableEntry, GlobalVariableScope};
Expand All @@ -15,13 +16,13 @@ lazy_static! {
}

impl PlayerScriptHandler {
pub fn handle_setglobalvariable(&self, params: &[Value]) {
pub fn handle_setglobalvariable(&self, params: &[Value], execution_thread : &Thread) {
let variable_name = params[0].string_value().unwrap();
let variable_scope = params[1].string_value().unwrap();
let value = params[2].clone();
match variable_scope.as_str() {
"char_permanent" => {
let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
let character = self.server.state().get_character_unsafe(char_id);
if self.store_special_char_variable(character, variable_name, &value) {
return;
Expand All @@ -34,9 +35,9 @@ impl PlayerScriptHandler {
}
"account_permanent" => {
if value.is_number() {
self.server.repository.script_variable_account_num_save(self.session.account_id, variable_name.to_string(), 0, value.number_value().unwrap());
self.server.repository.script_variable_account_num_save(execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_ACCOUNT_ID), variable_name.to_string(), 0, value.number_value().unwrap());
} else {
self.server.repository.script_variable_account_str_save(self.session.account_id, variable_name.to_string(), 0, value.string_value().unwrap().clone());
self.server.repository.script_variable_account_str_save(execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_ACCOUNT_ID), variable_name.to_string(), 0, value.string_value().unwrap().clone());
}
}
"server_permanent" => {
Expand All @@ -47,7 +48,7 @@ impl PlayerScriptHandler {
}
}
"char_temporary" => {
let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
let character = self.server.state().get_character_unsafe(char_id);
let mut script_variable_store = character.script_variable_store.lock().unwrap();
let value = match value {
Expand All @@ -72,7 +73,7 @@ impl PlayerScriptHandler {
execution_thread.push_constant_on_stack(value);
return;
}
let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
let character = self.server.state().get_character_unsafe(char_id);
if let Some(value) = Self::load_special_char_variable(character, variable_name) {
execution_thread.push_constant_on_stack(value);
Expand All @@ -85,7 +86,7 @@ impl PlayerScriptHandler {
}
}
"account_permanent" => {
let account_id = self.session.account_id;
let account_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_ACCOUNT_ID);
if variable_name.ends_with('$') {
execution_thread.push_constant_on_stack(Value::new_string(self.server.repository.script_variable_account_str_fetch_one(account_id, variable_name.clone(), 0)));
} else {
Expand All @@ -100,7 +101,7 @@ impl PlayerScriptHandler {
}
}
"char_temporary" => {
let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
let character = self.server.state().get_character_unsafe(char_id);
let script_variable_store = character.script_variable_store.lock().unwrap();
let entry = script_variable_store.find_global_by_name_and_scope(variable_name, &GlobalVariableScope::CharTemporary);
Expand All @@ -120,10 +121,10 @@ impl PlayerScriptHandler {
}
}

pub fn handle_setglobalarray(&self, params: &Vec<Value>) {
pub fn handle_setglobalarray(&self, params: &Vec<Value>, execution_thread: &Thread) {
let variable_name = params[0].string_value().unwrap();
let variable_scope = params[1].string_value().unwrap();
let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
let character = self.server.state().get_character_unsafe(char_id);
let mut char_temporary_mutex = if variable_scope == "char_temporary" {
Some(character.script_variable_store.lock().unwrap())
Expand All @@ -140,16 +141,16 @@ impl PlayerScriptHandler {
match variable_scope.as_str() {
"char_permanent" => {
if value.is_number() {
self.server.repository.script_variable_char_num_save(self.session.char_id(), variable_name.to_string(), array_index as u32, value.number_value().unwrap());
self.server.repository.script_variable_char_num_save(execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID), variable_name.to_string(), array_index as u32, value.number_value().unwrap());
} else {
self.server.repository.script_variable_char_str_save(self.session.char_id(), variable_name.to_string(), array_index as u32, value.string_value().unwrap().clone());
self.server.repository.script_variable_char_str_save(execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID), variable_name.to_string(), array_index as u32, value.string_value().unwrap().clone());
}
}
"account_permanent" => {
if value.is_number() {
self.server.repository.script_variable_account_num_save(self.session.account_id, variable_name.to_string(), array_index as u32, value.number_value().unwrap());
self.server.repository.script_variable_account_num_save(execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_ACCOUNT_ID), variable_name.to_string(), array_index as u32, value.number_value().unwrap());
} else {
self.server.repository.script_variable_account_str_save(self.session.account_id, variable_name.to_string(), array_index as u32, value.string_value().unwrap().clone());
self.server.repository.script_variable_account_str_save(execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_ACCOUNT_ID), variable_name.to_string(), array_index as u32, value.string_value().unwrap().clone());
}
}
"server_permanent" => {
Expand All @@ -175,13 +176,13 @@ impl PlayerScriptHandler {
index += 2;
}
}
pub fn handle_remove_item_from_globalarray(&self, params: &[Value]) {
pub fn handle_remove_item_from_globalarray(&self, params: &[Value], execution_thread: &Thread) {
let variable_name = params[0].string_value().unwrap();
let variable_scope = params[1].string_value().unwrap();
let start_index = params[2].number_value().unwrap();
let end_index = params[3].number_value().unwrap();
if variable_scope == "char_temporary" {
let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
let character = self.server.state().get_character_unsafe(char_id);
let mut script_variable_store = character.script_variable_store.lock().unwrap();
for i in start_index..end_index {
Expand All @@ -195,7 +196,7 @@ impl PlayerScriptHandler {
let variable_name = params[0].string_value().unwrap();
let variable_scope = params[1].string_value().unwrap();

let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
let character = self.server.state().get_character_unsafe(char_id);
let mut char_temporary_mutex = if variable_scope == "char_temporary" {
Some(character.script_variable_store.lock().unwrap())
Expand All @@ -205,7 +206,7 @@ impl PlayerScriptHandler {

match variable_scope.as_str() {
"char_permanent" => {
let char_id = self.session.char_id();
let char_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_CHAR_ID);
if variable_name.ends_with('$') {
let rows = self.server.repository.script_variable_char_str_fetch_all(char_id, variable_name.clone());
Self::push_array_str_elements_on_stack(execution_thread, rows);
Expand All @@ -215,7 +216,7 @@ impl PlayerScriptHandler {
}
}
"account_permanent" => {
let account_id = self.session.account_id;
let account_id = execution_thread.get_constant(VM_THREAD_CONSTANT_INDEX_ACCOUNT_ID);
if variable_name.ends_with('$') {
let rows = self.server.repository.script_variable_account_str_fetch_all(account_id, variable_name.clone());
Self::push_array_str_elements_on_stack(execution_thread, rows);
Expand Down
Loading

0 comments on commit 7443f66

Please sign in to comment.