Skip to content

Commit

Permalink
Create ragnarok_formats crate with byte convertable structs
Browse files Browse the repository at this point in the history
  • Loading branch information
vE5li committed May 8, 2024
1 parent 344e6a4 commit c2fb2ae
Show file tree
Hide file tree
Showing 56 changed files with 1,267 additions and 1,126 deletions.
24 changes: 11 additions & 13 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ korangar_interface = { path = "korangar_interface" }
korangar_networking = { path = "korangar_networking" }
num = "0.4.1"
ragnarok_bytes = { path = "ragnarok_bytes" }
ragnarok_formats = { path = "ragnarok_formats" }
ragnarok_packets = { path = "ragnarok_packets" }
ragnarok_procedural = { path = "ragnarok_procedural" }
serde = "1.0.137"
2 changes: 1 addition & 1 deletion korangar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ option-ext = "0.2.0"
pathfinding = "2.2.2"
korangar_debug = { workspace = true, optional = true }
ragnarok_bytes = { workspace = true, features = ["derive", "cgmath"] }
ragnarok_formats = { workspace = true, features = ["interface"] }
ragnarok_packets = { workspace = true, features = ["derive", "interface", "packet-to-prototype-element"] }
rand = "0.8.5"
random_color = { version = "0.6.1", optional = true }
Expand All @@ -32,7 +33,6 @@ serde-xml-rs = "0.6.0"
tokio = { version = "1.37.0", features = ["full"] }
vulkano = { git = "https://github.com/vulkano-rs/vulkano.git", rev = "db3df4e55f80c137ea6187250957eb92c2291627" }
vulkano-shaders = { git = "https://github.com/vulkano-rs/vulkano.git", rev = "db3df4e55f80c137ea6187250957eb92c2291627" }
vulkano-win = { git = "https://github.com/vulkano-rs/vulkano.git", rev = "db3df4e55f80c137ea6187250957eb92c2291627" }
walkdir = "2"
winit = "0.28.7"
xml-rs = "0.8.0"
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/cameras/debug.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::f32::consts::FRAC_PI_4;

use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, Rad, SquareMatrix, Vector2, Vector3, Vector4};
use ragnarok_formats::transform::Transform;

use super::Camera;
use crate::graphics::Transform;
use crate::interface::layout::{ScreenPosition, ScreenSize};

const LOOK_AROUND_SPEED: f32 = 0.005;
Expand Down
3 changes: 2 additions & 1 deletion korangar/src/graphics/cameras/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ mod shadow;
mod start;

use cgmath::{InnerSpace, Matrix4, Vector2, Vector3, Vector4};
use ragnarok_formats::transform::Transform;

#[cfg(feature = "debug")]
pub use self::debug::DebugCamera;
pub use self::player::PlayerCamera;
pub use self::shadow::ShadowCamera;
pub use self::start::StartCamera;
use crate::graphics::{SmoothedValue, Transform};
use crate::graphics::SmoothedValue;
use crate::interface::layout::{ScreenPosition, ScreenSize};

fn direction(vector: Vector2<f32>) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/cameras/player.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::f32::consts::FRAC_PI_2;

use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, Rad, SquareMatrix, Vector2, Vector3, Vector4};
use ragnarok_formats::transform::Transform;

use super::{Camera, SmoothedValue};
use crate::graphics::Transform;
use crate::interface::layout::{ScreenPosition, ScreenSize};

const ZOOM_SPEED: f32 = 2.0;
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/cameras/shadow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, SquareMatrix, Vector2, Vector3, Vector4};
use ragnarok_formats::transform::Transform;

use super::Camera;
use crate::graphics::Transform;
use crate::interface::layout::{ScreenPosition, ScreenSize};

pub struct ShadowCamera {
Expand Down
2 changes: 1 addition & 1 deletion korangar/src/graphics/cameras/start.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::f32::consts::{FRAC_PI_2, FRAC_PI_4};

use cgmath::{Array, EuclideanSpace, InnerSpace, Matrix4, MetricSpace, Point3, Rad, SquareMatrix, Vector2, Vector3, Vector4};
use ragnarok_formats::transform::Transform;

use super::Camera;
use crate::graphics::Transform;
use crate::interface::layout::{ScreenPosition, ScreenSize};

const DEFAULT_ZOOM: f32 = 150.0;
Expand Down
13 changes: 13 additions & 0 deletions korangar/src/graphics/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ragnarok_formats::color::ColorRGB;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -116,3 +117,15 @@ impl From<Color> for [f32; 4] {
[val.red, val.green, val.blue, val.alpha]
}
}

impl From<ColorRGB> for Color {
fn from(value: ColorRGB) -> Self {
let ColorRGB { red, blue, green } = value;
Color {
red,
green,
blue,
alpha: 1.0,
}
}
}
2 changes: 0 additions & 2 deletions korangar/src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod particles;
mod renderers;
mod settings;
mod smoothed;
mod transform;
mod vertices;

use std::sync::Arc;
Expand All @@ -22,7 +21,6 @@ pub use self::particles::*;
pub use self::renderers::*;
pub use self::settings::GraphicsSettings;
pub use self::smoothed::SmoothedValue;
pub use self::transform::Transform;
pub use self::vertices::*;

pub type CommandBuilder = AutoCommandBufferBuilder<PrimaryAutoCommandBuffer<MemoryAllocator>, MemoryAllocator>;
1 change: 1 addition & 0 deletions korangar/src/graphics/renderers/deferred/box/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fragment_shader!("src/graphics/renderers/deferred/box/fragment_shader.glsl");
use std::sync::Arc;

use cgmath::Vector3;
use ragnarok_formats::transform::Transform;
use vulkano::descriptor_set::WriteDescriptorSet;
use vulkano::device::{Device, DeviceOwned};
use vulkano::pipeline::graphics::input_assembly::PrimitiveTopology;
Expand Down
2 changes: 2 additions & 0 deletions korangar/src/graphics/renderers/deferred/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use std::sync::Arc;
use cgmath::SquareMatrix;
use cgmath::{Matrix4, Vector2, Vector3};
use korangar_interface::application::FontSizeTrait;
#[cfg(feature = "debug")]
use ragnarok_formats::transform::Transform;
use ragnarok_packets::EntityId;
use vulkano::device::{DeviceOwned, Queue};
use vulkano::format::Format;
Expand Down
21 changes: 0 additions & 21 deletions korangar/src/graphics/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,6 @@ use cgmath::{Deg, Rad, Vector3};
use korangar_interface::elements::PrototypeElement;
use ragnarok_bytes::{ByteStream, ConversionResult, ConversionResultExt, FromBytes};

#[derive(Copy, Clone, Debug, PrototypeElement)]
pub struct Transform {
pub position: Vector3<f32>,
#[hidden_element] // TODO: unhide
pub rotation: Vector3<Rad<f32>>,
pub scale: Vector3<f32>,
}

impl FromBytes for Transform {
fn from_bytes<Meta>(byte_stream: &mut ByteStream<Meta>) -> ConversionResult<Self> {
let mut position = <Vector3<f32>>::from_bytes(byte_stream).trace::<Self>()?;
let rotation = <Vector3<f32>>::from_bytes(byte_stream).trace::<Self>()?;
let scale = <Vector3<f32>>::from_bytes(byte_stream).trace::<Self>()?;

// TODO: make this nicer
position.y = -position.y;

Ok(Transform::from(position, rotation.map(Deg), scale))
}
}

impl Transform {
pub fn from(position: Vector3<f32>, rotation: Vector3<Deg<f32>>, scale: Vector3<f32>) -> Self {
let rotation = rotation.map(|degrees| degrees.into());
Expand Down
90 changes: 8 additions & 82 deletions korangar/src/loaders/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ use derive_new::new;
#[cfg(feature = "debug")]
use korangar_debug::logging::{print_debug, Colorize, Timer};
use korangar_interface::elements::PrototypeElement;
use ragnarok_bytes::{ByteConvertable, ByteStream, FromBytes};
use ragnarok_bytes::{ByteStream, FromBytes};
use ragnarok_formats::action::{Action, ActionsData};
use ragnarok_formats::version::InternalVersion;
use ragnarok_packets::ClientTick;
use vulkano::image::view::ImageView;

use super::version::InternalVersion;
use super::error::LoadError;
use super::Sprite;
use crate::graphics::{Color, Renderer, SpriteRenderer};
use crate::interface::application::InterfaceSettings;
use crate::interface::layout::{ScreenClip, ScreenPosition, ScreenSize};
use crate::loaders::{GameFileLoader, MinorFirst, Version, FALLBACK_ACTIONS_FILE};
use crate::loaders::{GameFileLoader, FALLBACK_ACTIONS_FILE};

#[derive(Clone, Debug, new)]
pub struct AnimationState {
Expand Down Expand Up @@ -195,95 +197,19 @@ impl Actions {
}
}

#[derive(Debug, Clone, ByteConvertable, PrototypeElement)]
struct SpriteClip {
pub position: Vector2<i32>,
pub sprite_number: u32,
pub mirror_on: u32,
#[version_equals_or_above(2, 0)]
pub color: Option<u32>,
#[version_smaller(2, 4)]
pub zoom: Option<f32>,
#[version_equals_or_above(2, 4)]
pub zoom2: Option<Vector2<f32>>,
#[version_equals_or_above(2, 0)]
pub angle: Option<i32>,
#[version_equals_or_above(2, 0)]
pub sprite_type: Option<u32>,
#[version_equals_or_above(2, 5)]
pub size: Option<Vector2<u32>>,
}

#[derive(Debug, Clone, ByteConvertable, PrototypeElement)]
struct AttachPoint {
pub ignored: u32,
pub position: Vector2<i32>,
pub attribute: u32,
}

#[derive(Debug, Clone, ByteConvertable, PrototypeElement)]
struct Motion {
pub range1: [i32; 4], // maybe just skip this?
pub range2: [i32; 4], // maybe just skip this?
pub sprite_clip_count: u32,
#[repeating(self.sprite_clip_count)]
pub sprite_clips: Vec<SpriteClip>,
#[version_equals_or_above(2, 0)]
pub event_id: Option<i32>, // if version == 2.0 this maybe needs to be set to None ?
// (after it is parsed)
#[version_equals_or_above(2, 3)]
pub attach_point_count: Option<u32>,
#[repeating(self.attach_point_count.unwrap_or_default())]
pub attach_points: Vec<AttachPoint>,
}

#[derive(Debug, Clone, ByteConvertable, PrototypeElement)]
struct Action {
pub motion_count: u32,
#[repeating(self.motion_count)]
pub motions: Vec<Motion>,
}

#[derive(Debug, Clone, FromBytes, PrototypeElement)]
struct Event {
#[length_hint(40)]
pub name: String,
}

#[derive(Debug, Clone, FromBytes, PrototypeElement)]
struct ActionsData {
#[version]
pub version: Version<MinorFirst>,
pub action_count: u16,
pub reserved: [u8; 10],
#[repeating(self.action_count)]
pub actions: Vec<Action>,
#[version_equals_or_above(2, 1)]
pub event_count: Option<u32>,
#[repeating(self.event_count.unwrap_or_default())]
pub events: Vec<Event>,
#[version_equals_or_above(2, 2)]
#[repeating(self.action_count)]
pub delays: Option<Vec<f32>>,
}

#[derive(Default)]
pub struct ActionLoader {
cache: HashMap<String, Arc<Actions>>,
}

impl ActionLoader {
fn load(&mut self, path: &str, game_file_loader: &mut GameFileLoader) -> Result<Arc<Actions>, String> {
fn load(&mut self, path: &str, game_file_loader: &mut GameFileLoader) -> Result<Arc<Actions>, LoadError> {
#[cfg(feature = "debug")]
let timer = Timer::new_dynamic(format!("load actions from {}", path.magenta()));

let bytes = game_file_loader.get(&format!("data\\sprite\\{path}"))?;
let bytes = game_file_loader.get(&format!("data\\sprite\\{path}")).map_err(LoadError::File)?;
let mut byte_stream: ByteStream<Option<InternalVersion>> = ByteStream::without_metadata(&bytes);

if <[u8; 2]>::from_bytes(&mut byte_stream).unwrap() != [b'A', b'C'] {
return Err(format!("failed to read magic number from {path}"));
}

let actions_data = match ActionsData::from_bytes(&mut byte_stream) {
Ok(actions_data) => actions_data,
Err(_error) => {
Expand Down Expand Up @@ -319,7 +245,7 @@ impl ActionLoader {
Ok(sprite)
}

pub fn get(&mut self, path: &str, game_file_loader: &mut GameFileLoader) -> Result<Arc<Actions>, String> {
pub fn get(&mut self, path: &str, game_file_loader: &mut GameFileLoader) -> Result<Arc<Actions>, LoadError> {
match self.cache.get(path) {
Some(sprite) => Ok(sprite.clone()),
None => self.load(path, game_file_loader),
Expand Down
15 changes: 0 additions & 15 deletions korangar/src/loaders/archive/native/assettable.rs

This file was deleted.

12 changes: 6 additions & 6 deletions korangar/src/loaders/archive/native/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use std::path::{Path, PathBuf};

use ragnarok_bytes::ToBytes;
use ragnarok_formats::archive::{AssetTable, FileTableRow, Header};
use yazi::{compress, CompressionLevel, Format};

use super::assettable::AssetTable;
use super::filetablerow::FileTableRow;
use super::header::Header;
use super::{FileTable, MAGIC_BYTES};
use super::FileTable;
use crate::loaders::archive::Writable;

pub struct NativeArchiveBuilder {
Expand Down Expand Up @@ -61,7 +59,6 @@ impl Writable for NativeArchiveBuilder {

let mut bytes = Vec::new();

bytes.extend_from_slice(MAGIC_BYTES);
bytes.extend_from_slice(&file_header.to_bytes().unwrap());
bytes.extend_from_slice(&self.data);

Expand All @@ -72,7 +69,10 @@ impl Writable for NativeArchiveBuilder {
}

let compressed_file_information_data = compress(&file_table_data, Format::Zlib, CompressionLevel::Default).unwrap();
let file_table = AssetTable::new(compressed_file_information_data.len() as u32, file_table_data.len() as u32);
let file_table = AssetTable {
compressed_size: compressed_file_information_data.len() as u32,
uncompressed_size: file_table_data.len() as u32,
};

bytes.extend_from_slice(&file_table.to_bytes().unwrap());
bytes.extend_from_slice(&compressed_file_information_data);
Expand Down
Loading

0 comments on commit c2fb2ae

Please sign in to comment.