Skip to content

Commit

Permalink
Added initial serverlist editor, without saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Rechdan committed Dec 29, 2023
1 parent b9cd037 commit 1d46807
Show file tree
Hide file tree
Showing 13 changed files with 427 additions and 111 deletions.
195 changes: 102 additions & 93 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ members = [
resolver = "2"

[workspace.package]
version = "0.0.5"
version = "0.0.6"
description = "W2.Rust is a set of tools and GameServer made using the Rust language."
readme = "README.md"
repository = "https://github.com/Rechdan/W2.Rust"
Expand Down
1 change: 1 addition & 0 deletions apps/editors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ edition.workspace = true
eframe = "0.24.1"
egui = "0.24.1"
egui_extras = "0.24.2"
fixedstr = { version = "0.5.4", features = ["serde"] }
once_cell = "1.19.0"
rfd = "0.12.1"
14 changes: 14 additions & 0 deletions apps/editors/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::mem::size_of;

use crate::structs::ServerList;

pub const SERVERLIST_KEY_LEN: usize = 63;

pub const SERVERLIST_KEY: [u8; SERVERLIST_KEY_LEN] = [
0xc1, 0xb6, 0xc0, 0xcc, 0xc0, 0xd3, 0xc6, 0xd1, 0xc6, 0xae, 0xbe, 0xcf, 0xc8, 0xa3, 0xc8, 0xad,
0xc0, 0xdb, 0xbe, 0xf7, 0xc0, 0xbb, 0xc0, 0xa7, 0xc7, 0xd1, 0xbd, 0xba, 0xc5, 0xa9, 0xb8, 0xb3,
0xc6, 0xae, 0xc0, 0xd4, 0xb4, 0xcf, 0xb4, 0xd9, 0xb8, 0xb8, 0xc7, 0xd1, 0xb1, 0xdb, 0xb7, 0xce,
0xbe, 0xcf, 0xc8, 0xad, 0xc8, 0xad, 0xc7, 0xd2, 0xc1, 0xd9, 0xa4, 0xbb, 0xa4, 0xbb, 0x00,
];

pub const SERVER_LIST_SIZE: usize = size_of::<ServerList>();
32 changes: 32 additions & 0 deletions apps/editors/src/decoders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::consts::{SERVERLIST_KEY, SERVERLIST_KEY_LEN};

pub fn server_list(buf: &mut [u8]) -> bool {
for i in 0..10 {
for j in 0..11 {
for k in (64 - SERVERLIST_KEY_LEN)..64 {
let buf_index = 704 * i + 64 * j + k;
let key_index = 63 - k;

match buf.get_mut(buf_index) {
Some(buf_value) => match SERVERLIST_KEY.get(key_index) {
Some(key_value) => {
*buf_value = ((*buf_value as i16) - (*key_value as i16)) as u8;
}

None => {
println!("Server list invalid key index: {}", key_index);
return false;
}
},

None => {
println!("Server list invalid buf index: {}", buf_index);
return false;
}
}
}
}
}

return true;
}
126 changes: 126 additions & 0 deletions apps/editors/src/editors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use egui::{Button, ScrollArea, Ui};
use egui_extras::{Size, StripBuilder};
use rfd::FileDialog;
use std::{fs::File, io::Read, path::PathBuf, sync::Mutex};

use self::server_list::ServerListEditor;

pub mod server_list;

#[derive(Default, Clone)]
enum EditorSelected {
#[default]
None,
ServerList(ServerListEditor),
}

#[derive(Default)]
pub struct Editors {
folder_selected: Mutex<Option<PathBuf>>,
editor_selected: Mutex<EditorSelected>,
}

impl Editors {
pub fn render(&self, ui: &mut Ui) {
let folder_selected = self.folder_selected.lock().unwrap().clone();

match folder_selected.clone() {
Some(folder) => {
self.render_selected_folder(ui, folder.clone());
self.render_body(ui, folder.clone());
}

None => {
if ui.button("Selecionar pasta do cliente").clicked() {
match FileDialog::new().pick_folder() {
Some(new_folder) => {
*self.folder_selected.lock().unwrap() = Some(new_folder);
}
None => {}
};
}
}
};
}

fn render_selected_folder(&self, ui: &mut Ui, folder: PathBuf) {
ui.horizontal(|ui| {
ui.label("Pasta:");

if ui.link(folder.display().to_string()).clicked() {}
});
}

fn render_body(&self, ui: &mut Ui, folder: PathBuf) {
StripBuilder::new(ui)
.size(Size::exact(200.0))
.size(Size::remainder())
.horizontal(|mut strip| {
strip.cell(|ui| {
ui.group(|ui| {
ui.set_min_size(ui.available_size());

self.render_editors_list(ui, folder.clone());
});
});

strip.cell(|ui| {
ui.group(|ui| {
ui.set_min_size(ui.available_size());

ScrollArea::vertical()
.id_source("editor_scroll")
.show(ui, |ui| {
match self.editor_selected.lock().unwrap().clone() {
EditorSelected::None => {
ui.label("Selecione um editor ao lado");
}

EditorSelected::ServerList(server_list) => {
server_list.render(ui);
}
};
});
});
});
});
}

fn render_editors_list(&self, ui: &mut Ui, folder: PathBuf) {
ui.vertical_centered_justified(|ui| {
self.render_server_list_editor_button(ui, folder);
});
}

fn render_server_list_editor_button(&self, ui: &mut Ui, folder: PathBuf) {
let path = folder.join("serverlist.bin");

ui.add_enabled_ui(path.exists(), |ui| {
let mut editor_selected = self.editor_selected.lock().unwrap();

let selected = match *editor_selected {
EditorSelected::ServerList(_) => true,
_ => false,
};

if ui
.add(Button::new("Server List").selected(selected))
.clicked()
{
let buf = &mut Vec::new();

match File::open(path).unwrap().read_to_end(buf) {
Ok(_size) => match ServerListEditor::new(buf) {
Some(server_list) => {
*editor_selected = EditorSelected::ServerList(server_list);
}

None => {}
},

Err(_error) => {}
};
}
});
}
}
88 changes: 88 additions & 0 deletions apps/editors/src/editors/server_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use egui::{Button, Ui};
use fixedstr::zstr;
use std::{
str::FromStr,
sync::{Arc, Mutex},
};

use crate::structs::ServerList;

#[derive(Clone)]
pub struct ServerListEditor {
server_list: Arc<Mutex<ServerList>>,
selected_world_index: Arc<Mutex<usize>>,
}

impl ServerListEditor {
pub fn new(buf: &[u8]) -> Option<Self> {
match ServerList::new(buf) {
Some(server_list) => Some(Self {
server_list: Arc::new(Mutex::new(server_list)),
selected_world_index: Arc::new(Mutex::new(0)),
}),
None => None,
}
}

pub fn render(&self, ui: &mut Ui) {
let mut server_list = self.server_list.lock().unwrap();

ui.vertical(|ui| {
ui.label("Key:");

let key = &mut server_list.key.to_string();
ui.text_edit_singleline(key);
server_list.key = match FromStr::from_str(key) {
Ok(v) => v,
Err(_error) => server_list.key.clone(),
};
});

ui.add_space(4.0);

let mut selected_world_index = self.selected_world_index.lock().unwrap();

ui.vertical(|ui| {
ui.label("Mundo:");

ui.horizontal_wrapped(|ui| {
for i in 0..10 {
let selected = *selected_world_index == i;

if ui
.add(Button::new((i + 1).to_string()).selected(selected))
.clicked()
{
*selected_world_index = i;
}
}
});
});

ui.add_space(4.0);

let (world_url, world_channels) =
server_list.worlds.get_mut(*selected_world_index).unwrap();

ui.vertical(|ui| {
let url = &mut world_url.to_string();

ui.label("Url:");

if ui.text_edit_singleline(url).changed() {
*world_url = zstr::from(url);
}
});

ui.add_space(4.0);

ui.label("Canais:");

for channel in world_channels {
let addr = &mut channel.to_string();
if ui.text_edit_singleline(addr).changed() {
*channel = zstr::from(addr);
}
}
}
}
4 changes: 4 additions & 0 deletions apps/editors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use eframe::{run_native, NativeOptions};
use egui::{vec2, ViewportBuilder};
use main_window::MainWindow;

pub mod consts;
pub mod decoders;
pub mod editors;
pub mod main_window;
pub mod structs;

fn main() {
let options = NativeOptions {
Expand Down
46 changes: 32 additions & 14 deletions apps/editors/src/main_window.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
use eframe::App;
use egui::CentralPanel;
use rfd::FileDialog;
use eframe::{App, Frame};
use egui::{CentralPanel, Context};

pub struct MainWindow {}
use crate::editors::Editors;

pub struct MainWindow {
editors: Editors,
}

impl Default for MainWindow {
fn default() -> Self {
Self {}
Self {
editors: Default::default(),
}
}
}

impl App for MainWindow {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
CentralPanel::default().show(ctx, |ui| {
ui.vertical(|ui| {
ui.heading("W2.Rust Editors");

if ui.button("Selecionar").clicked() {
match FileDialog::new().pick_folder() {
Some(folder) => {
println!("Folder: {}", folder.display());
}
None => {}
};
}
self.editors.render(ui);

// match &mut self.state {
// State::None => {
// if ui.button("Selecionar pasta do cliente").clicked() {
// match FileDialog::new().pick_folder() {
// Some(folder) => {
// self.state = State::ClientSelected(ClientSelected {
// path: folder,
// editor_selected: EditorSelected::None,
// });
// }
// None => {}
// };
// }
// }

// State::ClientSelected(client_selected) => {
// client_selected.render(ui);
// }
// }
});
});
}
Expand Down
24 changes: 24 additions & 0 deletions apps/editors/src/structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use fixedstr::zstr;
use std::{mem::transmute, panic::catch_unwind};

use crate::{consts::SERVER_LIST_SIZE, decoders};

#[repr(C, packed(4))]
pub struct ServerList {
pub key: u32,
pub worlds: [(zstr<64>, [zstr<64>; 10]); 10],
}

impl ServerList {
pub fn new(buf: &[u8]) -> Option<Self> {
match catch_unwind(|| {
let mut buf = TryInto::<[u8; SERVER_LIST_SIZE]>::try_into(buf).unwrap();
let buf_encoded = buf.get_mut(4..).unwrap();
decoders::server_list(buf_encoded);
unsafe { transmute(buf) }
}) {
Ok(result) => Some(result),
Err(_error) => None,
}
}
}
2 changes: 1 addition & 1 deletion apps/manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ eframe = "0.24.1"
egui = "0.24.1"
egui_extras = "0.24.2"
once_cell = "1.19.0"
tokio = { version = "1.35.0", features = ["full"] }
tokio = { version = "1.35.1", features = ["full"] }
2 changes: 1 addition & 1 deletion apps/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ edition.workspace = true
[dependencies]
enc_dec = { path = "../../libs/enc_dec" }
packets = { path = "../../libs/packets" }
tokio = { version = "1.35.0", features = ["full"] }
tokio = { version = "1.35.1", features = ["full"] }
2 changes: 1 addition & 1 deletion apps/sniffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ eframe = "0.24.1"
egui = "0.24.1"
egui_extras = "0.24.2"
once_cell = "1.19.0"
tokio = { version = "1.35.0", features = ["full"] }
tokio = { version = "1.35.1", features = ["full"] }

0 comments on commit 1d46807

Please sign in to comment.