Skip to content

Commit

Permalink
1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aNaOH authored Dec 28, 2023
1 parent c862214 commit dd927ad
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
[package]
name = "mod_io"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[patch."https://github.com/godot-rust/godot4-prebuilt"]
godot4-prebuilt = { git = "https://github.com//godot-rust/godot4-prebuilt", branch = "4.1.2"}

[dependencies]
godot = { git = "https://github.com/godot-rust/gdext", branch = "master" }
modio = { git = "https://github.com/nickelc/modio-rs", branch = "master" }
Expand Down
78 changes: 53 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use godot::builtin::meta::GodotConvert;
use godot::prelude::*;
use godot::engine::Node;
use godot::engine::NodeVirtual;


use godot::prelude::meta::VariantMetadata;
use modio::mods::filters::Tags;
use modio::types::id::GameId;
use modio::{Credentials, Modio};
use modio::filter::prelude::*;
use modio::mods::Mod;
Expand All @@ -15,11 +14,11 @@ unsafe impl ExtensionLibrary for ModIOAddon {}

struct ModIOClient {
client: Modio,
id: u32
id: u64
}

impl ModIOClient {
fn new(api: &String, game: u32) -> Option<Self> {
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,
Expand All @@ -34,7 +33,7 @@ struct ModIO {
}

#[godot_api]
impl NodeVirtual for ModIO {
impl INode for ModIO {
fn init(_node: Base<Node>) -> Self {
godot_print!("Hello, world!");

Expand All @@ -43,12 +42,12 @@ impl NodeVirtual for ModIO {
}

struct ModIOMod {
pub id: u32,
pub id: u64,
pub date_updated: i64,
pub date_live: i64,
pub profile_url: GodotString,
pub modfile_url: GodotString,
pub modfile_name: GodotString,
pub profile_url: GString,
pub modfile_url: GString,
pub modfile_name: GString,
pub modfile_size: i64,
pub tags: PackedStringArray,
}
Expand All @@ -72,7 +71,7 @@ impl ModIOMod {
.collect();

Self {
id: mod_info.id,
id: mod_info.id.get(),
date_updated: mod_info.date_updated as i64,
date_live: mod_info.date_live as i64,
profile_url: mod_info.profile_url.as_str().into(),
Expand All @@ -86,8 +85,14 @@ impl ModIOMod {

}

impl ToVariant for ModIOMod {
fn to_variant(&self) -> Variant {
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);
Expand All @@ -99,21 +104,43 @@ impl ToVariant for ModIOMod {
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());


impl VariantMetadata for ModIOMod {
fn variant_type() -> VariantType {
VariantType::Dictionary
dictionary
}
}

#[godot_api]
impl ModIO {
#[func]
fn connect(&mut self, api_key: GodotString, game: u32) -> bool {
fn connect(&mut self, api_key: GString, game: u64) -> bool {
if self.client.is_none() {
if let Some(client) = ModIOClient::new(&api_key.to_string(), game) {
self.client = Some(client);
Expand All @@ -126,18 +153,18 @@ impl ModIO {
self.client.is_some()
}

async fn get_mods_async_inner(&self, query: GodotString) -> Option<Array<ModIOMod>> {
async fn get_mods_async_inner(&self, query: GString, tags: GString) -> Option<Array<Dictionary>> {
if let Some(ref client) = self.client {
// Example: Get mods (replace with your actual parameters)
let mut f = Filter::default();

if query != "".into() {
f = Fulltext::eq(query);
f = Fulltext::eq(query).and(Tags::_in(tags));
}

match client
.client
.game(client.id)
.game(GameId::new(client.id))
.mods()
.search(f)
.collect()
Expand All @@ -147,7 +174,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 @@ -165,10 +192,11 @@ impl ModIO {

// Función #[func] que invoca la función asíncrona intermedia
#[func]
fn get_mods(&self, query: GodotString) -> Array<ModIOMod> {
fn get_mods(&self, query: GString) -> Array<Dictionary> {

// Crear una nueva tarea y ejecutarla
let result = async {
match self.get_mods_async_inner(query).await {
match self.get_mods_async_inner(query, "".into()).await {
Some(mods) => {
// Imprimir información sobre los mods
godot_print!("Mods found");
Expand Down

0 comments on commit dd927ad

Please sign in to comment.