Skip to content

Commit

Permalink
ModIOMod returns to dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
aNaOH committed Apr 29, 2024
1 parent b33b3ac commit d816ea0
Showing 1 changed file with 83 additions and 33 deletions.
116 changes: 83 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use godot::builtin::meta::GodotConvert;
use godot::prelude::*;
use godot::engine::Node;
use godot::builtin::meta::GodotConvert;

use modio::mods::filters::Tags;
use modio::types::id::GameId;
use modio::{Credentials, Modio};
Expand All @@ -12,36 +13,7 @@ struct ModIOAddon;
#[gdextension]
unsafe impl ExtensionLibrary for ModIOAddon {}

struct ModIOClient {
client: Modio,
id: u64
}

impl ModIOClient {
fn new(api: &String, game: u64) -> Option<Self> {
match Modio::new(Credentials::new(api)) {
Ok(modio_instance) => Some(Self { client: modio_instance, id: game }),
Err(_) => None,
}
}
}

#[derive(GodotClass)]
#[class(base = Node)]
struct ModIO {
client: Option<ModIOClient>,
}

#[godot_api]
impl INode for ModIO {
fn init(_node: Base<Node>) -> Self {
Self { client: None }
}
}

#[derive(GodotClass)]
#[class(tool, init, base=Resource)]
struct ModIOMod {
pub struct ModIOMod {
pub id: u64,
pub name: GString,
pub submitter: GString,
Expand All @@ -54,6 +26,58 @@ struct ModIOMod {
pub tags: PackedStringArray,
}

impl GodotConvert for ModIOMod {
type Via = Dictionary;
}

impl ToGodot for ModIOMod {


fn into_godot(self) -> Self::Via {
let mut dictionary = Dictionary::new();
dictionary.insert("id", self.id);
dictionary.insert("date_updated", self.date_updated);
dictionary.insert("date_live", self.date_live);
dictionary.insert("profile_url", self.profile_url.clone());
dictionary.insert("modfile_url", self.modfile_url.clone());
dictionary.insert("modfile_name", self.modfile_name.clone());
dictionary.insert("modfile_size", self.modfile_size.clone());
dictionary.insert("tags", self.tags.clone());


dictionary
}

fn to_variant(&self) -> Variant {
let mut dictionary = Dictionary::new();
dictionary.insert("id", self.id);
dictionary.insert("date_updated", self.date_updated);
dictionary.insert("date_live", self.date_live);
dictionary.insert("profile_url", self.profile_url.clone());
dictionary.insert("modfile_url", self.modfile_url.clone());
dictionary.insert("modfile_name", self.modfile_name.clone());
dictionary.insert("modfile_size", self.modfile_size.clone());
dictionary.insert("tags", self.tags.clone());

Variant::from(dictionary)
}

fn to_godot(&self) -> Self::Via {
let mut dictionary = Dictionary::new();
dictionary.insert("id", self.id);
dictionary.insert("date_updated", self.date_updated);
dictionary.insert("date_live", self.date_live);
dictionary.insert("profile_url", self.profile_url.clone());
dictionary.insert("modfile_url", self.modfile_url.clone());
dictionary.insert("modfile_name", self.modfile_name.clone());
dictionary.insert("modfile_size", self.modfile_size.clone());
dictionary.insert("tags", self.tags.clone());


dictionary
}
}

impl ModIOMod {
fn from_mod(mod_info: &Mod) -> Self {
let (modfile_url, modfile_name, modfile_size) = if let Some(modfile) = &mod_info.modfile {
Expand Down Expand Up @@ -85,8 +109,33 @@ impl ModIOMod {
tags,
}
}
}


struct ModIOClient {
client: Modio,
id: u64
}

impl ModIOClient {
fn new(api: &String, game: u64) -> Option<Self> {
match Modio::new(Credentials::new(api)) {
Ok(modio_instance) => Some(Self { client: modio_instance, id: game }),
Err(_) => None,
}
}
}

#[derive(GodotClass)]
#[class(base = Node)]
struct ModIO {
client: Option<ModIOClient>,
}

#[godot_api]
impl INode for ModIO {
fn init(_node: Base<Node>) -> Self {
Self { client: None }
}
}

#[godot_api]
Expand Down Expand Up @@ -126,7 +175,7 @@ impl ModIO {
let mut mod_vec = Array::new();
for m in mods {

mod_vec.insert(mod_vec.len(), ModIOMod::from_mod(&m))
mod_vec.insert(mod_vec.len(), ModIOMod::from_mod(&m).to_godot())
}

Some(mod_vec)
Expand All @@ -141,6 +190,7 @@ impl ModIO {
None
}
}


// Función #[func] que invoca la función asíncrona intermedia
#[func]
Expand Down

0 comments on commit d816ea0

Please sign in to comment.